forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashboardLayout.tsx
More file actions
404 lines (361 loc) · 12.1 KB
/
DashboardLayout.tsx
File metadata and controls
404 lines (361 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import React, {
type ComponentType,
type ReactElement,
useCallback,
useEffect,
useMemo,
useState,
} from 'react';
import type GoldenLayout from '@deephaven/golden-layout';
import type {
Container,
ItemConfig,
ReactComponentConfig,
AbstractContentItem,
} from '@deephaven/golden-layout';
import Log from '@deephaven/log';
import { usePrevious, useThrottledCallback } from '@deephaven/react-hooks';
import { ErrorBoundary } from '@deephaven/components';
import { type RootState } from '@deephaven/redux';
import { useDispatch, useSelector } from 'react-redux';
import PanelManager, { type ClosedPanels } from './PanelManager';
import PanelErrorBoundary from './PanelErrorBoundary';
import LayoutUtils, { isReactComponentConfig } from './layout/LayoutUtils';
import {
canHaveRef,
dehydrate as dehydrateDefault,
hydrate as hydrateDefault,
} from './DashboardUtils';
import PanelEvent from './PanelEvent';
import { useListener } from './layout';
import { getDashboardData, updateDashboardData } from './redux';
import {
type PanelConfig,
type PanelComponentType,
type PanelDehydrateFunction,
type PanelHydrateFunction,
type PanelProps,
type DehydratedPanelProps,
} from './DashboardPlugin';
import DashboardPanelWrapper from './DashboardPanelWrapper';
import { PanelIdContext } from './usePanelId';
export type DashboardLayoutConfig = ItemConfig[];
const log = Log.module('DashboardLayout');
const EMPTY_OBJECT = Object.freeze({});
const DEFAULT_LAYOUT_CONFIG: DashboardLayoutConfig = [];
const DEFAULT_CALLBACK = (): void => undefined;
const STATE_CHANGE_THROTTLE_MS = 1000;
// If a component isn't registered, just pass through the props so they are saved if a plugin is loaded later
const FALLBACK_CALLBACK = <P,>(props: P): P => props;
type DashboardData = {
closed?: ClosedPanels;
};
type DashboardLayoutProps = React.PropsWithChildren<{
id: string;
// Default hydrate/dehydration functions
hydrate?: PanelHydrateFunction;
dehydrate?: PanelDehydrateFunction;
layout: GoldenLayout;
layoutConfig?: DashboardLayoutConfig;
onLayoutChange?: (dehydratedLayout: DashboardLayoutConfig) => void;
onLayoutInitialized?: () => void;
data?: DashboardData;
emptyDashboard?: React.ReactNode;
/** Component to wrap each panel with */
panelWrapper?: ComponentType<React.PropsWithChildren<PanelProps>>;
}>;
/**
* DashboardLayout component. Handles hydrating, dehydrating components, listening for dragging panels.
*/
export function DashboardLayout({
id,
children,
emptyDashboard = <div className="dashboard-empty">Dashboard is empty.</div>,
layout,
layoutConfig = DEFAULT_LAYOUT_CONFIG,
onLayoutChange = DEFAULT_CALLBACK,
onLayoutInitialized = DEFAULT_CALLBACK,
hydrate = hydrateDefault,
dehydrate = dehydrateDefault,
panelWrapper,
}: DashboardLayoutProps): JSX.Element {
const dispatch = useDispatch();
const data =
useSelector<RootState>(state => getDashboardData(state, id)) ??
EMPTY_OBJECT;
const [isDashboardEmpty, setIsDashboardEmpty] = useState(false);
const [isItemDragging, setIsItemDragging] = useState(false);
const [lastConfig, setLastConfig] = useState<DashboardLayoutConfig>();
const [initialClosedPanels] = useState<ReactComponentConfig[] | undefined>(
(data as DashboardData)?.closed ?? []
);
const [layoutChildren, setLayoutChildren] = useState(
layout.getReactChildren()
);
const hydrateMap = useMemo(() => new Map<string, PanelHydrateFunction>(), []);
const dehydrateMap = useMemo(
() => new Map<string, PanelDehydrateFunction>(),
[]
);
const registerComponent = useCallback(
(
name: string,
componentType: PanelComponentType,
componentHydrate = hydrate,
componentDehydrate = dehydrate
) => {
log.debug2(
'registerComponent',
name,
componentType,
componentHydrate,
componentDehydrate
);
function wrappedComponent(
props: PanelProps,
ref: React.Ref<unknown>
): JSX.Element {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const CType = componentType as any;
const PanelWrapperType = panelWrapper;
/**
* The ref is used to detect changes to class component state so we
* can track changes to panelState. We should opt for more explicit
* state changes in the future and in functional components.
*/
const hasRef = canHaveRef(CType);
const innerElem = hasRef ? (
// eslint-disable-next-line react/jsx-props-no-spreading
<CType {...props} ref={ref} />
) : (
// eslint-disable-next-line react/jsx-props-no-spreading
<CType {...props} />
);
// Props supplied by GoldenLayout
const { glContainer, glEventHub } = props;
const panelId = LayoutUtils.getIdFromContainer(glContainer);
return (
<PanelErrorBoundary glContainer={glContainer} glEventHub={glEventHub}>
<PanelIdContext.Provider value={panelId as string | null}>
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
<DashboardPanelWrapper {...props}>
{PanelWrapperType == null ? (
innerElem
) : (
// eslint-disable-next-line react/jsx-props-no-spreading
<PanelWrapperType {...props}>{innerElem}</PanelWrapperType>
)}
</DashboardPanelWrapper>
</PanelIdContext.Provider>
</PanelErrorBoundary>
);
}
wrappedComponent.displayName = `DashboardWrapper(${
componentType.displayName ?? name
})`;
const cleanup = layout.registerComponent(
name,
React.forwardRef(wrappedComponent)
);
hydrateMap.set(name, componentHydrate);
dehydrateMap.set(name, componentDehydrate);
return cleanup;
},
[hydrate, dehydrate, hydrateMap, dehydrateMap, layout, panelWrapper]
);
const hydrateComponent = useCallback(
(name: string, props: DehydratedPanelProps) =>
(hydrateMap.get(name) ?? (FALLBACK_CALLBACK as PanelHydrateFunction))(
props,
id
),
[hydrateMap, id]
);
const dehydrateComponent = useCallback(
(name: string, config: PanelConfig) =>
(dehydrateMap.get(name) ?? (FALLBACK_CALLBACK as PanelDehydrateFunction))(
config,
id
),
[dehydrateMap, id]
);
const panelManager = useMemo(
() =>
new PanelManager(
layout,
hydrateComponent,
dehydrateComponent,
new Map(),
initialClosedPanels,
({ closed, openedMap }) => {
dispatch(updateDashboardData(id, { closed, openedMap }));
}
),
[
dehydrateComponent,
dispatch,
hydrateComponent,
id,
initialClosedPanels,
layout,
]
);
// Throttle the calls so that we don't flood comparing these layouts
const throttledProcessDehydratedLayoutConfig = useThrottledCallback(
(dehydratedLayoutConfig: DashboardLayoutConfig) => {
const hasChanged =
lastConfig == null ||
!LayoutUtils.isEqual(lastConfig, dehydratedLayoutConfig);
log.debug('handleLayoutStateChanged', hasChanged, dehydratedLayoutConfig);
if (hasChanged) {
log.debug(
'Layout config has changed',
'lastConfig',
lastConfig,
'dehydratedLayoutConfig',
dehydratedLayoutConfig
);
setIsDashboardEmpty(layout.root.contentItems.length === 0);
setLastConfig(dehydratedLayoutConfig);
onLayoutChange(dehydratedLayoutConfig);
setLayoutChildren(layout.getReactChildren());
}
},
STATE_CHANGE_THROTTLE_MS,
{ flushOnUnmount: true }
);
useEffect(
() => () => throttledProcessDehydratedLayoutConfig.flush(),
[throttledProcessDehydratedLayoutConfig]
);
const handleLayoutStateChanged = useCallback(() => {
// we don't want to emit stateChanges that happen during item drags or else
// we risk the last saved state being one without that panel in the layout entirely
if (isItemDragging) return;
const glConfig = layout.toConfig();
const contentConfig = glConfig.content;
const dehydratedLayoutConfig = LayoutUtils.dehydrateLayoutConfig(
contentConfig,
dehydrateComponent
);
throttledProcessDehydratedLayoutConfig(dehydratedLayoutConfig);
}, [
dehydrateComponent,
isItemDragging,
layout,
throttledProcessDehydratedLayoutConfig,
]);
const handleLayoutItemPickedUp = useCallback(
(component: Container) => {
const componentId = LayoutUtils.getIdFromContainer(component);
layout.eventHub.emit(PanelEvent.DRAGGING, componentId);
setIsItemDragging(true);
},
[layout.eventHub]
);
const handleLayoutItemDropped = useCallback(
(component: Container) => {
const componentId = LayoutUtils.getIdFromContainer(component);
layout.eventHub.emit(PanelEvent.DROPPED, componentId);
setIsItemDragging(false);
},
[layout.eventHub]
);
const handleComponentCreated = useCallback((item: AbstractContentItem) => {
log.debug2('handleComponentCreated', item);
if (
item == null ||
item.config == null ||
!isReactComponentConfig(item.config) ||
item.config.component == null ||
item.element == null
) {
return;
}
const cssComponent = item.config.component
.replace(/([a-z])([A-Z])/g, '$1-$2')
.toLowerCase();
const cssClass = `${cssComponent}-component`;
item.element.addClass(cssClass);
}, []);
const handleReactChildrenChange = useCallback(() => {
setLayoutChildren(layout.getReactChildren());
}, [layout]);
useListener(layout, 'stateChanged', handleLayoutStateChanged);
useListener(layout, 'itemPickedUp', handleLayoutItemPickedUp);
useListener(layout, 'itemDropped', handleLayoutItemDropped);
useListener(layout, 'componentCreated', handleComponentCreated);
useListener(
layout.eventHub,
PanelEvent.TITLE_CHANGED,
handleLayoutStateChanged
);
useListener(layout, 'reactChildrenChanged', handleReactChildrenChange);
const previousLayoutConfig = usePrevious(layoutConfig);
useEffect(
function loadNewConfig() {
if (
previousLayoutConfig !== layoutConfig &&
layoutConfig !== lastConfig
) {
log.debug(
'loadNewConfig effect triggered. previousLayoutConfig',
previousLayoutConfig,
'layoutConfig',
layoutConfig,
'lastConfig',
lastConfig
);
log.debug('Setting new layout content...');
const content = LayoutUtils.hydrateLayoutConfig(
layoutConfig,
hydrateComponent
);
// Remove the old layout before add the new one
while (layout.root.contentItems.length > 0) {
layout.root.contentItems[0].remove();
}
// Add the new content. It is usally just one item from the root
for (let i = 0; i < content.length; i += 1) {
layout.root.addChild(content[i]);
}
setIsDashboardEmpty(layout.root.contentItems.length === 0);
}
},
[
hydrateComponent,
layout,
layoutConfig,
lastConfig,
panelManager,
previousLayoutConfig,
]
);
// This should be the last hook called in this component
// Ensures it runs after any other effects on mount
// Fire only once after the layout is mounted
// This should ensure DashboardPlugins have been mounted
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => onLayoutInitialized(), []);
return (
<>
{isDashboardEmpty && emptyDashboard}
{layoutChildren}
{React.Children.map(children, child =>
child != null ? (
// Have fallback be an empty array so that we don't show the error message over entire app
// Look into using toast message in the future
<ErrorBoundary fallback={[]}>
{React.cloneElement(child as ReactElement, {
id,
layout,
panelManager,
registerComponent,
})}
</ErrorBoundary>
) : null
)}
</>
);
}
export default DashboardLayout;