-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdashboard-provider-api.ts
More file actions
460 lines (429 loc) · 16.7 KB
/
dashboard-provider-api.ts
File metadata and controls
460 lines (429 loc) · 16.7 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { useCallback, useMemo } from 'react';
import { PanelGroupItemLayout, PanelGroupDefinition, PanelGroupItemId } from '@perses-dev/core'; // TODO
import { DurationString, Link, PanelDefinition, PanelGroupId } from '@perses-dev/spec';
import { DashboardResource } from '../../model';
import { DashboardStoreState, useDashboardStore } from './DashboardProvider';
import { DeletePanelGroupDialogState } from './delete-panel-group-slice';
import { PanelGroupEditor } from './panel-group-editor-slice';
import { PanelEditorState } from './panel-editor-slice';
import { DeletePanelDialogState } from './delete-panel-slice';
import { SaveChangesConfirmationDialogState } from './save-changes-dialog-slice';
import { DiscardChangesConfirmationDialogState } from './discard-changes-dialog-slice';
import { EditJsonDialogState } from './edit-json-dialog-slice';
import { ViewPanelSlice } from './view-panel-slice';
const selectEditMode: ({ isEditMode, setEditMode }: DashboardStoreState) => {
setEditMode: (isEditMode: boolean) => void;
isEditMode: boolean;
} = ({ isEditMode, setEditMode }: DashboardStoreState) => ({ isEditMode, setEditMode });
export function useEditMode(): { setEditMode: (isEditMode: boolean) => void; isEditMode: boolean } {
return useDashboardStore(selectEditMode);
}
const selectDashboardActions: ({ setDashboard, openAddPanelGroup, openAddPanel }: DashboardStoreState) => {
openAddPanelGroup: () => void;
openAddPanel: (panelGroupId?: PanelGroupId) => void;
setDashboard: (dashboard: DashboardResource) => void;
} = ({ setDashboard, openAddPanelGroup, openAddPanel }: DashboardStoreState) => ({
setDashboard,
openAddPanelGroup,
openAddPanel,
});
/**
* Returns actions that can be performed on the current dashboard.
*/
export function useDashboardActions(): {
openAddPanelGroup: () => void;
openAddPanel: () => void;
setDashboard: (dashboard: DashboardResource) => void;
} {
const { setDashboard, openAddPanelGroup, openAddPanel } = useDashboardStore(selectDashboardActions);
return {
setDashboard,
openAddPanelGroup: () => openAddPanelGroup(),
openAddPanel: () => openAddPanel(),
};
}
const selectDashboardLinks: (state: DashboardStoreState) => Link[] | undefined = (state: DashboardStoreState) =>
state.links;
/**
* Returns the dashboard links.
*/
export function useDashboardLinks(): Link[] {
return useDashboardStore(selectDashboardLinks) ?? [];
}
export interface DashboardLinksActions {
setLinks?: (links: Link[]) => void;
}
const selectDashboardLinksActions: (state: DashboardStoreState) => DashboardLinksActions = (
state: DashboardStoreState
) => ({
setLinks: state.setLinks,
});
/**
* Returns actions that can be performed on dashboard links.
*/
export function useDashboardLinksActions(): DashboardLinksActions {
return useDashboardStore(selectDashboardLinksActions);
}
const selectPanelGroupOrder = (state: DashboardStoreState): number[] => state.panelGroupOrder;
/**
* Returns an array of PanelGroupIds in the order they appear in the dashboard.
*/
export function usePanelGroupIds(): number[] {
return useDashboardStore(selectPanelGroupOrder);
}
const selectPanelGroups: (state: DashboardStoreState) => Record<number, PanelGroupDefinition> = (
state: DashboardStoreState
) => state.panelGroups;
/**
* Returns an array of PanelGroupDefinitions in the order they appear in the dashboard.
*/
export function useListPanelGroups(): PanelGroupDefinition[] {
const panelGroupIds = usePanelGroupIds();
const panelGroups = useDashboardStore(selectPanelGroups);
return useMemo(() => {
return panelGroupIds.map((id) => {
const group = panelGroups[id];
if (group === undefined) {
throw new Error(`Invalid panel group Id found ${id}`);
}
return group;
});
}, [panelGroupIds, panelGroups]);
}
/**
* Gets a specific panel group by its id. Throws if the panel group does not exist.
*/
export function usePanelGroup(panelGroupId: PanelGroupId): PanelGroupDefinition {
const panelGroup = useDashboardStore(useCallback((state) => state.panelGroups[panelGroupId], [panelGroupId]));
if (panelGroup === undefined) {
throw new Error(`Panel group with Id ${panelGroupId} was not found`);
}
return panelGroup;
}
const selectPanelGroupActions: ({
openEditPanelGroup,
deletePanelGroup,
openAddPanel,
updatePanelGroupLayouts,
}: DashboardStoreState) => {
updatePanelGroupLayouts: (panelGroupId: PanelGroupId, itemLayouts: PanelGroupDefinition['itemLayouts']) => void;
openEditPanelGroup: (panelGroupId: PanelGroupId) => void;
openAddPanel: (panelGroupId?: PanelGroupId) => void;
deletePanelGroup: (panelGroupId: PanelGroupId) => void;
} = ({ openEditPanelGroup, deletePanelGroup, openAddPanel, updatePanelGroupLayouts }: DashboardStoreState) => ({
openEditPanelGroup,
deletePanelGroup,
openAddPanel,
updatePanelGroupLayouts,
});
/**
* Returns actions that can be performed on the given panel group.
*/
export function usePanelGroupActions(panelGroupId: PanelGroupId): {
updatePanelGroupLayouts: (itemLayouts: PanelGroupItemLayout[]) => void;
openEditPanelGroup: () => void;
openAddPanel: () => void;
moveDown: (() => void) | undefined;
deletePanelGroup: () => void;
moveUp: (() => void) | undefined;
} {
const { moveUp, moveDown } = useMovePanelGroup(panelGroupId);
const { openEditPanelGroup, deletePanelGroup, openAddPanel, updatePanelGroupLayouts } =
useDashboardStore(selectPanelGroupActions);
return {
openEditPanelGroup: () => openEditPanelGroup(panelGroupId),
deletePanelGroup: () => deletePanelGroup(panelGroupId),
openAddPanel: () => openAddPanel(panelGroupId),
moveUp,
moveDown,
updatePanelGroupLayouts: (itemLayouts: PanelGroupItemLayout[]) =>
updatePanelGroupLayouts(panelGroupId, itemLayouts),
};
}
const selectSwapPanelGroups: (state: DashboardStoreState) => (xIndex: number, yIndex: number) => void = (
state: DashboardStoreState
) => state.swapPanelGroups;
const selectPanelGroupsLength: (state: DashboardStoreState) => number = (state: DashboardStoreState) =>
state.panelGroupOrder.length;
/**
* Returns functions for moving a panel group up or down. A function will be undefined if the panel group can't be
* moved in that direction.
*/
function useMovePanelGroup(panelGroupId: PanelGroupId): {
moveDown: (() => void) | undefined;
moveUp: (() => void) | undefined;
} {
const currentIndex = useDashboardStore(
useCallback((store) => store.panelGroupOrder.findIndex((id) => id === panelGroupId), [panelGroupId])
);
const panelGroupsLength = useDashboardStore(selectPanelGroupsLength);
const swapPanelGroups = useDashboardStore(selectSwapPanelGroups);
if (currentIndex < 0) {
throw new Error(`Could not find panel group with Id ${panelGroupId} in order array`);
}
const moveUp: () => void = () => swapPanelGroups(currentIndex, currentIndex - 1);
const moveDown: () => void = () => swapPanelGroups(currentIndex, currentIndex + 1);
return {
moveUp: currentIndex > 0 ? moveUp : undefined,
moveDown: currentIndex < panelGroupsLength - 1 ? moveDown : undefined,
};
}
const selectPanelGroupEditor: (state: DashboardStoreState) => PanelGroupEditor | undefined = (
state: DashboardStoreState
) => state.panelGroupEditor;
/**
* Gets the Panel Group editor state.
*/
export function usePanelGroupEditor(): PanelGroupEditor | undefined {
return useDashboardStore(selectPanelGroupEditor);
}
const selectDeletePanelGroupDialog: ({
deletePanelGroupDialog,
openDeletePanelGroupDialog,
closeDeletePanelGroupDialog,
deletePanelGroup,
}: DashboardStoreState) => {
deletePanelGroupDialog: DeletePanelGroupDialogState | undefined;
closeDeletePanelGroupDialog: () => void;
openDeletePanelGroupDialog: (panelGroupId: PanelGroupId) => void;
deletePanelGroup: (panelGroupId: PanelGroupId) => void;
} = ({
deletePanelGroupDialog,
openDeletePanelGroupDialog,
closeDeletePanelGroupDialog,
deletePanelGroup,
}: DashboardStoreState) => ({
deletePanelGroupDialog,
openDeletePanelGroupDialog,
closeDeletePanelGroupDialog,
deletePanelGroup,
});
/**
* Gets the Delete Panel Group dialog state.
*/
export function useDeletePanelGroupDialog(): {
deletePanelGroupDialog: DeletePanelGroupDialogState | undefined;
closeDeletePanelGroupDialog: () => void;
openDeletePanelGroupDialog: (panelGroupId: PanelGroupId) => void;
deletePanelGroup: (panelGroupId: PanelGroupId) => void;
} {
const { deletePanelGroupDialog, openDeletePanelGroupDialog, closeDeletePanelGroupDialog, deletePanelGroup } =
useDashboardStore(selectDeletePanelGroupDialog);
return {
deletePanelGroupDialog,
deletePanelGroup,
openDeletePanelGroupDialog,
closeDeletePanelGroupDialog: () => closeDeletePanelGroupDialog(),
};
}
export function usePanelKey(panelGroupItemId?: PanelGroupItemId): string | undefined {
const panelKey = useDashboardStore(
useCallback(
(store) => {
if (panelGroupItemId === undefined) {
return undefined;
}
return store.panelGroups[panelGroupItemId.panelGroupId]?.itemPanelKeys[panelGroupItemId.panelGroupItemLayoutId];
},
[panelGroupItemId]
)
);
return panelKey;
}
/**
* Gets an individual panel in the store. Throws if the panel can't be found.
*/
export function usePanel(panelGroupItemId: PanelGroupItemId): PanelDefinition {
const { panelGroupId, panelGroupItemLayoutId: panelGroupLayoutId } = panelGroupItemId;
const panel = useDashboardStore(
useCallback(
(store) => {
const panelKey = store.panelGroups[panelGroupId]?.itemPanelKeys[panelGroupLayoutId];
if (panelKey === undefined) return;
return store.panels[panelKey];
},
[panelGroupId, panelGroupLayoutId]
)
);
if (panel === undefined) {
throw new Error(`Could not find panel for Id ${panelGroupItemId}`);
}
return panel;
}
const selectPanelActions: ({
openEditPanel,
openDeletePanelDialog,
duplicatePanel,
setViewPanel,
}: DashboardStoreState) => {
openDeletePanelDialog: (panelGroupItemId: PanelGroupItemId) => void;
duplicatePanel: (panelGroupItemId: PanelGroupItemId) => void;
openEditPanel: (panelGroupItemId: PanelGroupItemId) => void;
setViewPanel: (panelGroupItemId?: PanelGroupItemId) => void;
} = ({ openEditPanel, openDeletePanelDialog, duplicatePanel, setViewPanel }: DashboardStoreState) => ({
openEditPanel,
openDeletePanelDialog,
duplicatePanel,
setViewPanel,
});
/**
* Returns actions that can be performed on the given Panel.
*/
export function usePanelActions(panelGroupItemId: PanelGroupItemId): {
openDeletePanelDialog: () => void;
duplicatePanel: () => void;
openEditPanel: () => void;
viewPanel: (panelGroupItemId?: PanelGroupItemId) => void;
} {
const { openEditPanel, openDeletePanelDialog, duplicatePanel, setViewPanel } = useDashboardStore(selectPanelActions);
return {
openEditPanel: () => openEditPanel(panelGroupItemId),
openDeletePanelDialog: () => openDeletePanelDialog(panelGroupItemId),
duplicatePanel: () => duplicatePanel(panelGroupItemId),
viewPanel: (panelGroupItemId?: PanelGroupItemId) => setViewPanel(panelGroupItemId),
};
}
const selectPanelEditor: (state: DashboardStoreState) => PanelEditorState | undefined = (state: DashboardStoreState) =>
state.panelEditor;
/**
* Gets the state for the Panel Editor.
*/
export function usePanelEditor(): PanelEditorState | undefined {
return useDashboardStore(selectPanelEditor);
}
const selectDeletePanelDialog: ({ deletePanelDialog, deletePanel, closeDeletePanelDialog }: DashboardStoreState) => {
deletePanelDialog: DeletePanelDialogState | undefined;
closeDeletePanelDialog: () => void;
deletePanel: (panelGroupItemId: PanelGroupItemId) => void;
} = ({ deletePanelDialog, deletePanel, closeDeletePanelDialog }: DashboardStoreState) => ({
deletePanelDialog,
deletePanel,
closeDeletePanelDialog,
});
/**
* Gets the state for the Delete Panel dialog.
*/
export function useDeletePanelDialog(): {
deletePanelDialog: DeletePanelDialogState | undefined;
closeDeletePanelDialog: () => void;
deletePanel: (panelGroupItemId: PanelGroupItemId) => void;
} {
// TODO: Refactor similar to other dialogs/editors so these are on the editor state itself
return useDashboardStore(selectDeletePanelDialog);
}
const selectDashboardDuration: (state: DashboardStoreState) => DurationString = (state: DashboardStoreState) =>
state.duration;
export function useDashboardDuration(): DurationString {
return useDashboardStore(selectDashboardDuration);
}
const selectViewPanel: (state: DashboardStoreState) => {
setViewPanel: DashboardStoreState['setViewPanel'];
getViewPanel: DashboardStoreState['getViewPanel'];
viewPanelId: DashboardStoreState['viewPanel']['panelGroupItemId'];
} = (state: DashboardStoreState) => ({
setViewPanel: state.setViewPanel,
getViewPanel: state.getViewPanel,
viewPanelId: state.getViewPanel(),
});
/**
* Returns actions related to the ViewPanel.
*/
export function useViewPanel(): {
setViewPanel: ViewPanelSlice['setViewPanel'];
getViewPanel: ViewPanelSlice['getViewPanel'];
viewPanelId: ViewPanelSlice['viewPanel']['panelGroupItemId'];
} {
return useDashboardStore(selectViewPanel);
}
const selectViewPanelGroup: (state: DashboardStoreState) => PanelGroupItemId | undefined = (
state: DashboardStoreState
) => state.getViewPanel();
/**
* Gets the Panel Group for the view panel.
*/
export function useViewPanelGroup(): PanelGroupItemId | undefined {
return useDashboardStore(selectViewPanelGroup);
}
const selectSaveChangesConfirmationDialog: ({
saveChangesConfirmationDialog,
openSaveChangesConfirmationDialog,
closeSaveChangesConfirmationDialog,
}: DashboardStoreState) => {
closeSaveChangesConfirmationDialog: () => void;
openSaveChangesConfirmationDialog: (saveChangesConfirmationDialog: SaveChangesConfirmationDialogState) => void;
saveChangesConfirmationDialog: SaveChangesConfirmationDialogState | undefined;
} = ({
saveChangesConfirmationDialog,
openSaveChangesConfirmationDialog,
closeSaveChangesConfirmationDialog,
}: DashboardStoreState) => ({
saveChangesConfirmationDialog,
openSaveChangesConfirmationDialog,
closeSaveChangesConfirmationDialog,
});
export function useSaveChangesConfirmationDialog(): {
closeSaveChangesConfirmationDialog: () => void;
openSaveChangesConfirmationDialog: (saveChangesConfirmationDialog: SaveChangesConfirmationDialogState) => void;
saveChangesConfirmationDialog: SaveChangesConfirmationDialogState | undefined;
} {
return useDashboardStore(selectSaveChangesConfirmationDialog);
}
const selectDiscardChangesConfirmationDialog: ({
discardChangesConfirmationDialog,
openDiscardChangesConfirmationDialog,
closeDiscardChangesConfirmationDialog,
}: DashboardStoreState) => {
discardChangesConfirmationDialog: DiscardChangesConfirmationDialogState | undefined;
closeDiscardChangesConfirmationDialog: () => void;
openDiscardChangesConfirmationDialog: (
discardChangesConfirmationDialog: DiscardChangesConfirmationDialogState
) => void;
} = ({
discardChangesConfirmationDialog,
openDiscardChangesConfirmationDialog,
closeDiscardChangesConfirmationDialog,
}: DashboardStoreState) => ({
discardChangesConfirmationDialog,
openDiscardChangesConfirmationDialog,
closeDiscardChangesConfirmationDialog,
});
export function useDiscardChangesConfirmationDialog(): {
discardChangesConfirmationDialog: DiscardChangesConfirmationDialogState | undefined;
closeDiscardChangesConfirmationDialog: () => void;
openDiscardChangesConfirmationDialog: (
discardChangesConfirmationDialog: DiscardChangesConfirmationDialogState
) => void;
} {
return useDashboardStore(selectDiscardChangesConfirmationDialog);
}
const selectEditJsonDialog: ({ editJsonDialog, openEditJsonDialog, closeEditJsonDialog }: DashboardStoreState) => {
openEditJsonDialog: () => void;
closeEditJsonDialog: () => void;
editJsonDialog: EditJsonDialogState | undefined;
} = ({ editJsonDialog, openEditJsonDialog, closeEditJsonDialog }: DashboardStoreState) => ({
editJsonDialog,
openEditJsonDialog,
closeEditJsonDialog,
});
/**
* Gets the state for the edit JSON dialog.
*/
export function useEditJsonDialog(): {
openEditJsonDialog: () => void;
closeEditJsonDialog: () => void;
editJsonDialog: EditJsonDialogState | undefined;
} {
return useDashboardStore(selectEditJsonDialog);
}