@perses-dev/dashboards 0.7.0 → 0.8.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 (161) hide show
  1. package/dist/cjs/components/Dashboard.js +1 -1
  2. package/dist/cjs/components/DashboardToolbar.js +3 -18
  3. package/dist/cjs/components/GridLayout/GridItemContent.js +3 -3
  4. package/dist/cjs/components/GridLayout/GridLayout.js +8 -9
  5. package/dist/cjs/components/GridLayout/GridTitle.js +15 -9
  6. package/dist/cjs/components/Panel/Panel.js +11 -6
  7. package/dist/cjs/components/Panel/Panel.test.js +17 -13
  8. package/dist/cjs/components/Panel/PanelContent.js +15 -0
  9. package/dist/cjs/components/Panel/index.js +29 -0
  10. package/dist/cjs/components/PanelDrawer/PanelDrawer.js +124 -0
  11. package/dist/cjs/components/PanelDrawer/PanelDrawer.test.js +111 -0
  12. package/dist/cjs/components/PanelDrawer/PanelOptionsEditor.js +19 -0
  13. package/dist/cjs/components/PanelGroupDialog/PanelGroupDialog.js +60 -0
  14. package/dist/cjs/components/PanelGroupDialog/PanelGroupDialog.test.js +95 -0
  15. package/dist/cjs/components/{TimeRangeControls.js → TimeRangeControls/TimeRangeControls.js} +20 -31
  16. package/dist/cjs/components/TimeRangeControls/TimeRangeControls.test.js +51 -0
  17. package/dist/cjs/components/TimeRangeControls/index.js +29 -0
  18. package/dist/cjs/components/Variables/Variable.js +57 -0
  19. package/dist/cjs/components/Variables/VariableList.js +27 -0
  20. package/dist/cjs/components/{VariableList → Variables}/index.js +1 -0
  21. package/dist/cjs/components/index.js +2 -3
  22. package/dist/cjs/context/DashboardAppSlice.js +45 -0
  23. package/dist/cjs/context/DashboardProvider.js +54 -36
  24. package/dist/cjs/context/LayoutsSlice.js +42 -0
  25. package/dist/cjs/context/QueryStringProvider.js +35 -0
  26. package/dist/cjs/context/TemplateVariableProvider.js +216 -0
  27. package/dist/cjs/context/TimeRangeProvider.js +66 -0
  28. package/dist/cjs/context/index.js +6 -3
  29. package/dist/cjs/css/styles.js +188 -0
  30. package/dist/cjs/test/plugin-registry.js +24 -17
  31. package/dist/cjs/test/render.js +11 -2
  32. package/dist/cjs/test/testDashboard.js +14 -37
  33. package/dist/cjs/views/DashboardApp.js +4 -4
  34. package/dist/cjs/views/ViewDashboard.js +30 -2
  35. package/dist/cjs/views/index.js +1 -1
  36. package/dist/components/Dashboard.d.ts.map +1 -1
  37. package/dist/components/Dashboard.js +1 -1
  38. package/dist/components/DashboardToolbar.d.ts +0 -1
  39. package/dist/components/DashboardToolbar.d.ts.map +1 -1
  40. package/dist/components/DashboardToolbar.js +1 -1
  41. package/dist/components/GridLayout/GridItemContent.d.ts +1 -0
  42. package/dist/components/GridLayout/GridItemContent.d.ts.map +1 -1
  43. package/dist/components/GridLayout/GridItemContent.js +1 -1
  44. package/dist/components/GridLayout/GridLayout.d.ts +2 -3
  45. package/dist/components/GridLayout/GridLayout.d.ts.map +1 -1
  46. package/dist/components/GridLayout/GridLayout.js +1 -1
  47. package/dist/components/GridLayout/GridTitle.d.ts +1 -0
  48. package/dist/components/GridLayout/GridTitle.d.ts.map +1 -1
  49. package/dist/components/GridLayout/GridTitle.js +1 -1
  50. package/dist/components/Panel/Panel.d.ts +2 -0
  51. package/dist/components/Panel/Panel.d.ts.map +1 -1
  52. package/dist/components/Panel/Panel.js +1 -1
  53. package/dist/components/Panel/Panel.test.d.ts.map +1 -1
  54. package/dist/components/Panel/Panel.test.js +1 -1
  55. package/dist/components/Panel/PanelContent.d.ts +11 -0
  56. package/dist/components/Panel/PanelContent.d.ts.map +1 -0
  57. package/dist/components/Panel/PanelContent.js +1 -0
  58. package/dist/components/Panel/index.d.ts +2 -0
  59. package/dist/components/Panel/index.d.ts.map +1 -0
  60. package/dist/components/Panel/index.js +1 -0
  61. package/dist/components/PanelDrawer/PanelDrawer.d.ts +4 -0
  62. package/dist/components/PanelDrawer/PanelDrawer.d.ts.map +1 -0
  63. package/dist/components/PanelDrawer/PanelDrawer.js +1 -0
  64. package/dist/components/PanelDrawer/PanelDrawer.test.d.ts +2 -0
  65. package/dist/components/PanelDrawer/PanelDrawer.test.d.ts.map +1 -0
  66. package/dist/components/PanelDrawer/PanelDrawer.test.js +1 -0
  67. package/dist/components/PanelDrawer/PanelOptionsEditor.d.ts +9 -0
  68. package/dist/components/PanelDrawer/PanelOptionsEditor.d.ts.map +1 -0
  69. package/dist/components/PanelDrawer/PanelOptionsEditor.js +1 -0
  70. package/dist/components/PanelGroupDialog/PanelGroupDialog.d.ts +4 -0
  71. package/dist/components/PanelGroupDialog/PanelGroupDialog.d.ts.map +1 -0
  72. package/dist/components/PanelGroupDialog/PanelGroupDialog.js +1 -0
  73. package/dist/components/PanelGroupDialog/PanelGroupDialog.test.d.ts +2 -0
  74. package/dist/components/PanelGroupDialog/PanelGroupDialog.test.d.ts.map +1 -0
  75. package/dist/components/PanelGroupDialog/PanelGroupDialog.test.js +1 -0
  76. package/dist/components/{TimeRangeControls.d.ts → TimeRangeControls/TimeRangeControls.d.ts} +0 -0
  77. package/dist/components/TimeRangeControls/TimeRangeControls.d.ts.map +1 -0
  78. package/dist/components/TimeRangeControls/TimeRangeControls.js +1 -0
  79. package/dist/components/TimeRangeControls/TimeRangeControls.test.d.ts +2 -0
  80. package/dist/components/TimeRangeControls/TimeRangeControls.test.d.ts.map +1 -0
  81. package/dist/components/TimeRangeControls/TimeRangeControls.test.js +1 -0
  82. package/dist/components/TimeRangeControls/index.d.ts +2 -0
  83. package/dist/components/TimeRangeControls/index.d.ts.map +1 -0
  84. package/dist/components/TimeRangeControls/index.js +1 -0
  85. package/dist/components/Variables/Variable.d.ts +8 -0
  86. package/dist/components/Variables/Variable.d.ts.map +1 -0
  87. package/dist/components/Variables/Variable.js +1 -0
  88. package/dist/components/Variables/VariableList.d.ts +3 -0
  89. package/dist/components/Variables/VariableList.d.ts.map +1 -0
  90. package/dist/components/Variables/VariableList.js +1 -0
  91. package/dist/components/Variables/index.d.ts +3 -0
  92. package/dist/components/Variables/index.d.ts.map +1 -0
  93. package/dist/components/Variables/index.js +1 -0
  94. package/dist/components/index.d.ts +2 -3
  95. package/dist/components/index.d.ts.map +1 -1
  96. package/dist/components/index.js +1 -1
  97. package/dist/context/DashboardAppSlice.d.ts +26 -0
  98. package/dist/context/DashboardAppSlice.d.ts.map +1 -0
  99. package/dist/context/DashboardAppSlice.js +1 -0
  100. package/dist/context/DashboardProvider.d.ts +8 -19
  101. package/dist/context/DashboardProvider.d.ts.map +1 -1
  102. package/dist/context/DashboardProvider.js +1 -1
  103. package/dist/context/LayoutsSlice.d.ts +12 -0
  104. package/dist/context/LayoutsSlice.d.ts.map +1 -0
  105. package/dist/context/LayoutsSlice.js +1 -0
  106. package/dist/context/QueryStringProvider.d.ts +13 -0
  107. package/dist/context/QueryStringProvider.d.ts.map +1 -0
  108. package/dist/context/QueryStringProvider.js +1 -0
  109. package/dist/context/TemplateVariableProvider.d.ts +25 -0
  110. package/dist/context/TemplateVariableProvider.d.ts.map +1 -0
  111. package/dist/context/TemplateVariableProvider.js +1 -0
  112. package/dist/context/TimeRangeProvider.d.ts +12 -0
  113. package/dist/context/TimeRangeProvider.d.ts.map +1 -0
  114. package/dist/context/TimeRangeProvider.js +1 -0
  115. package/dist/context/index.d.ts +5 -2
  116. package/dist/context/index.d.ts.map +1 -1
  117. package/dist/context/index.js +1 -1
  118. package/dist/css/styles.d.ts +172 -0
  119. package/dist/css/styles.d.ts.map +1 -0
  120. package/dist/css/styles.js +1 -0
  121. package/dist/test/plugin-registry.d.ts +4 -2
  122. package/dist/test/plugin-registry.d.ts.map +1 -1
  123. package/dist/test/plugin-registry.js +1 -1
  124. package/dist/test/render.d.ts +2 -1
  125. package/dist/test/render.d.ts.map +1 -1
  126. package/dist/test/render.js +1 -1
  127. package/dist/test/testDashboard.d.ts.map +1 -1
  128. package/dist/test/testDashboard.js +1 -1
  129. package/dist/views/DashboardApp.js +1 -1
  130. package/dist/views/ViewDashboard.d.ts.map +1 -1
  131. package/dist/views/ViewDashboard.js +1 -1
  132. package/package.json +9 -11
  133. package/dist/cjs/components/AddPanel/AddPanel.js +0 -75
  134. package/dist/cjs/components/VariableAutocomplete.js +0 -63
  135. package/dist/cjs/components/VariableList/VariableList.js +0 -42
  136. package/dist/cjs/components/VariableList/VariableList.test.js +0 -86
  137. package/dist/cjs/context/TemplateVariablesProvider.js +0 -142
  138. package/dist/cjs/context/TimeRangeStateProvider.js +0 -49
  139. package/dist/components/AddPanel/AddPanel.d.ts +0 -8
  140. package/dist/components/AddPanel/AddPanel.d.ts.map +0 -1
  141. package/dist/components/AddPanel/AddPanel.js +0 -1
  142. package/dist/components/TimeRangeControls.d.ts.map +0 -1
  143. package/dist/components/TimeRangeControls.js +0 -1
  144. package/dist/components/VariableAutocomplete.d.ts +0 -21
  145. package/dist/components/VariableAutocomplete.d.ts.map +0 -1
  146. package/dist/components/VariableAutocomplete.js +0 -1
  147. package/dist/components/VariableList/VariableList.d.ts +0 -11
  148. package/dist/components/VariableList/VariableList.d.ts.map +0 -1
  149. package/dist/components/VariableList/VariableList.js +0 -1
  150. package/dist/components/VariableList/VariableList.test.d.ts +0 -2
  151. package/dist/components/VariableList/VariableList.test.d.ts.map +0 -1
  152. package/dist/components/VariableList/VariableList.test.js +0 -1
  153. package/dist/components/VariableList/index.d.ts +0 -2
  154. package/dist/components/VariableList/index.d.ts.map +0 -1
  155. package/dist/components/VariableList/index.js +0 -1
  156. package/dist/context/TemplateVariablesProvider.d.ts +0 -23
  157. package/dist/context/TemplateVariablesProvider.d.ts.map +0 -1
  158. package/dist/context/TemplateVariablesProvider.js +0 -1
  159. package/dist/context/TimeRangeStateProvider.d.ts +0 -22
  160. package/dist/context/TimeRangeStateProvider.d.ts.map +0 -1
  161. package/dist/context/TimeRangeStateProvider.js +0 -1
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const jsx_runtime_1 = require("react/jsx-runtime");
7
+ // Copyright 2022 The Perses Authors
8
+ // Licensed under the Apache License, Version 2.0 (the "License");
9
+ // you may not use this file except in compliance with the License.
10
+ // You may obtain a copy of the License at
11
+ //
12
+ // http://www.apache.org/licenses/LICENSE-2.0
13
+ //
14
+ // Unless required by applicable law or agreed to in writing, software
15
+ // distributed under the License is distributed on an "AS IS" BASIS,
16
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ // See the License for the specific language governing permissions and
18
+ // limitations under the License.
19
+ const react_1 = require("react");
20
+ const material_1 = require("@mui/material");
21
+ const Close_1 = __importDefault(require("mdi-material-ui/Close"));
22
+ const context_1 = require("../../context");
23
+ const PanelGroupDialog = () => {
24
+ var _a, _b, _c, _d, _e;
25
+ const { layouts, updateLayout } = (0, context_1.useLayouts)();
26
+ const { panelGroupDialog, closePanelGroupDialog } = (0, context_1.useDashboardApp)();
27
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
28
+ const { groupIndex } = panelGroupDialog; // PanelGroupDialog is only mounted if panelGroupDialog is defined
29
+ const isEditingPanelGroup = groupIndex !== undefined;
30
+ const [isCollapsed, setIsCollapsed] = (0, react_1.useState)(isEditingPanelGroup && !((_c = (_b = (_a = layouts[groupIndex]) === null || _a === void 0 ? void 0 : _a.spec.display) === null || _b === void 0 ? void 0 : _b.collapse) === null || _c === void 0 ? void 0 : _c.open));
31
+ const [name, setName] = (0, react_1.useState)(isEditingPanelGroup ? (_e = (_d = layouts[groupIndex]) === null || _d === void 0 ? void 0 : _d.spec.display) === null || _e === void 0 ? void 0 : _e.title : '');
32
+ const handleSubmit = (e) => {
33
+ var _a, _b;
34
+ e.preventDefault();
35
+ const newLayout = {
36
+ kind: 'Grid',
37
+ spec: {
38
+ display: {
39
+ title: name !== null && name !== void 0 ? name : '',
40
+ collapse: {
41
+ open: !isCollapsed,
42
+ },
43
+ },
44
+ items: groupIndex === undefined ? [] : (_b = (_a = layouts[groupIndex]) === null || _a === void 0 ? void 0 : _a.spec.items) !== null && _b !== void 0 ? _b : [],
45
+ },
46
+ };
47
+ updateLayout(newLayout, groupIndex);
48
+ closePanelGroupDialog();
49
+ };
50
+ const handleSelectCollapsedStateChange = (e) => {
51
+ const isCollapsed = e.target.value === 'Close';
52
+ setIsCollapsed(isCollapsed);
53
+ };
54
+ return ((0, jsx_runtime_1.jsxs)(material_1.Dialog, { open: true, children: [(0, jsx_runtime_1.jsx)(material_1.DialogTitle, { children: "Panel Group" }), (0, jsx_runtime_1.jsx)(material_1.IconButton, { "aria-label": "Close", onClick: () => closePanelGroupDialog(), sx: (theme) => ({
55
+ position: 'absolute',
56
+ top: theme.spacing(0.5),
57
+ right: theme.spacing(0.5),
58
+ }), children: (0, jsx_runtime_1.jsx)(Close_1.default, {}) }), (0, jsx_runtime_1.jsxs)("form", { onSubmit: handleSubmit, children: [(0, jsx_runtime_1.jsx)(material_1.DialogContent, { sx: { width: '500px' }, children: (0, jsx_runtime_1.jsxs)(material_1.Stack, { spacing: 2, children: [(0, jsx_runtime_1.jsx)(material_1.FormControl, { children: (0, jsx_runtime_1.jsx)(material_1.TextField, { required: true, label: "Name", variant: "outlined", value: name, onChange: (e) => setName(e.target.value) }) }), (0, jsx_runtime_1.jsxs)(material_1.Box, { sx: { display: 'flex', alignItems: 'center', width: '100%', justifyContent: 'space-between' }, children: [(0, jsx_runtime_1.jsx)(material_1.InputLabel, { children: "Collapse State" }), (0, jsx_runtime_1.jsxs)(material_1.Select, { required: true, displayEmpty: true, labelId: "select-collapse-state", size: "small", value: isCollapsed ? 'Close' : 'Open', onChange: handleSelectCollapsedStateChange, children: [(0, jsx_runtime_1.jsx)(material_1.MenuItem, { value: 'Open', children: "Open" }, 'open'), (0, jsx_runtime_1.jsx)(material_1.MenuItem, { value: 'Close', children: "Close" }, 'close')] })] })] }) }), (0, jsx_runtime_1.jsxs)(material_1.DialogActions, { children: [(0, jsx_runtime_1.jsx)(material_1.Button, { variant: "contained", type: "submit", children: isEditingPanelGroup ? 'Apply' : 'Add' }), (0, jsx_runtime_1.jsx)(material_1.Button, { onClick: () => closePanelGroupDialog(), children: "Cancel" })] })] })] }));
59
+ };
60
+ exports.default = PanelGroupDialog;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const jsx_runtime_1 = require("react/jsx-runtime");
30
+ const react_1 = require("@testing-library/react");
31
+ const user_event_1 = __importDefault(require("@testing-library/user-event"));
32
+ const immer_1 = __importDefault(require("immer"));
33
+ const dashboardAppSlice = __importStar(require("../../context/DashboardAppSlice"));
34
+ const layoutsSlice = __importStar(require("../../context/LayoutsSlice"));
35
+ const test_1 = require("../../test");
36
+ const testDashboard_1 = __importDefault(require("../../test/testDashboard"));
37
+ const PanelGroupDialog_1 = __importDefault(require("./PanelGroupDialog"));
38
+ const dashboardApp = {
39
+ panelDrawer: undefined,
40
+ openPanelDrawer: jest.fn(),
41
+ closePanelDrawer: jest.fn(),
42
+ panelGroupDialog: {
43
+ groupIndex: undefined,
44
+ },
45
+ openPanelGroupDialog: jest.fn(),
46
+ closePanelGroupDialog: jest.fn(),
47
+ };
48
+ const updateLayout = jest.fn();
49
+ jest.spyOn(layoutsSlice, 'useLayouts').mockReturnValue({
50
+ updateLayout,
51
+ addItemToLayout: jest.fn(),
52
+ layouts: testDashboard_1.default.spec.layouts,
53
+ });
54
+ describe('Add Panel Group', () => {
55
+ beforeEach(() => {
56
+ jest.clearAllMocks();
57
+ });
58
+ it('should add new panel group', () => {
59
+ jest.spyOn(dashboardAppSlice, 'useDashboardApp').mockReturnValue(dashboardApp);
60
+ (0, test_1.renderWithContext)((0, jsx_runtime_1.jsx)(PanelGroupDialog_1.default, {}));
61
+ const nameInput = react_1.screen.getByLabelText(/Name/);
62
+ user_event_1.default.type(nameInput, 'New Panel Group');
63
+ user_event_1.default.click(react_1.screen.getByText('Add'));
64
+ expect(updateLayout).toHaveBeenCalledWith({
65
+ kind: 'Grid',
66
+ spec: {
67
+ display: {
68
+ title: 'New Panel Group',
69
+ collapse: {
70
+ open: true,
71
+ },
72
+ },
73
+ items: [],
74
+ },
75
+ }, undefined);
76
+ });
77
+ it('should edit existing panel group', () => {
78
+ jest.spyOn(dashboardAppSlice, 'useDashboardApp').mockReturnValue({
79
+ ...dashboardApp,
80
+ panelGroupDialog: {
81
+ groupIndex: 0,
82
+ },
83
+ });
84
+ (0, test_1.renderWithContext)((0, jsx_runtime_1.jsx)(PanelGroupDialog_1.default, {}));
85
+ const nameInput = react_1.screen.getByLabelText(/Name/);
86
+ user_event_1.default.type(nameInput, 'New Name');
87
+ user_event_1.default.click(react_1.screen.getByText('Apply'));
88
+ expect(updateLayout).toHaveBeenCalledWith((0, immer_1.default)(testDashboard_1.default.spec.layouts[0], (draftState) => {
89
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
90
+ draftState.spec.display.title += 'New Name';
91
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
92
+ draftState.spec.display.collapse = { open: false };
93
+ }), 0);
94
+ });
95
+ });
@@ -16,11 +16,10 @@ const jsx_runtime_1 = require("react/jsx-runtime");
16
16
  // limitations under the License.
17
17
  const react_1 = require("react");
18
18
  const material_1 = require("@mui/material");
19
- const date_fns_1 = require("date-fns");
20
19
  const components_1 = require("@perses-dev/components");
21
20
  const core_1 = require("@perses-dev/core");
22
21
  const plugin_system_1 = require("@perses-dev/plugin-system");
23
- const TimeRangeStateProvider_1 = require("../context/TimeRangeStateProvider");
22
+ const context_1 = require("../../context");
24
23
  // TODO: add time shortcut if one does not match duration
25
24
  exports.TIME_OPTIONS = [
26
25
  { value: { pastDuration: '5m' }, display: 'Last 5 minutes' },
@@ -33,17 +32,13 @@ exports.TIME_OPTIONS = [
33
32
  { value: { pastDuration: '7d' }, display: 'Last 7 days' },
34
33
  { value: { pastDuration: '14d' }, display: 'Last 14 days' },
35
34
  ];
36
- const FORM_CONTROL_LABEL = 'Time Range';
37
35
  function TimeRangeControls() {
38
- const { setTimeRange } = (0, TimeRangeStateProvider_1.useTimeRangeSetter)();
39
- const { defaultDuration } = (0, plugin_system_1.useTimeRange)();
40
- const defaultStart = (0, core_1.parseDurationString)(defaultDuration);
41
- // TODO: default to URL param if populated
42
- const [selectedTimeRange, setSelectedTimeRange] = (0, react_1.useState)({ pastDuration: defaultDuration });
43
- const [absoluteTimeRange, setAbsoluteTime] = (0, react_1.useState)({
44
- start: (0, date_fns_1.sub)(new Date(), { ...defaultStart }),
45
- end: new Date(),
46
- });
36
+ const { timeRange, setTimeRange } = (0, plugin_system_1.useTimeRange)();
37
+ const { dashboard } = (0, context_1.useDashboard)();
38
+ const { queryString } = (0, plugin_system_1.useQueryString)();
39
+ const defaultTimeRange = (0, core_1.getDefaultTimeRange)(dashboard.duration, queryString);
40
+ // selected form value can be relative or absolute, timeRange from plugin-system is only absolute
41
+ const [selectedTimeRange, setSelectedTimeRange] = (0, react_1.useState)(defaultTimeRange);
47
42
  const [showCustomDateSelector, setShowCustomDateSelector] = (0, react_1.useState)(false);
48
43
  const anchorEl = (0, react_1.useRef)();
49
44
  return ((0, jsx_runtime_1.jsxs)(material_1.Stack, { direction: "row", spacing: 1, children: [(0, jsx_runtime_1.jsx)(material_1.Popover, { anchorEl: anchorEl.current, anchorOrigin: {
@@ -51,27 +46,21 @@ function TimeRangeControls() {
51
46
  horizontal: 'center',
52
47
  }, open: showCustomDateSelector, onClose: () => setShowCustomDateSelector(false), sx: (theme) => ({
53
48
  padding: theme.spacing(2),
54
- }), children: (0, jsx_runtime_1.jsx)(components_1.AbsoluteTimePicker, { initialTimeRange: absoluteTimeRange, onChange: (timeRange) => {
49
+ }), children: (0, jsx_runtime_1.jsx)(components_1.AbsoluteTimePicker, { initialTimeRange: timeRange, onChange: (timeRange) => {
55
50
  setTimeRange(timeRange);
56
- if (!(0, core_1.isRelativeValue)(timeRange)) {
57
- setAbsoluteTime({ start: timeRange.start, end: timeRange.end });
58
- }
59
51
  setSelectedTimeRange(timeRange);
60
52
  setShowCustomDateSelector(false);
61
- } }) }), (0, jsx_runtime_1.jsxs)(material_1.FormControl, { fullWidth: true, children: [(0, jsx_runtime_1.jsx)(material_1.InputLabel, { children: FORM_CONTROL_LABEL }), (0, jsx_runtime_1.jsx)(material_1.Box, { ref: anchorEl, children: (0, jsx_runtime_1.jsx)(components_1.TimeRangeSelector, { inputLabel: FORM_CONTROL_LABEL, timeOptions: exports.TIME_OPTIONS, value: selectedTimeRange, onSelectChange: (event) => {
62
- const duration = event.target.value;
63
- const relativeTimeInput = {
64
- pastDuration: duration,
65
- end: new Date(),
66
- };
67
- // TODO: consolidate unnecessary state
68
- setSelectedTimeRange(relativeTimeInput);
69
- const convertedAbsoluteTime = (0, core_1.toAbsoluteTimeRange)(relativeTimeInput);
70
- setTimeRange(convertedAbsoluteTime);
71
- setAbsoluteTime(convertedAbsoluteTime);
72
- setShowCustomDateSelector(false);
73
- }, onCustomClick: () => {
74
- setShowCustomDateSelector(true);
75
- } }) })] })] }));
53
+ } }) }), (0, jsx_runtime_1.jsx)(material_1.FormControl, { fullWidth: true, children: (0, jsx_runtime_1.jsx)(material_1.Box, { ref: anchorEl, children: (0, jsx_runtime_1.jsx)(components_1.TimeRangeSelector, { timeOptions: exports.TIME_OPTIONS, value: selectedTimeRange, onSelectChange: (event) => {
54
+ const duration = event.target.value;
55
+ const relativeTimeInput = {
56
+ pastDuration: duration,
57
+ end: new Date(),
58
+ };
59
+ setTimeRange(relativeTimeInput);
60
+ setSelectedTimeRange(relativeTimeInput);
61
+ setShowCustomDateSelector(false);
62
+ }, onCustomClick: () => {
63
+ setShowCustomDateSelector(true);
64
+ } }) }) })] }));
76
65
  }
77
66
  exports.TimeRangeControls = TimeRangeControls;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const jsx_runtime_1 = require("react/jsx-runtime");
7
+ // Copyright 2022 The Perses Authors
8
+ // Licensed under the Apache License, Version 2.0 (the "License");
9
+ // you may not use this file except in compliance with the License.
10
+ // You may obtain a copy of the License at
11
+ //
12
+ // http://www.apache.org/licenses/LICENSE-2.0
13
+ //
14
+ // Unless required by applicable law or agreed to in writing, software
15
+ // distributed under the License is distributed on an "AS IS" BASIS,
16
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ // See the License for the specific language governing permissions and
18
+ // limitations under the License.
19
+ const user_event_1 = __importDefault(require("@testing-library/user-event"));
20
+ const core_1 = require("@perses-dev/core");
21
+ const react_1 = require("@testing-library/react");
22
+ const test_1 = require("../../test");
23
+ const testDashboard_1 = __importDefault(require("../../test/testDashboard"));
24
+ const context_1 = require("../../context");
25
+ const TimeRangeControls_1 = require("./TimeRangeControls");
26
+ describe('TimeRangeControls', () => {
27
+ let initialState;
28
+ beforeEach(() => {
29
+ initialState = {
30
+ dashboardSpec: testDashboard_1.default.spec,
31
+ };
32
+ });
33
+ const renderTimeRangeControls = () => {
34
+ const queryString = new URLSearchParams();
35
+ const defaultTimeRange = (0, core_1.getDefaultTimeRange)(initialState.dashboardSpec.duration, queryString);
36
+ (0, test_1.renderWithContext)((0, jsx_runtime_1.jsx)(context_1.QueryStringProvider, { queryString: queryString, children: (0, jsx_runtime_1.jsx)(context_1.DashboardProvider, { initialState: initialState, children: (0, jsx_runtime_1.jsx)(context_1.TimeRangeProvider, { initialTimeRange: defaultTimeRange, children: (0, jsx_runtime_1.jsx)(TimeRangeControls_1.TimeRangeControls, {}) }) }) }));
37
+ };
38
+ it('should render correct initial relative time shortcut', async () => {
39
+ renderTimeRangeControls();
40
+ expect(react_1.screen.getByText('Last 1 day')).toBeInTheDocument();
41
+ });
42
+ it('should be able to select the first option', () => {
43
+ renderTimeRangeControls();
44
+ const dateButton = react_1.screen.getByRole('button');
45
+ user_event_1.default.click(dateButton);
46
+ const firstOption = react_1.screen.getByRole('option', { name: 'Last 5 minutes' });
47
+ user_event_1.default.click(firstOption);
48
+ expect(dateButton).toHaveTextContent(/5 minutes/i);
49
+ });
50
+ // TODO: add additional tests for absolute time selection, other inputs, form validation, etc.
51
+ });
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./TimeRangeControls"), exports);
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TemplateVariable = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ // Copyright 2022 The Perses Authors
6
+ // Licensed under the Apache License, Version 2.0 (the "License");
7
+ // you may not use this file except in compliance with the License.
8
+ // You may obtain a copy of the License at
9
+ //
10
+ // http://www.apache.org/licenses/LICENSE-2.0
11
+ //
12
+ // Unless required by applicable law or agreed to in writing, software
13
+ // distributed under the License is distributed on an "AS IS" BASIS,
14
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ // See the License for the specific language governing permissions and
16
+ // limitations under the License.
17
+ const react_1 = require("react");
18
+ const material_1 = require("@mui/material");
19
+ const mdi_material_ui_1 = require("mdi-material-ui");
20
+ const context_1 = require("../../context");
21
+ function TemplateVariable({ name }) {
22
+ var _a;
23
+ const ctx = (0, context_1.useTemplateVariable)(name);
24
+ const kind = (_a = ctx.definition) === null || _a === void 0 ? void 0 : _a.kind;
25
+ switch (kind) {
26
+ case 'TextVariable':
27
+ return (0, jsx_runtime_1.jsx)(TextVariable, { name: name });
28
+ case 'ListVariable':
29
+ return (0, jsx_runtime_1.jsx)(ListVariable, { name: name });
30
+ }
31
+ return (0, jsx_runtime_1.jsxs)("div", { children: ["Unsupported Variable Kind: $", kind] });
32
+ }
33
+ exports.TemplateVariable = TemplateVariable;
34
+ function ListVariable({ name }) {
35
+ var _a, _b, _c, _d;
36
+ const ctx = (0, context_1.useTemplateVariable)(name);
37
+ const definition = ctx.definition;
38
+ const { setVariableValue, loadTemplateVariable } = (0, context_1.useTemplateVariableActions)();
39
+ const allowMultiple = (definition === null || definition === void 0 ? void 0 : definition.options.allowMultiple) === true;
40
+ (0, react_1.useEffect)(() => {
41
+ loadTemplateVariable(name);
42
+ }, [name, loadTemplateVariable]);
43
+ let value = (_a = ctx.state) === null || _a === void 0 ? void 0 : _a.value;
44
+ if (allowMultiple && !Array.isArray(value)) {
45
+ value = [];
46
+ }
47
+ return ((0, jsx_runtime_1.jsxs)(material_1.Box, { display: 'flex', children: [(0, jsx_runtime_1.jsxs)(material_1.FormControl, { children: [(0, jsx_runtime_1.jsx)(material_1.InputLabel, { id: name, children: name }), (0, jsx_runtime_1.jsx)(material_1.Select, { id: name, label: name, value: value, onChange: (e) => {
48
+ setVariableValue(name, e.target.value);
49
+ }, multiple: allowMultiple, children: (_c = (_b = ctx.state) === null || _b === void 0 ? void 0 : _b.options) === null || _c === void 0 ? void 0 : _c.map((option) => ((0, jsx_runtime_1.jsx)(material_1.MenuItem, { value: option.value, children: option.label }, option.value))) }), ((_d = ctx.state) === null || _d === void 0 ? void 0 : _d.loading) && (0, jsx_runtime_1.jsx)(material_1.LinearProgress, {})] }), (0, jsx_runtime_1.jsx)(material_1.IconButton, { onClick: () => loadTemplateVariable(name), children: (0, jsx_runtime_1.jsx)(mdi_material_ui_1.Reload, {}) })] }));
50
+ }
51
+ function TextVariable({ name }) {
52
+ const { state } = (0, context_1.useTemplateVariable)(name);
53
+ const s = (0, context_1.useTemplateVariableStore)();
54
+ const setVariableValue = s.setVariableValue;
55
+ const ref = (0, react_1.useRef)(null);
56
+ return ((0, jsx_runtime_1.jsx)(material_1.TextField, { ref: ref, defaultValue: state === null || state === void 0 ? void 0 : state.value, onBlur: (e) => setVariableValue(name, e.target.value), placeholder: name, label: name }));
57
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TemplateVariableList = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ // Copyright 2022 The Perses Authors
6
+ // Licensed under the Apache License, Version 2.0 (the "License");
7
+ // you may not use this file except in compliance with the License.
8
+ // You may obtain a copy of the License at
9
+ //
10
+ // http://www.apache.org/licenses/LICENSE-2.0
11
+ //
12
+ // Unless required by applicable law or agreed to in writing, software
13
+ // distributed under the License is distributed on an "AS IS" BASIS,
14
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ // See the License for the specific language governing permissions and
16
+ // limitations under the License.
17
+ const react_1 = require("react");
18
+ const material_1 = require("@mui/material");
19
+ const context_1 = require("../../context");
20
+ const Variable_1 = require("./Variable");
21
+ function TemplateVariableList() {
22
+ const [isEditing, setIsEditing] = (0, react_1.useState)(false);
23
+ const variableDefinitions = (0, context_1.useTemplateVariableDefinitions)();
24
+ const { isEditMode } = (0, context_1.useEditMode)();
25
+ return ((0, jsx_runtime_1.jsxs)(material_1.Box, { m: 2, children: [(0, jsx_runtime_1.jsx)(material_1.Drawer, { anchor: 'right', open: isEditing, onClose: () => setIsEditing(false), children: (0, jsx_runtime_1.jsxs)(material_1.Box, { width: 900, p: 4, children: [(0, jsx_runtime_1.jsx)(material_1.TableContainer, { component: material_1.Paper, children: (0, jsx_runtime_1.jsxs)(material_1.Table, { sx: { minWidth: 650 }, "aria-label": "simple table", children: [(0, jsx_runtime_1.jsx)(material_1.TableHead, { children: (0, jsx_runtime_1.jsxs)(material_1.TableRow, { children: [(0, jsx_runtime_1.jsx)(material_1.TableCell, { children: "Variable Name" }), (0, jsx_runtime_1.jsx)(material_1.TableCell, { align: "right", children: "Type" })] }) }), (0, jsx_runtime_1.jsx)(material_1.TableBody, { children: variableDefinitions.map((v) => ((0, jsx_runtime_1.jsxs)(material_1.TableRow, { sx: { '&:last-child td, &:last-child th': { border: 0 } }, children: [(0, jsx_runtime_1.jsx)(material_1.TableCell, { component: "th", scope: "row", sx: { fontWeight: 'bold' }, children: v.name }), (0, jsx_runtime_1.jsx)(material_1.TableCell, { align: "right", children: v.kind })] }, v.name))) })] }) }), (0, jsx_runtime_1.jsx)("pre", { children: JSON.stringify(variableDefinitions, null, 2) })] }) }), (0, jsx_runtime_1.jsx)(material_1.Box, { display: 'flex', justifyContent: "space-between", children: (0, jsx_runtime_1.jsxs)(material_1.Stack, { direction: 'row', spacing: 2, children: [variableDefinitions.map((v) => ((0, jsx_runtime_1.jsx)(material_1.Box, { children: (0, jsx_runtime_1.jsx)(Variable_1.TemplateVariable, { name: v.name }, v.name) }, v.name))), isEditMode && (0, jsx_runtime_1.jsx)(material_1.Button, { onClick: () => setIsEditing(true), children: "Modify Variables" })] }) })] }));
26
+ }
27
+ exports.TemplateVariableList = TemplateVariableList;
@@ -26,4 +26,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
26
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
+ __exportStar(require("./Variable"), exports);
29
30
  __exportStar(require("./VariableList"), exports);
@@ -28,7 +28,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  __exportStar(require("./Dashboard"), exports);
30
30
  __exportStar(require("./GridLayout"), exports);
31
- __exportStar(require("./Panel/Panel"), exports);
31
+ __exportStar(require("./Panel"), exports);
32
32
  __exportStar(require("./TimeRangeControls"), exports);
33
- __exportStar(require("./VariableAutocomplete"), exports);
34
- __exportStar(require("./VariableList"), exports);
33
+ __exportStar(require("./Variables"), exports);
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.useDashboardApp = exports.createDashboardAppSlice = void 0;
16
+ const immer_1 = require("zustand/middleware/immer");
17
+ const DashboardProvider_1 = require("./DashboardProvider");
18
+ exports.createDashboardAppSlice = (0, immer_1.immer)((set) => ({
19
+ openPanelDrawer: ({ groupIndex, panelKey }) => set((state) => {
20
+ state.panelDrawer = {
21
+ groupIndex,
22
+ panelKey,
23
+ };
24
+ }),
25
+ closePanelDrawer: () => set((state) => {
26
+ state.panelDrawer = undefined;
27
+ }),
28
+ openPanelGroupDialog: (groupIndex) => set((state) => {
29
+ state.panelGroupDialog = { groupIndex };
30
+ }),
31
+ closePanelGroupDialog: () => set((state) => {
32
+ state.panelGroupDialog = undefined;
33
+ }),
34
+ }));
35
+ function useDashboardApp() {
36
+ return (0, DashboardProvider_1.useDashboardStore)(({ panelDrawer, openPanelDrawer, closePanelDrawer, panelGroupDialog, openPanelGroupDialog, closePanelGroupDialog, }) => ({
37
+ panelDrawer,
38
+ openPanelDrawer,
39
+ closePanelDrawer,
40
+ panelGroupDialog,
41
+ openPanelGroupDialog,
42
+ closePanelGroupDialog,
43
+ }));
44
+ }
45
+ exports.useDashboardApp = useDashboardApp;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.DashboardProvider = exports.useDashboard = exports.useEditMode = exports.useLayouts = exports.usePanels = void 0;
6
+ exports.DashboardProvider = exports.useDashboardStore = exports.useDashboard = exports.useEditMode = exports.usePanels = void 0;
7
7
  const jsx_runtime_1 = require("react/jsx-runtime");
8
8
  // Copyright 2022 The Perses Authors
9
9
  // Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,64 +17,82 @@ const jsx_runtime_1 = require("react/jsx-runtime");
17
17
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
18
  // See the License for the specific language governing permissions and
19
19
  // limitations under the License.
20
- const zustand_1 = __importDefault(require("zustand"));
21
- const context_1 = __importDefault(require("zustand/context"));
22
- const immer_1 = __importDefault(require("immer"));
23
- const { Provider, useStore } = (0, context_1.default)();
20
+ const zustand_1 = require("zustand");
21
+ const immer_1 = require("zustand/middleware/immer");
22
+ const shallow_1 = __importDefault(require("zustand/shallow"));
23
+ const react_1 = require("react");
24
+ const immer_2 = __importDefault(require("immer"));
25
+ const DashboardAppSlice_1 = require("./DashboardAppSlice");
26
+ const LayoutsSlice_1 = require("./LayoutsSlice");
24
27
  function usePanels() {
25
- const { panels, addPanel } = useStore(({ panels, addPanel }) => ({ panels, addPanel }));
26
- return { panels, addPanel };
28
+ return useDashboardStore(({ panels, updatePanel }) => ({ panels, updatePanel }));
27
29
  }
28
30
  exports.usePanels = usePanels;
29
- function useLayouts() {
30
- const { layouts, setLayouts, addLayout, addItemToLayout } = useStore(({ layouts, setLayouts, addLayout, addItemToLayout }) => ({
31
- layouts,
32
- setLayouts,
33
- addLayout,
34
- addItemToLayout,
35
- }));
36
- return { layouts, setLayouts, addLayout, addItemToLayout };
37
- }
38
- exports.useLayouts = useLayouts;
39
31
  function useEditMode() {
40
- const { isEditMode, setEditMode } = useStore(({ isEditMode, setEditMode }) => ({ isEditMode, setEditMode }));
41
- return { isEditMode, setEditMode };
32
+ return useDashboardStore(({ isEditMode, setEditMode }) => ({ isEditMode, setEditMode }));
42
33
  }
43
34
  exports.useEditMode = useEditMode;
44
35
  function useDashboard() {
45
36
  const selectDashboardSpec = (state) => {
46
- return (0, immer_1.default)(state.dashboard, (draftState) => {
37
+ return (0, immer_2.default)(state.dashboard, (draftState) => {
47
38
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
39
  draftState.panels = state.panels;
49
40
  draftState.layouts = state.layouts;
50
41
  });
51
42
  };
52
- const dashboard = useStore(selectDashboardSpec);
43
+ const dashboard = useDashboardStore(selectDashboardSpec);
53
44
  return { dashboard };
54
45
  }
55
46
  exports.useDashboard = useDashboard;
47
+ const DashboardContext = (0, react_1.createContext)(undefined);
48
+ function useDashboardStore(selector) {
49
+ const store = (0, react_1.useContext)(DashboardContext);
50
+ if (store === undefined) {
51
+ throw new Error('No DashboardContext found. Did you forget a Provider?');
52
+ }
53
+ return (0, zustand_1.useStore)(store, selector, shallow_1.default);
54
+ }
55
+ exports.useDashboardStore = useDashboardStore;
56
56
  function DashboardProvider(props) {
57
57
  const { children, initialState: { dashboardSpec, isEditMode }, } = props;
58
58
  const { layouts, panels } = dashboardSpec;
59
- return ((0, jsx_runtime_1.jsx)(Provider, { createStore: () => (0, zustand_1.default)((set) => ({
59
+ const dashboardStore = (0, zustand_1.createStore)()((0, immer_1.immer)((set, get, api) => {
60
+ return {
61
+ ...(0, DashboardAppSlice_1.createDashboardAppSlice)(set, get, api, []),
62
+ ...(0, LayoutsSlice_1.createLayoutsSlice)(set, get, api, []),
60
63
  layouts,
61
64
  panels,
62
65
  dashboard: dashboardSpec,
66
+ updatePanel: (name, panel, groupIndex = 0) => set((state) => {
67
+ var _a, _b;
68
+ // add new panel to layouts if panels[name] is undefined
69
+ if (state.panels[name] === undefined) {
70
+ // find maximum y so new panel is added to the end of the grid
71
+ let maxY = 0;
72
+ (_a = state.layouts[groupIndex]) === null || _a === void 0 ? void 0 : _a.spec.items.forEach((layout) => {
73
+ if (layout.y > maxY) {
74
+ maxY = layout.y;
75
+ }
76
+ });
77
+ const panelLayout = {
78
+ x: 0,
79
+ y: maxY + 1,
80
+ width: 12,
81
+ height: 6,
82
+ content: { $ref: `#/spec/panels/${name}` },
83
+ };
84
+ const layouts = state.layouts;
85
+ if (layouts && layouts[groupIndex]) {
86
+ (_b = layouts[groupIndex]) === null || _b === void 0 ? void 0 : _b.spec.items.push(panelLayout);
87
+ }
88
+ }
89
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
90
+ state.panels[name] = panel;
91
+ }),
63
92
  isEditMode: !!isEditMode,
64
93
  setEditMode: (isEditMode) => set({ isEditMode }),
65
- setLayouts: (layouts) => set({ layouts }),
66
- addLayout: (layout) => set((0, immer_1.default)((state) => {
67
- state.layouts.push(layout);
68
- })),
69
- addItemToLayout: (index, item) => set((0, immer_1.default)((state) => {
70
- state.layouts[index].spec.items.push(item);
71
- })),
72
- setPanels: (panels) => set({ panels }),
73
- addPanel: (name, panel) => {
74
- set((0, immer_1.default)((state) => {
75
- state.panels[name] = panel;
76
- }, {}));
77
- },
78
- })), children: children }));
94
+ };
95
+ }));
96
+ return ((0, jsx_runtime_1.jsx)(DashboardContext.Provider, { value: dashboardStore, children: children }));
79
97
  }
80
98
  exports.DashboardProvider = DashboardProvider;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ // Copyright 2022 The Perses Authors
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.useLayouts = exports.createLayoutsSlice = void 0;
16
+ const immer_1 = require("zustand/middleware/immer");
17
+ const DashboardProvider_1 = require("./DashboardProvider");
18
+ exports.createLayoutsSlice = (0, immer_1.immer)((set) => ({
19
+ updateLayout: (layout, index) => set((state) => {
20
+ if (index === undefined) {
21
+ state.layouts.unshift(layout);
22
+ }
23
+ else {
24
+ state.layouts[index] = layout;
25
+ }
26
+ }),
27
+ addItemToLayout: (index, item) => set((state) => {
28
+ var _a;
29
+ const layouts = state.layouts;
30
+ if (layouts && layouts[index]) {
31
+ (_a = layouts[index]) === null || _a === void 0 ? void 0 : _a.spec.items.push(item);
32
+ }
33
+ }),
34
+ }));
35
+ function useLayouts() {
36
+ return (0, DashboardProvider_1.useDashboardStore)(({ layouts, updateLayout, addItemToLayout }) => ({
37
+ layouts,
38
+ updateLayout,
39
+ addItemToLayout,
40
+ }));
41
+ }
42
+ exports.useLayouts = useLayouts;