@perses-dev/dashboards 0.9.0 → 0.11.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.
Files changed (268) hide show
  1. package/dist/cjs/components/Dashboard.js +6 -11
  2. package/dist/cjs/components/DashboardToolbar.js +12 -6
  3. package/dist/cjs/components/GridLayout/GridItemContent.js +7 -15
  4. package/dist/cjs/components/GridLayout/GridLayout.js +27 -22
  5. package/dist/cjs/components/GridLayout/GridTitle.js +27 -7
  6. package/dist/cjs/components/Panel/DeletePanelDialog.js +95 -0
  7. package/dist/cjs/components/Panel/Panel.js +12 -10
  8. package/dist/cjs/components/Panel/Panel.test.js +15 -6
  9. package/dist/cjs/components/Panel/PanelContent.js +3 -2
  10. package/dist/cjs/components/PanelDrawer/PanelDrawer.js +13 -12
  11. package/dist/cjs/components/PanelDrawer/PanelDrawer.test.js +9 -13
  12. package/dist/cjs/components/PanelDrawer/PanelEditorForm.js +87 -39
  13. package/dist/cjs/components/PanelDrawer/PanelPreview.js +54 -0
  14. package/dist/cjs/components/PanelGroupDialog/DeletePanelGroupDialog.js +88 -0
  15. package/dist/cjs/components/PanelGroupDialog/PanelGroupDialog.js +70 -118
  16. package/dist/cjs/components/PanelGroupDialog/PanelGroupDialog.test.js +21 -36
  17. package/dist/cjs/components/PanelGroupDialog/PanelGroupEditorForm.js +92 -0
  18. package/dist/cjs/components/PanelGroupDialog/index.js +29 -0
  19. package/dist/cjs/components/TimeRangeControls/TimeRangeControls.js +8 -11
  20. package/dist/cjs/components/TimeRangeControls/TimeRangeControls.test.js +20 -13
  21. package/dist/cjs/components/Variables/Variable.js +20 -11
  22. package/dist/cjs/components/Variables/VariableEditor.js +240 -0
  23. package/dist/cjs/components/Variables/VariableEditorForm/VariableEditorForm.js +236 -0
  24. package/dist/cjs/components/Variables/VariableEditorForm/index.js +28 -0
  25. package/dist/cjs/components/Variables/VariableEditorForm/variable-editor-form-model.js +88 -0
  26. package/dist/cjs/components/Variables/VariableList.js +49 -69
  27. package/dist/cjs/components/Variables/index.js +1 -0
  28. package/dist/cjs/components/index.js +1 -0
  29. package/dist/cjs/context/DashboardProvider/DashboardProvider.js +82 -0
  30. package/dist/cjs/context/DashboardProvider/common.js +18 -0
  31. package/dist/cjs/context/DashboardProvider/dashboard-provider-api.js +164 -0
  32. package/dist/cjs/context/DashboardProvider/index.js +29 -0
  33. package/dist/cjs/context/DashboardProvider/panel-editor-slice.js +175 -0
  34. package/dist/cjs/context/DashboardProvider/panel-group-editor-slice.js +102 -0
  35. package/dist/cjs/context/DashboardProvider/panel-group-slice.js +208 -0
  36. package/dist/cjs/context/DatasourceStoreProvider.js +109 -7
  37. package/dist/cjs/context/TemplateVariableProvider.js +8 -1
  38. package/dist/cjs/context/TimeRangeProvider.js +20 -44
  39. package/dist/cjs/context/index.js +1 -3
  40. package/dist/cjs/context/useDashboardSpec.js +61 -0
  41. package/dist/cjs/index.js +1 -0
  42. package/dist/cjs/test/render.js +25 -5
  43. package/dist/cjs/test/setup-tests.js +4 -1
  44. package/dist/cjs/test/testDashboard.js +24 -11
  45. package/dist/cjs/utils/index.js +28 -0
  46. package/dist/cjs/utils/time-range-params.js +145 -0
  47. package/dist/cjs/views/ViewDashboard/DashboardApp.js +5 -8
  48. package/dist/cjs/views/ViewDashboard/ViewDashboard.js +5 -22
  49. package/dist/cjs/views/ViewDashboard/tests/panelGroups.test.js +114 -0
  50. package/dist/components/Dashboard.d.ts +1 -4
  51. package/dist/components/Dashboard.d.ts.map +1 -1
  52. package/dist/components/Dashboard.js +7 -12
  53. package/dist/components/Dashboard.js.map +1 -1
  54. package/dist/components/DashboardToolbar.d.ts.map +1 -1
  55. package/dist/components/DashboardToolbar.js +14 -8
  56. package/dist/components/DashboardToolbar.js.map +1 -1
  57. package/dist/components/GridLayout/GridItemContent.d.ts +2 -4
  58. package/dist/components/GridLayout/GridItemContent.d.ts.map +1 -1
  59. package/dist/components/GridLayout/GridItemContent.js +7 -15
  60. package/dist/components/GridLayout/GridItemContent.js.map +1 -1
  61. package/dist/components/GridLayout/GridLayout.d.ts +2 -4
  62. package/dist/components/GridLayout/GridLayout.d.ts.map +1 -1
  63. package/dist/components/GridLayout/GridLayout.js +28 -23
  64. package/dist/components/GridLayout/GridLayout.js.map +1 -1
  65. package/dist/components/GridLayout/GridTitle.d.ts +2 -1
  66. package/dist/components/GridLayout/GridTitle.d.ts.map +1 -1
  67. package/dist/components/GridLayout/GridTitle.js +28 -8
  68. package/dist/components/GridLayout/GridTitle.js.map +1 -1
  69. package/dist/components/Panel/DeletePanelDialog.d.ts +5 -0
  70. package/dist/components/Panel/DeletePanelDialog.d.ts.map +1 -0
  71. package/dist/components/Panel/DeletePanelDialog.js +84 -0
  72. package/dist/components/Panel/DeletePanelDialog.js.map +1 -0
  73. package/dist/components/Panel/Panel.d.ts +2 -2
  74. package/dist/components/Panel/Panel.d.ts.map +1 -1
  75. package/dist/components/Panel/Panel.js +13 -11
  76. package/dist/components/Panel/Panel.js.map +1 -1
  77. package/dist/components/Panel/Panel.test.d.ts +1 -1
  78. package/dist/components/Panel/Panel.test.d.ts.map +1 -1
  79. package/dist/components/Panel/Panel.test.js +10 -6
  80. package/dist/components/Panel/Panel.test.js.map +1 -1
  81. package/dist/components/Panel/PanelContent.d.ts +4 -1
  82. package/dist/components/Panel/PanelContent.d.ts.map +1 -1
  83. package/dist/components/Panel/PanelContent.js +3 -2
  84. package/dist/components/Panel/PanelContent.js.map +1 -1
  85. package/dist/components/PanelDrawer/PanelDrawer.d.ts.map +1 -1
  86. package/dist/components/PanelDrawer/PanelDrawer.js +14 -13
  87. package/dist/components/PanelDrawer/PanelDrawer.js.map +1 -1
  88. package/dist/components/PanelDrawer/PanelDrawer.test.js +9 -13
  89. package/dist/components/PanelDrawer/PanelDrawer.test.js.map +1 -1
  90. package/dist/components/PanelDrawer/PanelEditorForm.d.ts +3 -3
  91. package/dist/components/PanelDrawer/PanelEditorForm.d.ts.map +1 -1
  92. package/dist/components/PanelDrawer/PanelEditorForm.js +89 -41
  93. package/dist/components/PanelDrawer/PanelEditorForm.js.map +1 -1
  94. package/dist/components/PanelDrawer/PanelPreview.d.ts +4 -0
  95. package/dist/components/PanelDrawer/PanelPreview.d.ts.map +1 -0
  96. package/dist/components/PanelDrawer/PanelPreview.js +48 -0
  97. package/dist/components/PanelDrawer/PanelPreview.js.map +1 -0
  98. package/dist/components/PanelGroupDialog/DeletePanelGroupDialog.d.ts +3 -0
  99. package/dist/components/PanelGroupDialog/DeletePanelGroupDialog.d.ts.map +1 -0
  100. package/dist/components/PanelGroupDialog/DeletePanelGroupDialog.js +77 -0
  101. package/dist/components/PanelGroupDialog/DeletePanelGroupDialog.js.map +1 -0
  102. package/dist/components/PanelGroupDialog/PanelGroupDialog.d.ts +4 -2
  103. package/dist/components/PanelGroupDialog/PanelGroupDialog.d.ts.map +1 -1
  104. package/dist/components/PanelGroupDialog/PanelGroupDialog.js +73 -119
  105. package/dist/components/PanelGroupDialog/PanelGroupDialog.js.map +1 -1
  106. package/dist/components/PanelGroupDialog/PanelGroupDialog.test.js +20 -35
  107. package/dist/components/PanelGroupDialog/PanelGroupDialog.test.js.map +1 -1
  108. package/dist/components/PanelGroupDialog/PanelGroupEditorForm.d.ts +12 -0
  109. package/dist/components/PanelGroupDialog/PanelGroupEditorForm.d.ts.map +1 -0
  110. package/dist/components/PanelGroupDialog/PanelGroupEditorForm.js +82 -0
  111. package/dist/components/PanelGroupDialog/PanelGroupEditorForm.js.map +1 -0
  112. package/dist/components/PanelGroupDialog/index.d.ts +3 -0
  113. package/dist/components/PanelGroupDialog/index.d.ts.map +1 -0
  114. package/dist/components/PanelGroupDialog/index.js +16 -0
  115. package/dist/components/PanelGroupDialog/index.js.map +1 -0
  116. package/dist/components/TimeRangeControls/TimeRangeControls.d.ts.map +1 -1
  117. package/dist/components/TimeRangeControls/TimeRangeControls.js +11 -14
  118. package/dist/components/TimeRangeControls/TimeRangeControls.js.map +1 -1
  119. package/dist/components/TimeRangeControls/TimeRangeControls.test.js +21 -14
  120. package/dist/components/TimeRangeControls/TimeRangeControls.test.js.map +1 -1
  121. package/dist/components/Variables/Variable.js +22 -13
  122. package/dist/components/Variables/Variable.js.map +1 -1
  123. package/dist/components/Variables/VariableEditor.d.ts +8 -0
  124. package/dist/components/Variables/VariableEditor.d.ts.map +1 -0
  125. package/dist/components/Variables/VariableEditor.js +229 -0
  126. package/dist/components/Variables/VariableEditor.js.map +1 -0
  127. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.d.ts +8 -0
  128. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.d.ts.map +1 -0
  129. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js +225 -0
  130. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js.map +1 -0
  131. package/dist/components/Variables/VariableEditorForm/index.d.ts +2 -0
  132. package/dist/components/Variables/VariableEditorForm/index.d.ts.map +1 -0
  133. package/dist/components/Variables/VariableEditorForm/index.js +15 -0
  134. package/dist/components/Variables/VariableEditorForm/index.js.map +1 -0
  135. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.d.ts +21 -0
  136. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.d.ts.map +1 -0
  137. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.js +76 -0
  138. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.js.map +1 -0
  139. package/dist/components/Variables/VariableList.d.ts.map +1 -1
  140. package/dist/components/Variables/VariableList.js +47 -72
  141. package/dist/components/Variables/VariableList.js.map +1 -1
  142. package/dist/components/Variables/index.d.ts +1 -0
  143. package/dist/components/Variables/index.d.ts.map +1 -1
  144. package/dist/components/Variables/index.js +1 -0
  145. package/dist/components/Variables/index.js.map +1 -1
  146. package/dist/components/index.d.ts +1 -0
  147. package/dist/components/index.d.ts.map +1 -1
  148. package/dist/components/index.js +1 -0
  149. package/dist/components/index.js.map +1 -1
  150. package/dist/context/DashboardProvider/DashboardProvider.d.ts +25 -0
  151. package/dist/context/DashboardProvider/DashboardProvider.d.ts.map +1 -0
  152. package/dist/context/DashboardProvider/DashboardProvider.js +64 -0
  153. package/dist/context/DashboardProvider/DashboardProvider.js.map +1 -0
  154. package/dist/context/DashboardProvider/common.d.ts +5 -0
  155. package/dist/context/DashboardProvider/common.d.ts.map +1 -0
  156. package/dist/context/DashboardProvider/common.js +17 -0
  157. package/dist/context/DashboardProvider/common.js.map +1 -0
  158. package/dist/context/DashboardProvider/dashboard-provider-api.d.ts +74 -0
  159. package/dist/context/DashboardProvider/dashboard-provider-api.d.ts.map +1 -0
  160. package/dist/context/DashboardProvider/dashboard-provider-api.js +163 -0
  161. package/dist/context/DashboardProvider/dashboard-provider-api.js.map +1 -0
  162. package/dist/context/DashboardProvider/index.d.ts +5 -0
  163. package/dist/context/DashboardProvider/index.d.ts.map +1 -0
  164. package/dist/context/DashboardProvider/index.js +16 -0
  165. package/dist/context/DashboardProvider/index.js.map +1 -0
  166. package/dist/context/DashboardProvider/panel-editor-slice.d.ts +85 -0
  167. package/dist/context/DashboardProvider/panel-editor-slice.d.ts.map +1 -0
  168. package/dist/context/DashboardProvider/panel-editor-slice.js +171 -0
  169. package/dist/context/DashboardProvider/panel-editor-slice.js.map +1 -0
  170. package/dist/context/DashboardProvider/panel-group-editor-slice.d.ts +40 -0
  171. package/dist/context/DashboardProvider/panel-group-editor-slice.d.ts.map +1 -0
  172. package/dist/context/DashboardProvider/panel-group-editor-slice.js +96 -0
  173. package/dist/context/DashboardProvider/panel-group-editor-slice.js.map +1 -0
  174. package/dist/context/DashboardProvider/panel-group-slice.d.ts +80 -0
  175. package/dist/context/DashboardProvider/panel-group-slice.d.ts.map +1 -0
  176. package/dist/context/DashboardProvider/panel-group-slice.js +204 -0
  177. package/dist/context/DashboardProvider/panel-group-slice.js.map +1 -0
  178. package/dist/context/DatasourceStoreProvider.d.ts +10 -2
  179. package/dist/context/DatasourceStoreProvider.d.ts.map +1 -1
  180. package/dist/context/DatasourceStoreProvider.js +111 -9
  181. package/dist/context/DatasourceStoreProvider.js.map +1 -1
  182. package/dist/context/TemplateVariableProvider.d.ts +2 -0
  183. package/dist/context/TemplateVariableProvider.d.ts.map +1 -1
  184. package/dist/context/TemplateVariableProvider.js +8 -1
  185. package/dist/context/TemplateVariableProvider.js.map +1 -1
  186. package/dist/context/TimeRangeProvider.d.ts +9 -2
  187. package/dist/context/TimeRangeProvider.d.ts.map +1 -1
  188. package/dist/context/TimeRangeProvider.js +15 -43
  189. package/dist/context/TimeRangeProvider.js.map +1 -1
  190. package/dist/context/index.d.ts +1 -3
  191. package/dist/context/index.d.ts.map +1 -1
  192. package/dist/context/index.js +1 -3
  193. package/dist/context/index.js.map +1 -1
  194. package/dist/context/useDashboardSpec.d.ts +3 -0
  195. package/dist/context/useDashboardSpec.d.ts.map +1 -0
  196. package/dist/context/useDashboardSpec.js +55 -0
  197. package/dist/context/useDashboardSpec.js.map +1 -0
  198. package/dist/index.d.ts +1 -0
  199. package/dist/index.d.ts.map +1 -1
  200. package/dist/index.js +1 -0
  201. package/dist/index.js.map +1 -1
  202. package/dist/test/plugin-registry.d.ts +2 -2
  203. package/dist/test/plugin-registry.d.ts.map +1 -1
  204. package/dist/test/plugin-registry.js.map +1 -1
  205. package/dist/test/render.d.ts +2 -1
  206. package/dist/test/render.d.ts.map +1 -1
  207. package/dist/test/render.js +25 -5
  208. package/dist/test/render.js.map +1 -1
  209. package/dist/test/setup-tests.d.ts.map +1 -1
  210. package/dist/test/setup-tests.js +4 -0
  211. package/dist/test/setup-tests.js.map +1 -1
  212. package/dist/test/testDashboard.d.ts.map +1 -1
  213. package/dist/test/testDashboard.js +24 -11
  214. package/dist/test/testDashboard.js.map +1 -1
  215. package/dist/utils/index.d.ts +2 -0
  216. package/dist/utils/index.d.ts.map +1 -0
  217. package/dist/utils/index.js +15 -0
  218. package/dist/utils/index.js.map +1 -0
  219. package/dist/utils/time-range-params.d.ts +25 -0
  220. package/dist/utils/time-range-params.d.ts.map +1 -0
  221. package/dist/utils/time-range-params.js +137 -0
  222. package/dist/utils/time-range-params.js.map +1 -0
  223. package/dist/views/ViewDashboard/DashboardApp.d.ts.map +1 -1
  224. package/dist/views/ViewDashboard/DashboardApp.js +6 -9
  225. package/dist/views/ViewDashboard/DashboardApp.js.map +1 -1
  226. package/dist/views/ViewDashboard/ViewDashboard.d.ts.map +1 -1
  227. package/dist/views/ViewDashboard/ViewDashboard.js +5 -22
  228. package/dist/views/ViewDashboard/ViewDashboard.js.map +1 -1
  229. package/dist/views/ViewDashboard/tests/panelGroups.test.d.ts +2 -0
  230. package/dist/views/ViewDashboard/tests/panelGroups.test.d.ts.map +1 -0
  231. package/dist/views/ViewDashboard/tests/panelGroups.test.js +107 -0
  232. package/dist/views/ViewDashboard/tests/panelGroups.test.js.map +1 -0
  233. package/package.json +5 -4
  234. package/dist/cjs/components/PanelDrawer/PanelSpecEditor.js +0 -40
  235. package/dist/cjs/components/PanelDrawer/PanelTypeSelect.js +0 -38
  236. package/dist/cjs/components/PanelDrawer/panel-editor-model.js +0 -140
  237. package/dist/cjs/context/DashboardAppSlice.js +0 -57
  238. package/dist/cjs/context/DashboardProvider.js +0 -128
  239. package/dist/cjs/context/LayoutsSlice.js +0 -55
  240. package/dist/cjs/context/QueryStringProvider.js +0 -89
  241. package/dist/components/PanelDrawer/PanelSpecEditor.d.ts +0 -7
  242. package/dist/components/PanelDrawer/PanelSpecEditor.d.ts.map +0 -1
  243. package/dist/components/PanelDrawer/PanelSpecEditor.js +0 -34
  244. package/dist/components/PanelDrawer/PanelSpecEditor.js.map +0 -1
  245. package/dist/components/PanelDrawer/PanelTypeSelect.d.ts +0 -8
  246. package/dist/components/PanelDrawer/PanelTypeSelect.d.ts.map +0 -1
  247. package/dist/components/PanelDrawer/PanelTypeSelect.js +0 -34
  248. package/dist/components/PanelDrawer/PanelTypeSelect.js.map +0 -1
  249. package/dist/components/PanelDrawer/panel-editor-model.d.ts +0 -27
  250. package/dist/components/PanelDrawer/panel-editor-model.d.ts.map +0 -1
  251. package/dist/components/PanelDrawer/panel-editor-model.js +0 -133
  252. package/dist/components/PanelDrawer/panel-editor-model.js.map +0 -1
  253. package/dist/context/DashboardAppSlice.d.ts +0 -26
  254. package/dist/context/DashboardAppSlice.d.ts.map +0 -1
  255. package/dist/context/DashboardAppSlice.js +0 -45
  256. package/dist/context/DashboardAppSlice.js.map +0 -1
  257. package/dist/context/DashboardProvider.d.ts +0 -36
  258. package/dist/context/DashboardProvider.d.ts.map +0 -1
  259. package/dist/context/DashboardProvider.js +0 -107
  260. package/dist/context/DashboardProvider.js.map +0 -1
  261. package/dist/context/LayoutsSlice.d.ts +0 -12
  262. package/dist/context/LayoutsSlice.d.ts.map +0 -1
  263. package/dist/context/LayoutsSlice.js +0 -43
  264. package/dist/context/LayoutsSlice.js.map +0 -1
  265. package/dist/context/QueryStringProvider.d.ts +0 -13
  266. package/dist/context/QueryStringProvider.d.ts.map +0 -1
  267. package/dist/context/QueryStringProvider.js +0 -40
  268. package/dist/context/QueryStringProvider.js.map +0 -1
@@ -0,0 +1,171 @@
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
+ previousPanels: panels,
21
+ panels,
22
+ panelEditor: undefined,
23
+ savePanels () {
24
+ set((state)=>{
25
+ state.previousPanels = state.panels;
26
+ });
27
+ },
28
+ resetPanels () {
29
+ set((state)=>{
30
+ state.panels = state.previousPanels;
31
+ });
32
+ },
33
+ openEditPanel (item) {
34
+ const { panels , getPanelKey } = get();
35
+ // Ask the layout store for the panel key at that location
36
+ const panelKey = getPanelKey(item);
37
+ // Find the panel to edit
38
+ const panelToEdit = panels[panelKey];
39
+ if (panelToEdit === undefined) {
40
+ throw new Error(`Cannot find Panel with key '${panelKey}'`);
41
+ }
42
+ var _description;
43
+ const editorState = {
44
+ mode: 'Edit',
45
+ initialValues: {
46
+ name: panelToEdit.spec.display.name,
47
+ description: (_description = panelToEdit.spec.display.description) !== null && _description !== void 0 ? _description : '',
48
+ groupId: item.panelGroupId,
49
+ kind: panelToEdit.spec.plugin.kind,
50
+ spec: panelToEdit.spec.plugin.spec
51
+ },
52
+ applyChanges: (next)=>{
53
+ const panelDefinititon = createPanelDefinitionFromEditorValues(next);
54
+ set((state)=>{
55
+ state.panels[panelKey] = panelDefinititon;
56
+ });
57
+ // Move the panel to another group if it changed
58
+ if (next.groupId !== item.panelGroupId) {
59
+ get().movePanelToGroup(item, next.groupId);
60
+ }
61
+ },
62
+ close: ()=>{
63
+ set((state)=>{
64
+ state.panelEditor = undefined;
65
+ });
66
+ }
67
+ };
68
+ // Open the editor with the new state
69
+ set((state)=>{
70
+ state.panelEditor = editorState;
71
+ });
72
+ },
73
+ openAddPanel (panelGroupId) {
74
+ // If a panel group isn't supplied, add to the first group
75
+ if (panelGroupId === undefined) {
76
+ const firstGroupId = get().panelGroupIdOrder[0];
77
+ if (firstGroupId === undefined) {
78
+ throw new Error('No panel groups to add a panel to');
79
+ }
80
+ panelGroupId = firstGroupId;
81
+ }
82
+ const editorState = {
83
+ mode: 'Add',
84
+ initialValues: {
85
+ name: '',
86
+ description: '',
87
+ groupId: panelGroupId,
88
+ // TODO: If we knew what plugins were available (and how to create the initial spec), we might be able to
89
+ // set a smarter default here?
90
+ kind: '',
91
+ spec: {}
92
+ },
93
+ applyChanges: (next)=>{
94
+ const panelDef = createPanelDefinitionFromEditorValues(next);
95
+ const panelKey = removeWhiteSpacesAndSpecialCharacters(next.name);
96
+ set((state)=>{
97
+ state.panels[panelKey] = panelDef;
98
+ });
99
+ get().addPanelToGroup(panelKey, next.groupId);
100
+ },
101
+ close: ()=>{
102
+ set((state)=>{
103
+ state.panelEditor = undefined;
104
+ });
105
+ }
106
+ };
107
+ // Open the editor with the new state
108
+ set((state)=>{
109
+ state.panelEditor = editorState;
110
+ });
111
+ },
112
+ deletePanels (items) {
113
+ const { mapPanelToPanelGroups , deletePanelInPanelGroup , getPanelKey } = get();
114
+ const map = mapPanelToPanelGroups();
115
+ // get panel key first before deleting panel from panel group since getPanelKey relies on index
116
+ const panels = items.map((panel)=>{
117
+ return {
118
+ ...panel,
119
+ panelKey: getPanelKey(panel)
120
+ };
121
+ });
122
+ panels.forEach(({ panelKey , ...panel })=>{
123
+ var ref;
124
+ deletePanelInPanelGroup(panel);
125
+ // make sure panel is only referenced in one panel group before deleting it from state.panels
126
+ if (map[panelKey] && ((ref = map[panelKey]) === null || ref === void 0 ? void 0 : ref.length) === 1) {
127
+ set((state)=>{
128
+ delete state.panels[panelKey];
129
+ });
130
+ }
131
+ });
132
+ },
133
+ openDeletePanelDialog (item) {
134
+ const { panels , getPanelKey , panelGroups } = get();
135
+ const panelKey = getPanelKey(item);
136
+ set((state)=>{
137
+ var ref, ref1;
138
+ var _name, ref2;
139
+ state.deletePanelDialog = {
140
+ panelGroupItemId: item,
141
+ panelName: (_name = (ref = panels[panelKey]) === null || ref === void 0 ? void 0 : ref.spec.display.name) !== null && _name !== void 0 ? _name : '',
142
+ panelGroupName: (ref2 = (ref1 = panelGroups[item.panelGroupId]) === null || ref1 === void 0 ? void 0 : ref1.title) !== null && ref2 !== void 0 ? ref2 : ''
143
+ };
144
+ });
145
+ },
146
+ closeDeletePanelDialog () {
147
+ set((state)=>{
148
+ state.deletePanelDialog = undefined;
149
+ });
150
+ }
151
+ };
152
+ };
153
+ }
154
+ // Helper to create PanelDefinitions when saving
155
+ function createPanelDefinitionFromEditorValues(editorValues) {
156
+ return {
157
+ kind: 'Panel',
158
+ spec: {
159
+ display: {
160
+ name: editorValues.name,
161
+ description: editorValues.description !== '' ? editorValues.description : undefined
162
+ },
163
+ plugin: {
164
+ kind: editorValues.kind,
165
+ spec: editorValues.spec
166
+ }
167
+ }
168
+ };
169
+ }
170
+
171
+ //# sourceMappingURL=panel-editor-slice.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/context/DashboardProvider/panel-editor-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 { PanelGroupSlice, PanelGroupItemId, PanelGroupId } from './panel-group-slice';\n\n/**\n * Slice that handles the visual editor state and actions for Panels (i.e. add, edit, delete).\n */\nexport interface PanelEditorSlice {\n // TODO: Move panels state to its own slice so that other slices can depend on it (and modify the state)\n panels: Record<string, PanelDefinition>;\n previousPanels: Record<string, PanelDefinition>;\n\n /**\n * State for the panel editor when its open, otherwise undefined when it's closed.\n */\n panelEditor?: PanelEditorState;\n\n /**\n * Opens the editor for editing an existing panel by providing its layout coordinates.\n */\n openEditPanel: (item: PanelGroupItemId) => void;\n\n /**\n * Opens the editor for adding a new Panel to a panel group.\n */\n openAddPanel: (panelGroupId?: PanelGroupId) => void;\n\n /**\n * Delete panels\n */\n deletePanels: (panels: PanelGroupItemId[]) => void;\n\n /**\n * Reset panels to previous state\n */\n resetPanels: () => void;\n\n /**\n * Save panels\n */\n savePanels: () => void;\n\n /**\n * State for the delete panel dialog when it's open, otherwise undefined when it's closed.\n */\n deletePanelDialog?: DeletePanelDialog;\n\n /**\n * Open delete panel dialog\n */\n openDeletePanelDialog: (item: PanelGroupItemId) => void;\n\n /**\n * Close delete panel dialog\n */\n closeDeletePanelDialog: () => void;\n}\n\nexport interface DeletePanelDialog {\n panelGroupItemId: PanelGroupItemId;\n panelName: string;\n panelGroupName: string;\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 groupId: PanelGroupId;\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 & PanelGroupSlice, Middleware, [], PanelEditorSlice> {\n // Return the state creator function for Zustand that uses the panels provided as intitial state\n return (set, get) => ({\n previousPanels: panels,\n panels,\n\n panelEditor: undefined,\n\n savePanels() {\n set((state) => {\n state.previousPanels = state.panels;\n });\n },\n\n resetPanels() {\n set((state) => {\n state.panels = state.previousPanels;\n });\n },\n\n openEditPanel(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 groupId: item.panelGroupId,\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.groupId !== item.panelGroupId) {\n get().movePanelToGroup(item, next.groupId);\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 openAddPanel(panelGroupId) {\n // If a panel group isn't supplied, add to the first group\n if (panelGroupId === undefined) {\n const firstGroupId = get().panelGroupIdOrder[0];\n if (firstGroupId === undefined) {\n throw new Error('No panel groups to add a panel to');\n }\n panelGroupId = firstGroupId;\n }\n\n const editorState: PanelEditorState = {\n mode: 'Add',\n initialValues: {\n name: '',\n description: '',\n groupId: panelGroupId,\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.groupId);\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: PanelGroupItemId[]) {\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: PanelGroupItemId) {\n const { panels, getPanelKey, panelGroups } = get();\n const panelKey = getPanelKey(item);\n set((state) => {\n state.deletePanelDialog = {\n panelGroupItemId: item,\n panelName: panels[panelKey]?.spec.display.name ?? '',\n panelGroupName: panelGroups[item.panelGroupId]?.title ?? '',\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","previousPanels","panelEditor","undefined","savePanels","state","resetPanels","openEditPanel","item","getPanelKey","panelKey","panelToEdit","Error","editorState","mode","initialValues","name","spec","display","description","groupId","panelGroupId","kind","plugin","applyChanges","next","panelDefinititon","createPanelDefinitionFromEditorValues","movePanelToGroup","close","openAddPanel","firstGroupId","panelGroupIdOrder","panelDef","addPanelToGroup","deletePanels","items","mapPanelToPanelGroups","deletePanelInPanelGroup","map","panel","forEach","length","openDeletePanelDialog","panelGroups","deletePanelDialog","panelGroupItemId","panelName","panelGroupName","title","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;AAiG9E;;CAEC,GACD,OAAO,SAASC,sBAAsB,CACpCC,MAAkC,EACkD;IACpF,gGAAgG;IAChG,OAAO,C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BuB,IAA8B,WAAO,GAArCA,KAAAA,CAAqC,GAArCA,IAA8B,CAAEK,KAAK,cAArCL,IAAqC,cAArCA,IAAqC,GAAI,EAAE;qBAC5D,CAAC;gBACJ,CAAC,CAAC,CAAC;YACL,CAAC;YAEDM,sBAAsB,IAAG;gBACvBnD,GAAG,CAAC,CAACM,KAAK,GAAK;oBACbA,KAAK,CAACwC,iBAAiB,GAAG1C,SAAS,CAAC;gBACtC,CAAC,CAAC,CAAC;YACL,CAAC;SACF,CAAA;KAAC,CAAC;AACL,CAAC;AAED,gDAAgD;AAChD,SAASwB,qCAAqC,CAACwB,YAA+B,EAAmB;IAC/F,OAAO;QACL7B,IAAI,EAAE,OAAO;QACbL,IAAI,EAAE;YACJC,OAAO,EAAE;gBACPF,IAAI,EAAEmC,YAAY,CAACnC,IAAI;gBACvBG,WAAW,EAAEgC,YAAY,CAAChC,WAAW,KAAK,EAAE,GAAGgC,YAAY,CAAChC,WAAW,GAAGhB,SAAS;aACpF;YACDoB,MAAM,EAAE;gBACND,IAAI,EAAE6B,YAAY,CAAC7B,IAAI;gBACvBL,IAAI,EAAEkC,YAAY,CAAClC,IAAI;aACxB;SACF;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,40 @@
1
+ import { StateCreator } from 'zustand';
2
+ import { Middleware } from './common';
3
+ import { PanelGroupSlice, PanelGroupId } from './panel-group-slice';
4
+ export interface PanelGroupEditor {
5
+ mode: 'Add' | 'Edit';
6
+ initialValues: PanelGroupEditorValues;
7
+ applyChanges: (next: PanelGroupEditorValues) => void;
8
+ close: () => void;
9
+ }
10
+ export interface PanelGroupEditorValues {
11
+ title: string;
12
+ isCollapsed: boolean;
13
+ }
14
+ export interface DeletePanelGroupDialog {
15
+ panelGroupId: PanelGroupId;
16
+ panelGroupName?: string;
17
+ }
18
+ /**
19
+ * Slice that handles the visual editor state and actions for Panel Groups (i.e. add, edit, delete).
20
+ */
21
+ export interface PanelGroupEditorSlice {
22
+ /**
23
+ * State that's present when the panel group editor is open.
24
+ */
25
+ panelGroupEditor?: PanelGroupEditor;
26
+ /**
27
+ * Opens the panel group editor to add a new panel group.
28
+ */
29
+ openAddPanelGroup: () => void;
30
+ /**
31
+ * Opens the panel group editor to edit an existing panel group.
32
+ */
33
+ openEditPanelGroup: (panelGroupId: PanelGroupId) => void;
34
+ deletePanelGroupDialog?: DeletePanelGroupDialog;
35
+ openDeletePanelGroupDialog: (panelGroupId: PanelGroupId) => void;
36
+ closeDeletePanelGroupDialog: () => void;
37
+ }
38
+ export declare const createPanelGroupEditorSlice: StateCreator<PanelGroupEditorSlice & PanelGroupSlice, Middleware, [
39
+ ], PanelGroupEditorSlice>;
40
+ //# sourceMappingURL=panel-group-editor-slice.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"panel-group-editor-slice.d.ts","sourceRoot":"","sources":["../../../src/context/DashboardProvider/panel-group-editor-slice.tsx"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,EAAE,eAAe,EAAwB,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAE1F,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,KAAK,GAAG,MAAM,CAAC;IACrB,aAAa,EAAE,sBAAsB,CAAC;IACtC,YAAY,EAAE,CAAC,IAAI,EAAE,sBAAsB,KAAK,IAAI,CAAC;IACrD,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB;AAED,MAAM,WAAW,sBAAsB;IACrC,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,sBAAsB;IACrC,YAAY,EAAE,YAAY,CAAC;IAC3B,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,gBAAgB,CAAC,EAAE,gBAAgB,CAAC;IAEpC;;OAEG;IACH,iBAAiB,EAAE,MAAM,IAAI,CAAC;IAE9B;;OAEG;IACH,kBAAkB,EAAE,CAAC,YAAY,EAAE,YAAY,KAAK,IAAI,CAAC;IAEzD,sBAAsB,CAAC,EAAE,sBAAsB,CAAC;IAChD,0BAA0B,EAAE,CAAC,YAAY,EAAE,YAAY,KAAK,IAAI,CAAC;IACjE,2BAA2B,EAAE,MAAM,IAAI,CAAC;CACzC;AAED,eAAO,MAAM,2BAA2B,EAAE,YAAY,CACpD,qBAAqB,GAAG,eAAe,EACvC,UAAU,EACV;CAAE,EACF,qBAAqB,CAqFrB,CAAC"}
@@ -0,0 +1,96 @@
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 createPanelGroupEditorSlice = (set, get)=>({
14
+ panelGroupEditor: undefined,
15
+ openAddPanelGroup: ()=>{
16
+ // Create the editor state
17
+ const editor = {
18
+ mode: 'Add',
19
+ initialValues: {
20
+ title: '',
21
+ isCollapsed: false
22
+ },
23
+ applyChanges (next) {
24
+ const newGroup = {
25
+ id: get().createPanelGroupId(),
26
+ items: [],
27
+ ...next
28
+ };
29
+ set((draft)=>{
30
+ draft.panelGroups[newGroup.id] = newGroup;
31
+ draft.panelGroupIdOrder.unshift(newGroup.id);
32
+ });
33
+ },
34
+ close () {
35
+ set((draft)=>{
36
+ draft.panelGroupEditor = undefined;
37
+ });
38
+ }
39
+ };
40
+ // Open the editor
41
+ set((draft)=>{
42
+ draft.panelGroupEditor = editor;
43
+ });
44
+ },
45
+ openEditPanelGroup: (panelGroupId)=>{
46
+ const existingGroup = get().panelGroups[panelGroupId];
47
+ if (existingGroup === undefined) {
48
+ throw new Error(`Panel group with Id ${panelGroupId} does not exist`);
49
+ }
50
+ var _title;
51
+ // Create the editor state
52
+ const editor = {
53
+ mode: 'Edit',
54
+ initialValues: {
55
+ title: (_title = existingGroup.title) !== null && _title !== void 0 ? _title : '',
56
+ isCollapsed: existingGroup.isCollapsed
57
+ },
58
+ applyChanges (next) {
59
+ set((draft)=>{
60
+ const group = draft.panelGroups[panelGroupId];
61
+ if (group === undefined) {
62
+ throw new Error(`Panel group with Id ${panelGroupId} does not exist`);
63
+ }
64
+ group.title = next.title;
65
+ group.isCollapsed = next.isCollapsed;
66
+ });
67
+ },
68
+ close () {
69
+ set((draft)=>{
70
+ draft.panelGroupEditor = undefined;
71
+ });
72
+ }
73
+ };
74
+ // Open the editor
75
+ set((draft)=>{
76
+ draft.panelGroupEditor = editor;
77
+ });
78
+ },
79
+ openDeletePanelGroupDialog: (panelGroupId)=>{
80
+ const panelGroup = get().panelGroups[panelGroupId];
81
+ if (panelGroup === undefined) {
82
+ throw new Error(`Panel group with Id ${panelGroupId} not found`);
83
+ }
84
+ set((state)=>{
85
+ state.deletePanelGroupDialog = {
86
+ panelGroupId,
87
+ panelGroupName: panelGroup.title
88
+ };
89
+ });
90
+ },
91
+ closeDeletePanelGroupDialog: ()=>set((state)=>{
92
+ state.deletePanelGroupDialog = undefined;
93
+ })
94
+ });
95
+
96
+ //# sourceMappingURL=panel-group-editor-slice.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/context/DashboardProvider/panel-group-editor-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';\nimport { PanelGroupSlice, PanelGroupDefinition, PanelGroupId } from './panel-group-slice';\n\nexport interface PanelGroupEditor {\n mode: 'Add' | 'Edit';\n initialValues: PanelGroupEditorValues;\n applyChanges: (next: PanelGroupEditorValues) => void;\n close: () => void;\n}\n\nexport interface PanelGroupEditorValues {\n title: string;\n isCollapsed: boolean;\n}\n\nexport interface DeletePanelGroupDialog {\n panelGroupId: PanelGroupId;\n panelGroupName?: string;\n}\n\n/**\n * Slice that handles the visual editor state and actions for Panel Groups (i.e. add, edit, delete).\n */\nexport interface PanelGroupEditorSlice {\n /**\n * State that's present when the panel group editor is open.\n */\n panelGroupEditor?: PanelGroupEditor;\n\n /**\n * Opens the panel group editor to add a new panel group.\n */\n openAddPanelGroup: () => void;\n\n /**\n * Opens the panel group editor to edit an existing panel group.\n */\n openEditPanelGroup: (panelGroupId: PanelGroupId) => void;\n\n deletePanelGroupDialog?: DeletePanelGroupDialog;\n openDeletePanelGroupDialog: (panelGroupId: PanelGroupId) => void;\n closeDeletePanelGroupDialog: () => void;\n}\n\nexport const createPanelGroupEditorSlice: StateCreator<\n PanelGroupEditorSlice & PanelGroupSlice,\n Middleware,\n [],\n PanelGroupEditorSlice\n> = (set, get) => ({\n panelGroupEditor: undefined,\n\n openAddPanelGroup: () => {\n // Create the editor state\n const editor: PanelGroupEditor = {\n mode: 'Add',\n initialValues: {\n title: '',\n isCollapsed: false,\n },\n applyChanges(next) {\n const newGroup: PanelGroupDefinition = {\n id: get().createPanelGroupId(),\n items: [],\n ...next,\n };\n set((draft) => {\n draft.panelGroups[newGroup.id] = newGroup;\n draft.panelGroupIdOrder.unshift(newGroup.id);\n });\n },\n close() {\n set((draft) => {\n draft.panelGroupEditor = undefined;\n });\n },\n };\n\n // Open the editor\n set((draft) => {\n draft.panelGroupEditor = editor;\n });\n },\n\n openEditPanelGroup: (panelGroupId) => {\n const existingGroup = get().panelGroups[panelGroupId];\n if (existingGroup === undefined) {\n throw new Error(`Panel group with Id ${panelGroupId} does not exist`);\n }\n\n // Create the editor state\n const editor: PanelGroupEditor = {\n mode: 'Edit',\n initialValues: {\n title: existingGroup.title ?? '',\n isCollapsed: existingGroup.isCollapsed,\n },\n applyChanges(next) {\n set((draft) => {\n const group = draft.panelGroups[panelGroupId];\n if (group === undefined) {\n throw new Error(`Panel group with Id ${panelGroupId} does not exist`);\n }\n group.title = next.title;\n group.isCollapsed = next.isCollapsed;\n });\n },\n close() {\n set((draft) => {\n draft.panelGroupEditor = undefined;\n });\n },\n };\n\n // Open the editor\n set((draft) => {\n draft.panelGroupEditor = editor;\n });\n },\n\n openDeletePanelGroupDialog: (panelGroupId) => {\n const panelGroup = get().panelGroups[panelGroupId];\n if (panelGroup === undefined) {\n throw new Error(`Panel group with Id ${panelGroupId} not found`);\n }\n set((state) => {\n state.deletePanelGroupDialog = { panelGroupId, panelGroupName: panelGroup.title };\n });\n },\n closeDeletePanelGroupDialog: () =>\n set((state) => {\n state.deletePanelGroupDialog = undefined;\n }),\n});\n"],"names":["createPanelGroupEditorSlice","set","get","panelGroupEditor","undefined","openAddPanelGroup","editor","mode","initialValues","title","isCollapsed","applyChanges","next","newGroup","id","createPanelGroupId","items","draft","panelGroups","panelGroupIdOrder","unshift","close","openEditPanelGroup","panelGroupId","existingGroup","Error","group","openDeletePanelGroupDialog","panelGroup","state","deletePanelGroupDialog","panelGroupName","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;AA+CjC,OAAO,MAAMA,2BAA2B,GAKpC,CAACC,GAAG,EAAEC,GAAG,GAAM,CAAA;QACjBC,gBAAgB,EAAEC,SAAS;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oBAAEP,YAAY;oBAAEQ,cAAc,EAAEH,UAAU,CAACnB,KAAK;iBAAE,CAAC;YACpF,CAAC,CAAC,CAAC;QACL,CAAC;QACDuB,2BAA2B,EAAE,IAC3B/B,GAAG,CAAC,CAAC4B,KAAK,GAAK;gBACbA,KAAK,CAACC,sBAAsB,GAAG1B,SAAS,CAAC;YAC3C,CAAC,CAAC;KACL,CAAA,AAAC,CAAC"}
@@ -0,0 +1,80 @@
1
+ import { GridItemDefinition, LayoutDefinition } from '@perses-dev/core';
2
+ import { StateCreator } from 'zustand';
3
+ import { Middleware } from './common';
4
+ import { PanelEditorSlice } from './panel-editor-slice';
5
+ /**
6
+ * Slice with the state of Panel Groups, as well as any actions that modify only Panel Group state.
7
+ */
8
+ export interface PanelGroupSlice {
9
+ /**
10
+ * Panel groups indexed by their ID.
11
+ */
12
+ panelGroups: Record<PanelGroupId, PanelGroupDefinition>;
13
+ /**
14
+ * An array of panel group IDs, representing their order in the dashboard.
15
+ */
16
+ panelGroupIdOrder: PanelGroupId[];
17
+ /**
18
+ * previous state
19
+ */
20
+ previousPanelGroupStates: {
21
+ panelGroups: PanelGroupSlice['panelGroups'];
22
+ panelGroupIdOrder: PanelGroupSlice['panelGroupIdOrder'];
23
+ };
24
+ createPanelGroupId: () => PanelGroupId;
25
+ /**
26
+ * Given a LayoutItem location, returns the panel's unique key at that location.
27
+ */
28
+ getPanelKey: (layoutItem: PanelGroupItemId) => string;
29
+ /**
30
+ * Add a panel with the specified key to an existing group.
31
+ */
32
+ addPanelToGroup: (panelKey: string, panelGroupId: PanelGroupId) => void;
33
+ /**
34
+ * Move an existing Panel to a new panel group.
35
+ */
36
+ movePanelToGroup: (layoutItem: PanelGroupItemId, newPanelGroupId: PanelGroupId) => void;
37
+ /**
38
+ * Rearrange the order of panel groups by swapping the positions
39
+ */
40
+ swapPanelGroups: (xIndex: number, yIndex: number) => void;
41
+ /**
42
+ * Delete panel group and all the panels within the group
43
+ */
44
+ deletePanelGroup: (panelGroupId: PanelGroupId) => void;
45
+ /**
46
+ * Delete panel in panel group
47
+ */
48
+ deletePanelInPanelGroup: (layoutItem: PanelGroupItemId) => void;
49
+ /**
50
+ * Map panel to panel groups
51
+ */
52
+ mapPanelToPanelGroups: () => Record<string, PanelGroupId[]>;
53
+ /**
54
+ * save
55
+ */
56
+ savePanelGroups: () => void;
57
+ /**
58
+ * reset to previous panel group states
59
+ */
60
+ resetPanelGroups: () => void;
61
+ }
62
+ export declare type PanelGroupId = number;
63
+ export interface PanelGroupDefinition {
64
+ id: PanelGroupId;
65
+ items: GridItemDefinition[];
66
+ isCollapsed: boolean;
67
+ title?: string;
68
+ }
69
+ /**
70
+ * Uniquely identifies an item in a PanelGroup.
71
+ */
72
+ export interface PanelGroupItemId {
73
+ panelGroupId: PanelGroupId;
74
+ itemIndex: number;
75
+ }
76
+ /**
77
+ * Curried function for creating a PanelGroupSlice.
78
+ */
79
+ export declare function createPanelGroupSlice(layouts: LayoutDefinition[]): StateCreator<PanelGroupSlice & PanelEditorSlice, Middleware, [], PanelGroupSlice>;
80
+ //# 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,EAAsC,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAC5G,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAExD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC,YAAY,EAAE,oBAAoB,CAAC,CAAC;IAExD;;OAEG;IACH,iBAAiB,EAAE,YAAY,EAAE,CAAC;IAElC;;OAEG;IACH,wBAAwB,EAAE;QACxB,WAAW,E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EAAE,GAC1B,YAAY,CAAC,eAAe,GAAG,gBAAgB,EAAE,UAAU,EAAE,EAAE,EAAE,eAAe,CAAC,CAkKnF"}
@@ -0,0 +1,204 @@
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 { createPanelRef, getPanelKeyFromRef } from '@perses-dev/core';
14
+ /**
15
+ * Curried function for creating a PanelGroupSlice.
16
+ */ export function createPanelGroupSlice(layouts) {
17
+ // Helper function for generating unique IDs for a PanelGroup
18
+ let id = -1;
19
+ function createPanelGroupId() {
20
+ id++;
21
+ return id;
22
+ }
23
+ // Convert the initial layouts from the JSON to panel groups and keep track of the order
24
+ const panelGroups = {};
25
+ const panelGroupIdOrder = [];
26
+ for (const layout of layouts){
27
+ var ref, ref1, ref2;
28
+ const id1 = createPanelGroupId();
29
+ panelGroups[id1] = {
30
+ id: id1,
31
+ items: layout.spec.items,
32
+ isCollapsed: ((ref = layout.spec.display) === null || ref === void 0 ? void 0 : (ref1 = ref.collapse) === null || ref1 === void 0 ? void 0 : ref1.open) === false,
33
+ title: (ref2 = layout.spec.display) === null || ref2 === void 0 ? void 0 : ref2.title
34
+ };
35
+ panelGroupIdOrder.push(id1);
36
+ }
37
+ // Return the state creator function for Zustand
38
+ return (set, get)=>{
39
+ return {
40
+ panelGroups,
41
+ panelGroupIdOrder,
42
+ previousPanelGroupStates: {
43
+ panelGroups,
44
+ panelGroupIdOrder
45
+ },
46
+ // TODO: Reorder init logic so this isn't exposed
47
+ createPanelGroupId,
48
+ savePanelGroups () {
49
+ set((state)=>{
50
+ state.previousPanelGroupStates = {
51
+ panelGroups: state.panelGroups,
52
+ panelGroupIdOrder: state.panelGroupIdOrder
53
+ };
54
+ });
55
+ },
56
+ resetPanelGroups () {
57
+ set((state)=>{
58
+ state.panelGroups = state.previousPanelGroupStates.panelGroups;
59
+ state.panelGroupIdOrder = state.previousPanelGroupStates.panelGroupIdOrder;
60
+ });
61
+ },
62
+ getPanelKey ({ panelGroupId , itemIndex }) {
63
+ const { panelGroups } = get();
64
+ const group = findGroup(panelGroups, panelGroupId);
65
+ const item = findItem(group, itemIndex);
66
+ return getPanelKeyFromRef(item.content);
67
+ },
68
+ addPanelToGroup (panelKey, panelGroupId) {
69
+ const { panelGroups } = get();
70
+ const group = findGroup(panelGroups, panelGroupId);
71
+ const gridItem = {
72
+ x: 0,
73
+ y: getYForNewRow(group),
74
+ width: 12,
75
+ height: 6,
76
+ content: createPanelRef(panelKey)
77
+ };
78
+ set((state)=>{
79
+ var ref;
80
+ (ref = state.panelGroups[panelGroupId]) === null || ref === void 0 ? void 0 : ref.items.push(gridItem);
81
+ });
82
+ },
83
+ movePanelToGroup ({ panelGroupId , itemIndex }, newPanelGroupId) {
84
+ const { panelGroups } = get();
85
+ // Find the existing item to make sure it exists
86
+ const group = findGroup(panelGroups, panelGroupId);
87
+ const item = findItem(group, itemIndex);
88
+ // Find the new group and figure out where a new row should go
89
+ const newGroup = findGroup(panelGroups, newPanelGroupId);
90
+ const newGroupY = getYForNewRow(newGroup);
91
+ set((state)=>{
92
+ var // Remove the item from its current group
93
+ ref, // Add a new item to the new group
94
+ ref1;
95
+ (ref = state.panelGroups[panelGroupId]) === null || ref === void 0 ? void 0 : ref.items.splice(itemIndex, 1);
96
+ (ref1 = state.panelGroups[newPanelGroupId]) === null || ref1 === void 0 ? void 0 : ref1.items.push({
97
+ x: 0,
98
+ y: newGroupY,
99
+ width: item.width,
100
+ height: item.height,
101
+ content: item.content
102
+ });
103
+ });
104
+ },
105
+ swapPanelGroups (x, y) {
106
+ set((state)=>{
107
+ if (x < 0 || x >= state.panelGroupIdOrder.length || y < 0 || y >= state.panelGroupIdOrder.length) {
108
+ throw new Error('index out of bound');
109
+ }
110
+ const xPanelGroup = state.panelGroupIdOrder[x];
111
+ const yPanelGroup = state.panelGroupIdOrder[y];
112
+ if (xPanelGroup === undefined || yPanelGroup === undefined) {
113
+ throw new Error('panel group is undefined');
114
+ }
115
+ // assign yPanelGroup to layouts[x] and assign xGroup to layouts[y], swapping two panel groups
116
+ [state.panelGroupIdOrder[x], state.panelGroupIdOrder[y]] = [
117
+ yPanelGroup,
118
+ xPanelGroup
119
+ ];
120
+ });
121
+ },
122
+ deletePanelInPanelGroup ({ panelGroupId , itemIndex }) {
123
+ set((state)=>{
124
+ const group = state.panelGroups[panelGroupId];
125
+ if (group === undefined) {
126
+ throw new Error(`No panel group found: ${panelGroupId}`);
127
+ }
128
+ // remove panel from panel group
129
+ group.items.splice(itemIndex, 1);
130
+ });
131
+ },
132
+ deletePanelGroup (panelGroupId) {
133
+ const { panelGroups , panelGroupIdOrder: panelGroupOrder , deletePanels } = get();
134
+ const group = findGroup(panelGroups, panelGroupId);
135
+ const orderIdx = panelGroupOrder.findIndex((id)=>id === panelGroupId);
136
+ if (orderIdx === -1) {
137
+ throw new Error(`Could not find panel group Id ${panelGroupId} in order array`);
138
+ }
139
+ // remove panels from group first
140
+ const panelsToBeDeleted = [];
141
+ for(let i = 0; i < group.items.length; i++){
142
+ panelsToBeDeleted.push({
143
+ panelGroupId,
144
+ itemIndex: i
145
+ });
146
+ }
147
+ deletePanels(panelsToBeDeleted);
148
+ // remove group from both panelGroups and panelGroupOrder
149
+ set((state)=>{
150
+ state.panelGroupIdOrder.splice(orderIdx, 1);
151
+ delete state.panelGroups[panelGroupId];
152
+ });
153
+ },
154
+ // Return an object that maps each panel to the groups it belongs
155
+ mapPanelToPanelGroups () {
156
+ const map = {}; // { panel key: [group ids] }
157
+ Object.values(get().panelGroups).forEach((group)=>{
158
+ // for each panel in a group, add the group id to map[panelKey]
159
+ group.items.forEach((panel)=>{
160
+ const panelKey = getPanelKeyFromRef(panel.content);
161
+ if (map[panelKey]) {
162
+ var ref;
163
+ (ref = map[panelKey]) === null || ref === void 0 ? void 0 : ref.push(group.id);
164
+ } else {
165
+ map[panelKey] = [
166
+ group.id
167
+ ];
168
+ }
169
+ });
170
+ });
171
+ return map;
172
+ }
173
+ };
174
+ };
175
+ }
176
+ // Helper to find a group and throw if not found
177
+ function findGroup(panelGroups, groupId) {
178
+ const group = panelGroups[groupId];
179
+ if (group === undefined) {
180
+ throw new Error(`No panel group found for Id ${groupId}`);
181
+ }
182
+ return group;
183
+ }
184
+ // Helper to get an item in a group and throw if not found
185
+ function findItem(group, itemIndex) {
186
+ const item = group.items[itemIndex];
187
+ if (item === undefined) {
188
+ throw new Error(`No grid item found at position ${itemIndex}`);
189
+ }
190
+ return item;
191
+ }
192
+ // Given a Grid, will find the Y coordinate for adding a new row to the grid, taking into account the items present
193
+ function getYForNewRow(group) {
194
+ let newRowY = 0;
195
+ for (const item of group.items){
196
+ const itemMaxY = item.y + item.height;
197
+ if (itemMaxY > newRowY) {
198
+ newRowY = itemMaxY;
199
+ }
200
+ }
201
+ return newRowY;
202
+ }
203
+
204
+ //# sourceMappingURL=panel-group-slice.js.map