@dxos/app-framework 0.8.4-main.2e9d522 → 0.8.4-main.3c1ae3b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.storybook/main.mts +11 -0
- package/.storybook/preview.mts +8 -0
- package/.swc/plugins/linux_x86_64_19.0.0/727453fb3a62f7f1d952a41e051ca8a6f88cadc45cee43c6a4d1aa45f9b75665.wasmer-v7 +0 -0
- package/.swc/plugins/{v7_linux_x86_64_13.0.0/fce1bdb8e20a094e4af08bad09cc81497ed0e2e7c51223b07d371063cca18429 → linux_x86_64_19.0.0/fce1bdb8e20a094e4af08bad09cc81497ed0e2e7c51223b07d371063cca18429.wasmer-v7} +0 -0
- package/dist/lib/browser/{app-graph-builder-LYF7EKNN.mjs → app-graph-builder-7HSPOVXY.mjs} +32 -31
- package/dist/lib/browser/app-graph-builder-7HSPOVXY.mjs.map +7 -0
- package/dist/lib/browser/chunk-256DQP2E.mjs +627 -0
- package/dist/lib/browser/chunk-256DQP2E.mjs.map +7 -0
- package/dist/lib/browser/{chunk-ORWHM7CO.mjs → chunk-SCPE4ZO2.mjs} +11 -8
- package/dist/lib/browser/chunk-SCPE4ZO2.mjs.map +7 -0
- package/dist/lib/browser/{chunk-FMN65HSW.mjs → chunk-ZP65K47V.mjs} +397 -265
- package/dist/lib/browser/chunk-ZP65K47V.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +20 -56
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/{intent-dispatcher-LSYQZSEB.mjs → intent-dispatcher-MGSN6NWK.mjs} +2 -2
- package/dist/lib/browser/{intent-resolver-ZTNOSO3A.mjs → intent-resolver-UGFUFECV.mjs} +7 -7
- package/dist/lib/browser/intent-resolver-UGFUFECV.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/react/index.mjs +36 -0
- package/dist/lib/browser/{store-KML2R4IE.mjs → store-TF6FXKUY.mjs} +5 -5
- package/dist/lib/browser/store-TF6FXKUY.mjs.map +7 -0
- package/dist/lib/browser/testing/index.mjs +17 -21
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/{app-graph-builder-SAOWGJDK.mjs → app-graph-builder-SD2CIDSL.mjs} +32 -31
- package/dist/lib/node-esm/app-graph-builder-SD2CIDSL.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-AKNEI7PK.mjs +628 -0
- package/dist/lib/node-esm/chunk-AKNEI7PK.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-ZEZ4FVEU.mjs → chunk-GVTAHU3L.mjs} +397 -265
- package/dist/lib/node-esm/chunk-GVTAHU3L.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-UMZQERLE.mjs → chunk-ZX63QUGE.mjs} +11 -8
- package/dist/lib/node-esm/chunk-ZX63QUGE.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +20 -56
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/{intent-dispatcher-6CYNGPSW.mjs → intent-dispatcher-2M6HMLXG.mjs} +2 -2
- package/dist/lib/node-esm/{intent-resolver-W7Z7WFFM.mjs → intent-resolver-IFG2IKBH.mjs} +7 -7
- package/dist/lib/node-esm/intent-resolver-IFG2IKBH.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/react/index.mjs +37 -0
- package/dist/lib/node-esm/{store-QEXGXLWZ.mjs → store-2CNGWED2.mjs} +5 -5
- package/dist/lib/node-esm/store-2CNGWED2.mjs.map +7 -0
- package/dist/lib/node-esm/testing/index.mjs +17 -21
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/common/capabilities.d.ts +110 -39
- package/dist/types/src/common/capabilities.d.ts.map +1 -1
- package/dist/types/src/common/collaboration.d.ts +10 -9
- package/dist/types/src/common/collaboration.d.ts.map +1 -1
- package/dist/types/src/common/events.d.ts.map +1 -1
- package/dist/types/src/common/file.d.ts +1 -1
- package/dist/types/src/common/file.d.ts.map +1 -1
- package/dist/types/src/common/index.d.ts +1 -1
- package/dist/types/src/common/index.d.ts.map +1 -1
- package/dist/types/src/common/layout.d.ts +1 -3
- package/dist/types/src/common/layout.d.ts.map +1 -1
- package/dist/types/src/common/surface.d.ts +21 -16
- package/dist/types/src/common/surface.d.ts.map +1 -1
- package/dist/types/src/common/translations.d.ts +1 -1
- package/dist/types/src/common/translations.d.ts.map +1 -1
- package/dist/types/src/core/capabilities.d.ts +19 -16
- package/dist/types/src/core/capabilities.d.ts.map +1 -1
- package/dist/types/src/core/manager.d.ts +7 -3
- package/dist/types/src/core/manager.d.ts.map +1 -1
- package/dist/types/src/core/plugin.d.ts +8 -1
- package/dist/types/src/core/plugin.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +0 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/playground/debug/Debug.d.ts +1 -1
- package/dist/types/src/playground/debug/plugin.d.ts +1 -1
- package/dist/types/src/playground/debug/plugin.d.ts.map +1 -1
- package/dist/types/src/playground/generator/Main.d.ts +1 -1
- package/dist/types/src/playground/generator/Main.d.ts.map +1 -1
- package/dist/types/src/playground/generator/Toolbar.d.ts +1 -1
- package/dist/types/src/playground/generator/Toolbar.d.ts.map +1 -1
- package/dist/types/src/playground/generator/generator.d.ts +1 -1
- package/dist/types/src/playground/generator/generator.d.ts.map +1 -1
- package/dist/types/src/playground/generator/plugin.d.ts +1 -1
- package/dist/types/src/playground/generator/plugin.d.ts.map +1 -1
- package/dist/types/src/playground/layout/Layout.d.ts +2 -2
- package/dist/types/src/playground/layout/plugin.d.ts +1 -1
- package/dist/types/src/playground/layout/plugin.d.ts.map +1 -1
- package/dist/types/src/playground/logger/Toolbar.d.ts +1 -1
- package/dist/types/src/playground/logger/Toolbar.d.ts.map +1 -1
- package/dist/types/src/playground/logger/plugin.d.ts +1 -1
- package/dist/types/src/playground/logger/plugin.d.ts.map +1 -1
- package/dist/types/src/playground/logger/schema.d.ts +1 -1
- package/dist/types/src/playground/logger/schema.d.ts.map +1 -1
- package/dist/types/src/playground/playground.stories.d.ts +5 -4
- package/dist/types/src/playground/playground.stories.d.ts.map +1 -1
- package/dist/types/src/plugin-intent/IntentPlugin.d.ts +1 -1
- package/dist/types/src/plugin-intent/IntentPlugin.d.ts.map +1 -1
- package/dist/types/src/plugin-intent/actions.d.ts +5 -7
- package/dist/types/src/plugin-intent/actions.d.ts.map +1 -1
- package/dist/types/src/plugin-intent/errors.d.ts.map +1 -1
- package/dist/types/src/plugin-intent/index.d.ts +1 -0
- package/dist/types/src/plugin-intent/index.d.ts.map +1 -1
- package/dist/types/src/plugin-intent/intent-dispatcher.d.ts +7 -7
- package/dist/types/src/plugin-intent/intent-dispatcher.d.ts.map +1 -1
- package/dist/types/src/plugin-intent/intent.d.ts +1 -1
- package/dist/types/src/plugin-intent/intent.d.ts.map +1 -1
- package/dist/types/src/plugin-intent/meta.d.ts +3 -0
- package/dist/types/src/plugin-intent/meta.d.ts.map +1 -0
- package/dist/types/src/plugin-settings/SettingsPlugin.d.ts +1 -1
- package/dist/types/src/plugin-settings/SettingsPlugin.d.ts.map +1 -1
- package/dist/types/src/plugin-settings/actions.d.ts +5 -7
- package/dist/types/src/plugin-settings/actions.d.ts.map +1 -1
- package/dist/types/src/plugin-settings/app-graph-builder.d.ts +1 -1
- package/dist/types/src/plugin-settings/app-graph-builder.d.ts.map +1 -1
- package/dist/types/src/plugin-settings/intent-resolver.d.ts +1 -1
- package/dist/types/src/plugin-settings/intent-resolver.d.ts.map +1 -1
- package/dist/types/src/plugin-settings/meta.d.ts +3 -0
- package/dist/types/src/plugin-settings/meta.d.ts.map +1 -0
- package/dist/types/src/plugin-settings/store.d.ts +1 -1
- package/dist/types/src/plugin-settings/store.d.ts.map +1 -1
- package/dist/types/src/plugin-settings/translations.d.ts +2 -1
- package/dist/types/src/plugin-settings/translations.d.ts.map +1 -1
- package/dist/types/src/react/App.d.ts +10 -0
- package/dist/types/src/react/App.d.ts.map +1 -0
- package/dist/types/src/react/App.stories.d.ts +14 -0
- package/dist/types/src/react/App.stories.d.ts.map +1 -0
- package/dist/types/src/react/DefaultFallback.d.ts +8 -0
- package/dist/types/src/react/DefaultFallback.d.ts.map +1 -0
- package/dist/types/src/react/ErrorBoundary.d.ts +13 -14
- package/dist/types/src/react/ErrorBoundary.d.ts.map +1 -1
- package/dist/types/src/react/IntentContext.d.ts.map +1 -1
- package/dist/types/src/react/Surface.d.ts +7 -5
- package/dist/types/src/react/Surface.d.ts.map +1 -1
- package/dist/types/src/react/Surface.stories.d.ts +8 -10
- package/dist/types/src/react/Surface.stories.d.ts.map +1 -1
- package/dist/types/src/react/SurfaceInfo.d.ts +11 -0
- package/dist/types/src/react/SurfaceInfo.d.ts.map +1 -0
- package/dist/types/src/react/common.d.ts.map +1 -1
- package/dist/types/src/react/index.d.ts +2 -0
- package/dist/types/src/react/index.d.ts.map +1 -1
- package/dist/types/src/react/types.d.ts +14 -0
- package/dist/types/src/react/types.d.ts.map +1 -0
- package/dist/types/src/{App.d.ts → react/useApp.d.ts} +7 -6
- package/dist/types/src/react/useApp.d.ts.map +1 -0
- package/dist/types/src/react/useCapabilities.d.ts.map +1 -1
- package/dist/types/src/react/useLoading.d.ts +19 -0
- package/dist/types/src/react/useLoading.d.ts.map +1 -0
- package/dist/types/src/testing/withPluginManager.d.ts +9 -8
- package/dist/types/src/testing/withPluginManager.d.ts.map +1 -1
- package/dist/types/src/testing/withPluginManager.stories.d.ts +9 -3
- package/dist/types/src/testing/withPluginManager.stories.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/moon.yml +5 -1
- package/package.json +48 -38
- package/src/common/capabilities.ts +116 -20
- package/src/common/collaboration.ts +6 -9
- package/src/common/events.ts +3 -1
- package/src/common/file.ts +1 -1
- package/src/common/index.ts +1 -1
- package/src/common/layout.ts +3 -4
- package/src/common/surface.ts +23 -20
- package/src/common/translations.ts +1 -1
- package/src/core/capabilities.test.ts +3 -3
- package/src/core/capabilities.ts +36 -27
- package/src/core/manager.test.ts +22 -21
- package/src/core/manager.ts +141 -56
- package/src/core/plugin.ts +13 -2
- package/src/helpers.test.ts +1 -1
- package/src/index.ts +0 -2
- package/src/playground/debug/Debug.tsx +1 -1
- package/src/playground/debug/plugin.ts +7 -8
- package/src/playground/generator/Main.tsx +0 -1
- package/src/playground/generator/Toolbar.tsx +2 -1
- package/src/playground/generator/generator.ts +4 -4
- package/src/playground/generator/plugin.ts +12 -13
- package/src/playground/layout/plugin.ts +10 -9
- package/src/playground/logger/Toolbar.tsx +2 -1
- package/src/playground/logger/plugin.ts +30 -25
- package/src/playground/logger/schema.ts +1 -1
- package/src/playground/playground.stories.tsx +30 -17
- package/src/plugin-intent/IntentPlugin.ts +13 -13
- package/src/plugin-intent/actions.ts +4 -6
- package/src/plugin-intent/errors.ts +2 -1
- package/src/plugin-intent/index.ts +1 -0
- package/src/plugin-intent/intent-dispatcher.test.ts +10 -3
- package/src/plugin-intent/intent-dispatcher.ts +21 -12
- package/src/plugin-intent/intent.ts +1 -1
- package/src/plugin-intent/meta.ts +10 -0
- package/src/plugin-settings/SettingsPlugin.ts +27 -28
- package/src/plugin-settings/actions.ts +9 -13
- package/src/plugin-settings/app-graph-builder.ts +25 -22
- package/src/plugin-settings/intent-resolver.ts +5 -4
- package/src/plugin-settings/meta.ts +10 -0
- package/src/plugin-settings/store.ts +3 -3
- package/src/plugin-settings/translations.ts +4 -4
- package/src/react/App.stories.tsx +34 -0
- package/src/react/App.tsx +59 -0
- package/src/react/DefaultFallback.tsx +26 -0
- package/src/react/ErrorBoundary.tsx +26 -15
- package/src/react/IntentContext.tsx +3 -2
- package/src/react/Surface.stories.tsx +100 -55
- package/src/react/Surface.tsx +134 -43
- package/src/react/SurfaceInfo.tsx +107 -0
- package/src/react/common.ts +2 -1
- package/src/react/index.ts +4 -0
- package/src/react/types.ts +38 -0
- package/src/react/useApp.tsx +165 -0
- package/src/react/useCapabilities.ts +4 -3
- package/src/react/useLoading.tsx +70 -0
- package/src/testing/withPluginManager.stories.tsx +9 -5
- package/src/testing/withPluginManager.tsx +33 -32
- package/tsconfig.json +11 -9
- package/vitest.config.ts +8 -6
- package/.swc/plugins/v7_linux_x86_64_13.0.0/f45bdff002284d9e8f9ef3f0be909de12da36c049cbcf261ac78fc00abb09a2d +0 -0
- package/dist/lib/browser/app-graph-builder-LYF7EKNN.mjs.map +0 -7
- package/dist/lib/browser/chunk-FMN65HSW.mjs.map +0 -7
- package/dist/lib/browser/chunk-FO2PH7M3.mjs +0 -415
- package/dist/lib/browser/chunk-FO2PH7M3.mjs.map +0 -7
- package/dist/lib/browser/chunk-ORWHM7CO.mjs.map +0 -7
- package/dist/lib/browser/intent-resolver-ZTNOSO3A.mjs.map +0 -7
- package/dist/lib/browser/store-KML2R4IE.mjs.map +0 -7
- package/dist/lib/browser/worker.mjs +0 -79
- package/dist/lib/node-esm/app-graph-builder-SAOWGJDK.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-73HGSHKE.mjs +0 -416
- package/dist/lib/node-esm/chunk-73HGSHKE.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-UMZQERLE.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-ZEZ4FVEU.mjs.map +0 -7
- package/dist/lib/node-esm/intent-resolver-W7Z7WFFM.mjs.map +0 -7
- package/dist/lib/node-esm/store-QEXGXLWZ.mjs.map +0 -7
- package/dist/lib/node-esm/worker.mjs +0 -80
- package/dist/types/src/App.d.ts.map +0 -1
- package/dist/types/src/worker.d.ts +0 -4
- package/dist/types/src/worker.d.ts.map +0 -1
- package/src/App.tsx +0 -276
- package/src/worker.ts +0 -11
- /package/dist/lib/browser/{intent-dispatcher-LSYQZSEB.mjs.map → intent-dispatcher-MGSN6NWK.mjs.map} +0 -0
- /package/dist/lib/browser/{worker.mjs.map → react/index.mjs.map} +0 -0
- /package/dist/lib/node-esm/{intent-dispatcher-6CYNGPSW.mjs.map → intent-dispatcher-2M6HMLXG.mjs.map} +0 -0
- /package/dist/lib/node-esm/{worker.mjs.map → react/index.mjs.map} +0 -0
package/src/react/Surface.tsx
CHANGED
|
@@ -2,26 +2,124 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import React, {
|
|
5
|
+
import React, {
|
|
6
|
+
type ComponentType,
|
|
7
|
+
type Context,
|
|
8
|
+
Fragment,
|
|
9
|
+
type NamedExoticComponent,
|
|
10
|
+
type RefAttributes,
|
|
11
|
+
Suspense,
|
|
12
|
+
createContext,
|
|
13
|
+
forwardRef,
|
|
14
|
+
memo,
|
|
15
|
+
useContext,
|
|
16
|
+
useMemo,
|
|
17
|
+
} from 'react';
|
|
6
18
|
|
|
19
|
+
import { raise } from '@dxos/debug';
|
|
20
|
+
import { log } from '@dxos/log';
|
|
7
21
|
import { useDefaultValue } from '@dxos/react-hooks';
|
|
8
22
|
import { byPosition } from '@dxos/util';
|
|
9
23
|
|
|
10
|
-
import { ErrorBoundary } from './ErrorBoundary';
|
|
11
|
-
import { useCapabilities } from './useCapabilities';
|
|
12
24
|
import { Capabilities, type SurfaceDefinition, type SurfaceProps } from '../common';
|
|
13
25
|
import { type PluginContext } from '../core';
|
|
14
26
|
|
|
27
|
+
import { ErrorBoundary } from './ErrorBoundary';
|
|
28
|
+
import { SurfaceInfo } from './SurfaceInfo';
|
|
29
|
+
import { useCapabilities } from './useCapabilities';
|
|
30
|
+
|
|
15
31
|
const DEFAULT_PLACEHOLDER = <Fragment />;
|
|
16
32
|
|
|
33
|
+
const DEBUG = import.meta.env.VITE_DEBUG;
|
|
34
|
+
|
|
35
|
+
export type SurfaceContext = Pick<SurfaceProps, 'id' | 'role' | 'data'>;
|
|
36
|
+
|
|
37
|
+
// TODO(burdon): Use @radix-ui/react-context
|
|
38
|
+
const SurfaceContext: Context<SurfaceContext | undefined> = createContext<SurfaceContext | undefined>(undefined);
|
|
39
|
+
|
|
17
40
|
/**
|
|
18
|
-
*
|
|
41
|
+
* Wrapper component that provides context for a surface.
|
|
19
42
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
43
|
+
const SurfaceContextProvider = memo(
|
|
44
|
+
forwardRef<HTMLElement, SurfaceProps & { component: ComponentType<any> }>(
|
|
45
|
+
({ id, role, data, limit, fallback = DefaultFallback, component: Component, ...rest }, forwardedRef) => {
|
|
46
|
+
const contextValue = useMemo(() => ({ id, role, data }), [id, role, data]);
|
|
47
|
+
|
|
48
|
+
// TODO(burdon): Remove from production build?
|
|
49
|
+
const active = DEBUG || '__DX_DEBUG__' in window;
|
|
50
|
+
if (active) {
|
|
51
|
+
return (
|
|
52
|
+
<ErrorBoundary data={data} fallback={fallback}>
|
|
53
|
+
<SurfaceContext.Provider value={contextValue}>
|
|
54
|
+
<SurfaceInfo ref={forwardedRef}>
|
|
55
|
+
<Component id={id} role={role} data={data} limit={limit} {...rest} />
|
|
56
|
+
</SurfaceInfo>
|
|
57
|
+
</SurfaceContext.Provider>
|
|
58
|
+
</ErrorBoundary>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
<ErrorBoundary data={data} fallback={fallback}>
|
|
64
|
+
<SurfaceContext.Provider value={contextValue}>
|
|
65
|
+
<Component id={id} role={role} data={data} limit={limit} {...rest} ref={forwardedRef} />
|
|
66
|
+
</SurfaceContext.Provider>
|
|
67
|
+
</ErrorBoundary>
|
|
68
|
+
);
|
|
69
|
+
},
|
|
70
|
+
),
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
SurfaceContextProvider.displayName = 'SurfaceContextProvider';
|
|
74
|
+
|
|
75
|
+
export const useSurface = (): SurfaceContext => {
|
|
76
|
+
const context = useContext(SurfaceContext) ?? raise(new Error('Missing SurfaceContext'));
|
|
77
|
+
return context;
|
|
23
78
|
};
|
|
24
79
|
|
|
80
|
+
/**
|
|
81
|
+
* A surface is a named region of the screen that can be populated by plugins.
|
|
82
|
+
*/
|
|
83
|
+
export const Surface: NamedExoticComponent<SurfaceProps & RefAttributes<HTMLElement>> = memo(
|
|
84
|
+
forwardRef(({ id: _id, role, data: dataParam, limit, placeholder = DEFAULT_PLACEHOLDER, ...rest }, forwardedRef) => {
|
|
85
|
+
const data = useDefaultValue(dataParam, () => ({}));
|
|
86
|
+
|
|
87
|
+
// TODO(wittjosiah): This will make all surfaces depend on a single signal.
|
|
88
|
+
// This isn't ideal because it means that any change to the data will cause all surfaces to re-render.
|
|
89
|
+
// This effectively means that plugin modules which contribute surfaces need to all be activated at startup.
|
|
90
|
+
// This should be fine for now because it's how it worked prior to capabilities api anyway.
|
|
91
|
+
// In the future, it would be nice to be able to bucket the surface contributions by role.
|
|
92
|
+
const surfaces = useSurfaces();
|
|
93
|
+
|
|
94
|
+
// NOTE: Memoizing the candidates makes the surface not re-render based on reactivity within data.
|
|
95
|
+
const definitions = findCandidates(surfaces, { role, data });
|
|
96
|
+
const candidates = limit ? definitions.slice(0, limit) : definitions;
|
|
97
|
+
if (DEBUG && candidates.length === 0) {
|
|
98
|
+
log.warn('no candidates for surface', { role, data });
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return (
|
|
103
|
+
<Suspense fallback={placeholder}>
|
|
104
|
+
{candidates.map(({ id, component }) => (
|
|
105
|
+
<SurfaceContextProvider
|
|
106
|
+
key={id}
|
|
107
|
+
id={id}
|
|
108
|
+
role={role}
|
|
109
|
+
data={data}
|
|
110
|
+
limit={limit}
|
|
111
|
+
component={component}
|
|
112
|
+
ref={forwardedRef}
|
|
113
|
+
{...rest}
|
|
114
|
+
/>
|
|
115
|
+
))}
|
|
116
|
+
</Suspense>
|
|
117
|
+
);
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
Surface.displayName = 'Surface';
|
|
122
|
+
|
|
25
123
|
const findCandidates = (surfaces: SurfaceDefinition[], { role, data }: Pick<SurfaceProps, 'role' | 'data'>) => {
|
|
26
124
|
return Object.values(surfaces)
|
|
27
125
|
.filter((definition) =>
|
|
@@ -31,6 +129,35 @@ const findCandidates = (surfaces: SurfaceDefinition[], { role, data }: Pick<Surf
|
|
|
31
129
|
.toSorted(byPosition);
|
|
32
130
|
};
|
|
33
131
|
|
|
132
|
+
// TODO(burdon): Make user facing, with telemetry.
|
|
133
|
+
// TODO(burdon): Change based on dev/prod mode; infer subject type, id.
|
|
134
|
+
const DefaultFallback = ({ data, error, dev }: { data: any; error: Error; dev?: boolean }) => {
|
|
135
|
+
if (dev) {
|
|
136
|
+
return (
|
|
137
|
+
<div className='flex flex-col gap-4 p-4 is-full overflow-y-auto'>
|
|
138
|
+
<h1 className='flex gap-2 text-sm mbs-2'>{error.message}</h1>
|
|
139
|
+
<pre className='overflow-auto text-xs text-description'>{JSON.stringify(data, null, 2)}</pre>
|
|
140
|
+
</div>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<div className='flex flex-col gap-4 p-4 is-full overflow-y-auto border border-roseFill'>
|
|
146
|
+
<h1 className='flex gap-2 text-sm mbs-2 text-rose-500'>{error.message}</h1>
|
|
147
|
+
<pre className='overflow-auto text-xs text-description'>{error.stack}</pre>
|
|
148
|
+
<pre className='overflow-auto text-xs text-description'>{JSON.stringify(data, null, 2)}</pre>
|
|
149
|
+
</div>
|
|
150
|
+
);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @internal
|
|
155
|
+
*/
|
|
156
|
+
export const useSurfaces = () => {
|
|
157
|
+
const surfaces = useCapabilities(Capabilities.ReactSurface);
|
|
158
|
+
return useMemo(() => surfaces.flat(), [surfaces]);
|
|
159
|
+
};
|
|
160
|
+
|
|
34
161
|
/**
|
|
35
162
|
* @returns `true` if there is a contributed surface which matches the specified role & data, `false` otherwise.
|
|
36
163
|
*/
|
|
@@ -39,39 +166,3 @@ export const isSurfaceAvailable = (context: PluginContext, { role, data }: Pick<
|
|
|
39
166
|
const candidates = findCandidates(surfaces.flat(), { role, data });
|
|
40
167
|
return candidates.length > 0;
|
|
41
168
|
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* A surface is a named region of the screen that can be populated by plugins.
|
|
45
|
-
*/
|
|
46
|
-
export const Surface = memo(
|
|
47
|
-
forwardRef<HTMLElement, SurfaceProps>(
|
|
48
|
-
({ id: _id, role, data: _data, limit, fallback, placeholder = DEFAULT_PLACEHOLDER, ...rest }, forwardedRef) => {
|
|
49
|
-
// TODO(wittjosiah): This will make all surfaces depend on a single signal.
|
|
50
|
-
// This isn't ideal because it means that any change to the data will cause all surfaces to re-render.
|
|
51
|
-
// This effectively means that plugin modules which contribute surfaces need to all be activated at startup.
|
|
52
|
-
// This should be fine for now because it's how it worked prior to capabilities api anyways.
|
|
53
|
-
// In the future, it would be nice to be able to bucket the surface contributions by role.
|
|
54
|
-
const surfaces = useSurfaces();
|
|
55
|
-
const data = useDefaultValue(_data, () => ({}));
|
|
56
|
-
|
|
57
|
-
// NOTE: Memoizing the candidates makes the surface not re-render based on reactivity within data.
|
|
58
|
-
const definitions = findCandidates(surfaces, { role, data });
|
|
59
|
-
const candidates = limit ? definitions.slice(0, limit) : definitions;
|
|
60
|
-
const nodes = candidates.map(({ component: Component, id }) => (
|
|
61
|
-
<Component ref={forwardedRef} key={id} id={id} role={role} data={data} limit={limit} {...rest} />
|
|
62
|
-
));
|
|
63
|
-
|
|
64
|
-
const suspense = <Suspense fallback={placeholder}>{nodes}</Suspense>;
|
|
65
|
-
|
|
66
|
-
return fallback ? (
|
|
67
|
-
<ErrorBoundary data={data} fallback={fallback}>
|
|
68
|
-
{suspense}
|
|
69
|
-
</ErrorBoundary>
|
|
70
|
-
) : (
|
|
71
|
-
suspense
|
|
72
|
-
);
|
|
73
|
-
},
|
|
74
|
-
),
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
Surface.displayName = 'Surface';
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, {
|
|
6
|
+
type ReactElement,
|
|
7
|
+
type Ref,
|
|
8
|
+
cloneElement,
|
|
9
|
+
forwardRef,
|
|
10
|
+
useCallback,
|
|
11
|
+
useLayoutEffect,
|
|
12
|
+
useState,
|
|
13
|
+
} from 'react';
|
|
14
|
+
import { createPortal } from 'react-dom';
|
|
15
|
+
|
|
16
|
+
import { addEventListener, combine } from '@dxos/async';
|
|
17
|
+
import { useMergeRefs } from '@dxos/react-hooks';
|
|
18
|
+
|
|
19
|
+
import { useSurface } from './Surface';
|
|
20
|
+
|
|
21
|
+
export type SurfaceInfoProps = {
|
|
22
|
+
children: ReactElement<{ ref?: Ref<HTMLElement> }>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Debug wrapper for surfaces.
|
|
27
|
+
*/
|
|
28
|
+
export const SurfaceInfo = forwardRef<HTMLElement, SurfaceInfoProps>(({ children }, forwardedRef) => {
|
|
29
|
+
const [rect, setRect] = useState<DOMRect | null>(null);
|
|
30
|
+
const [expand, setExpand] = useState(false);
|
|
31
|
+
const info = useSurface();
|
|
32
|
+
|
|
33
|
+
const [root, setRoot] = useState<HTMLElement | null>(null);
|
|
34
|
+
const measureRef = useCallback((node: HTMLElement | null) => setRoot(node), []);
|
|
35
|
+
const mergedRef = useMergeRefs([measureRef, forwardedRef]);
|
|
36
|
+
const childWithRef = cloneElement(children, { ref: mergedRef });
|
|
37
|
+
|
|
38
|
+
useLayoutEffect(() => {
|
|
39
|
+
if (!root) {
|
|
40
|
+
setRect(null);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const measure = () => {
|
|
45
|
+
setRect(root.getBoundingClientRect());
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const observer = new ResizeObserver(measure);
|
|
49
|
+
observer.observe(root);
|
|
50
|
+
measure();
|
|
51
|
+
|
|
52
|
+
return combine(
|
|
53
|
+
addEventListener(window, 'scroll', measure, true),
|
|
54
|
+
addEventListener(window, 'resize', measure),
|
|
55
|
+
() => {
|
|
56
|
+
observer.disconnect();
|
|
57
|
+
},
|
|
58
|
+
);
|
|
59
|
+
}, [root]);
|
|
60
|
+
|
|
61
|
+
const padding = 0;
|
|
62
|
+
return (
|
|
63
|
+
<>
|
|
64
|
+
{childWithRef}
|
|
65
|
+
{rect &&
|
|
66
|
+
createPortal(
|
|
67
|
+
<div
|
|
68
|
+
role='none'
|
|
69
|
+
className='z-[100] fixed flex flex-col-reverse scrollbar-none overflow-auto pointer-events-none'
|
|
70
|
+
style={{
|
|
71
|
+
top: rect.top + padding,
|
|
72
|
+
left: rect.left + padding,
|
|
73
|
+
width: rect.width - padding * 2,
|
|
74
|
+
height: rect.height - padding * 2,
|
|
75
|
+
}}
|
|
76
|
+
>
|
|
77
|
+
{expand ? (
|
|
78
|
+
<div
|
|
79
|
+
className='absolute inset-0 bg-deckSurface border border-green-500 cursor-pointer pointer-events-auto overflow-auto'
|
|
80
|
+
onPointerDown={(ev) => ev.stopPropagation()}
|
|
81
|
+
onClick={(ev) => {
|
|
82
|
+
ev.stopPropagation();
|
|
83
|
+
setExpand(false);
|
|
84
|
+
}}
|
|
85
|
+
>
|
|
86
|
+
<pre className='p-2 text-xs text-description font-mono'>{JSON.stringify({ info }, null, 2)}</pre>
|
|
87
|
+
</div>
|
|
88
|
+
) : (
|
|
89
|
+
<span
|
|
90
|
+
className='absolute right-1 bottom-0 flex items-center p-1 text-green-500 opacity-80 hover:opacity-100 text-xl cursor-pointer pointer-events-auto'
|
|
91
|
+
title={info.id}
|
|
92
|
+
onPointerDown={(ev) => ev.stopPropagation()}
|
|
93
|
+
onClick={(ev) => {
|
|
94
|
+
ev.stopPropagation();
|
|
95
|
+
setExpand(true);
|
|
96
|
+
}}
|
|
97
|
+
>
|
|
98
|
+
ⓘ
|
|
99
|
+
</span>
|
|
100
|
+
)}
|
|
101
|
+
</div>,
|
|
102
|
+
// TODO(burdon): Create well-known element to gather all debug portals.
|
|
103
|
+
document.body,
|
|
104
|
+
)}
|
|
105
|
+
</>
|
|
106
|
+
);
|
|
107
|
+
});
|
package/src/react/common.ts
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { useCapability } from './useCapabilities';
|
|
6
5
|
import { Capabilities } from '../common';
|
|
7
6
|
|
|
7
|
+
import { useCapability } from './useCapabilities';
|
|
8
|
+
|
|
8
9
|
export const useIntentDispatcher = () => useCapability(Capabilities.IntentDispatcher);
|
|
9
10
|
|
|
10
11
|
export const useAppGraph = () => useCapability(Capabilities.AppGraph);
|
package/src/react/index.ts
CHANGED
|
@@ -3,8 +3,12 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
export * from './common';
|
|
6
|
+
export * from './types';
|
|
7
|
+
|
|
6
8
|
export * from './ErrorBoundary';
|
|
7
9
|
export * from './PluginManagerProvider';
|
|
8
10
|
export * from './Surface';
|
|
11
|
+
|
|
12
|
+
export * from './useApp';
|
|
9
13
|
export * from './useCapabilities';
|
|
10
14
|
export * from './useIntentResolver';
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
|
|
7
|
+
import { type Obj } from '@dxos/echo';
|
|
8
|
+
|
|
9
|
+
export const SurfaceCardRole = Schema.Literal(
|
|
10
|
+
'card',
|
|
11
|
+
'card--popover',
|
|
12
|
+
'card--intrinsic',
|
|
13
|
+
'card--extrinsic',
|
|
14
|
+
'card--transclusion',
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
export type SurfaceCardRole = Schema.Schema.Type<typeof SurfaceCardRole>;
|
|
18
|
+
|
|
19
|
+
// TODO(burdon): Define all roles.
|
|
20
|
+
export type SurfaceRole =
|
|
21
|
+
| 'item'
|
|
22
|
+
| 'article'
|
|
23
|
+
| 'complementary' // (for companion?)
|
|
24
|
+
| 'section'
|
|
25
|
+
| SurfaceCardRole;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Base type for surface components.
|
|
29
|
+
*/
|
|
30
|
+
// TODO(burdon): Standardize PluginSettings and ObjectProperties.
|
|
31
|
+
// TODO(burdon): Include attendableId?
|
|
32
|
+
// TODO(burdon): companionTo?
|
|
33
|
+
export type SurfaceComponentProps<Subject extends Obj.Any = Obj.Any, Role extends string = string> = {
|
|
34
|
+
role?: Role;
|
|
35
|
+
|
|
36
|
+
/** The primary object being displayed. */
|
|
37
|
+
subject: Subject;
|
|
38
|
+
};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { RegistryContext } from '@effect-atom/atom-react';
|
|
6
|
+
import { effect } from '@preact/signals-core';
|
|
7
|
+
import React, { type FC, useCallback, useEffect, useMemo } from 'react';
|
|
8
|
+
|
|
9
|
+
import { invariant } from '@dxos/invariant';
|
|
10
|
+
import { live } from '@dxos/live-object';
|
|
11
|
+
import { useAsyncEffect, useDefaultValue } from '@dxos/react-hooks';
|
|
12
|
+
|
|
13
|
+
import { Capabilities, Events } from '../common';
|
|
14
|
+
import { type Plugin, PluginManager, type PluginManagerOptions } from '../core';
|
|
15
|
+
|
|
16
|
+
import { App } from './App';
|
|
17
|
+
import { DefaultFallback } from './DefaultFallback';
|
|
18
|
+
import { ErrorBoundary } from './ErrorBoundary';
|
|
19
|
+
import { PluginManagerProvider } from './PluginManagerProvider';
|
|
20
|
+
|
|
21
|
+
const ENABLED_KEY = 'dxos.org/app-framework/enabled';
|
|
22
|
+
|
|
23
|
+
export type UseAppOptions = {
|
|
24
|
+
pluginManager?: PluginManager;
|
|
25
|
+
pluginLoader?: PluginManagerOptions['pluginLoader'];
|
|
26
|
+
plugins?: Plugin[];
|
|
27
|
+
core?: string[];
|
|
28
|
+
defaults?: string[];
|
|
29
|
+
placeholder?: FC<{ stage: number }>;
|
|
30
|
+
fallback?: ErrorBoundary['props']['fallback'];
|
|
31
|
+
cacheEnabled?: boolean;
|
|
32
|
+
safeMode?: boolean;
|
|
33
|
+
debounce?: number;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Expected usage is for this to be the entrypoint of the application.
|
|
38
|
+
* Initializes plugins and renders the root components.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* const plugins = [LayoutPlugin(), MyPlugin()];
|
|
42
|
+
* const core = [LayoutPluginId];
|
|
43
|
+
* const default = [MyPluginId];
|
|
44
|
+
* const fallback = <div>Initializing Plugins...</div>;
|
|
45
|
+
* const App = useApp({ plugins, core, default, fallback });
|
|
46
|
+
* createRoot(document.getElementById('root')!).render(
|
|
47
|
+
* <StrictMode>
|
|
48
|
+
* <App />
|
|
49
|
+
* </StrictMode>,
|
|
50
|
+
* );
|
|
51
|
+
*
|
|
52
|
+
* @param params.pluginLoader A function which loads new plugins.
|
|
53
|
+
* @param params.plugins All plugins available to the application.
|
|
54
|
+
* @param params.core Core plugins which will always be enabled.
|
|
55
|
+
* @param params.defaults Default plugins are enabled by default but can be disabled by the user.
|
|
56
|
+
* @param params.placeholder Placeholder component to render during startup.
|
|
57
|
+
* @param params.fallback Fallback component to render if an error occurs during startup.
|
|
58
|
+
* @param params.cacheEnabled Whether to cache enabled plugins in localStorage.
|
|
59
|
+
* @param params.safeMode Whether to enable safe mode, which disables optional plugins.
|
|
60
|
+
*/
|
|
61
|
+
export const useApp = ({
|
|
62
|
+
pluginManager,
|
|
63
|
+
pluginLoader: pluginLoaderParam,
|
|
64
|
+
plugins: pluginsParam,
|
|
65
|
+
core: coreParam,
|
|
66
|
+
defaults: defaultsParam,
|
|
67
|
+
placeholder,
|
|
68
|
+
fallback = DefaultFallback,
|
|
69
|
+
cacheEnabled = false,
|
|
70
|
+
safeMode = false,
|
|
71
|
+
debounce = 0,
|
|
72
|
+
}: UseAppOptions) => {
|
|
73
|
+
const plugins = useDefaultValue(pluginsParam, () => []);
|
|
74
|
+
const core = useDefaultValue(coreParam, () => plugins.map(({ meta }) => meta.id));
|
|
75
|
+
const defaults = useDefaultValue(defaultsParam, () => []);
|
|
76
|
+
|
|
77
|
+
// TODO(wittjosiah): Provide a custom plugin loader which supports loading via url.
|
|
78
|
+
const pluginLoader = useMemo(
|
|
79
|
+
() =>
|
|
80
|
+
pluginLoaderParam ??
|
|
81
|
+
((id: string) => {
|
|
82
|
+
const plugin = plugins.find((plugin) => plugin.meta.id === id);
|
|
83
|
+
invariant(plugin, `Plugin not found: ${id}`);
|
|
84
|
+
return plugin;
|
|
85
|
+
}),
|
|
86
|
+
[pluginLoaderParam, plugins],
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const state = useMemo(() => live({ ready: false, error: null }), []);
|
|
90
|
+
const cached: string[] = useMemo(() => JSON.parse(localStorage.getItem(ENABLED_KEY) ?? '[]'), []);
|
|
91
|
+
const enabled = useMemo(
|
|
92
|
+
() => (safeMode ? [] : cacheEnabled && cached.length > 0 ? cached : defaults),
|
|
93
|
+
[safeMode, cacheEnabled, cached, defaults],
|
|
94
|
+
);
|
|
95
|
+
const manager = useMemo(
|
|
96
|
+
() => pluginManager ?? new PluginManager({ pluginLoader, plugins, core, enabled }),
|
|
97
|
+
[pluginManager, pluginLoader, plugins, core, enabled],
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
return manager.activation.on(({ event, state: _state, error }) => {
|
|
102
|
+
// Once the app is ready the first time, don't show the fallback again.
|
|
103
|
+
if (!state.ready && event === Events.Startup.id) {
|
|
104
|
+
state.ready = _state === 'activated';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (error && !state.ready && !state.error) {
|
|
108
|
+
state.error = error;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}, [manager, state]);
|
|
112
|
+
|
|
113
|
+
useEffect(() => {
|
|
114
|
+
effect(() => {
|
|
115
|
+
cacheEnabled && localStorage.setItem(ENABLED_KEY, JSON.stringify(manager.enabled));
|
|
116
|
+
});
|
|
117
|
+
}, [cacheEnabled, manager]);
|
|
118
|
+
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
setupDevtools(manager);
|
|
121
|
+
}, [manager]);
|
|
122
|
+
|
|
123
|
+
useAsyncEffect(async () => {
|
|
124
|
+
manager.context.contributeCapability({
|
|
125
|
+
interface: Capabilities.PluginManager,
|
|
126
|
+
implementation: manager,
|
|
127
|
+
module: 'dxos.org/app-framework/plugin-manager',
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
manager.context.contributeCapability({
|
|
131
|
+
interface: Capabilities.AtomRegistry,
|
|
132
|
+
implementation: manager.registry,
|
|
133
|
+
module: 'dxos.org/app-framework/atom-registry',
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
await Promise.all([
|
|
137
|
+
// TODO(wittjosiah): Factor out such that this could be called per surface role when attempting to render.
|
|
138
|
+
manager.activate(Events.SetupReactSurface),
|
|
139
|
+
manager.activate(Events.Startup),
|
|
140
|
+
]);
|
|
141
|
+
|
|
142
|
+
return () => {
|
|
143
|
+
manager.context.removeCapability(Capabilities.PluginManager, manager);
|
|
144
|
+
manager.context.removeCapability(Capabilities.AtomRegistry, manager.registry);
|
|
145
|
+
};
|
|
146
|
+
}, [manager]);
|
|
147
|
+
|
|
148
|
+
return useCallback(
|
|
149
|
+
() => (
|
|
150
|
+
<ErrorBoundary fallback={fallback}>
|
|
151
|
+
<PluginManagerProvider value={manager}>
|
|
152
|
+
<RegistryContext.Provider value={manager.registry}>
|
|
153
|
+
<App placeholder={placeholder} state={state} debounce={debounce} />
|
|
154
|
+
</RegistryContext.Provider>
|
|
155
|
+
</PluginManagerProvider>
|
|
156
|
+
</ErrorBoundary>
|
|
157
|
+
),
|
|
158
|
+
[fallback, manager, placeholder, state],
|
|
159
|
+
);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const setupDevtools = (manager: PluginManager) => {
|
|
163
|
+
(globalThis as any).composer ??= {};
|
|
164
|
+
(globalThis as any).composer.manager = manager;
|
|
165
|
+
};
|
|
@@ -2,20 +2,21 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { useAtomValue } from '@effect-atom/atom-react';
|
|
6
6
|
|
|
7
7
|
import { invariant } from '@dxos/invariant';
|
|
8
8
|
|
|
9
|
-
import { usePluginManager } from './PluginManagerProvider';
|
|
10
9
|
import { type InterfaceDef } from '../core';
|
|
11
10
|
|
|
11
|
+
import { usePluginManager } from './PluginManagerProvider';
|
|
12
|
+
|
|
12
13
|
/**
|
|
13
14
|
* Hook to request capabilities from the plugin context.
|
|
14
15
|
* @returns An array of capabilities.
|
|
15
16
|
*/
|
|
16
17
|
export const useCapabilities = <T>(interfaceDef: InterfaceDef<T>) => {
|
|
17
18
|
const manager = usePluginManager();
|
|
18
|
-
return
|
|
19
|
+
return useAtomValue(manager.context.capabilities(interfaceDef));
|
|
19
20
|
};
|
|
20
21
|
|
|
21
22
|
/**
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { useEffect, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { type AppProps } from './App';
|
|
8
|
+
|
|
9
|
+
export enum LoadingState {
|
|
10
|
+
Loading = 0,
|
|
11
|
+
FadeIn = 1,
|
|
12
|
+
FadeOut = 2,
|
|
13
|
+
Done = 3,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* To avoid "flashing" the placeholder, we wait a period of time before starting the loading animation.
|
|
18
|
+
* If loading completes during this time the placehoder is not shown, otherwise is it displayed for a minimum period of time.
|
|
19
|
+
*
|
|
20
|
+
* States:
|
|
21
|
+
* 0: Loading - Wait for a period of time before starting the loading animation.
|
|
22
|
+
* 1: Fade-in - Display a loading animation.
|
|
23
|
+
* 2: Fade-out - Fade out the loading animation.
|
|
24
|
+
* 3: Done - Remove the placeholder.
|
|
25
|
+
*/
|
|
26
|
+
export const useLoading = (state: AppProps['state'], debounce = 0) => {
|
|
27
|
+
const [stage, setStage] = useState<LoadingState>(LoadingState.Loading);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!debounce) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const i = setInterval(() => {
|
|
34
|
+
setStage((stage) => {
|
|
35
|
+
switch (stage) {
|
|
36
|
+
case LoadingState.Loading: {
|
|
37
|
+
if (!state.ready) {
|
|
38
|
+
return LoadingState.FadeIn;
|
|
39
|
+
} else {
|
|
40
|
+
clearInterval(i);
|
|
41
|
+
return LoadingState.Done;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
case LoadingState.FadeIn: {
|
|
46
|
+
if (state.ready) {
|
|
47
|
+
return LoadingState.FadeOut;
|
|
48
|
+
}
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
case LoadingState.FadeOut: {
|
|
53
|
+
clearInterval(i);
|
|
54
|
+
return LoadingState.Done;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return stage;
|
|
59
|
+
});
|
|
60
|
+
}, debounce);
|
|
61
|
+
|
|
62
|
+
return () => clearInterval(i);
|
|
63
|
+
}, [debounce]);
|
|
64
|
+
|
|
65
|
+
if (!debounce) {
|
|
66
|
+
return state.ready ? LoadingState.Done : LoadingState.Loading;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return stage;
|
|
70
|
+
};
|
|
@@ -2,15 +2,17 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
5
6
|
import React from 'react';
|
|
6
7
|
|
|
7
|
-
import { withTheme
|
|
8
|
+
import { withTheme } from '@dxos/react-ui/testing';
|
|
8
9
|
|
|
9
|
-
import { withPluginManager } from './withPluginManager';
|
|
10
10
|
import { Capabilities, createSurface } from '../common';
|
|
11
11
|
import { contributes } from '../core';
|
|
12
12
|
import { Surface } from '../react';
|
|
13
13
|
|
|
14
|
+
import { withPluginManager } from './withPluginManager';
|
|
15
|
+
|
|
14
16
|
const DefaultStory = () => {
|
|
15
17
|
console.log('Render');
|
|
16
18
|
return (
|
|
@@ -21,7 +23,7 @@ const DefaultStory = () => {
|
|
|
21
23
|
);
|
|
22
24
|
};
|
|
23
25
|
|
|
24
|
-
const meta
|
|
26
|
+
const meta = {
|
|
25
27
|
title: 'sdk/app-framework/withPluginManager',
|
|
26
28
|
render: DefaultStory,
|
|
27
29
|
decorators: [
|
|
@@ -39,8 +41,10 @@ const meta: Meta = {
|
|
|
39
41
|
],
|
|
40
42
|
}),
|
|
41
43
|
],
|
|
42
|
-
}
|
|
44
|
+
} satisfies Meta<typeof DefaultStory>;
|
|
43
45
|
|
|
44
46
|
export default meta;
|
|
45
47
|
|
|
46
|
-
|
|
48
|
+
type Story = StoryObj<typeof meta>;
|
|
49
|
+
|
|
50
|
+
export const Default: Story = {};
|