@vitessce/vit-s 2.0.3-beta.0 → 2.0.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.
Files changed (184) hide show
  1. package/dist/index.mjs +84801 -0
  2. package/{dist → dist-tsc}/index.js +2 -2
  3. package/package.json +7 -7
  4. package/src/CallbackPublisher.js +81 -0
  5. package/src/LoadingIndicator.js +34 -0
  6. package/src/TitleInfo.js +240 -0
  7. package/src/VitS.js +180 -0
  8. package/src/VitessceGrid.js +110 -0
  9. package/src/VitessceGrid.test.jsx +48 -0
  10. package/src/Warning.js +62 -0
  11. package/{dist → src}/classNames.js +0 -0
  12. package/src/component-registry.js +21 -0
  13. package/src/data/AbstractLoader.js +46 -0
  14. package/{dist → src}/data/AbstractTwoStepLoader.js +5 -4
  15. package/src/data/LoaderResult.js +8 -0
  16. package/{dist → src}/data/index.js +0 -0
  17. package/src/data/loader-registry.js +19 -0
  18. package/src/data-hook-utils.js +197 -0
  19. package/src/data-hooks.js +354 -0
  20. package/src/errors/AbstractLoaderError.js +14 -0
  21. package/src/errors/DataSourceFetchError.js +15 -0
  22. package/src/errors/DatasetNotFoundError.js +23 -0
  23. package/src/errors/LoaderNotFoundError.js +24 -0
  24. package/src/errors/LoaderValidationError.js +23 -0
  25. package/src/errors/OptionsValidationError.js +25 -0
  26. package/{dist → src}/errors/index.js +0 -0
  27. package/src/file-options-schemas-legacy.js +139 -0
  28. package/src/file-options-schemas-legacy.test.js +138 -0
  29. package/src/file-options-schemas.js +327 -0
  30. package/src/file-options-schemas.test.js +26 -0
  31. package/src/hooks.js +252 -0
  32. package/src/index.js +101 -0
  33. package/src/joint-file-types-legacy.js +298 -0
  34. package/src/joint-file-types-legacy.test.js +412 -0
  35. package/src/joint-file-types.js +171 -0
  36. package/src/joint-file-types.test.js +144 -0
  37. package/{dist/schemas.js → src/json-schemas.js} +6 -1
  38. package/{dist → src}/plugins.js +102 -70
  39. package/src/plugins.test.fixtures.js +78 -0
  40. package/src/plugins.test.js +42 -0
  41. package/src/schemas/config-0.1.0.schema.json +85 -0
  42. package/src/schemas/config-1.0.0.schema.json +733 -0
  43. package/src/schemas/config-1.0.1.schema.json +909 -0
  44. package/src/schemas/config-1.0.10.schema.json +969 -0
  45. package/src/schemas/config-1.0.11.schema.json +990 -0
  46. package/src/schemas/config-1.0.12.schema.json +997 -0
  47. package/src/schemas/config-1.0.13.schema.json +1013 -0
  48. package/src/schemas/config-1.0.14.schema.json +1048 -0
  49. package/src/schemas/config-1.0.15.schema.json +1048 -0
  50. package/src/schemas/config-1.0.16.schema.json +1069 -0
  51. package/src/schemas/config-1.0.16.schema.test.js +32 -0
  52. package/src/schemas/config-1.0.2.schema.json +911 -0
  53. package/src/schemas/config-1.0.3.schema.json +911 -0
  54. package/src/schemas/config-1.0.4.schema.json +949 -0
  55. package/src/schemas/config-1.0.5.schema.json +949 -0
  56. package/src/schemas/config-1.0.6.schema.json +950 -0
  57. package/src/schemas/config-1.0.7.schema.json +950 -0
  58. package/src/schemas/config-1.0.8.schema.json +966 -0
  59. package/src/schemas/config-1.0.9.schema.json +965 -0
  60. package/src/schemas/fixtures/cellSets.good.json +16 -0
  61. package/src/schemas/fixtures/config-1.0.1.bad.json +1 -0
  62. package/src/schemas/fixtures/config-1.0.1.bad.message.json +11 -0
  63. package/src/schemas/fixtures/config-1.0.1.good.json +37 -0
  64. package/src/schemas/fixtures/config-1.0.2.bad.json +1 -0
  65. package/src/schemas/fixtures/config-1.0.2.bad.message.json +11 -0
  66. package/src/schemas/fixtures/config-1.0.2.good.json +38 -0
  67. package/src/schemas/fixtures/config-1.0.3.bad.json +1 -0
  68. package/src/schemas/fixtures/config-1.0.3.bad.message.json +11 -0
  69. package/src/schemas/fixtures/config-1.0.3.good.json +38 -0
  70. package/src/schemas/fixtures/obsSets.bad.json +1 -0
  71. package/src/schemas/fixtures/obsSets.bad.message.json +11 -0
  72. package/src/schemas/fixtures/obsSets.good.json +16 -0
  73. package/src/schemas/fixtures/obsSetsTabular.bad.json +1 -0
  74. package/src/schemas/fixtures/obsSetsTabular.bad.message.json +11 -0
  75. package/src/schemas/fixtures/obsSetsTabular.good.json +26 -0
  76. package/src/schemas/fixtures/raster.bad.json +1 -0
  77. package/src/schemas/fixtures/raster.bad.message.json +11 -0
  78. package/src/schemas/fixtures/raster.good.json +39 -0
  79. package/src/schemas/obsSets.schema.js +195 -0
  80. package/src/schemas/obsSetsTabular.schema.js +39 -0
  81. package/src/schemas/raster.schema.js +121 -0
  82. package/src/schemas/schema-schema.sh +34 -0
  83. package/{dist → src}/schemas/schema.test-old.js +0 -1
  84. package/src/shared-mui/components.js +79 -0
  85. package/src/shared-mui/container.js +212 -0
  86. package/src/shared-mui/styles.js +110 -0
  87. package/src/shared-plot-options/CellColorEncodingOption.js +43 -0
  88. package/src/shared-plot-options/OptionSelect.js +19 -0
  89. package/src/shared-plot-options/OptionsContainer.js +26 -0
  90. package/{dist → src}/shared-plot-options/index.js +0 -0
  91. package/src/shared-plot-options/styles.js +49 -0
  92. package/src/state/hooks.js +633 -0
  93. package/src/view-config-upgraders.js +628 -0
  94. package/src/view-config-utils.js +315 -0
  95. package/src/view-config-utils.test.fixtures.js +893 -0
  96. package/src/view-config-utils.test.js +95 -0
  97. package/src/view-config-versions.js +68 -0
  98. package/src/vitessce-grid-layout/VitessceGridLayout.js +158 -0
  99. package/src/vitessce-grid-layout/VitessceGridLayout.test.jsx +56 -0
  100. package/{dist → src}/vitessce-grid-layout/index.js +0 -0
  101. package/src/vitessce-grid-layout/layout-utils.js +76 -0
  102. package/src/vitessce-grid-layout/layout-utils.test.js +167 -0
  103. package/src/vitessce-grid-utils.js +160 -0
  104. package/dist/CallbackPublisher.js +0 -68
  105. package/dist/LoadingIndicator.js +0 -27
  106. package/dist/TitleInfo.js +0 -151
  107. package/dist/VitS.js +0 -117
  108. package/dist/Vitessce.integration.test.js +0 -36
  109. package/dist/Vitessce.js +0 -101
  110. package/dist/VitessceGrid.js +0 -59
  111. package/dist/VitessceGrid.test.js +0 -36
  112. package/dist/Warning.js +0 -45
  113. package/dist/component-registry.js +0 -16
  114. package/dist/data/AbstractLoader.js +0 -35
  115. package/dist/data/LoaderResult.js +0 -7
  116. package/dist/data/loader-registry.js +0 -15
  117. package/dist/data-hook-utils.js +0 -175
  118. package/dist/data-hooks.js +0 -241
  119. package/dist/errors/AbstractLoaderError.js +0 -13
  120. package/dist/errors/DataSourceFetchError.js +0 -13
  121. package/dist/errors/DatasetNotFoundError.js +0 -17
  122. package/dist/errors/LoaderNotFoundError.js +0 -17
  123. package/dist/errors/LoaderValidationError.js +0 -15
  124. package/dist/errors/OptionsValidationError.js +0 -15
  125. package/dist/export-utils.js +0 -70
  126. package/dist/export-utils.test.js +0 -66
  127. package/dist/file-options-schemas-legacy.js +0 -136
  128. package/dist/file-options-schemas-legacy.test.js +0 -126
  129. package/dist/file-options-schemas.js +0 -322
  130. package/dist/file-options-schemas.test.js +0 -23
  131. package/dist/hooks.js +0 -215
  132. package/dist/joint-file-types-legacy.js +0 -278
  133. package/dist/joint-file-types-legacy.test.js +0 -400
  134. package/dist/joint-file-types.js +0 -148
  135. package/dist/joint-file-types.test.js +0 -139
  136. package/dist/loader-registry.js +0 -98
  137. package/dist/plugins.test.fixtures.js +0 -76
  138. package/dist/plugins.test.js +0 -30
  139. package/dist/schemas/PrettyJsonSchema.js +0 -34
  140. package/dist/schemas/config-0.1.0.schema.json +0 -84
  141. package/dist/schemas/config-1.0.0.schema.json +0 -733
  142. package/dist/schemas/config-1.0.1.schema.json +0 -909
  143. package/dist/schemas/config-1.0.10.schema.json +0 -969
  144. package/dist/schemas/config-1.0.11.schema.json +0 -990
  145. package/dist/schemas/config-1.0.12.schema.json +0 -997
  146. package/dist/schemas/config-1.0.13.schema.json +0 -1013
  147. package/dist/schemas/config-1.0.14.schema.json +0 -1048
  148. package/dist/schemas/config-1.0.15.schema.json +0 -1048
  149. package/dist/schemas/config-1.0.15.schema.test.js +0 -22
  150. package/dist/schemas/config-1.0.16.schema.json +0 -1109
  151. package/dist/schemas/config-1.0.16.schema.test.js +0 -22
  152. package/dist/schemas/config-1.0.2.schema.json +0 -910
  153. package/dist/schemas/config-1.0.3.schema.json +0 -910
  154. package/dist/schemas/config-1.0.4.schema.json +0 -949
  155. package/dist/schemas/config-1.0.5.schema.json +0 -949
  156. package/dist/schemas/config-1.0.6.schema.json +0 -949
  157. package/dist/schemas/config-1.0.7.schema.json +0 -949
  158. package/dist/schemas/config-1.0.8.schema.json +0 -965
  159. package/dist/schemas/config-1.0.9.schema.json +0 -965
  160. package/dist/schemas/obs-sets-tabular.schema.js +0 -37
  161. package/dist/schemas/obs-sets.schema.js +0 -194
  162. package/dist/schemas/obsSets.schema.js +0 -195
  163. package/dist/schemas/obsSetsTabular.schema.js +0 -38
  164. package/dist/schemas/raster.schema.js +0 -120
  165. package/dist/schemas/schema.test.js +0 -46
  166. package/dist/schemas-extra.js +0 -3
  167. package/dist/shared-mui/components.js +0 -35
  168. package/dist/shared-mui/container.js +0 -211
  169. package/dist/shared-mui/styles.js +0 -107
  170. package/dist/shared-plot-options/CellColorEncodingOption.js +0 -18
  171. package/dist/shared-plot-options/OptionSelect.js +0 -12
  172. package/dist/shared-plot-options/OptionsContainer.js +0 -12
  173. package/dist/shared-plot-options/styles.js +0 -48
  174. package/dist/state/hooks.js +0 -549
  175. package/dist/view-config-upgraders.js +0 -525
  176. package/dist/view-config-utils.js +0 -286
  177. package/dist/view-config-utils.test.fixtures.js +0 -765
  178. package/dist/view-config-utils.test.js +0 -66
  179. package/dist/view-config-versions.js +0 -47
  180. package/dist/vitessce-grid-layout/VitessceGridLayout.js +0 -95
  181. package/dist/vitessce-grid-layout/VitessceGridLayout.test.js +0 -40
  182. package/dist/vitessce-grid-layout/layout-utils.js +0 -60
  183. package/dist/vitessce-grid-layout/layout-utils.test.js +0 -146
  184. package/dist/vitessce-grid-utils.js +0 -140
@@ -0,0 +1,633 @@
1
+ import { useRef, useCallback, useMemo } from 'react';
2
+ import create from 'zustand';
3
+ import createContext from 'zustand/context';
4
+ import shallow from 'zustand/shallow';
5
+ import isMatch from 'lodash/isMatch';
6
+ import { CoordinationType } from '@vitessce/constants-internal';
7
+ import { fromEntries, capitalize } from '@vitessce/utils';
8
+
9
+ // Reference: https://github.com/pmndrs/zustand#react-context
10
+ // Reference: https://github.com/pmndrs/zustand/blob/e47ea03/tests/context.test.tsx#L60
11
+ const {
12
+ Provider: ViewConfigProviderLocal,
13
+ useStore: useViewConfigStoreLocal,
14
+ useStoreApi: useViewConfigStoreApiLocal,
15
+ } = createContext();
16
+
17
+ export const ViewConfigProvider = ViewConfigProviderLocal;
18
+ export const useViewConfigStore = useViewConfigStoreLocal;
19
+ export const useViewConfigStoreApi = useViewConfigStoreApiLocal;
20
+
21
+ const {
22
+ Provider: AuxiliaryProviderLocal,
23
+ useStore: useAuxiliaryStoreLocal,
24
+ } = createContext();
25
+
26
+ export const AuxiliaryProvider = AuxiliaryProviderLocal;
27
+ export const useAuxiliaryStore = useAuxiliaryStoreLocal;
28
+
29
+
30
+ /**
31
+ * The useViewConfigStore hook is initialized via the zustand
32
+ * create() function, which sets up both the state variables
33
+ * and the reducer-type functions.
34
+ * Reference: https://github.com/react-spring/zustand
35
+ * @returns {function} The useStore hook.
36
+ */
37
+ export const createViewConfigStore = () => create(set => ({
38
+ // State:
39
+ // The viewConfig is an object which must conform to the schema
40
+ // found in src/schemas/config.schema.json.
41
+ viewConfig: null,
42
+ // The loaders object is a mapping from dataset ID to
43
+ // data type to loader object instance.
44
+ loaders: null,
45
+ // Reducer functions which update the state
46
+ // (although technically also part of state):
47
+ setViewConfig: viewConfig => set({ viewConfig }),
48
+ setLoaders: loaders => set({ loaders }),
49
+ setCoordinationValue: ({ parameter, scope, value }) => set(state => ({
50
+ viewConfig: {
51
+ ...state.viewConfig,
52
+ coordinationSpace: {
53
+ ...state.viewConfig.coordinationSpace,
54
+ [parameter]: {
55
+ ...state.viewConfig.coordinationSpace[parameter],
56
+ [scope]: value,
57
+ },
58
+ },
59
+ },
60
+ })),
61
+ removeComponent: uid => set((state) => {
62
+ const newLayout = state.viewConfig.layout.filter(c => c.uid !== uid);
63
+ return {
64
+ viewConfig: {
65
+ ...state.viewConfig,
66
+ layout: newLayout,
67
+ },
68
+ };
69
+ }),
70
+ changeLayout: newComponentProps => set((state) => {
71
+ const newLayout = state.viewConfig.layout
72
+ .slice()
73
+ .map(componentProps => ({
74
+ ...componentProps,
75
+ ...newComponentProps[componentProps.uid],
76
+ }));
77
+ return {
78
+ viewConfig: {
79
+ ...state.viewConfig,
80
+ layout: newLayout,
81
+ },
82
+ };
83
+ }),
84
+ }));
85
+
86
+ /**
87
+ * Hook for getting components' layout from the view config based on
88
+ * matching all coordination scopes.
89
+ * @returns {Object} The components' layout.
90
+ */
91
+ export const useComponentLayout = (component, scopes, coordinationScopes) => useViewConfigStore(
92
+ state => state.viewConfig.layout.filter(l => l.component === component).filter(
93
+ l => scopes.every(scope => l.coordinationScopes[scope]
94
+ === coordinationScopes[scope]),
95
+ ),
96
+ );
97
+
98
+ /**
99
+ * The useAuxiliaryStore hook is initialized via the zustand
100
+ * create() function, which sets up both the state variables
101
+ * and the reducer-type functions.
102
+ * Reference: https://github.com/react-spring/zustand
103
+ * It is meant to be used for non-viewconfig-based coordination between components.
104
+ * For example, as currently happens, the layer controller can coordinate
105
+ * on-load callbacks with spatial view based on whether or not they are
106
+ * coordinated via `spatialImageLayer` - the callbacks are not part of the view config
107
+ * though so they live here.
108
+ * @returns {function} The useStore hook.
109
+ */
110
+ export const createAuxiliaryStore = () => create(set => ({
111
+ auxiliaryStore: null,
112
+ setCoordinationValue: ({ parameter, scope, value }) => set(state => ({
113
+ auxiliaryStore: {
114
+ ...state.auxiliaryStore,
115
+ [parameter]: {
116
+ [scope]: value,
117
+ },
118
+ },
119
+ })),
120
+ }));
121
+
122
+ /**
123
+ * The hover store can be used to store global state
124
+ * related to which component is currently hovered,
125
+ * which is required for tooltip / crossover elements.
126
+ * @returns {function} The useStore hook.
127
+ */
128
+ const useHoverStore = create(set => ({
129
+ // Components may need to know if they are the "hover source"
130
+ // for tooltip interactions. This value should be a unique
131
+ // component ID, such as its index in the view config layout.
132
+ componentHover: null,
133
+ setComponentHover: componentHover => set({ componentHover }),
134
+ }));
135
+
136
+ /**
137
+ * The warning store can be used to store global state
138
+ * related to app warning messages.
139
+ * @returns {function} The useStore hook.
140
+ */
141
+ const useWarnStore = create(set => ({
142
+ // Want a global state to collect warning messages
143
+ // that occur anywhere in the app.
144
+ warning: null,
145
+ setWarning: warning => set({ warning }),
146
+ }));
147
+
148
+ /**
149
+ * The view info store can be used to store component-level
150
+ * viewInfo objects,
151
+ * which are required for tooltip / crossover elements.
152
+ * @returns {function} The useStore hook.
153
+ */
154
+ const useViewInfoStore = create(set => ({
155
+ // The viewInfo object is a mapping from
156
+ // component IDs to component view info objects.
157
+ // Each view info object must have a project() function.
158
+ viewInfo: {},
159
+ setComponentViewInfo: (uuid, viewInfo) => set(state => ({
160
+ viewInfo: {
161
+ ...state.viewInfo,
162
+ [uuid]: viewInfo,
163
+ },
164
+ })),
165
+ }));
166
+
167
+ /**
168
+ * The grid size store can be used to store a
169
+ * counter which updates on each window or react-grid-layout
170
+ * resize event.
171
+ * @returns {function} The useStore hook.
172
+ */
173
+ const useGridSizeStore = create(set => ({
174
+ resizeCount: {},
175
+ incrementResizeCount: () => set(state => ({
176
+ resizeCount: state.resizeCount + 1,
177
+ })),
178
+ }));
179
+
180
+ /**
181
+ * The useCoordination hook returns both the
182
+ * values and setter functions for the coordination objects
183
+ * in a particular coordination scope mapping.
184
+ * This hook is intended to be used within the ___Subscriber
185
+ * components to allow them to "hook into" only those coordination
186
+ * objects and setter functions of relevance.
187
+ * @param {string[]} parameters Array of coordination types.
188
+ * @param {object} coordinationScopes Mapping of coordination types
189
+ * to scope names.
190
+ * @returns {array} Returns a tuple [values, setters]
191
+ * where values is an object containing all coordination values,
192
+ * and setters is an object containing all coordination setter
193
+ * functions for the values in `values`, named with a "set"
194
+ * prefix.
195
+ */
196
+ export function useCoordination(parameters, coordinationScopes) {
197
+ const setCoordinationValue = useViewConfigStore(state => state.setCoordinationValue);
198
+
199
+ const values = useViewConfigStore((state) => {
200
+ const { coordinationSpace } = state.viewConfig;
201
+ return fromEntries(parameters.map((parameter) => {
202
+ if (coordinationSpace && coordinationSpace[parameter]) {
203
+ const value = coordinationSpace[parameter][coordinationScopes[parameter]];
204
+ return [parameter, value];
205
+ }
206
+ return [parameter, undefined];
207
+ }));
208
+ }, shallow);
209
+
210
+ const setters = useMemo(() => fromEntries(parameters.map((parameter) => {
211
+ const setterName = `set${capitalize(parameter)}`;
212
+ const setterFunc = value => setCoordinationValue({
213
+ parameter,
214
+ scope: coordinationScopes[parameter],
215
+ value,
216
+ });
217
+ return [setterName, setterFunc];
218
+ // eslint-disable-next-line react-hooks/exhaustive-deps
219
+ })), [parameters, coordinationScopes]);
220
+
221
+ return [values, setters];
222
+ }
223
+
224
+ export function useMultiCoordinationValues(parameter, coordinationScopes) {
225
+ const scopes = coordinationScopes[parameter];
226
+
227
+ // Mapping from dataset coordination scope name to dataset uid
228
+ const vals = useViewConfigStore((state) => {
229
+ const { coordinationSpace } = state.viewConfig;
230
+ // Convert a single scope to an array of scopes to be consistent.
231
+ const scopesArr = Array.isArray(scopes) ? scopes : [scopes];
232
+ return fromEntries(scopesArr.map((scope) => {
233
+ if (coordinationSpace && coordinationSpace[parameter]) {
234
+ const value = coordinationSpace[parameter][scope];
235
+ return [scope, value];
236
+ }
237
+ return [scope, undefined];
238
+ // eslint-disable-next-line no-unused-vars
239
+ }).filter(([k, v]) => v !== undefined));
240
+ }, shallow);
241
+
242
+ return vals;
243
+ }
244
+
245
+ /**
246
+ * Get a mapping from dataset coordination scopes to dataset UIDs.
247
+ * @param {object} coordinationScopes The coordination scope mapping object for a view.
248
+ * @returns {object} Mapping from dataset coordination scope names to dataset UIDs.
249
+ */
250
+ export function useDatasetUids(coordinationScopes) {
251
+ return useMultiCoordinationValues(CoordinationType.DATASET, coordinationScopes);
252
+ }
253
+
254
+ /**
255
+ * Use coordination values and coordination setter functions corresponding to
256
+ * {coordinationType}-specific coordination scopes for each coordination type.
257
+ * @param {string[]} parameters An array of coordination types supported by a view.
258
+ * @param {object} coordinationScopes The coordination scope mapping object for a view.
259
+ * @param {object} coordinationScopesBy The per-coordinationType coordination scope
260
+ * mapping object for a view.
261
+ * @param {string} byType The {coordinationType} to use for per-{coordinationType} coordination
262
+ * scope mappings.
263
+ * @returns {array} [cValues, cSetters] where
264
+ * cValues is a mapping from coordination scope name to { coordinationType: coordinationValue },
265
+ * and cSetters is a mapping from coordination scope name to { setCoordinationType }
266
+ * setter functions.
267
+ */
268
+ export function useComplexCoordination(
269
+ parameters, coordinationScopes, coordinationScopesBy, byType,
270
+ ) {
271
+ const setCoordinationValue = useViewConfigStore(state => state.setCoordinationValue);
272
+
273
+ const typeScopes = coordinationScopes[byType];
274
+ const byTypeScopes = coordinationScopesBy[byType];
275
+
276
+ // Convert a single scope to an array of scopes to be consistent.
277
+ const typeScopesArr = Array.isArray(typeScopes) ? typeScopes : [typeScopes];
278
+
279
+ const values = useViewConfigStore((state) => {
280
+ const { coordinationSpace } = state.viewConfig;
281
+ return fromEntries(typeScopesArr.map((datasetScope) => {
282
+ const datasetValues = fromEntries(parameters.map((parameter) => {
283
+ if (coordinationSpace && coordinationSpace[parameter]) {
284
+ let value;
285
+ const parameterSpace = coordinationSpace[parameter];
286
+ const parameterScopeGlobal = coordinationScopes[parameter];
287
+ const parameterScopeByDataset = byTypeScopes?.[parameter];
288
+ if (parameterScopeByDataset && parameterScopeByDataset[datasetScope]) {
289
+ value = parameterSpace[parameterScopeByDataset[datasetScope]];
290
+ } else if (parameterScopeGlobal) {
291
+ value = parameterSpace[parameterScopeGlobal];
292
+ } else {
293
+ console.error(`coordination scope for ${parameter} was not found.`);
294
+ }
295
+ return [parameter, value];
296
+ }
297
+ return [parameter, undefined];
298
+ }));
299
+ return [datasetScope, datasetValues];
300
+ }));
301
+ }, shallow);
302
+
303
+ const setters = useMemo(() => fromEntries(typeScopesArr.map((datasetScope) => {
304
+ const datasetSetters = fromEntries(parameters.map((parameter) => {
305
+ const setterName = `set${capitalize(parameter)}`;
306
+ let setterFunc;
307
+ const parameterScopeGlobal = coordinationScopes[parameter];
308
+ const parameterScopeByDataset = byTypeScopes?.[parameter];
309
+ if (parameterScopeByDataset && parameterScopeByDataset[datasetScope]) {
310
+ setterFunc = value => setCoordinationValue({
311
+ parameter,
312
+ scope: parameterScopeByDataset[datasetScope],
313
+ value,
314
+ });
315
+ } else if (parameterScopeGlobal) {
316
+ setterFunc = value => setCoordinationValue({
317
+ parameter,
318
+ scope: parameterScopeGlobal,
319
+ value,
320
+ });
321
+ } else {
322
+ console.error(`coordination scope for ${parameter} was not found.`);
323
+ }
324
+ return [setterName, setterFunc];
325
+ }));
326
+ return [datasetScope, datasetSetters];
327
+ // eslint-disable-next-line react-hooks/exhaustive-deps
328
+ })), [parameters, coordinationScopes]);
329
+
330
+ return [values, setters];
331
+ }
332
+
333
+ /**
334
+ * Use coordination values and coordination setter functions corresponding to
335
+ * dataset-specific coordination scopes for each coordination type.
336
+ * @param {string[]} parameters An array of coordination types supported by a view.
337
+ * @param {object} coordinationScopes The coordination scope mapping object for a view.
338
+ * @param {object} coordinationScopesBy The per-coordinationType coordination scope
339
+ * mapping object for a view.
340
+ * @returns {array} [cValues, cSetters] where
341
+ * cValues is a mapping from coordination scope name to { coordinationType: coordinationValue },
342
+ * and cSetters is a mapping from coordination scope name to { setCoordinationType }
343
+ * setter functions.
344
+ */
345
+ export function useMultiDatasetCoordination(parameters, coordinationScopes, coordinationScopesBy) {
346
+ return useComplexCoordination(
347
+ parameters, coordinationScopes, coordinationScopesBy,
348
+ CoordinationType.DATASET,
349
+ );
350
+ }
351
+
352
+ const AUXILIARY_COORDINATION_TYPES_MAP = {
353
+ spatialImageLayer: ['imageLayerCallbacks', 'areLoadingImageChannels'],
354
+ spatialSegmentationLayer: ['segmentationLayerCallbacks', 'areLoadingSegmentationChannels'],
355
+ };
356
+
357
+ /**
358
+ * The maps the coordination types of incoming scopes to new types
359
+ * for the auxiliary store.
360
+ * @param {object} coordinationScopes Mapping of coordination types
361
+ * to scope names.
362
+ * @returns {object} Mapping of coordination types
363
+ * to new scope names for the auxiliary store.
364
+ */
365
+ const mapCoordinationScopes = (coordinationScopes) => {
366
+ const newCoordinationScopes = {};
367
+ Object.keys(coordinationScopes).forEach((key) => {
368
+ const newCoordinationTypes = AUXILIARY_COORDINATION_TYPES_MAP[key] || [];
369
+ newCoordinationTypes.forEach((coordinationType) => {
370
+ newCoordinationScopes[coordinationType || key] = coordinationScopes[key];
371
+ });
372
+ });
373
+ return newCoordinationScopes;
374
+ };
375
+
376
+ const mapParameters = parameters => parameters
377
+ .map(parameter => AUXILIARY_COORDINATION_TYPES_MAP[parameter]).filter(Boolean).flat();
378
+
379
+ /**
380
+ * The useAuxiliaryCoordination hook returns both the
381
+ * values and setter functions for the auxiliary coordination objects
382
+ * in a particular coordination scope mapping.
383
+ * This hook is intended to be used within the ___Subscriber
384
+ * components to allow them to "hook into" only those auxiliary coordination
385
+ * objects and setter functions of relevance, for example "on load" callbacks.
386
+ * @param {string[]} parameters Array of coordination types.
387
+ * @param {object} coordinationScopes Mapping of coordination types
388
+ * to scope names.
389
+ * @returns {array} Returns a tuple [values, setters]
390
+ * where values is an object containing all coordination values,
391
+ * and setters is an object containing all coordination setter
392
+ * functions for the values in `values`, named with a "set"
393
+ * prefix.
394
+ */
395
+ export function useAuxiliaryCoordination(parameters, coordinationScopes) {
396
+ const setCoordinationValue = useAuxiliaryStore(state => state.setCoordinationValue);
397
+ const mappedCoordinationScopes = mapCoordinationScopes(coordinationScopes);
398
+ const mappedParameters = mapParameters(parameters);
399
+ const values = useAuxiliaryStore((state) => {
400
+ const { auxiliaryStore } = state;
401
+ return fromEntries(mappedParameters.map((parameter) => {
402
+ if (auxiliaryStore && auxiliaryStore[parameter]) {
403
+ const value = auxiliaryStore[parameter][mappedCoordinationScopes[parameter]];
404
+ return [parameter, value];
405
+ }
406
+ return [parameter, undefined];
407
+ }));
408
+ }, shallow);
409
+ const setters = useMemo(() => fromEntries(mappedParameters.map((parameter) => {
410
+ const setterName = `set${capitalize(parameter)}`;
411
+ const setterFunc = value => setCoordinationValue({
412
+ parameter,
413
+ scope: mappedCoordinationScopes[parameter],
414
+ value,
415
+ });
416
+ return [setterName, setterFunc];
417
+ // eslint-disable-next-line react-hooks/exhaustive-deps
418
+ })), [parameters, coordinationScopes]);
419
+
420
+ return [values, setters];
421
+ }
422
+
423
+ /**
424
+ * Obtain the loaders object from
425
+ * the global app state.
426
+ * @returns {object} The loaders object
427
+ * in the `useViewConfigStore` store.
428
+ */
429
+ export function useLoaders() {
430
+ return useViewConfigStore(state => state.loaders);
431
+ }
432
+
433
+ /**
434
+ * Find a specific loader instance for a particular dataset, data type, and view
435
+ * coordination values (mapping from coordination types to coordination values).
436
+ * Uses lodash/isMatch to perform matching against the file definition's
437
+ * coordination value mapping.
438
+ * @param {object} loaders The value returned by useLoaders.
439
+ * @param {string} dataset The dataset UID.
440
+ * @param {string} dataType The data type for the matching file.
441
+ * @param {object} viewCoordinationValues Current coordination values
442
+ * from the view. Match these against a subset of file definition coordination
443
+ * values.
444
+ * @returns The matching loader instance or `null`.
445
+ */
446
+ export function useMatchingLoader(loaders, dataset, dataType, viewCoordinationValues) {
447
+ return useMemo(() => {
448
+ if (!loaders[dataset]) {
449
+ return null;
450
+ }
451
+ const loaderInternMap = loaders[dataset].loaders[dataType];
452
+ if (!loaderInternMap) {
453
+ return null;
454
+ }
455
+ const loaderKeys = Array.from(loaderInternMap.keys());
456
+ const matchingKey = loaderKeys
457
+ .find(fileCoordinationValues => isMatch(fileCoordinationValues, viewCoordinationValues));
458
+ if (!matchingKey) {
459
+ return null;
460
+ }
461
+ return loaderInternMap.get(matchingKey);
462
+ }, [loaders, dataset, dataType, viewCoordinationValues]);
463
+ }
464
+
465
+ /**
466
+ * Find a specific loader instance for a particular dataset, data type, and view
467
+ * coordination values (mapping from coordination types to coordination values).
468
+ * Uses lodash/isMatch to perform matching against the file definition's
469
+ * coordination value mapping.
470
+ * @param {object} loaders The value returned by useLoaders.
471
+ * @param {string} dataset The dataset UID.
472
+ * @param {string} dataType The data type for the matching file.
473
+ * @param {object} viewCoordinationValues Current coordination values
474
+ * from the view. Match these against a subset of file definition coordination
475
+ * values.
476
+ * @returns The matching loader instance or `null`.
477
+ */
478
+ export function useMatchingLoaders(loaders, dataset, dataType, viewCoordinationValuesObj) {
479
+ return useMemo(() => {
480
+ if (!loaders[dataset]) {
481
+ return null;
482
+ }
483
+ const loaderInternMap = loaders[dataset].loaders[dataType];
484
+ if (!loaderInternMap) {
485
+ return null;
486
+ }
487
+ const loaderKeys = Array.from(loaderInternMap.keys());
488
+ function getLoader(viewCoordinationValues) {
489
+ const matchingKey = loaderKeys
490
+ .find(fileCoordinationValues => isMatch(fileCoordinationValues, viewCoordinationValues));
491
+ return loaderInternMap.get(matchingKey);
492
+ }
493
+ return fromEntries(
494
+ Object.entries(viewCoordinationValuesObj).map(([key, viewCoordinationValues]) => ([
495
+ key,
496
+ getLoader(viewCoordinationValues),
497
+ ])),
498
+ );
499
+ }, [loaders, dataset, dataType, viewCoordinationValuesObj]);
500
+ }
501
+
502
+ /**
503
+ * Obtain the view config layout array from
504
+ * the global app state.
505
+ * @returns {object[]} The layout array
506
+ * in the `useViewConfigStore` store.
507
+ */
508
+ export function useLayout() {
509
+ return useViewConfigStore(state => state.viewConfig?.layout);
510
+ }
511
+
512
+ /**
513
+ * Obtain the component removal function from
514
+ * the global app state.
515
+ * @returns {function} The remove component function
516
+ * in the `useViewInfoStore` store.
517
+ */
518
+ export function useRemoveComponent() {
519
+ return useViewConfigStore(state => state.removeComponent);
520
+ }
521
+
522
+ /**
523
+ * Obtain the component prop setter function from
524
+ * the global app state.
525
+ * @returns {function} The set component props function
526
+ * in the `useViewInfoStore` store.
527
+ */
528
+ export function useChangeLayout() {
529
+ return useViewConfigStore(state => state.changeLayout);
530
+ }
531
+
532
+ /**
533
+ * Obtain the loaders setter function from
534
+ * the global app state.
535
+ * @returns {function} The loaders setter function
536
+ * in the `useViewConfigStore` store.
537
+ */
538
+ export function useSetLoaders() {
539
+ return useViewConfigStore(state => state.setLoaders);
540
+ }
541
+
542
+ /**
543
+ * Obtain the view config setter function from
544
+ * the global app state.
545
+ * @returns {function} The view config setter function
546
+ * in the `useViewConfigStore` store.
547
+ */
548
+ export function useSetViewConfig(viewConfigStoreApi) {
549
+ const setViewConfigRef = useRef(viewConfigStoreApi.getState().setViewConfig);
550
+ const setViewConfig = setViewConfigRef.current;
551
+ return setViewConfig;
552
+ }
553
+
554
+ /**
555
+ * Obtain the component hover value from
556
+ * the global app state.
557
+ * @returns {number} The hovered component ID
558
+ * in the `useHoverStore` store.
559
+ */
560
+ export function useComponentHover() {
561
+ return useHoverStore(state => state.componentHover);
562
+ }
563
+
564
+ /**
565
+ * Obtain the component hover setter function from
566
+ * the global app state.
567
+ * @returns {function} The component hover setter function
568
+ * in the `useHoverStore` store.
569
+ */
570
+ export function useSetComponentHover() {
571
+ return useHoverStore(state => state.setComponentHover);
572
+ }
573
+
574
+ /**
575
+ * Obtain the warning message from
576
+ * the global app state.
577
+ * @returns {string} The warning message
578
+ * in the `useWarnStore` store.
579
+ */
580
+ export function useWarning() {
581
+ return useWarnStore(state => state.warning);
582
+ }
583
+
584
+ /**
585
+ * Obtain the warning setter function from
586
+ * the global app state.
587
+ * @returns {function} The warning setter function
588
+ * in the `useWarnStore` store.
589
+ */
590
+ export function useSetWarning() {
591
+ return useWarnStore(state => state.setWarning);
592
+ }
593
+
594
+ /**
595
+ * Obtain the component view info value from
596
+ * the global app state.
597
+ * @returns {object} The view info object for the component
598
+ * in the `useViewInfoStore` store.
599
+ */
600
+ export function useComponentViewInfo(uuid) {
601
+ return useViewInfoStore(useCallback(state => state.viewInfo[uuid], [uuid]));
602
+ }
603
+
604
+ /**
605
+ * Obtain the component view info setter function from
606
+ * the global app state.
607
+ * @returns {function} The component view info setter function
608
+ * in the `useViewInfoStore` store.
609
+ */
610
+ export function useSetComponentViewInfo(uuid) {
611
+ const setViewInfoRef = useRef(useViewInfoStore.getState().setComponentViewInfo);
612
+ const setComponentViewInfo = viewInfo => setViewInfoRef.current(uuid, viewInfo);
613
+ return setComponentViewInfo;
614
+ }
615
+
616
+ /**
617
+ * Obtain the grid resize count value
618
+ * from the global app state.
619
+ * @returns {number} The grid resize increment value.
620
+ */
621
+ export function useGridResize() {
622
+ return useGridSizeStore(state => state.resizeCount);
623
+ }
624
+
625
+ /**
626
+ * Obtain the grid resize count increment function
627
+ * from the global app state.
628
+ * @returns {function} The grid resize count increment
629
+ * function.
630
+ */
631
+ export function useEmitGridResize() {
632
+ return useGridSizeStore(state => state.incrementResizeCount);
633
+ }