@perses-dev/dashboards 0.9.0 → 0.10.0
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/dist/cjs/components/Dashboard.js +7 -11
- package/dist/cjs/components/DashboardToolbar.js +4 -4
- package/dist/cjs/components/GridLayout/GridItemContent.js +13 -13
- package/dist/cjs/components/GridLayout/GridLayout.js +24 -21
- package/dist/cjs/components/GridLayout/GridTitle.js +27 -5
- package/dist/cjs/components/Panel/DeletePanelDialog.js +91 -0
- package/dist/cjs/components/Panel/Panel.js +16 -5
- package/dist/cjs/components/Panel/Panel.test.js +12 -4
- package/dist/cjs/components/Panel/PanelContent.js +3 -2
- package/dist/cjs/components/PanelDrawer/PanelDrawer.js +13 -12
- package/dist/cjs/components/PanelDrawer/PanelDrawer.test.js +8 -12
- package/dist/cjs/components/PanelDrawer/PanelEditorForm.js +77 -35
- package/dist/cjs/components/PanelDrawer/PanelPreview.js +48 -0
- package/dist/cjs/components/PanelGroupDialog/DeletePanelGroupDialog.js +86 -0
- package/dist/cjs/components/PanelGroupDialog/PanelGroupDialog.js +12 -19
- package/dist/cjs/components/PanelGroupDialog/PanelGroupDialog.test.js +10 -31
- package/dist/cjs/components/TimeRangeControls/TimeRangeControls.js +2 -2
- package/dist/cjs/components/Variables/Variable.js +19 -11
- package/dist/cjs/components/Variables/VariableEditor.js +207 -0
- package/dist/cjs/components/Variables/VariableEditorForm/VariableEditorForm.js +236 -0
- package/dist/cjs/components/Variables/VariableEditorForm/index.js +28 -0
- package/dist/cjs/components/Variables/VariableEditorForm/variable-editor-form-model.js +88 -0
- package/dist/cjs/components/Variables/VariableList.js +50 -69
- package/dist/cjs/components/Variables/index.js +1 -0
- package/dist/cjs/context/DashboardProvider/DashboardProvider.js +70 -0
- package/dist/cjs/context/DashboardProvider/common.js +18 -0
- package/dist/cjs/context/DashboardProvider/dashboard-provider-api.js +71 -0
- package/dist/cjs/context/DashboardProvider/index.js +29 -0
- package/dist/cjs/context/DashboardProvider/layout-slice.js +200 -0
- package/dist/cjs/context/DashboardProvider/panel-editing-slice.js +156 -0
- package/dist/{context/DashboardAppSlice.js → cjs/context/DashboardProvider/panel-group-slice.js} +18 -25
- package/dist/cjs/context/DatasourceStoreProvider.js +109 -7
- package/dist/cjs/context/TemplateVariableProvider.js +8 -1
- package/dist/cjs/context/index.js +0 -2
- package/dist/cjs/test/testDashboard.js +13 -1
- package/dist/cjs/views/ViewDashboard/DashboardApp.js +6 -7
- package/dist/cjs/views/ViewDashboard/tests/panelGroups.test.js +165 -0
- package/dist/components/Dashboard.d.ts +1 -4
- package/dist/components/Dashboard.d.ts.map +1 -1
- package/dist/components/Dashboard.js +8 -12
- package/dist/components/Dashboard.js.map +1 -1
- package/dist/components/DashboardToolbar.d.ts.map +1 -1
- package/dist/components/DashboardToolbar.js +5 -5
- package/dist/components/DashboardToolbar.js.map +1 -1
- package/dist/components/GridLayout/GridItemContent.d.ts +3 -3
- package/dist/components/GridLayout/GridItemContent.d.ts.map +1 -1
- package/dist/components/GridLayout/GridItemContent.js +14 -14
- package/dist/components/GridLayout/GridItemContent.js.map +1 -1
- package/dist/components/GridLayout/GridLayout.d.ts +2 -3
- package/dist/components/GridLayout/GridLayout.d.ts.map +1 -1
- package/dist/components/GridLayout/GridLayout.js +24 -21
- package/dist/components/GridLayout/GridLayout.js.map +1 -1
- package/dist/components/GridLayout/GridTitle.d.ts.map +1 -1
- package/dist/components/GridLayout/GridTitle.js +28 -6
- package/dist/components/GridLayout/GridTitle.js.map +1 -1
- package/dist/components/Panel/DeletePanelDialog.d.ts +5 -0
- package/dist/components/Panel/DeletePanelDialog.d.ts.map +1 -0
- package/dist/components/Panel/DeletePanelDialog.js +80 -0
- package/dist/components/Panel/DeletePanelDialog.js.map +1 -0
- package/dist/components/Panel/Panel.d.ts +1 -1
- package/dist/components/Panel/Panel.d.ts.map +1 -1
- package/dist/components/Panel/Panel.js +17 -6
- package/dist/components/Panel/Panel.js.map +1 -1
- package/dist/components/Panel/Panel.test.js +7 -4
- package/dist/components/Panel/Panel.test.js.map +1 -1
- package/dist/components/Panel/PanelContent.d.ts +4 -1
- package/dist/components/Panel/PanelContent.d.ts.map +1 -1
- package/dist/components/Panel/PanelContent.js +3 -2
- package/dist/components/Panel/PanelContent.js.map +1 -1
- package/dist/components/PanelDrawer/PanelDrawer.d.ts.map +1 -1
- package/dist/components/PanelDrawer/PanelDrawer.js +14 -13
- package/dist/components/PanelDrawer/PanelDrawer.js.map +1 -1
- package/dist/components/PanelDrawer/PanelDrawer.test.js +8 -12
- package/dist/components/PanelDrawer/PanelDrawer.test.js.map +1 -1
- package/dist/components/PanelDrawer/PanelEditorForm.d.ts +3 -3
- package/dist/components/PanelDrawer/PanelEditorForm.d.ts.map +1 -1
- package/dist/components/PanelDrawer/PanelEditorForm.js +78 -36
- package/dist/components/PanelDrawer/PanelEditorForm.js.map +1 -1
- package/dist/components/PanelDrawer/PanelPreview.d.ts +4 -0
- package/dist/components/PanelDrawer/PanelPreview.d.ts.map +1 -0
- package/dist/components/PanelDrawer/PanelPreview.js +42 -0
- package/dist/components/PanelDrawer/PanelPreview.js.map +1 -0
- package/dist/components/PanelGroupDialog/DeletePanelGroupDialog.d.ts +4 -0
- package/dist/components/PanelGroupDialog/DeletePanelGroupDialog.d.ts.map +1 -0
- package/dist/components/PanelGroupDialog/DeletePanelGroupDialog.js +75 -0
- package/dist/components/PanelGroupDialog/DeletePanelGroupDialog.js.map +1 -0
- package/dist/components/PanelGroupDialog/PanelGroupDialog.d.ts.map +1 -1
- package/dist/components/PanelGroupDialog/PanelGroupDialog.js +13 -20
- package/dist/components/PanelGroupDialog/PanelGroupDialog.js.map +1 -1
- package/dist/components/PanelGroupDialog/PanelGroupDialog.test.js +10 -31
- package/dist/components/PanelGroupDialog/PanelGroupDialog.test.js.map +1 -1
- package/dist/components/TimeRangeControls/TimeRangeControls.js +3 -3
- package/dist/components/TimeRangeControls/TimeRangeControls.js.map +1 -1
- package/dist/components/Variables/Variable.js +21 -13
- package/dist/components/Variables/Variable.js.map +1 -1
- package/dist/components/Variables/VariableEditor.d.ts +8 -0
- package/dist/components/Variables/VariableEditor.d.ts.map +1 -0
- package/dist/components/Variables/VariableEditor.js +196 -0
- package/dist/components/Variables/VariableEditor.js.map +1 -0
- package/dist/components/Variables/VariableEditorForm/VariableEditorForm.d.ts +8 -0
- package/dist/components/Variables/VariableEditorForm/VariableEditorForm.d.ts.map +1 -0
- package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js +225 -0
- package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js.map +1 -0
- package/dist/components/Variables/VariableEditorForm/index.d.ts +2 -0
- package/dist/components/Variables/VariableEditorForm/index.d.ts.map +1 -0
- package/dist/components/Variables/VariableEditorForm/index.js +15 -0
- package/dist/components/Variables/VariableEditorForm/index.js.map +1 -0
- package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.d.ts +21 -0
- package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.d.ts.map +1 -0
- package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.js +76 -0
- package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.js.map +1 -0
- package/dist/components/Variables/VariableList.d.ts.map +1 -1
- package/dist/components/Variables/VariableList.js +48 -72
- package/dist/components/Variables/VariableList.js.map +1 -1
- package/dist/components/Variables/index.d.ts +1 -0
- package/dist/components/Variables/index.d.ts.map +1 -1
- package/dist/components/Variables/index.js +1 -0
- package/dist/components/Variables/index.js.map +1 -1
- package/dist/context/DashboardProvider/DashboardProvider.d.ts +23 -0
- package/dist/context/DashboardProvider/DashboardProvider.d.ts.map +1 -0
- package/dist/context/DashboardProvider/DashboardProvider.js +52 -0
- package/dist/context/DashboardProvider/DashboardProvider.js.map +1 -0
- package/dist/context/DashboardProvider/common.d.ts +5 -0
- package/dist/context/DashboardProvider/common.d.ts.map +1 -0
- package/dist/context/DashboardProvider/common.js +17 -0
- package/dist/context/DashboardProvider/common.js.map +1 -0
- package/dist/context/DashboardProvider/dashboard-provider-api.d.ts +32 -0
- package/dist/context/DashboardProvider/dashboard-provider-api.d.ts.map +1 -0
- package/dist/context/DashboardProvider/dashboard-provider-api.js +56 -0
- package/dist/context/DashboardProvider/dashboard-provider-api.js.map +1 -0
- package/dist/context/DashboardProvider/index.d.ts +3 -0
- package/dist/context/DashboardProvider/index.d.ts.map +1 -0
- package/dist/context/DashboardProvider/index.js +16 -0
- package/dist/context/DashboardProvider/index.js.map +1 -0
- package/dist/context/DashboardProvider/layout-slice.d.ts +57 -0
- package/dist/context/DashboardProvider/layout-slice.d.ts.map +1 -0
- package/dist/context/DashboardProvider/layout-slice.js +196 -0
- package/dist/context/DashboardProvider/layout-slice.js.map +1 -0
- package/dist/context/DashboardProvider/panel-editing-slice.d.ts +70 -0
- package/dist/context/DashboardProvider/panel-editing-slice.d.ts.map +1 -0
- package/dist/context/DashboardProvider/panel-editing-slice.js +152 -0
- package/dist/context/DashboardProvider/panel-editing-slice.js.map +1 -0
- package/dist/context/DashboardProvider/panel-group-slice.d.ts +15 -0
- package/dist/context/DashboardProvider/panel-group-slice.d.ts.map +1 -0
- package/dist/context/DashboardProvider/panel-group-slice.js +32 -0
- package/dist/context/DashboardProvider/panel-group-slice.js.map +1 -0
- package/dist/context/DatasourceStoreProvider.d.ts +10 -2
- package/dist/context/DatasourceStoreProvider.d.ts.map +1 -1
- package/dist/context/DatasourceStoreProvider.js +111 -9
- package/dist/context/DatasourceStoreProvider.js.map +1 -1
- package/dist/context/TemplateVariableProvider.d.ts +2 -0
- package/dist/context/TemplateVariableProvider.d.ts.map +1 -1
- package/dist/context/TemplateVariableProvider.js +8 -1
- package/dist/context/TemplateVariableProvider.js.map +1 -1
- package/dist/context/index.d.ts +0 -2
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +0 -2
- package/dist/context/index.js.map +1 -1
- package/dist/test/plugin-registry.d.ts +2 -2
- package/dist/test/plugin-registry.d.ts.map +1 -1
- package/dist/test/plugin-registry.js.map +1 -1
- package/dist/test/testDashboard.d.ts.map +1 -1
- package/dist/test/testDashboard.js +13 -1
- package/dist/test/testDashboard.js.map +1 -1
- package/dist/views/ViewDashboard/DashboardApp.d.ts.map +1 -1
- package/dist/views/ViewDashboard/DashboardApp.js +6 -7
- package/dist/views/ViewDashboard/DashboardApp.js.map +1 -1
- package/dist/views/ViewDashboard/tests/panelGroups.test.d.ts +2 -0
- package/dist/views/ViewDashboard/tests/panelGroups.test.d.ts.map +1 -0
- package/dist/views/ViewDashboard/tests/panelGroups.test.js +158 -0
- package/dist/views/ViewDashboard/tests/panelGroups.test.js.map +1 -0
- package/package.json +4 -4
- package/dist/cjs/components/PanelDrawer/PanelSpecEditor.js +0 -40
- package/dist/cjs/components/PanelDrawer/PanelTypeSelect.js +0 -38
- package/dist/cjs/components/PanelDrawer/panel-editor-model.js +0 -140
- package/dist/cjs/context/DashboardAppSlice.js +0 -57
- package/dist/cjs/context/DashboardProvider.js +0 -128
- package/dist/cjs/context/LayoutsSlice.js +0 -55
- package/dist/components/PanelDrawer/PanelSpecEditor.d.ts +0 -7
- package/dist/components/PanelDrawer/PanelSpecEditor.d.ts.map +0 -1
- package/dist/components/PanelDrawer/PanelSpecEditor.js +0 -34
- package/dist/components/PanelDrawer/PanelSpecEditor.js.map +0 -1
- package/dist/components/PanelDrawer/PanelTypeSelect.d.ts +0 -8
- package/dist/components/PanelDrawer/PanelTypeSelect.d.ts.map +0 -1
- package/dist/components/PanelDrawer/PanelTypeSelect.js +0 -34
- package/dist/components/PanelDrawer/PanelTypeSelect.js.map +0 -1
- package/dist/components/PanelDrawer/panel-editor-model.d.ts +0 -27
- package/dist/components/PanelDrawer/panel-editor-model.d.ts.map +0 -1
- package/dist/components/PanelDrawer/panel-editor-model.js +0 -133
- package/dist/components/PanelDrawer/panel-editor-model.js.map +0 -1
- package/dist/context/DashboardAppSlice.d.ts +0 -26
- package/dist/context/DashboardAppSlice.d.ts.map +0 -1
- package/dist/context/DashboardAppSlice.js.map +0 -1
- package/dist/context/DashboardProvider.d.ts +0 -36
- package/dist/context/DashboardProvider.d.ts.map +0 -1
- package/dist/context/DashboardProvider.js +0 -107
- package/dist/context/DashboardProvider.js.map +0 -1
- package/dist/context/LayoutsSlice.d.ts +0 -12
- package/dist/context/LayoutsSlice.d.ts.map +0 -1
- package/dist/context/LayoutsSlice.js +0 -43
- package/dist/context/LayoutsSlice.js.map +0 -1
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// Copyright 2022 The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
import { removeWhiteSpacesAndSpecialCharacters } from '../../utils/functions';
|
|
14
|
+
/**
|
|
15
|
+
* Curried function for creating the PanelEditorSlice.
|
|
16
|
+
*/ export function createPanelEditorSlice(panels) {
|
|
17
|
+
// Return the state creator function for Zustand that uses the panels provided as intitial state
|
|
18
|
+
return (set, get)=>{
|
|
19
|
+
return {
|
|
20
|
+
panels,
|
|
21
|
+
panelEditor: undefined,
|
|
22
|
+
editPanel (item) {
|
|
23
|
+
const { panels , getPanelKey } = get();
|
|
24
|
+
// Ask the layout store for the panel key at that location
|
|
25
|
+
const panelKey = getPanelKey(item);
|
|
26
|
+
// Find the panel to edit
|
|
27
|
+
const panelToEdit = panels[panelKey];
|
|
28
|
+
if (panelToEdit === undefined) {
|
|
29
|
+
throw new Error(`Cannot find Panel with key '${panelKey}'`);
|
|
30
|
+
}
|
|
31
|
+
var _description;
|
|
32
|
+
const editorState = {
|
|
33
|
+
mode: 'Edit',
|
|
34
|
+
initialValues: {
|
|
35
|
+
name: panelToEdit.spec.display.name,
|
|
36
|
+
description: (_description = panelToEdit.spec.display.description) !== null && _description !== void 0 ? _description : '',
|
|
37
|
+
groupIndex: item.groupIndex,
|
|
38
|
+
kind: panelToEdit.spec.plugin.kind,
|
|
39
|
+
spec: panelToEdit.spec.plugin.spec
|
|
40
|
+
},
|
|
41
|
+
applyChanges: (next)=>{
|
|
42
|
+
const panelDefinititon = createPanelDefinitionFromEditorValues(next);
|
|
43
|
+
set((state)=>{
|
|
44
|
+
state.panels[panelKey] = panelDefinititon;
|
|
45
|
+
});
|
|
46
|
+
// Move the panel to another group if it changed
|
|
47
|
+
if (next.groupIndex !== item.groupIndex) {
|
|
48
|
+
get().movePanelToGroup(item, next.groupIndex);
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
close: ()=>{
|
|
52
|
+
set((state)=>{
|
|
53
|
+
state.panelEditor = undefined;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
// Open the editor with the new state
|
|
58
|
+
set((state)=>{
|
|
59
|
+
state.panelEditor = editorState;
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
addPanel (initialGroup) {
|
|
63
|
+
const editorState = {
|
|
64
|
+
mode: 'Add',
|
|
65
|
+
initialValues: {
|
|
66
|
+
name: '',
|
|
67
|
+
description: '',
|
|
68
|
+
groupIndex: initialGroup,
|
|
69
|
+
// TODO: If we knew what plugins were available (and how to create the initial spec), we might be able to
|
|
70
|
+
// set a smarter default here?
|
|
71
|
+
kind: '',
|
|
72
|
+
spec: {}
|
|
73
|
+
},
|
|
74
|
+
applyChanges: (next)=>{
|
|
75
|
+
const panelDef = createPanelDefinitionFromEditorValues(next);
|
|
76
|
+
const panelKey = removeWhiteSpacesAndSpecialCharacters(next.name);
|
|
77
|
+
set((state)=>{
|
|
78
|
+
state.panels[panelKey] = panelDef;
|
|
79
|
+
});
|
|
80
|
+
get().addPanelToGroup(panelKey, next.groupIndex);
|
|
81
|
+
},
|
|
82
|
+
close: ()=>{
|
|
83
|
+
set((state)=>{
|
|
84
|
+
state.panelEditor = undefined;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
// Open the editor with the new state
|
|
89
|
+
set((state)=>{
|
|
90
|
+
state.panelEditor = editorState;
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
deletePanels (items) {
|
|
94
|
+
const { mapPanelToPanelGroups , deletePanelInPanelGroup , getPanelKey } = get();
|
|
95
|
+
const map = mapPanelToPanelGroups();
|
|
96
|
+
// get panel key first before deleting panel from panel group since getPanelKey relies on index
|
|
97
|
+
const panels = items.map((panel)=>{
|
|
98
|
+
return {
|
|
99
|
+
...panel,
|
|
100
|
+
panelKey: getPanelKey(panel)
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
panels.forEach(({ panelKey , ...panel })=>{
|
|
104
|
+
var ref;
|
|
105
|
+
deletePanelInPanelGroup(panel);
|
|
106
|
+
// make sure panel is only referenced in one panel group before deleting it from state.panels
|
|
107
|
+
if (map[panelKey] && ((ref = map[panelKey]) === null || ref === void 0 ? void 0 : ref.length) === 1) {
|
|
108
|
+
set((state)=>{
|
|
109
|
+
delete state.panels[panelKey];
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
openDeletePanelDialog (item) {
|
|
115
|
+
set((state)=>{
|
|
116
|
+
var ref;
|
|
117
|
+
const { panels , getPanelKey } = get();
|
|
118
|
+
const panelKey = getPanelKey(item);
|
|
119
|
+
var _name;
|
|
120
|
+
state.deletePanelDialog = {
|
|
121
|
+
panelKey,
|
|
122
|
+
panelName: (_name = (ref = panels[panelKey]) === null || ref === void 0 ? void 0 : ref.spec.display.name) !== null && _name !== void 0 ? _name : '',
|
|
123
|
+
layoutItem: item
|
|
124
|
+
};
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
closeDeletePanelDialog () {
|
|
128
|
+
set((state)=>{
|
|
129
|
+
state.deletePanelDialog = undefined;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
// Helper to create PanelDefinitions when saving
|
|
136
|
+
function createPanelDefinitionFromEditorValues(editorValues) {
|
|
137
|
+
return {
|
|
138
|
+
kind: 'Panel',
|
|
139
|
+
spec: {
|
|
140
|
+
display: {
|
|
141
|
+
name: editorValues.name,
|
|
142
|
+
description: editorValues.description !== '' ? editorValues.description : undefined
|
|
143
|
+
},
|
|
144
|
+
plugin: {
|
|
145
|
+
kind: editorValues.kind,
|
|
146
|
+
spec: editorValues.spec
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
//# sourceMappingURL=panel-editing-slice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/context/DashboardProvider/panel-editing-slice.tsx"],"sourcesContent":["// Copyright 2022 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { PanelDefinition, UnknownSpec } from '@perses-dev/core';\nimport { StateCreator } from 'zustand';\nimport { removeWhiteSpacesAndSpecialCharacters } from '../../utils/functions';\nimport { Middleware } from './common';\nimport { LayoutSlice, LayoutItem } from './layout-slice';\n\nexport interface PanelEditorSlice {\n panels: Record<string, PanelDefinition>;\n deletePanelDialog?: DeletePanelDialog;\n /**\n * State for the panel editor when its open, otherwise undefined when it's closed.\n */\n panelEditor?: PanelEditorState;\n\n /**\n * Edit an existing panel by providing its layout coordinates.\n */\n editPanel: (item: LayoutItem) => void;\n\n /**\n * Add a new Panel to a panel group.\n */\n addPanel: (initialGroup: number) => void;\n\n /**\n * Delete panels\n */\n deletePanels: (panels: LayoutItem[]) => void;\n\n /**\n * Open delete panel dialog\n */\n openDeletePanelDialog: (item: LayoutItem) => void;\n\n /**\n * Close delete panel dialog\n */\n closeDeletePanelDialog: () => void;\n}\n\nexport interface DeletePanelDialog {\n panelKey: string;\n panelName: string;\n layoutItem: LayoutItem;\n}\n\nexport interface PanelEditorState {\n /**\n * Whether we're adding a new panel, or editing an existing panel.\n */\n mode: 'Add' | 'Edit';\n\n /**\n * Initial values for the things that can be edited about a panel.\n */\n initialValues: PanelEditorValues;\n\n /**\n * Applies changes, but doesn't close the editor.\n */\n applyChanges: (next: PanelEditorValues) => void;\n\n /**\n * Close the editor.\n */\n close: () => void;\n}\n\n/**\n * Panel values that can be edited in the panel editor.\n */\nexport interface PanelEditorValues {\n name: string;\n description: string;\n groupIndex: number;\n kind: string;\n spec: UnknownSpec;\n}\n\n/**\n * Curried function for creating the PanelEditorSlice.\n */\nexport function createPanelEditorSlice(\n panels: PanelEditorSlice['panels']\n): StateCreator<PanelEditorSlice & LayoutSlice, Middleware, [], PanelEditorSlice> {\n // Return the state creator function for Zustand that uses the panels provided as intitial state\n return (set, get) => ({\n panels,\n\n panelEditor: undefined,\n\n editPanel(item) {\n const { panels, getPanelKey } = get();\n\n // Ask the layout store for the panel key at that location\n const panelKey = getPanelKey(item);\n\n // Find the panel to edit\n const panelToEdit = panels[panelKey];\n if (panelToEdit === undefined) {\n throw new Error(`Cannot find Panel with key '${panelKey}'`);\n }\n\n const editorState: PanelEditorState = {\n mode: 'Edit',\n initialValues: {\n name: panelToEdit.spec.display.name,\n description: panelToEdit.spec.display.description ?? '',\n groupIndex: item.groupIndex,\n kind: panelToEdit.spec.plugin.kind,\n spec: panelToEdit.spec.plugin.spec,\n },\n applyChanges: (next) => {\n const panelDefinititon = createPanelDefinitionFromEditorValues(next);\n set((state) => {\n state.panels[panelKey] = panelDefinititon;\n });\n\n // Move the panel to another group if it changed\n if (next.groupIndex !== item.groupIndex) {\n get().movePanelToGroup(item, next.groupIndex);\n }\n },\n close: () => {\n set((state) => {\n state.panelEditor = undefined;\n });\n },\n };\n\n // Open the editor with the new state\n set((state) => {\n state.panelEditor = editorState;\n });\n },\n\n addPanel(initialGroup) {\n const editorState: PanelEditorState = {\n mode: 'Add',\n initialValues: {\n name: '',\n description: '',\n groupIndex: initialGroup,\n // TODO: If we knew what plugins were available (and how to create the initial spec), we might be able to\n // set a smarter default here?\n kind: '',\n spec: {},\n },\n applyChanges: (next) => {\n const panelDef = createPanelDefinitionFromEditorValues(next);\n const panelKey = removeWhiteSpacesAndSpecialCharacters(next.name);\n set((state) => {\n state.panels[panelKey] = panelDef;\n });\n get().addPanelToGroup(panelKey, next.groupIndex);\n },\n close: () => {\n set((state) => {\n state.panelEditor = undefined;\n });\n },\n };\n\n // Open the editor with the new state\n set((state) => {\n state.panelEditor = editorState;\n });\n },\n\n deletePanels(items: LayoutItem[]) {\n const { mapPanelToPanelGroups, deletePanelInPanelGroup, getPanelKey } = get();\n const map = mapPanelToPanelGroups();\n // get panel key first before deleting panel from panel group since getPanelKey relies on index\n const panels = items.map((panel) => {\n return { ...panel, panelKey: getPanelKey(panel) };\n });\n panels.forEach(({ panelKey, ...panel }) => {\n deletePanelInPanelGroup(panel);\n // make sure panel is only referenced in one panel group before deleting it from state.panels\n if (map[panelKey] && map[panelKey]?.length === 1) {\n set((state) => {\n delete state.panels[panelKey];\n });\n }\n });\n },\n\n openDeletePanelDialog(item: LayoutItem) {\n set((state) => {\n const { panels, getPanelKey } = get();\n const panelKey = getPanelKey(item);\n state.deletePanelDialog = {\n panelKey,\n panelName: panels[panelKey]?.spec.display.name ?? '',\n layoutItem: item,\n };\n });\n },\n\n closeDeletePanelDialog() {\n set((state) => {\n state.deletePanelDialog = undefined;\n });\n },\n });\n}\n\n// Helper to create PanelDefinitions when saving\nfunction createPanelDefinitionFromEditorValues(editorValues: PanelEditorValues): PanelDefinition {\n return {\n kind: 'Panel',\n spec: {\n display: {\n name: editorValues.name,\n description: editorValues.description !== '' ? editorValues.description : undefined,\n },\n plugin: {\n kind: editorValues.kind,\n spec: editorValues.spec,\n },\n },\n };\n}\n"],"names":["removeWhiteSpacesAndSpecialCharacters","createPanelEditorSlice","panels","set","get","panelEditor","undefined","editPanel","item","getPanelKey","panelKey","panelToEdit","Error","editorState","mode","initialValues","name","spec","display","description","groupIndex","kind","plugin","applyChanges","next","panelDefinititon","createPanelDefinitionFromEditorValues","state","movePanelToGroup","close","addPanel","initialGroup","panelDef","addPanelToGroup","deletePanels","items","mapPanelToPanelGroups","deletePanelInPanelGroup","map","panel","forEach","length","openDeletePanelDialog","deletePanelDialog","panelName","layoutItem","closeDeletePanelDialog","editorValues"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAIjC,SAASA,qCAAqC,QAAQ,uBAAuB,CAAC;AA6E9E;;CAEC,GACD,OAAO,SAASC,sBAAsB,CACpCC,MAAkC,EAC8C;IAChF,gGAAgG;IAChG,OAAO,CAACC,GAAG,EAAEC,GAAG;QAAM,OAAA;YACpBF,MAAM;YAENG,WAAW,EAAEC,SAAS;YAEtBC,SAAS,EAACC,IAAI,EAAE;gBACd,MAAM,EAAEN,MAAM,CAAA,EAAEO,WAAW,CAAA,EAAE,GAAGL,GAAG,EAAE,AAAC;gBAEtC,0DAA0D;gBAC1D,MAAMM,QAAQ,GAAGD,WAAW,CAACD,IAAI,CAAC,AAAC;gBAEnC,yBAAyB;gBACzB,MAAMG,WAAW,GAAGT,MAAM,CAACQ,QAAQ,CAAC,AAAC;gBACrC,IAAIC,WAAW,KAA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AgB,CAAEe,IAAI,CAACC,OAAO,CAACF,IAAI,cAAnCd,KAAmC,cAAnCA,KAAmC,GAAI,EAAE;wBACpD2C,UAAU,EAAErC,IAAI;qBACjB,CAAC;gBACJ,CAAC,CAAC,CAAC;YACL,CAAC;YAEDsC,sBAAsB,IAAG;gBACvB3C,GAAG,CAAC,CAACwB,KAAK,GAAK;oBACbA,KAAK,CAACgB,iBAAiB,GAAGrC,SAAS,CAAC;gBACtC,CAAC,CAAC,CAAC;YACL,CAAC;SACF,CAAA;KAAC,CAAC;AACL,CAAC;AAED,gDAAgD;AAChD,SAASoB,qCAAqC,CAACqB,YAA+B,EAAmB;IAC/F,OAAO;QACL1B,IAAI,EAAE,OAAO;QACbJ,IAAI,EAAE;YACJC,OAAO,EAAE;gBACPF,IAAI,EAAE+B,YAAY,CAAC/B,IAAI;gBACvBG,WAAW,EAAE4B,YAAY,CAAC5B,WAAW,KAAK,EAAE,GAAG4B,YAAY,CAAC5B,WAAW,GAAGb,SAAS;aACpF;YACDgB,MAAM,EAAE;gBACND,IAAI,EAAE0B,YAAY,CAAC1B,IAAI;gBACvBJ,IAAI,EAAE8B,YAAY,CAAC9B,IAAI;aACxB;SACF;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { StateCreator } from 'zustand';
|
|
2
|
+
import { Middleware } from './common';
|
|
3
|
+
export interface PanelGroupDialog {
|
|
4
|
+
groupIndex?: number;
|
|
5
|
+
}
|
|
6
|
+
export interface PanelGroupSlice {
|
|
7
|
+
panelGroupDialog?: PanelGroupDialog;
|
|
8
|
+
openPanelGroupDialog: (groupIndex?: number) => void;
|
|
9
|
+
closePanelGroupDialog: () => void;
|
|
10
|
+
deletePanelGroupDialog?: PanelGroupDialog;
|
|
11
|
+
openDeletePanelGroupDialog: (groupIndex: number) => void;
|
|
12
|
+
closeDeletePanelGroupDialog: () => void;
|
|
13
|
+
}
|
|
14
|
+
export declare const createPanelGroupSlice: StateCreator<PanelGroupSlice, Middleware, [], PanelGroupSlice>;
|
|
15
|
+
//# sourceMappingURL=panel-group-slice.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"panel-group-slice.d.ts","sourceRoot":"","sources":["../../../src/context/DashboardProvider/panel-group-slice.tsx"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAEtC,MAAM,WAAW,gBAAgB;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,eAAe;IAC9B,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IACpC,oBAAoB,EAAE,CAAC,UAAU,CAAC,EAAE,MAAM,KAAK,IAAI,CAAC;IACpD,qBAAqB,EAAE,MAAM,IAAI,CAAC;IAClC,sBAAsB,CAAC,EAAE,gBAAgB,CAAC;IAC1C,0BAA0B,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,IAAI,CAAC;IACzD,2BAA2B,EAAE,MAAM,IAAI,CAAC;CACzC;AAED,eAAO,MAAM,qBAAqB,EAAE,YAAY,CAAC,eAAe,EAAE,UAAU,EAAE,EAAE,EAAE,eAAe,CAiB/F,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Copyright 2022 The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
export const createPanelGroupSlice = (set)=>({
|
|
14
|
+
openPanelGroupDialog: (groupIndex)=>set((state)=>{
|
|
15
|
+
state.panelGroupDialog = {
|
|
16
|
+
groupIndex
|
|
17
|
+
};
|
|
18
|
+
}),
|
|
19
|
+
closePanelGroupDialog: ()=>set((state)=>{
|
|
20
|
+
state.panelGroupDialog = undefined;
|
|
21
|
+
}),
|
|
22
|
+
openDeletePanelGroupDialog: (groupIndex)=>set((state)=>{
|
|
23
|
+
state.deletePanelGroupDialog = {
|
|
24
|
+
groupIndex
|
|
25
|
+
};
|
|
26
|
+
}),
|
|
27
|
+
closeDeletePanelGroupDialog: ()=>set((state)=>{
|
|
28
|
+
state.deletePanelGroupDialog = undefined;
|
|
29
|
+
})
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=panel-group-slice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/context/DashboardProvider/panel-group-slice.tsx"],"sourcesContent":["// Copyright 2022 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { StateCreator } from 'zustand';\nimport { Middleware } from './common';\n\nexport interface PanelGroupDialog {\n groupIndex?: number;\n}\n\nexport interface PanelGroupSlice {\n panelGroupDialog?: PanelGroupDialog;\n openPanelGroupDialog: (groupIndex?: number) => void;\n closePanelGroupDialog: () => void;\n deletePanelGroupDialog?: PanelGroupDialog;\n openDeletePanelGroupDialog: (groupIndex: number) => void;\n closeDeletePanelGroupDialog: () => void;\n}\n\nexport const createPanelGroupSlice: StateCreator<PanelGroupSlice, Middleware, [], PanelGroupSlice> = (set) => ({\n openPanelGroupDialog: (groupIndex?: number) =>\n set((state) => {\n state.panelGroupDialog = { groupIndex };\n }),\n closePanelGroupDialog: () =>\n set((state) => {\n state.panelGroupDialog = undefined;\n }),\n openDeletePanelGroupDialog: (groupIndex: number) =>\n set((state) => {\n state.deletePanelGroupDialog = { groupIndex };\n }),\n closeDeletePanelGroupDialog: () =>\n set((state) => {\n state.deletePanelGroupDialog = undefined;\n }),\n});\n"],"names":["createPanelGroupSlice","set","openPanelGroupDialog","groupIndex","state","panelGroupDialog","closePanelGroupDialog","undefined","openDeletePanelGroupDialog","deletePanelGroupDialog","closeDeletePanelGroupDialog"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAkBjC,OAAO,MAAMA,qBAAqB,GAAmE,CAACC,GAAG,GAAM,CAAA;QAC7GC,oBAAoB,EAAE,CAACC,UAAmB,GACxCF,GAAG,CAAC,CAACG,KAAK,GAAK;gBACbA,KAAK,CAACC,gBAAgB,GAAG;oBAAEF,UAAU;iBAAE,CAAC;YAC1C,CAAC,CAAC;QACJG,qBAAqB,EAAE,IACrBL,GAAG,CAAC,CAACG,KAAK,GAAK;gBACbA,KAAK,CAACC,gBAAgB,GAAGE,SAAS,CAAC;YACrC,CAAC,CAAC;QACJC,0BAA0B,EAAE,CAACL,UAAkB,GAC7CF,GAAG,CAAC,CAACG,KAAK,GAAK;gBACbA,KAAK,CAACK,sBAAsB,GAAG;oBAAEN,UAAU;iBAAE,CAAC;YAChD,CAAC,CAAC;QACJO,2BAA2B,EAAE,IAC3BT,GAAG,CAAC,CAACG,KAAK,GAAK;gBACbA,KAAK,CAACK,sBAAsB,GAAGF,SAAS,CAAC;YAC3C,CAAC,CAAC;KACL,CAAA,AAAC,CAAC"}
|
|
@@ -6,8 +6,16 @@ export interface DatasourceStoreProviderProps {
|
|
|
6
6
|
children?: React.ReactNode;
|
|
7
7
|
}
|
|
8
8
|
export interface DatasourceApi {
|
|
9
|
-
getDatasource: (project: string, selector: DatasourceSelector) => Promise<
|
|
10
|
-
|
|
9
|
+
getDatasource: (project: string, selector: DatasourceSelector) => Promise<{
|
|
10
|
+
resource: Datasource;
|
|
11
|
+
proxyUrl: string;
|
|
12
|
+
} | undefined>;
|
|
13
|
+
getGlobalDatasource: (selector: DatasourceSelector) => Promise<{
|
|
14
|
+
resource: GlobalDatasource;
|
|
15
|
+
proxyUrl: string;
|
|
16
|
+
} | undefined>;
|
|
17
|
+
listDatasources: (project: string, pluginKind?: string) => Promise<Datasource[]>;
|
|
18
|
+
listGlobalDatasources: (pluginKind?: string) => Promise<GlobalDatasource[]>;
|
|
11
19
|
}
|
|
12
20
|
/**
|
|
13
21
|
* A `DatasourceContext` provider that uses an external API to resolve datasource selectors.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DatasourceStoreProvider.d.ts","sourceRoot":"","sources":["../../src/context/DatasourceStoreProvider.tsx"],"names":[],"mappings":";AAcA,OAAO,EACL,iBAAiB,EAEjB,UAAU,EACV,kBAAkB,EAElB,gBAAgB,EAEjB,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"DatasourceStoreProvider.d.ts","sourceRoot":"","sources":["../../src/context/DatasourceStoreProvider.tsx"],"names":[],"mappings":";AAcA,OAAO,EACL,iBAAiB,EAEjB,UAAU,EACV,kBAAkB,EAElB,gBAAgB,EAEjB,MAAM,kBAAkB,CAAC;AAQ1B,MAAM,WAAW,4BAA4B;IAC3C,iBAAiB,EAAE,iBAAiB,CAAC;IACrC,aAAa,EAAE,aAAa,CAAC;IAC7B,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CAC5B;AAGD,MAAM,WAAW,aAAa;IAC5B,aAAa,EAAE,CACb,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,kBAAkB,KACzB,OAAO,CAAC;QAAE,QAAQ,EAAE,UAAU,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC,CAAC;IAErE,mBAAmB,EAAE,CACnB,QAAQ,EAAE,kBAAkB,KACzB,OAAO,CAAC;QAAE,QAAQ,EAAE,gBAAgB,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC,CAAC;IAE3E,eAAe,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;IAEjF,qBAAqB,EAAE,CAAC,UAAU,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,gBAAgB,EAAE,CAAC,CAAC;CAC7E;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,4BAA4B,eAkG1E"}
|
|
@@ -11,37 +11,105 @@
|
|
|
11
11
|
// See the License for the specific language governing permissions and
|
|
12
12
|
// limitations under the License.
|
|
13
13
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
14
|
-
import { useMemo } from 'react';
|
|
14
|
+
import { useCallback, useMemo } from 'react';
|
|
15
15
|
import { useEvent } from '@perses-dev/core';
|
|
16
|
-
import { DatasourceStoreContext } from '@perses-dev/plugin-system';
|
|
16
|
+
import { DatasourceStoreContext, usePluginRegistry } from '@perses-dev/plugin-system';
|
|
17
17
|
/**
|
|
18
18
|
* A `DatasourceContext` provider that uses an external API to resolve datasource selectors.
|
|
19
19
|
*/ export function DatasourceStoreProvider(props) {
|
|
20
20
|
const { dashboardResource , datasourceApi , children } = props;
|
|
21
|
-
const
|
|
21
|
+
const { project } = dashboardResource.metadata;
|
|
22
|
+
const { getPlugin , listPluginMetadata } = usePluginRegistry();
|
|
23
|
+
const findDatasource = useEvent(async (selector)=>{
|
|
22
24
|
// Try to find it in dashboard spec
|
|
23
25
|
const { datasources } = dashboardResource.spec;
|
|
24
26
|
const dashboardDatasource = findDashboardDatasource(datasources, selector);
|
|
25
27
|
if (dashboardDatasource !== undefined) {
|
|
26
|
-
return
|
|
28
|
+
return {
|
|
29
|
+
spec: dashboardDatasource,
|
|
30
|
+
proxyUrl: undefined
|
|
31
|
+
};
|
|
27
32
|
}
|
|
28
33
|
// Try to find it at the project level as a Datasource resource
|
|
29
|
-
const { project } = dashboardResource.metadata;
|
|
30
34
|
const datasource = await datasourceApi.getDatasource(project, selector);
|
|
31
35
|
if (datasource !== undefined) {
|
|
32
|
-
return
|
|
36
|
+
return {
|
|
37
|
+
spec: datasource.resource.spec,
|
|
38
|
+
proxyUrl: datasource.proxyUrl
|
|
39
|
+
};
|
|
33
40
|
}
|
|
34
41
|
// Try to find it at the global level as a GlobalDatasource resource
|
|
35
42
|
const globalDatasource = await datasourceApi.getGlobalDatasource(selector);
|
|
36
43
|
if (globalDatasource !== undefined) {
|
|
37
|
-
return
|
|
44
|
+
return {
|
|
45
|
+
spec: globalDatasource.resource.spec,
|
|
46
|
+
proxyUrl: globalDatasource.proxyUrl
|
|
47
|
+
};
|
|
38
48
|
}
|
|
39
49
|
throw new Error(`No datasource found for kind '${selector.kind}' and name '${selector.name}'`);
|
|
40
50
|
});
|
|
51
|
+
// Gets a datasource spec for a given selector
|
|
52
|
+
const getDatasource = useCallback(async (selector)=>{
|
|
53
|
+
const { spec } = await findDatasource(selector);
|
|
54
|
+
return spec;
|
|
55
|
+
}, [
|
|
56
|
+
findDatasource
|
|
57
|
+
]);
|
|
58
|
+
// Given a Datasource selector, finds the spec for it and then uses its corresponding plugin the create a client
|
|
59
|
+
const getDatasourceClient = useCallback(async function getClient(selector) {
|
|
60
|
+
const { kind } = selector;
|
|
61
|
+
const [{ spec , proxyUrl }, plugin] = await Promise.all([
|
|
62
|
+
findDatasource(selector),
|
|
63
|
+
getPlugin('Datasource', kind)
|
|
64
|
+
]);
|
|
65
|
+
return plugin.createClient(spec.plugin.spec, {
|
|
66
|
+
proxyUrl
|
|
67
|
+
});
|
|
68
|
+
}, [
|
|
69
|
+
findDatasource,
|
|
70
|
+
getPlugin
|
|
71
|
+
]);
|
|
72
|
+
const listDatasourceMetadata = useEvent(async (datasourcePluginKind)=>{
|
|
73
|
+
const [pluginMetadata, datasources, globalDatasources] = await Promise.all([
|
|
74
|
+
listPluginMetadata('Datasource'),
|
|
75
|
+
datasourceApi.listDatasources(project, datasourcePluginKind),
|
|
76
|
+
datasourceApi.listGlobalDatasources(datasourcePluginKind)
|
|
77
|
+
]);
|
|
78
|
+
// Find the metadata for the plugin type they asked for so we can use it for the name of the default datasource
|
|
79
|
+
const datasourcePluginMetadata = pluginMetadata.find((metadata)=>metadata.kind === datasourcePluginKind);
|
|
80
|
+
if (datasourcePluginMetadata === undefined) {
|
|
81
|
+
throw new Error(`Could not find a Datasource plugin with kind '${datasourcePluginKind}'`);
|
|
82
|
+
}
|
|
83
|
+
// Get helper for de-duping results properly
|
|
84
|
+
const { results , addResult } = buildListDatasourceMetadataResults(datasourcePluginMetadata.display.name);
|
|
85
|
+
// Start with dashboard datasources that have highest precedence
|
|
86
|
+
if (dashboardResource.spec.datasources !== undefined) {
|
|
87
|
+
for(const selectorName in dashboardResource.spec.datasources){
|
|
88
|
+
const spec = dashboardResource.spec.datasources[selectorName];
|
|
89
|
+
if (spec === undefined || spec.plugin.kind !== datasourcePluginKind) continue;
|
|
90
|
+
addResult(spec, selectorName);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// Now look at project-level datasources
|
|
94
|
+
for (const datasource of datasources){
|
|
95
|
+
const selectorName1 = datasource.metadata.name;
|
|
96
|
+
addResult(datasource.spec, selectorName1);
|
|
97
|
+
}
|
|
98
|
+
// And finally global datasources
|
|
99
|
+
for (const globalDatasource of globalDatasources){
|
|
100
|
+
const selectorName2 = globalDatasource.metadata.name;
|
|
101
|
+
addResult(globalDatasource.spec, selectorName2);
|
|
102
|
+
}
|
|
103
|
+
return results;
|
|
104
|
+
});
|
|
41
105
|
const ctxValue = useMemo(()=>({
|
|
42
|
-
getDatasource
|
|
106
|
+
getDatasource,
|
|
107
|
+
getDatasourceClient,
|
|
108
|
+
listDatasourceMetadata
|
|
43
109
|
}), [
|
|
44
|
-
getDatasource
|
|
110
|
+
getDatasource,
|
|
111
|
+
getDatasourceClient,
|
|
112
|
+
listDatasourceMetadata
|
|
45
113
|
]);
|
|
46
114
|
return /*#__PURE__*/ _jsx(DatasourceStoreContext.Provider, {
|
|
47
115
|
value: ctxValue,
|
|
@@ -60,5 +128,39 @@ function findDashboardDatasource(dashboardDatasources, selector) {
|
|
|
60
128
|
// If only using a kind, try to find one with that kind that is the default
|
|
61
129
|
return Object.values(dashboardDatasources).find((ds)=>ds.plugin.kind === selector.kind && ds.default === true);
|
|
62
130
|
}
|
|
131
|
+
// Helper for building a list of DatasourceMetadata results that will take care of de-duping already used selectors
|
|
132
|
+
function buildListDatasourceMetadataResults(pluginDisplayName) {
|
|
133
|
+
const results = [];
|
|
134
|
+
const usedNames = new Set();
|
|
135
|
+
let defaultAdded = false;
|
|
136
|
+
const addResult = (spec, selectorName)=>{
|
|
137
|
+
var ref;
|
|
138
|
+
// If we haven't added a default yet and this is a default, add default option to the beginning of the results
|
|
139
|
+
if (spec.default && defaultAdded === false) {
|
|
140
|
+
results.unshift({
|
|
141
|
+
name: `Default ${pluginDisplayName}`,
|
|
142
|
+
selector: {
|
|
143
|
+
kind: spec.plugin.kind
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
defaultAdded = true;
|
|
147
|
+
}
|
|
148
|
+
// If we already have a datasource with this selector name, ignore it, otherwise add to end of list
|
|
149
|
+
if (usedNames.has(selectorName)) return;
|
|
150
|
+
var ref1;
|
|
151
|
+
results.push({
|
|
152
|
+
name: (ref1 = (ref = spec.display) === null || ref === void 0 ? void 0 : ref.name) !== null && ref1 !== void 0 ? ref1 : selectorName,
|
|
153
|
+
selector: {
|
|
154
|
+
kind: spec.plugin.kind,
|
|
155
|
+
name: selectorName
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
usedNames.add(selectorName);
|
|
159
|
+
};
|
|
160
|
+
return {
|
|
161
|
+
results,
|
|
162
|
+
addResult
|
|
163
|
+
};
|
|
164
|
+
}
|
|
63
165
|
|
|
64
166
|
//# sourceMappingURL=DatasourceStoreProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/context/DatasourceStoreProvider.tsx"],"sourcesContent":["// Copyright 2022 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { useMemo } from 'react';\nimport {\n DashboardResource,\n DashboardSpec,\n Datasource,\n DatasourceSelector,\n DatasourceSpec,\n GlobalDatasource,\n useEvent,\n} from '@perses-dev/core';\nimport { DatasourceStoreContext, DatasourceStore } from '@perses-dev/plugin-system';\n\nexport interface DatasourceStoreProviderProps {\n dashboardResource: DashboardResource;\n datasourceApi: DatasourceApi;\n children?: React.ReactNode;\n}\n\n// The external API for fetching datasource resources\nexport interface DatasourceApi {\n getDatasource: (project: string, selector: DatasourceSelector) => Promise<Datasource | undefined>;\n getGlobalDatasource: (selector: DatasourceSelector) => Promise<GlobalDatasource | undefined>;\n}\n\n/**\n * A `DatasourceContext` provider that uses an external API to resolve datasource selectors.\n */\nexport function DatasourceStoreProvider(props: DatasourceStoreProviderProps) {\n const { dashboardResource, datasourceApi, children } = props;\n\n const getDatasource = useEvent(async (selector: DatasourceSelector): Promise<DatasourceSpec> => {\n // Try to find it in dashboard spec\n const { datasources } = dashboardResource.spec;\n const dashboardDatasource = findDashboardDatasource(datasources, selector);\n if (dashboardDatasource !== undefined) {\n return dashboardDatasource;\n }\n\n // Try to find it at the project level as a Datasource resource\n const { project } = dashboardResource.metadata;\n const datasource = await datasourceApi.getDatasource(project, selector);\n if (datasource !== undefined) {\n return datasource.spec;\n }\n\n // Try to find it at the global level as a GlobalDatasource resource\n const globalDatasource = await datasourceApi.getGlobalDatasource(selector);\n if (globalDatasource !== undefined) {\n return globalDatasource.spec;\n }\n\n throw new Error(`No datasource found for kind '${selector.kind}' and name '${selector.name}'`);\n });\n\n const ctxValue: DatasourceStore = useMemo(\n () => ({\n getDatasource,\n }),\n [getDatasource]\n );\n\n return <DatasourceStoreContext.Provider value={ctxValue}>{children}</DatasourceStoreContext.Provider>;\n}\n\n// Helper to find a datasource in the list embedded in a dashboard spec\nfunction findDashboardDatasource(dashboardDatasources: DashboardSpec['datasources'], selector: DatasourceSelector) {\n if (dashboardDatasources === undefined) return undefined;\n\n // If using a name in the selector...\n if (selector.name !== undefined) {\n const named = dashboardDatasources[selector.name];\n if (named === undefined) return undefined;\n return named.plugin.kind === selector.kind ? named : undefined;\n }\n\n // If only using a kind, try to find one with that kind that is the default\n return Object.values(dashboardDatasources).find((ds) => ds.plugin.kind === selector.kind && ds.default === true);\n}\n"],"names":["useMemo","useEvent","DatasourceStoreContext","DatasourceStoreProvider","props","dashboardResource","datasourceApi","children","getDatasource","selector","datasources","spec","dashboardDatasource","findDashboardDatasource","undefined","project","metadata","datasource","globalDatasource","getGlobalDatasource","Error","kind","name","ctxValue","Provider","value","dashboardDatasources","named","plugin","Object","values","find","ds","default"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;AAAA,SAASA,OAAO,QAAQ,OAAO,CAAC;AAChC,SAOEC,QAAQ,QACH,kBAAkB,CAAC;AAC1B,SAASC,sBAAsB,QAAyB,2BAA2B,CAAC;AAcp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,CAACsB,QAAQ;QAACC,KAAK,EAAEF,QAAQ;kBAAGhB,QAAQ;MAAmC,CAAC;AACxG,CAAC;AAED,uEAAuE;AACvE,SAASM,uBAAuB,CAACa,oBAAkD,EAAEjB,QAA4B,EAAE;IACjH,IAAIiB,oBAAoB,KAAKZ,SAAS,EAAE,OAAOA,SAAS,CAAC;IAEzD,qCAAqC;IACrC,IAAIL,QAAQ,CAACa,IAAI,KAAKR,SAAS,EAAE;QAC/B,MAAMa,KAAK,GAAGD,oBAAoB,CAACjB,QAAQ,CAACa,IAAI,CAAC,AAAC;QAClD,IAAIK,KAAK,KAAKb,SAAS,EAAE,OAAOA,SAAS,CAAC;QAC1C,OAAOa,KAAK,CAACC,MAAM,CAACP,IAAI,KAAKZ,QAAQ,CAACY,IAAI,GAAGM,KAAK,GAAGb,SAAS,CAAC;IACjE,CAAC;IAED,2EAA2E;IAC3E,OAAOe,MAAM,CAACC,MAAM,CAACJ,oBAAoB,CAAC,CAACK,IAAI,CAAC,CAACC,EAAE,GAAKA,EAAE,CAACJ,MAAM,CAACP,IAAI,KAAKZ,QAAQ,CAACY,IAAI,IAAIW,EAAE,CAACC,OAAO,KAAK,IAAI,CAAC,CAAC;AACnH,CAAC"}
|
|
1
|
+
{"version":3,"sources":["../../src/context/DatasourceStoreProvider.tsx"],"sourcesContent":["// Copyright 2022 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { useCallback, useMemo } from 'react';\nimport {\n DashboardResource,\n DashboardSpec,\n Datasource,\n DatasourceSelector,\n DatasourceSpec,\n GlobalDatasource,\n useEvent,\n} from '@perses-dev/core';\nimport {\n DatasourceStoreContext,\n DatasourceStore,\n usePluginRegistry,\n DatasourceMetadata,\n} from '@perses-dev/plugin-system';\n\nexport interface DatasourceStoreProviderProps {\n dashboardResource: DashboardResource;\n datasourceApi: DatasourceApi;\n children?: React.ReactNode;\n}\n\n// The external API for fetching datasource resources\nexport interface DatasourceApi {\n getDatasource: (\n project: string,\n selector: DatasourceSelector\n ) => Promise<{ resource: Datasource; proxyUrl: string } | undefined>;\n\n getGlobalDatasource: (\n selector: DatasourceSelector\n ) => Promise<{ resource: GlobalDatasource; proxyUrl: string } | undefined>;\n\n listDatasources: (project: string, pluginKind?: string) => Promise<Datasource[]>;\n\n listGlobalDatasources: (pluginKind?: string) => Promise<GlobalDatasource[]>;\n}\n\n/**\n * A `DatasourceContext` provider that uses an external API to resolve datasource selectors.\n */\nexport function DatasourceStoreProvider(props: DatasourceStoreProviderProps) {\n const { dashboardResource, datasourceApi, children } = props;\n const { project } = dashboardResource.metadata;\n\n const { getPlugin, listPluginMetadata } = usePluginRegistry();\n\n const findDatasource = useEvent(async (selector: DatasourceSelector) => {\n // Try to find it in dashboard spec\n const { datasources } = dashboardResource.spec;\n const dashboardDatasource = findDashboardDatasource(datasources, selector);\n if (dashboardDatasource !== undefined) {\n return { spec: dashboardDatasource, proxyUrl: undefined };\n }\n\n // Try to find it at the project level as a Datasource resource\n const datasource = await datasourceApi.getDatasource(project, selector);\n if (datasource !== undefined) {\n return { spec: datasource.resource.spec, proxyUrl: datasource.proxyUrl };\n }\n\n // Try to find it at the global level as a GlobalDatasource resource\n const globalDatasource = await datasourceApi.getGlobalDatasource(selector);\n if (globalDatasource !== undefined) {\n return { spec: globalDatasource.resource.spec, proxyUrl: globalDatasource.proxyUrl };\n }\n\n throw new Error(`No datasource found for kind '${selector.kind}' and name '${selector.name}'`);\n });\n\n // Gets a datasource spec for a given selector\n const getDatasource = useCallback(\n async (selector: DatasourceSelector): Promise<DatasourceSpec> => {\n const { spec } = await findDatasource(selector);\n return spec;\n },\n [findDatasource]\n );\n\n // Given a Datasource selector, finds the spec for it and then uses its corresponding plugin the create a client\n const getDatasourceClient = useCallback(\n async function getClient<Client>(selector: DatasourceSelector): Promise<Client> {\n const { kind } = selector;\n const [{ spec, proxyUrl }, plugin] = await Promise.all([findDatasource(selector), getPlugin('Datasource', kind)]);\n return plugin.createClient(spec.plugin.spec, { proxyUrl }) as Client;\n },\n [findDatasource, getPlugin]\n );\n\n const listDatasourceMetadata = useEvent(async (datasourcePluginKind: string): Promise<DatasourceMetadata[]> => {\n const [pluginMetadata, datasources, globalDatasources] = await Promise.all([\n listPluginMetadata('Datasource'),\n datasourceApi.listDatasources(project, datasourcePluginKind),\n datasourceApi.listGlobalDatasources(datasourcePluginKind),\n ]);\n\n // Find the metadata for the plugin type they asked for so we can use it for the name of the default datasource\n const datasourcePluginMetadata = pluginMetadata.find((metadata) => metadata.kind === datasourcePluginKind);\n if (datasourcePluginMetadata === undefined) {\n throw new Error(`Could not find a Datasource plugin with kind '${datasourcePluginKind}'`);\n }\n\n // Get helper for de-duping results properly\n const { results, addResult } = buildListDatasourceMetadataResults(datasourcePluginMetadata.display.name);\n\n // Start with dashboard datasources that have highest precedence\n if (dashboardResource.spec.datasources !== undefined) {\n for (const selectorName in dashboardResource.spec.datasources) {\n const spec = dashboardResource.spec.datasources[selectorName];\n if (spec === undefined || spec.plugin.kind !== datasourcePluginKind) continue;\n addResult(spec, selectorName);\n }\n }\n\n // Now look at project-level datasources\n for (const datasource of datasources) {\n const selectorName = datasource.metadata.name;\n addResult(datasource.spec, selectorName);\n }\n\n // And finally global datasources\n for (const globalDatasource of globalDatasources) {\n const selectorName = globalDatasource.metadata.name;\n addResult(globalDatasource.spec, selectorName);\n }\n\n return results;\n });\n\n const ctxValue: DatasourceStore = useMemo(\n () => ({\n getDatasource,\n getDatasourceClient,\n listDatasourceMetadata,\n }),\n [getDatasource, getDatasourceClient, listDatasourceMetadata]\n );\n\n return <DatasourceStoreContext.Provider value={ctxValue}>{children}</DatasourceStoreContext.Provider>;\n}\n\n// Helper to find a datasource in the list embedded in a dashboard spec\nfunction findDashboardDatasource(dashboardDatasources: DashboardSpec['datasources'], selector: DatasourceSelector) {\n if (dashboardDatasources === undefined) return undefined;\n\n // If using a name in the selector...\n if (selector.name !== undefined) {\n const named = dashboardDatasources[selector.name];\n if (named === undefined) return undefined;\n return named.plugin.kind === selector.kind ? named : undefined;\n }\n\n // If only using a kind, try to find one with that kind that is the default\n return Object.values(dashboardDatasources).find((ds) => ds.plugin.kind === selector.kind && ds.default === true);\n}\n\n// Helper for building a list of DatasourceMetadata results that will take care of de-duping already used selectors\nfunction buildListDatasourceMetadataResults(pluginDisplayName: string) {\n const results: DatasourceMetadata[] = [];\n const usedNames = new Set<string>();\n let defaultAdded = false;\n const addResult = (spec: DatasourceSpec, selectorName: string) => {\n // If we haven't added a default yet and this is a default, add default option to the beginning of the results\n if (spec.default && defaultAdded === false) {\n results.unshift({\n name: `Default ${pluginDisplayName}`,\n selector: {\n kind: spec.plugin.kind,\n },\n });\n defaultAdded = true;\n }\n\n // If we already have a datasource with this selector name, ignore it, otherwise add to end of list\n if (usedNames.has(selectorName)) return;\n\n results.push({\n name: spec.display?.name ?? selectorName,\n selector: {\n kind: spec.plugin.kind,\n name: selectorName,\n },\n });\n usedNames.add(selectorName);\n };\n\n return { results, addResult };\n}\n"],"names":["useCallback","useMemo","useEvent","DatasourceStoreContext","usePluginRegistry","DatasourceStoreProvider","props","dashboardResource","datasourceApi","children","project","metadata","getPlugin","listPluginMetadata","findDatasource","selector","datasources","spec","dashboardDatasource","findDashboardDatasource","undefined","proxyUrl","datasource","getDatasource","resource","globalDatasource","getGlobalDatasource","Error","kind","name","getDatasourceClient","getClient","plugin","Promise","all","createClient","listDatasourceMetadata","datasourcePluginKind","pluginMetadata","globalDatasources","listDatasources","listGlobalDatasources","datasourcePluginMetadata","find","results","addResult","buildListDatasourceMetadataResults","display","selectorName","ctxValue","Provider","value","dashboardDatasources","named","Object","values","ds","default","pluginDisplayName","us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ACtBC,IAAI,EAAEmB,YAAY;aACnB;SACF,CAAC,CAAC;QACHW,SAAS,CAACM,GAAG,CAACjB,YAAY,CAAC,CAAC;IAC9B,CAAC,AAAC;IAEF,OAAO;QAAEJ,OAAO;QAAEC,SAAS;KAAE,CAAC;AAChC,CAAC"}
|
|
@@ -7,6 +7,7 @@ declare type TemplateVariableStore = {
|
|
|
7
7
|
setVariableValue: (variableName: VariableName, value: VariableValue) => void;
|
|
8
8
|
setVariableOptions: (name: VariableName, options: VariableOption[]) => void;
|
|
9
9
|
setVariableLoading: (name: VariableName, loading: boolean) => void;
|
|
10
|
+
setVariableDefinitions: (definitions: VariableDefinition[]) => void;
|
|
10
11
|
};
|
|
11
12
|
export declare function useTemplateVariableValues(variableNames?: string[]): VariableStateMap;
|
|
12
13
|
export declare function useTemplateVariable(name: string): {
|
|
@@ -17,6 +18,7 @@ export declare function useTemplateVariableActions(): {
|
|
|
17
18
|
setVariableValue: (variableName: string, value: VariableValue) => void;
|
|
18
19
|
setVariableLoading: (name: string, loading: boolean) => void;
|
|
19
20
|
setVariableOptions: (name: string, options: VariableOption[]) => void;
|
|
21
|
+
setVariableDefinitions: (definitions: VariableDefinition[]) => void;
|
|
20
22
|
};
|
|
21
23
|
export declare function useTemplateVariableDefinitions(): VariableDefinition[];
|
|
22
24
|
export declare function useTemplateVariableStore(): TemplateVariableStore;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TemplateVariableProvider.d.ts","sourceRoot":"","sources":["../../src/context/TemplateVariableProvider.tsx"],"names":[],"mappings":";AAiBA,OAAO,EAEL,gBAAgB,EAChB,aAAa,EACb,cAAc,EAEf,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEnF,aAAK,qBAAqB,GAAG;IAC3B,mBAAmB,EAAE,kBAAkB,EAAE,CAAC;IAC1C,aAAa,EAAE,gBAAgB,CAAC;IAChC,g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ACR,0BAA+B,GAChC,EAAE;IACD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,0BAA0B,CAAC,EAAE,kBAAkB,EAAE,CAAC;CACnD,eAQA"}
|
|
@@ -59,7 +59,8 @@ export function useTemplateVariableActions() {
|
|
|
59
59
|
return {
|
|
60
60
|
setVariableValue: s.setVariableValue,
|
|
61
61
|
setVariableLoading: s.setVariableLoading,
|
|
62
|
-
setVariableOptions: s.setVariableOptions
|
|
62
|
+
setVariableOptions: s.setVariableOptions,
|
|
63
|
+
setVariableDefinitions: s.setVariableDefinitions
|
|
63
64
|
};
|
|
64
65
|
});
|
|
65
66
|
}
|
|
@@ -104,6 +105,12 @@ function createTemplateVariableSrvStore({ initialVariableDefinitions =[] }) {
|
|
|
104
105
|
const store = createStore()(devtools(immer((set)=>({
|
|
105
106
|
variableState: hydrateTemplateVariableStates(initialVariableDefinitions),
|
|
106
107
|
variableDefinitions: initialVariableDefinitions,
|
|
108
|
+
setVariableDefinitions (definitions) {
|
|
109
|
+
set((state)=>{
|
|
110
|
+
state.variableDefinitions = definitions;
|
|
111
|
+
state.variableState = hydrateTemplateVariableStates(definitions);
|
|
112
|
+
});
|
|
113
|
+
},
|
|
107
114
|
setVariableOptions (name, options) {
|
|
108
115
|
set((state)=>{
|
|
109
116
|
const varState = state.variableState[name];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/context/TemplateVariableProvider.tsx"],"sourcesContent":["// Copyright 2022 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { createContext, useContext, useMemo, useState } from 'react';\nimport { createStore, useStore } from 'zustand';\nimport { immer } from 'zustand/middleware/immer';\nimport { devtools } from 'zustand/middleware';\nimport {\n TemplateVariableContext,\n VariableStateMap,\n VariableState,\n VariableOption,\n DEFAULT_ALL_VALUE as ALL_VALUE,\n} from '@perses-dev/plugin-system';\nimport { VariableName, VariableValue, VariableDefinition } from '@perses-dev/core';\n\ntype TemplateVariableStore = {\n variableDefinitions: VariableDefinition[];\n variableState: VariableStateMap;\n setVariableValue: (variableName: VariableName, value: VariableValue) => void;\n setVariableOptions: (name: VariableName, options: VariableOption[]) => void;\n setVariableLoading: (name: VariableName, loading: boolean) => void;\n};\n\nconst TemplateVariableStoreContext = createContext<ReturnType<typeof createTemplateVariableSrvStore> | undefined>(\n undefined\n);\nfunction useTemplateVariableStoreCtx() {\n const context = useContext(TemplateVariableStoreContext);\n if (!context) {\n throw new Error('TemplateVariableStoreContext not initialized');\n }\n return context;\n}\n\nexport function useTemplateVariableValues(variableNames?: string[]) {\n const store = useTemplateVariableStoreCtx();\n const state = useStore(\n store,\n (s) => {\n const names = variableNames ?? Object.keys(s.variableState);\n const vars: VariableStateMap = {};\n names.forEach((name) => {\n const varState = s.variableState[name];\n if (!varState) {\n return;\n }\n vars[name] = varState;\n });\n return vars;\n },\n (left, right) => {\n return JSON.stringify(left) === JSON.stringify(right);\n }\n );\n return state;\n}\n\nexport function useTemplateVariable(name: string) {\n const store = useTemplateVariableStoreCtx();\n return useStore(store, (s) => {\n const variableState = s.variableState[name];\n const definition = s.variableDefinitions.find((v) => v.spec.name === name);\n return {\n state: variableState,\n definition,\n };\n });\n}\n\nexport function useTemplateVariableActions() {\n const store = useTemplateVariableStoreCtx();\n return useStore(store, (s) => {\n return {\n setVariableValue: s.setVariableValue,\n setVariableLoading: s.setVariableLoading,\n setVariableOptions: s.setVariableOptions,\n };\n });\n}\n\nexport function useTemplateVariableDefinitions() {\n const store = useTemplateVariableStoreCtx();\n return useStore(store, (s) => s.variableDefinitions);\n}\n\nexport function useTemplateVariableStore() {\n const store = useTemplateVariableStoreCtx();\n return useStore(store);\n}\n\nfunction PluginProvider({ children }: { children: React.ReactNode }) {\n const originalValues = useTemplateVariableValues();\n\n const values = useMemo(() => {\n const contextValues: VariableStateMap = {};\n\n // This will loop through all the current variables values\n // and update any variables that have ALL_VALUE as their current value\n // to include all options.\n Object.keys(originalValues).forEach((name) => {\n const v = { ...originalValues[name] } as VariableState;\n if (v.value === ALL_VALUE) {\n v.value = v.options?.map((o: { value: string }) => o.value) ?? null;\n }\n contextValues[name] = v;\n });\n return contextValues;\n }, [originalValues]);\n\n return <TemplateVariableContext.Provider value={{ state: values }}>{children}</TemplateVariableContext.Provider>;\n}\n\ninterface TemplateVariableSrvArgs {\n initialVariableDefinitions?: VariableDefinition[];\n}\n\nfunction createTemplateVariableSrvStore({ initialVariableDefinitions = [] }: TemplateVariableSrvArgs) {\n const store = createStore<TemplateVariableStore>()(\n devtools(\n immer((set) => ({\n variableState: hydrateTemplateVariableStates(initialVariableDefinitions),\n variableDefinitions: initialVariableDefinitions,\n setVariableOptions(name, options) {\n set((state) => {\n const varState = state.variableState[name];\n if (!varState) {\n return;\n }\n varState.options = options;\n });\n },\n setVariableLoading(name, loading) {\n set((state) => {\n const varState = state.variableState[name];\n if (!varState) {\n return;\n }\n varState.loading = loading;\n });\n },\n\n setVariableValue: (name, value) =>\n set((state) => {\n let val = value;\n const varState = state.variableState[name];\n if (!varState) {\n return;\n }\n\n // Make sure there is only one all value\n if (Array.isArray(val) && val.includes(ALL_VALUE)) {\n if (val.at(-1) === ALL_VALUE) {\n val = ALL_VALUE;\n } else {\n val = val.filter((v) => v !== ALL_VALUE);\n }\n }\n varState.value = val;\n }),\n }))\n )\n );\n\n return store;\n}\n\nexport function TemplateVariableProvider({\n children,\n initialVariableDefinitions = [],\n}: {\n children: React.ReactNode;\n initialVariableDefinitions?: VariableDefinition[];\n}) {\n const [store] = useState(createTemplateVariableSrvStore({ initialVariableDefinitions }));\n\n return (\n <TemplateVariableStoreContext.Provider value={store}>\n <PluginProvider>{children}</PluginProvider>\n </TemplateVariableStoreContext.Provider>\n );\n}\n\n/** Helpers */\n\nfunction hydrateTemplateVariableState(definition: VariableDefinition) {\n const v = definition;\n const varState: VariableState = {\n value: v.spec.default_value ?? null,\n loading: false,\n };\n switch (v.kind) {\n case 'TextVariable':\n varState.value = v.spec.value;\n break;\n case 'ListVariable':\n varState.options = [];\n if (varState.options.length > 0 && !varState.value) {\n const firstOptionValue = varState.options[0]?.value ?? null;\n if (firstOptionValue !== null) {\n varState.value = v.spec.allow_multiple ? [firstOptionValue] : firstOptionValue;\n }\n }\n break;\n default:\n break;\n }\n return varState;\n}\n\nfunction hydrateTemplateVariableStates(definitions: VariableDefinition[]): VariableStateMap {\n const state: VariableStateMap = {};\n definitions.forEach((v) => {\n state[v.spec.name] = hydrateTemplateVariableState(v);\n });\n return state;\n}\n"],"names":["createContext","useContext","useMemo","useState","createStore","useStore","immer","devtools","TemplateVariableContext","DEFAULT_ALL_VALUE","ALL_VALUE","TemplateVariableStoreContext","undefined","useTemplateVariableStoreCtx","context","Error","useTemplateVariableValues","variableNames","store","state","s","names","Object","keys","variableState","vars","forEach","name","varState","left","right","JSON","stringify","useTemplateVariable","definition","variableDefinitions","find","v","spec","useTemplateVariableActions","setVariableValue","setVariableLoading","setVariableOptions","useTemplateVariableDefinitions","useTemplateVariableStore","PluginProvider","children","originalValues","values","contextValues","value","options","map","o","Provider","createTemplateVariableSrvStore","initialVariableDefinitions","set","hydrateTemplateVariableStates","loading","val","Array","isArray","includes","at","filter","TemplateVariableProvider","hydrateTemplateVariableState","default_value","kind","length","firstOptionValue","allow_multiple","definitions"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;AAAA,SAASA,aAAa,EAAEC,UAAU,EAAEC,O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B,GAAnBA,QAAQ,CAACuB,OAAO,CAAC,CAAC,CAAC,cAAnBvB,GAAmB,WAAO,GAA1BA,KAAAA,CAA0B,GAA1BA,GAAmB,CAAEsB,KAAK,cAA1BtB,IAA0B,cAA1BA,IAA0B,GAAI,IAAI,AAAC;gBAC5D,IAAI2C,gBAAgB,KAAK,IAAI,EAAE;oBAC7B3C,QAAQ,CAACsB,KAAK,GAAGb,CAAC,CAACC,IAAI,CAACkC,cAAc,GAAG;wBAACD,gBAAgB;qBAAC,GAAGA,gBAAgB,CAAC;gBACjF,CAAC;YACH,CAAC;YACD,MAAM;QACR;YACE,MAAM;KACT;IACD,OAAO3C,QAAQ,CAAC;AAClB,CAAC;AAED,SAAS8B,6BAA6B,CAACe,WAAiC,EAAoB;IAC1F,MAAMtD,KAAK,GAAqB,EAAE,AAAC;IACnCsD,WAAW,CAAC/C,OAAO,CAAC,CAACW,CAAC,GAAK;QACzBlB,KAAK,CAACkB,CAAC,CAACC,IAAI,CAACX,IAAI,CAAC,GAAGwC,4BAA4B,CAAC9B,CAAC,CAAC,CAAC;IACvD,CAAC,CAAC,CAAC;IACH,OAAOlB,KAAK,CAAC;AACf,CAAC"}
|
|
1
|
+
{"version":3,"sources":["../../src/context/TemplateVariableProvider.tsx"],"sourcesContent":["// Copyright 2022 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { createContext, useContext, useMemo, useState } from 'react';\nimport { createStore, useStore } from 'zustand';\nimport { immer } from 'zustand/middleware/immer';\nimport { devtools } from 'zustand/middleware';\nimport {\n TemplateVariableContext,\n VariableStateMap,\n VariableState,\n VariableOption,\n DEFAULT_ALL_VALUE as ALL_VALUE,\n} from '@perses-dev/plugin-system';\nimport { VariableName, VariableValue, VariableDefinition } from '@perses-dev/core';\n\ntype TemplateVariableStore = {\n variableDefinitions: VariableDefinition[];\n variableState: VariableStateMap;\n setVariableValue: (variableName: VariableName, value: VariableValue) => void;\n setVariableOptions: (name: VariableName, options: VariableOption[]) => void;\n setVariableLoading: (name: VariableName, loading: boolean) => void;\n setVariableDefinitions: (definitions: VariableDefinition[]) => void;\n};\n\nconst TemplateVariableStoreContext = createContext<ReturnType<typeof createTemplateVariableSrvStore> | undefined>(\n undefined\n);\nfunction useTemplateVariableStoreCtx() {\n const context = useContext(TemplateVariableStoreContext);\n if (!context) {\n throw new Error('TemplateVariableStoreContext not initialized');\n }\n return context;\n}\n\nexport function useTemplateVariableValues(variableNames?: string[]) {\n const store = useTemplateVariableStoreCtx();\n const state = useStore(\n store,\n (s) => {\n const names = variableNames ?? Object.keys(s.variableState);\n const vars: VariableStateMap = {};\n names.forEach((name) => {\n const varState = s.variableState[name];\n if (!varState) {\n return;\n }\n vars[name] = varState;\n });\n return vars;\n },\n (left, right) => {\n return JSON.stringify(left) === JSON.stringify(right);\n }\n );\n return state;\n}\n\nexport function useTemplateVariable(name: string) {\n const store = useTemplateVariableStoreCtx();\n return useStore(store, (s) => {\n const variableState = s.variableState[name];\n const definition = s.variableDefinitions.find((v) => v.spec.name === name);\n return {\n state: variableState,\n definition,\n };\n });\n}\n\nexport function useTemplateVariableActions() {\n const store = useTemplateVariableStoreCtx();\n return useStore(store, (s) => {\n return {\n setVariableValue: s.setVariableValue,\n setVariableLoading: s.setVariableLoading,\n setVariableOptions: s.setVariableOptions,\n setVariableDefinitions: s.setVariableDefinitions,\n };\n });\n}\n\nexport function useTemplateVariableDefinitions() {\n const store = useTemplateVariableStoreCtx();\n return useStore(store, (s) => s.variableDefinitions);\n}\n\nexport function useTemplateVariableStore() {\n const store = useTemplateVariableStoreCtx();\n return useStore(store);\n}\n\nfunction PluginProvider({ children }: { children: React.ReactNode }) {\n const originalValues = useTemplateVariableValues();\n\n const values = useMemo(() => {\n const contextValues: VariableStateMap = {};\n\n // This will loop through all the current variables values\n // and update any variables that have ALL_VALUE as their current value\n // to include all options.\n Object.keys(originalValues).forEach((name) => {\n const v = { ...originalValues[name] } as VariableState;\n if (v.value === ALL_VALUE) {\n v.value = v.options?.map((o: { value: string }) => o.value) ?? null;\n }\n contextValues[name] = v;\n });\n return contextValues;\n }, [originalValues]);\n\n return <TemplateVariableContext.Provider value={{ state: values }}>{children}</TemplateVariableContext.Provider>;\n}\n\ninterface TemplateVariableSrvArgs {\n initialVariableDefinitions?: VariableDefinition[];\n}\n\nfunction createTemplateVariableSrvStore({ initialVariableDefinitions = [] }: TemplateVariableSrvArgs) {\n const store = createStore<TemplateVariableStore>()(\n devtools(\n immer((set) => ({\n variableState: hydrateTemplateVariableStates(initialVariableDefinitions),\n variableDefinitions: initialVariableDefinitions,\n setVariableDefinitions(definitions: VariableDefinition[]) {\n set((state) => {\n state.variableDefinitions = definitions;\n state.variableState = hydrateTemplateVariableStates(definitions);\n });\n },\n setVariableOptions(name, options) {\n set((state) => {\n const varState = state.variableState[name];\n if (!varState) {\n return;\n }\n varState.options = options;\n });\n },\n setVariableLoading(name, loading) {\n set((state) => {\n const varState = state.variableState[name];\n if (!varState) {\n return;\n }\n varState.loading = loading;\n });\n },\n\n setVariableValue: (name, value) =>\n set((state) => {\n let val = value;\n const varState = state.variableState[name];\n if (!varState) {\n return;\n }\n\n // Make sure there is only one all value\n if (Array.isArray(val) && val.includes(ALL_VALUE)) {\n if (val.at(-1) === ALL_VALUE) {\n val = ALL_VALUE;\n } else {\n val = val.filter((v) => v !== ALL_VALUE);\n }\n }\n varState.value = val;\n }),\n }))\n )\n );\n\n return store;\n}\n\nexport function TemplateVariableProvider({\n children,\n initialVariableDefinitions = [],\n}: {\n children: React.ReactNode;\n initialVariableDefinitions?: VariableDefinition[];\n}) {\n const [store] = useState(createTemplateVariableSrvStore({ initialVariableDefinitions }));\n\n return (\n <TemplateVariableStoreContext.Provider value={store}>\n <PluginProvider>{children}</PluginProvider>\n </TemplateVariableStoreContext.Provider>\n );\n}\n\n/** Helpers */\n\nfunction hydrateTemplateVariableState(definition: VariableDefinition) {\n const v = definition;\n const varState: VariableState = {\n value: v.spec.default_value ?? null,\n loading: false,\n };\n switch (v.kind) {\n case 'TextVariable':\n varState.value = v.spec.value;\n break;\n case 'ListVariable':\n varState.options = [];\n if (varState.options.length > 0 && !varState.value) {\n const firstOptionValue = varState.options[0]?.value ?? null;\n if (firstOptionValue !== null) {\n varState.value = v.spec.allow_multiple ? [firstOptionValue] : firstOptionValue;\n }\n }\n break;\n default:\n break;\n }\n return varState;\n}\n\nfunction hydrateTemplateVariableStates(definitions: VariableDefinition[]): VariableStateMap {\n const state: VariableStateMap = {};\n definitions.forEach((v) => {\n state[v.spec.name] = hydrateTemplateVariableState(v);\n });\n return state;\n}\n"],"names":["createContext","useContext","useMemo","useState","createStore","useStore","immer","devtools","TemplateVariableContext","DEFAULT_ALL_VALUE","ALL_VALUE","TemplateVariableStoreContext","undefined","useTemplateVariableStoreCtx","context","Error","useTemplateVariableValues","variableNames","store","state","s","names","Object","keys","variableState","vars","forEach","name","varState","left","right","JSON","stringify","useTemplateVariable","definition","variableDefinitions","find","v","spec","useTemplateVariableActions","setVariableValue","setVariableLoading","setVariableOptions","setVariableDefinitions","useTemplateVariableDefinitions","useTemplateVariableStore","PluginProvider","children","originalValues","values","contextValues","value","options","map","o","Provider","createTemplateVariableSrvStore","initialVariableDefinitions","set","hydrateTemplateVariableStates","definitions","loading","val","Array","isArray","includes","at","filter","TemplateVariableProvider","hydrateTemplateVariableState","default_value","kind","length","firstOptionValue","allow_multiple"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;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AACuB,KAAK,GAAGd,CAAC,CAACC,IAAI,CAACa,KAAK,CAAC;YAC9B,MAAM;QACR,KAAK,cAAc;YACjBvB,QAAQ,CAACwB,OAAO,GAAG,EAAE,CAAC;YACtB,IAAIxB,QAAQ,CAACwB,OAAO,CAACoB,MAAM,GAAG,CAAC,IAAI,CAAC5C,QAAQ,CAACuB,KAAK,EAAE;oBACzBvB,GAAmB;oBAAnBA,IAA0B;gBAAnD,MAAM6C,gBAAgB,GAAG7C,CAAAA,IAA0B,GAA1BA,CAAAA,GAAmB,GAAnBA,QAAQ,CAACwB,OAAO,CAAC,CAAC,CAAC,cAAnBxB,GAAmB,WAAO,GAA1BA,KAAAA,CAA0B,GAA1BA,GAAmB,CAAEuB,KAAK,cAA1BvB,IAA0B,cAA1BA,IAA0B,GAAI,IAAI,AAAC;gBAC5D,IAAI6C,gBAAgB,KAAK,IAAI,EAAE;oBAC7B7C,QAAQ,CAACuB,KAAK,GAAGd,CAAC,CAACC,IAAI,CAACoC,cAAc,GAAG;wBAACD,gBAAgB;qBAAC,GAAGA,gBAAgB,CAAC;gBACjF,CAAC;YACH,CAAC;YACD,MAAM;QACR;YACE,MAAM;KACT;IACD,OAAO7C,QAAQ,CAAC;AAClB,CAAC;AAED,SAAS+B,6BAA6B,CAACC,WAAiC,EAAoB;IAC1F,MAAMzC,KAAK,GAAqB,EAAE,AAAC;IACnCyC,WAAW,CAAClC,OAAO,CAAC,CAACW,CAAC,GAAK;QACzBlB,KAAK,CAACkB,CAAC,CAACC,IAAI,CAACX,IAAI,CAAC,GAAG0C,4BAA4B,CAAChC,CAAC,CAAC,CAAC;IACvD,CAAC,CAAC,CAAC;IACH,OAAOlB,KAAK,CAAC;AACf,CAAC"}
|
package/dist/context/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
export * from './DashboardAppSlice';
|
|
2
1
|
export * from './DashboardProvider';
|
|
3
2
|
export * from './DatasourceStoreProvider';
|
|
4
|
-
export * from './LayoutsSlice';
|
|
5
3
|
export * from './QueryStringProvider';
|
|
6
4
|
export * from './TemplateVariableProvider';
|
|
7
5
|
export * from './TimeRangeProvider';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAC;AACpC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qBAAqB,CAAC"}
|
package/dist/context/index.js
CHANGED
|
@@ -10,10 +10,8 @@
|
|
|
10
10
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
11
|
// See the License for the specific language governing permissions and
|
|
12
12
|
// limitations under the License.
|
|
13
|
-
export * from './DashboardAppSlice';
|
|
14
13
|
export * from './DashboardProvider';
|
|
15
14
|
export * from './DatasourceStoreProvider';
|
|
16
|
-
export * from './LayoutsSlice';
|
|
17
15
|
export * from './QueryStringProvider';
|
|
18
16
|
export * from './TemplateVariableProvider';
|
|
19
17
|
export * from './TimeRangeProvider';
|