@genesislcap/foundation-react-utils 15.46.2-FUI-2628.3 → 15.46.2-FUI-2625.3

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/README.md CHANGED
@@ -47,65 +47,6 @@ resolving a second copy of the library, which breaks the `instanceof` checks `fl
47
47
  relies on internally; the entry point logs a warning when the version it loads differs from the one
48
48
  the theme was generated against.
49
49
 
50
- ## foundation-workspace
51
-
52
- Workspaces (save/load of layouts, grid column state and filters) work in React through the element
53
- wrappers `@genesislcap/foundation-workspace/react` already ships, plus one hook for filter state:
54
-
55
- ```tsx
56
- import { registerFactory } from '@genesislcap/foundation-layout';
57
- import { reactFactory } from '@genesislcap/foundation-react-utils';
58
- import { useWorkspaceFilter } from '@genesislcap/foundation-react-utils/workspace';
59
- import { GridRegister, LayoutWrapper, WorkspaceManager } from '@genesislcap/foundation-workspace/react';
60
-
61
- const PositionsPanel = () => {
62
- const [search, setSearch] = useState('');
63
- useWorkspaceFilter('positions-search', search, setSearch);
64
- return (
65
- <GridRegister gridKey="positions">
66
- <RapidGridPro persistColumnStateKey="positions">…</RapidGridPro>
67
- </GridRegister>
68
- );
69
- };
70
-
71
- registerFactory('positions', reactFactory(PositionsPanel));
72
-
73
- <LayoutWrapper layoutKey="my-layout">
74
- <rapid-layout auto-save-key="my-layout">
75
- <rapid-layout-region>
76
- <rapid-layout-item registration="positions" title="Positions" />
77
- </rapid-layout-region>
78
- </rapid-layout>
79
- </LayoutWrapper>
80
-
81
- <WorkspaceManager />
82
- ```
83
-
84
- - Register `workspaceComponents` from `@genesislcap/foundation-workspace` with your design system.
85
- - Render layout panes through `registerFactory` + `reactFactory`, not as JSX children of
86
- `rapid-layout-item`. Loading a saved layout rebuilds the panes, and cloned DOM loses React-set
87
- properties such as `gridOptions`.
88
- - `useWorkspaceFilter` also applies the current workspace's saved value on mount, so a filter
89
- inside a pane that is recreated by a layout load still picks up its saved value.
90
-
91
- ### flexlayout-react layouts
92
-
93
- `LayoutWrapper` only covers `rapid-layout`. For a flexlayout-react layout, hold the `Model` in
94
- state and register it with `useWorkspaceFlexLayout`; loading a workspace hands you a new model:
95
-
96
- ```tsx
97
- import { Layout, Model } from '@genesislcap/foundation-react-utils/flexlayout';
98
- import { useWorkspaceFlexLayout } from '@genesislcap/foundation-react-utils/workspace';
99
-
100
- const [model, setModel] = useState(() => Model.fromJson(defaultLayout));
101
- useWorkspaceFlexLayout('home-layout', model, setModel);
102
-
103
- return <Layout model={model} factory={factory} />;
104
- ```
105
-
106
- Tabs opened or closed by a loaded layout mount and unmount as usual, so `GridRegister` and
107
- `useWorkspaceFilter` inside them pick up the workspace's state as they register.
108
-
109
50
  ## License
110
51
 
111
52
  Note: this project provides front-end dependencies and uses licensed components listed in the next section; thus, licenses for those components are required during development. Contact [Genesis Global](https://genesis.global/contact-us/) for more details.
@@ -531,127 +531,6 @@
531
531
  }
532
532
  }
533
533
  ]
534
- },
535
- {
536
- "kind": "javascript-module",
537
- "path": "src/workspace/index.ts",
538
- "declarations": [],
539
- "exports": [
540
- {
541
- "kind": "js",
542
- "name": "useWorkspaceFilter",
543
- "declaration": {
544
- "name": "useWorkspaceFilter",
545
- "module": "./use-workspace-filter"
546
- }
547
- },
548
- {
549
- "kind": "js",
550
- "name": "useWorkspaceFlexLayout",
551
- "declaration": {
552
- "name": "useWorkspaceFlexLayout",
553
- "module": "./use-workspace-flex-layout"
554
- }
555
- }
556
- ]
557
- },
558
- {
559
- "kind": "javascript-module",
560
- "path": "src/workspace/use-workspace-filter.ts",
561
- "declarations": [
562
- {
563
- "kind": "function",
564
- "name": "useWorkspaceFilter",
565
- "return": {
566
- "type": {
567
- "text": "void"
568
- }
569
- },
570
- "parameters": [
571
- {
572
- "name": "filterKey",
573
- "type": {
574
- "text": "string"
575
- },
576
- "description": "Unique key the value is saved under. An empty key skips registration."
577
- },
578
- {
579
- "name": "value",
580
- "type": {
581
- "text": "string"
582
- },
583
- "description": "Current filter value."
584
- },
585
- {
586
- "name": "setValue",
587
- "type": {
588
- "text": "(value: string) => void"
589
- },
590
- "description": "Called with the saved value when a workspace is loaded."
591
- }
592
- ],
593
- "description": "Registers a piece of React filter state with the foundation-workspace `FilterRegistry`, so\n`<workspace-manager>` saves it with the workspace and restores it on load."
594
- }
595
- ],
596
- "exports": [
597
- {
598
- "kind": "js",
599
- "name": "useWorkspaceFilter",
600
- "declaration": {
601
- "name": "useWorkspaceFilter",
602
- "module": "src/workspace/use-workspace-filter.ts"
603
- }
604
- }
605
- ]
606
- },
607
- {
608
- "kind": "javascript-module",
609
- "path": "src/workspace/use-workspace-flex-layout.ts",
610
- "declarations": [
611
- {
612
- "kind": "function",
613
- "name": "useWorkspaceFlexLayout",
614
- "return": {
615
- "type": {
616
- "text": "void"
617
- }
618
- },
619
- "parameters": [
620
- {
621
- "name": "layoutKey",
622
- "type": {
623
- "text": "string"
624
- },
625
- "description": "Unique key the layout is saved under. An empty key skips registration."
626
- },
627
- {
628
- "name": "model",
629
- "type": {
630
- "text": "Model"
631
- },
632
- "description": "The model currently rendered by `<Layout>`."
633
- },
634
- {
635
- "name": "setModel",
636
- "type": {
637
- "text": "(model: Model) => void"
638
- },
639
- "description": "Called with a new model when a workspace is loaded."
640
- }
641
- ],
642
- "description": "Registers a flexlayout-react `Model` with the foundation-workspace `LayoutRegistry`, so\n`<workspace-manager>` saves the layout (which tabs are open, how they're split and sized, which\nis selected) with the workspace and restores it on load."
643
- }
644
- ],
645
- "exports": [
646
- {
647
- "kind": "js",
648
- "name": "useWorkspaceFlexLayout",
649
- "declaration": {
650
- "name": "useWorkspaceFlexLayout",
651
- "module": "src/workspace/use-workspace-flex-layout.ts"
652
- }
653
- }
654
- ]
655
534
  }
656
535
  ]
657
536
  }
@@ -18,7 +18,6 @@
18
18
  * Rapid Design System theme for it installed on import. Import from there rather than adding
19
19
  * `flexlayout-react` to an app's own dependencies.
20
20
  * - `./router` — route table helpers (`renderAppRoutes` and friends).
21
- * - `./workspace` — foundation-workspace helpers (`useWorkspaceFilter`).
22
21
  */
23
22
  export { reactFactory, reactFactoryWithProvider } from './react-layout-factory';
24
23
  export { createReactRenderer } from './create-react-renderer';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,YAAY,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,yBAAyB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,EAAE,wBAAwB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,YAAY,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACzE,YAAY,EACV,0BAA0B,EAC1B,wBAAwB,EACxB,wBAAwB,GACzB,MAAM,iCAAiC,CAAC;AACzC,YAAY,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,YAAY,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAChF,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,yBAAyB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,EAAE,wBAAwB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,YAAY,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACzE,YAAY,EACV,0BAA0B,EAC1B,wBAAwB,EACxB,wBAAwB,GACzB,MAAM,iCAAiC,CAAC;AACzC,YAAY,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC"}
package/dist/esm/index.js CHANGED
@@ -18,7 +18,6 @@
18
18
  * Rapid Design System theme for it installed on import. Import from there rather than adding
19
19
  * `flexlayout-react` to an app's own dependencies.
20
20
  * - `./router` — route table helpers (`renderAppRoutes` and friends).
21
- * - `./workspace` — foundation-workspace helpers (`useWorkspaceFilter`).
22
21
  */
23
22
  export { reactFactory, reactFactoryWithProvider } from './react-layout-factory';
24
23
  export { createReactRenderer } from './create-react-renderer';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@genesislcap/foundation-react-utils",
3
3
  "description": "Genesis Foundation React Utils",
4
- "version": "15.46.2-FUI-2628.3",
4
+ "version": "15.46.2-FUI-2625.3",
5
5
  "sideEffects": [
6
6
  "./dist/esm/flexlayout/index.js"
7
7
  ],
@@ -21,10 +21,6 @@
21
21
  "types": "./dist/dts/flexlayout/index.d.ts",
22
22
  "default": "./dist/esm/flexlayout/index.js"
23
23
  },
24
- "./workspace": {
25
- "types": "./dist/dts/workspace/index.d.ts",
26
- "default": "./dist/esm/workspace/index.js"
27
- },
28
24
  "./package.json": "./package.json"
29
25
  },
30
26
  "typesVersions": {
@@ -34,9 +30,6 @@
34
30
  ],
35
31
  "flexlayout": [
36
32
  "./dist/dts/flexlayout/index.d.ts"
37
- ],
38
- "workspace": [
39
- "./dist/dts/workspace/index.d.ts"
40
33
  ]
41
34
  }
42
35
  },
@@ -63,12 +56,12 @@
63
56
  }
64
57
  },
65
58
  "devDependencies": {
66
- "@genesislcap/foundation-testing": "15.46.2-FUI-2628.3",
67
- "@genesislcap/genx": "15.46.2-FUI-2628.3",
68
- "@genesislcap/ts-builder": "15.46.2-FUI-2628.3",
69
- "@genesislcap/uvu-playwright-builder": "15.46.2-FUI-2628.3",
70
- "@genesislcap/vite-builder": "15.46.2-FUI-2628.3",
71
- "@genesislcap/webpack-builder": "15.46.2-FUI-2628.3",
59
+ "@genesislcap/foundation-testing": "15.46.2-FUI-2625.3",
60
+ "@genesislcap/genx": "15.46.2-FUI-2625.3",
61
+ "@genesislcap/ts-builder": "15.46.2-FUI-2625.3",
62
+ "@genesislcap/uvu-playwright-builder": "15.46.2-FUI-2625.3",
63
+ "@genesislcap/vite-builder": "15.46.2-FUI-2625.3",
64
+ "@genesislcap/webpack-builder": "15.46.2-FUI-2625.3",
72
65
  "react-router-dom": "^7.1.3"
73
66
  },
74
67
  "peerDependencies": {
@@ -82,12 +75,11 @@
82
75
  }
83
76
  },
84
77
  "dependencies": {
85
- "@genesislcap/foundation-forms": "15.46.2-FUI-2628.3",
86
- "@genesislcap/foundation-layout": "15.46.2-FUI-2628.3",
87
- "@genesislcap/foundation-logger": "15.46.2-FUI-2628.3",
88
- "@genesislcap/foundation-workspace": "15.46.2-FUI-2628.3",
89
- "@genesislcap/rapid-design-system": "15.46.2-FUI-2628.3",
90
- "@genesislcap/web-core": "15.46.2-FUI-2628.3",
78
+ "@genesislcap/foundation-forms": "15.46.2-FUI-2625.3",
79
+ "@genesislcap/foundation-layout": "15.46.2-FUI-2625.3",
80
+ "@genesislcap/foundation-logger": "15.46.2-FUI-2625.3",
81
+ "@genesislcap/rapid-design-system": "15.46.2-FUI-2625.3",
82
+ "@genesislcap/web-core": "15.46.2-FUI-2625.3",
91
83
  "@jsonforms/core": "^3.2.1",
92
84
  "@r2wc/core": "1.3.1",
93
85
  "@r2wc/react-to-web-component": "^2.0.2",
@@ -1,22 +0,0 @@
1
- /**
2
- * `@genesislcap/foundation-react-utils/workspace`
3
- *
4
- * React helpers for `@genesislcap/foundation-workspace` (save/load of layouts, grid column state
5
- * and filters).
6
- *
7
- * Exposed as a subpath (`/workspace`) rather than from the package root so that consumers which
8
- * don't use workspaces don't load foundation-workspace (and the grid-pro graph behind it).
9
- *
10
- * Grids, `rapid-layout` and the save/load UI need no hook: use the element wrappers from
11
- * `@genesislcap/foundation-workspace/react` (`GridRegister`, `LayoutWrapper`, `WorkspaceManager`).
12
- * Inside a `rapid-layout`, render panes through `registerFactory` + `reactFactory` rather than as
13
- * JSX children, because loading a saved layout rebuilds panes and cloned DOM loses React state.
14
- *
15
- * Key exports:
16
- * - `useWorkspaceFilter` — register React filter state so workspaces save and restore it.
17
- * - `useWorkspaceFlexLayout` — register a flexlayout-react `Model` so workspaces save and restore
18
- * the layout.
19
- */
20
- export { useWorkspaceFilter } from './use-workspace-filter';
21
- export { useWorkspaceFlexLayout } from './use-workspace-flex-layout';
22
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/workspace/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC"}
@@ -1,31 +0,0 @@
1
- /**
2
- * Registers a piece of React filter state with the foundation-workspace `FilterRegistry`, so
3
- * `<workspace-manager>` saves it with the workspace and restores it on load.
4
- *
5
- * @remarks
6
- * Grids and layouts are covered by the `GridRegister` / `LayoutWrapper` elements; filters have no
7
- * element wrapper because their value lives in app state, so this hook is the React equivalent.
8
- *
9
- * - Registers on mount and unregisters on unmount (or when `filterKey` changes).
10
- * - The registry always reads the latest `value` and calls the latest `setValue`, so neither needs
11
- * to be memoised.
12
- * - On mount, the registry applies its stored value for `filterKey` (if any): the loaded
13
- * workspace's value, or this filter's last value from earlier in the session. This covers a
14
- * filter that wasn't mounted when the workspace loaded (e.g. on another route), and a filter
15
- * inside a layout pane that a layout reload remounts.
16
- * - On unmount, the registry keeps the last value, so saving a workspace while the filter isn't
17
- * mounted still includes it.
18
- *
19
- * @param filterKey - Unique key the value is saved under. An empty key skips registration.
20
- * @param value - Current filter value.
21
- * @param setValue - Called with the saved value when a workspace is loaded.
22
- *
23
- * @example
24
- * ```tsx
25
- * const [instrument, setInstrument] = useState('');
26
- * useWorkspaceFilter('positions-instrument', instrument, setInstrument);
27
- * ```
28
- * @alpha
29
- */
30
- export declare function useWorkspaceFilter(filterKey: string, value: string, setValue: (value: string) => void): void;
31
- //# sourceMappingURL=use-workspace-filter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-workspace-filter.d.ts","sourceRoot":"","sources":["../../../src/workspace/use-workspace-filter.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAChC,IAAI,CAwBN"}
@@ -1,34 +0,0 @@
1
- import { Model } from 'flexlayout-react';
2
- /**
3
- * Registers a flexlayout-react `Model` with the foundation-workspace `LayoutRegistry`, so
4
- * `<workspace-manager>` saves the layout (which tabs are open, how they're split and sized, which
5
- * is selected) with the workspace and restores it on load.
6
- *
7
- * @remarks
8
- * The React counterpart of `LayoutWrapper` (`layout-register`), which only covers `rapid-layout`.
9
- * A workspace stores the model's `toJson()`; loading one builds a new model with `Model.fromJson`
10
- * and hands it to `setModel`, so the app renders `<Layout model={model} />` from state.
11
- *
12
- * - Registers on mount and unregisters on unmount (or when `layoutKey` changes). Registering
13
- * applies the layout's stored config straight away, when a workspace loaded earlier has one.
14
- * - The registry always reads the latest `model` and calls the latest `setModel`, so neither needs
15
- * to be memoised.
16
- * - Tabs that a loaded layout closes unmount, and ones it opens mount: their grids and filters
17
- * (`GridRegister`, `useWorkspaceFilter`) pick up the workspace's state as they register.
18
- * - A saved config that flexlayout-react can't read is logged and ignored; the current layout
19
- * stays.
20
- *
21
- * @param layoutKey - Unique key the layout is saved under. An empty key skips registration.
22
- * @param model - The model currently rendered by `<Layout>`.
23
- * @param setModel - Called with a new model when a workspace is loaded.
24
- *
25
- * @example
26
- * ```tsx
27
- * const [model, setModel] = useState(() => Model.fromJson(defaultLayout));
28
- * useWorkspaceFlexLayout('home-layout', model, setModel);
29
- * return <Layout model={model} factory={factory} />;
30
- * ```
31
- * @alpha
32
- */
33
- export declare function useWorkspaceFlexLayout(layoutKey: string, model: Model, setModel: (model: Model) => void): void;
34
- //# sourceMappingURL=use-workspace-flex-layout.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-workspace-flex-layout.d.ts","sourceRoot":"","sources":["../../../src/workspace/use-workspace-flex-layout.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAmB,MAAM,kBAAkB,CAAC;AAG1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,GAC/B,IAAI,CAkCN"}
@@ -1,21 +0,0 @@
1
- /**
2
- * `@genesislcap/foundation-react-utils/workspace`
3
- *
4
- * React helpers for `@genesislcap/foundation-workspace` (save/load of layouts, grid column state
5
- * and filters).
6
- *
7
- * Exposed as a subpath (`/workspace`) rather than from the package root so that consumers which
8
- * don't use workspaces don't load foundation-workspace (and the grid-pro graph behind it).
9
- *
10
- * Grids, `rapid-layout` and the save/load UI need no hook: use the element wrappers from
11
- * `@genesislcap/foundation-workspace/react` (`GridRegister`, `LayoutWrapper`, `WorkspaceManager`).
12
- * Inside a `rapid-layout`, render panes through `registerFactory` + `reactFactory` rather than as
13
- * JSX children, because loading a saved layout rebuilds panes and cloned DOM loses React state.
14
- *
15
- * Key exports:
16
- * - `useWorkspaceFilter` — register React filter state so workspaces save and restore it.
17
- * - `useWorkspaceFlexLayout` — register a flexlayout-react `Model` so workspaces save and restore
18
- * the layout.
19
- */
20
- export { useWorkspaceFilter } from './use-workspace-filter';
21
- export { useWorkspaceFlexLayout } from './use-workspace-flex-layout';
@@ -1,48 +0,0 @@
1
- import { FilterRegistry } from '@genesislcap/foundation-workspace';
2
- import { DI } from '@genesislcap/web-core';
3
- import { useEffect, useLayoutEffect, useRef } from 'react';
4
- /**
5
- * Registers a piece of React filter state with the foundation-workspace `FilterRegistry`, so
6
- * `<workspace-manager>` saves it with the workspace and restores it on load.
7
- *
8
- * @remarks
9
- * Grids and layouts are covered by the `GridRegister` / `LayoutWrapper` elements; filters have no
10
- * element wrapper because their value lives in app state, so this hook is the React equivalent.
11
- *
12
- * - Registers on mount and unregisters on unmount (or when `filterKey` changes).
13
- * - The registry always reads the latest `value` and calls the latest `setValue`, so neither needs
14
- * to be memoised.
15
- * - On mount, the registry applies its stored value for `filterKey` (if any): the loaded
16
- * workspace's value, or this filter's last value from earlier in the session. This covers a
17
- * filter that wasn't mounted when the workspace loaded (e.g. on another route), and a filter
18
- * inside a layout pane that a layout reload remounts.
19
- * - On unmount, the registry keeps the last value, so saving a workspace while the filter isn't
20
- * mounted still includes it.
21
- *
22
- * @param filterKey - Unique key the value is saved under. An empty key skips registration.
23
- * @param value - Current filter value.
24
- * @param setValue - Called with the saved value when a workspace is loaded.
25
- *
26
- * @example
27
- * ```tsx
28
- * const [instrument, setInstrument] = useState('');
29
- * useWorkspaceFilter('positions-instrument', instrument, setInstrument);
30
- * ```
31
- * @alpha
32
- */
33
- export function useWorkspaceFilter(filterKey, value, setValue) {
34
- const latest = useRef({ value, setValue });
35
- useLayoutEffect(() => {
36
- latest.current = { value, setValue };
37
- });
38
- useEffect(() => {
39
- if (!filterKey) {
40
- return undefined;
41
- }
42
- const filterRegistry = DI.getOrCreateDOMContainer().get(FilterRegistry);
43
- filterRegistry.registerFilter(filterKey, () => ({ filterKey, value: latest.current.value }), (savedValue) => latest.current.setValue(savedValue));
44
- return () => {
45
- filterRegistry.unregisterFilter(filterKey);
46
- };
47
- }, [filterKey]);
48
- }
@@ -1,84 +0,0 @@
1
- import { LayoutRegistry } from '@genesislcap/foundation-workspace';
2
- import { DI } from '@genesislcap/web-core';
3
- import { Model } from 'flexlayout-react';
4
- import { useEffect, useLayoutEffect, useRef } from 'react';
5
- /**
6
- * Registers a flexlayout-react `Model` with the foundation-workspace `LayoutRegistry`, so
7
- * `<workspace-manager>` saves the layout (which tabs are open, how they're split and sized, which
8
- * is selected) with the workspace and restores it on load.
9
- *
10
- * @remarks
11
- * The React counterpart of `LayoutWrapper` (`layout-register`), which only covers `rapid-layout`.
12
- * A workspace stores the model's `toJson()`; loading one builds a new model with `Model.fromJson`
13
- * and hands it to `setModel`, so the app renders `<Layout model={model} />` from state.
14
- *
15
- * - Registers on mount and unregisters on unmount (or when `layoutKey` changes). Registering
16
- * applies the layout's stored config straight away, when a workspace loaded earlier has one.
17
- * - The registry always reads the latest `model` and calls the latest `setModel`, so neither needs
18
- * to be memoised.
19
- * - Tabs that a loaded layout closes unmount, and ones it opens mount: their grids and filters
20
- * (`GridRegister`, `useWorkspaceFilter`) pick up the workspace's state as they register.
21
- * - A saved config that flexlayout-react can't read is logged and ignored; the current layout
22
- * stays.
23
- *
24
- * @param layoutKey - Unique key the layout is saved under. An empty key skips registration.
25
- * @param model - The model currently rendered by `<Layout>`.
26
- * @param setModel - Called with a new model when a workspace is loaded.
27
- *
28
- * @example
29
- * ```tsx
30
- * const [model, setModel] = useState(() => Model.fromJson(defaultLayout));
31
- * useWorkspaceFlexLayout('home-layout', model, setModel);
32
- * return <Layout model={model} factory={factory} />;
33
- * ```
34
- * @alpha
35
- */
36
- export function useWorkspaceFlexLayout(layoutKey, model, setModel) {
37
- const latest = useRef({ model, setModel });
38
- useLayoutEffect(() => {
39
- latest.current = { model, setModel };
40
- });
41
- useEffect(() => {
42
- if (!layoutKey) {
43
- return undefined;
44
- }
45
- const layoutRegistry = DI.getOrCreateDOMContainer().get(LayoutRegistry);
46
- const layout = {
47
- // A flexlayout Model is complete as soon as it exists; there's no first load to wait for.
48
- hasFirstLoaded: true,
49
- getLayout: () => latest.current.model.toJson(),
50
- loadLayout: (saved) => {
51
- const next = readModel(saved);
52
- if (next) {
53
- latest.current.setModel(next);
54
- }
55
- else {
56
- console.warn(`useWorkspaceFlexLayout: ignoring unreadable layout for ${layoutKey}`);
57
- }
58
- },
59
- addEventListener: () => { },
60
- };
61
- layoutRegistry.registerLayout(layoutKey, layout);
62
- return () => {
63
- layoutRegistry.unregisterLayout(layoutKey);
64
- };
65
- }, [layoutKey]);
66
- }
67
- /**
68
- * Builds a model from a saved flexlayout config, or returns null for anything else — e.g. a
69
- * `rapid-layout` config saved under the same key. `Model.fromJson` doesn't validate: it builds a
70
- * broken model from malformed JSON rather than throwing.
71
- */
72
- function readModel(saved) {
73
- const json = saved;
74
- const root = json === null || json === void 0 ? void 0 : json.layout;
75
- if (!root || typeof root !== 'object' || typeof root.type !== 'string') {
76
- return null;
77
- }
78
- try {
79
- return Model.fromJson(json);
80
- }
81
- catch (_a) {
82
- return null;
83
- }
84
- }