@perses-dev/dashboards 0.8.1 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (258) hide show
  1. package/dist/cjs/components/Dashboard.js +25 -14
  2. package/dist/cjs/components/DashboardToolbar.js +148 -20
  3. package/dist/cjs/components/GridLayout/GridItemContent.js +25 -19
  4. package/dist/cjs/components/GridLayout/GridLayout.js +92 -27
  5. package/dist/cjs/components/GridLayout/GridTitle.js +91 -31
  6. package/dist/cjs/components/GridLayout/index.js +18 -19
  7. package/dist/cjs/components/Panel/DeletePanelDialog.js +91 -0
  8. package/dist/cjs/components/Panel/Panel.js +156 -55
  9. package/dist/cjs/components/Panel/Panel.test.js +58 -41
  10. package/dist/cjs/components/Panel/PanelContent.js +41 -12
  11. package/dist/cjs/components/Panel/index.js +16 -17
  12. package/dist/cjs/components/PanelDrawer/PanelDrawer.js +84 -108
  13. package/dist/cjs/components/PanelDrawer/PanelDrawer.test.js +87 -92
  14. package/dist/cjs/components/PanelDrawer/PanelEditorForm.js +194 -0
  15. package/dist/cjs/components/PanelDrawer/PanelPreview.js +48 -0
  16. package/dist/cjs/components/PanelDrawer/index.js +28 -0
  17. package/dist/cjs/components/PanelGroupDialog/DeletePanelGroupDialog.js +86 -0
  18. package/dist/cjs/components/PanelGroupDialog/PanelGroupDialog.js +121 -39
  19. package/dist/cjs/components/PanelGroupDialog/PanelGroupDialog.test.js +74 -88
  20. package/dist/cjs/components/TimeRangeControls/TimeRangeControls.js +120 -38
  21. package/dist/cjs/components/TimeRangeControls/TimeRangeControls.test.js +42 -27
  22. package/dist/cjs/components/TimeRangeControls/index.js +16 -17
  23. package/dist/cjs/components/Variables/Variable.js +195 -36
  24. package/dist/cjs/components/Variables/VariableEditor.js +207 -0
  25. package/dist/cjs/components/Variables/VariableEditorForm/VariableEditorForm.js +236 -0
  26. package/dist/cjs/components/Variables/VariableEditorForm/index.js +28 -0
  27. package/dist/cjs/components/Variables/VariableEditorForm/variable-editor-form-model.js +88 -0
  28. package/dist/cjs/components/Variables/VariableList.js +81 -13
  29. package/dist/cjs/components/Variables/index.js +18 -18
  30. package/dist/cjs/components/index.js +21 -21
  31. package/dist/cjs/context/DashboardProvider/DashboardProvider.js +70 -0
  32. package/dist/cjs/context/DashboardProvider/common.js +18 -0
  33. package/dist/cjs/context/DashboardProvider/dashboard-provider-api.js +71 -0
  34. package/dist/cjs/context/DashboardProvider/index.js +29 -0
  35. package/dist/cjs/context/DashboardProvider/layout-slice.js +200 -0
  36. package/dist/cjs/context/DashboardProvider/panel-editing-slice.js +156 -0
  37. package/dist/cjs/context/DashboardProvider/panel-group-slice.js +38 -0
  38. package/dist/cjs/context/DatasourceStoreProvider.js +170 -0
  39. package/dist/cjs/context/QueryStringProvider.js +69 -15
  40. package/dist/cjs/context/TemplateVariableProvider.js +135 -136
  41. package/dist/cjs/context/TimeRangeProvider.js +79 -30
  42. package/dist/cjs/context/index.js +20 -22
  43. package/dist/cjs/css/styles.js +43 -39
  44. package/dist/cjs/index.js +19 -20
  45. package/dist/cjs/test/dashboard-provider.js +51 -0
  46. package/dist/cjs/test/index.js +18 -18
  47. package/dist/cjs/test/plugin-registry.js +52 -25
  48. package/dist/cjs/test/render.js +25 -22
  49. package/dist/cjs/test/setup-tests.js +4 -2
  50. package/dist/cjs/test/testDashboard.js +203 -107
  51. package/dist/cjs/utils/functions.js +9 -5
  52. package/dist/cjs/views/ViewDashboard/DashboardApp.js +49 -23
  53. package/dist/cjs/views/ViewDashboard/ViewDashboard.js +62 -31
  54. package/dist/cjs/views/ViewDashboard/index.js +16 -17
  55. package/dist/cjs/views/ViewDashboard/tests/panelGroups.test.js +165 -0
  56. package/dist/cjs/views/index.js +16 -17
  57. package/dist/components/Dashboard.d.ts +1 -4
  58. package/dist/components/Dashboard.d.ts.map +1 -1
  59. package/dist/components/Dashboard.js +34 -1
  60. package/dist/components/Dashboard.js.map +1 -0
  61. package/dist/components/DashboardToolbar.d.ts.map +1 -1
  62. package/dist/components/DashboardToolbar.js +154 -1
  63. package/dist/components/DashboardToolbar.js.map +1 -0
  64. package/dist/components/GridLayout/GridItemContent.d.ts +3 -3
  65. package/dist/components/GridLayout/GridItemContent.d.ts.map +1 -1
  66. package/dist/components/GridLayout/GridItemContent.js +35 -1
  67. package/dist/components/GridLayout/GridItemContent.js.map +1 -0
  68. package/dist/components/GridLayout/GridLayout.d.ts +2 -3
  69. package/dist/components/GridLayout/GridLayout.d.ts.map +1 -1
  70. package/dist/components/GridLayout/GridLayout.js +102 -1
  71. package/dist/components/GridLayout/GridLayout.js.map +1 -0
  72. package/dist/components/GridLayout/GridTitle.d.ts.map +1 -1
  73. package/dist/components/GridLayout/GridTitle.js +99 -1
  74. package/dist/components/GridLayout/GridTitle.js.map +1 -0
  75. package/dist/components/GridLayout/index.js +16 -1
  76. package/dist/components/GridLayout/index.js.map +1 -0
  77. package/dist/components/Panel/DeletePanelDialog.d.ts +5 -0
  78. package/dist/components/Panel/DeletePanelDialog.d.ts.map +1 -0
  79. package/dist/components/Panel/DeletePanelDialog.js +80 -0
  80. package/dist/components/Panel/DeletePanelDialog.js.map +1 -0
  81. package/dist/components/Panel/Panel.d.ts +1 -1
  82. package/dist/components/Panel/Panel.d.ts.map +1 -1
  83. package/dist/components/Panel/Panel.js +181 -1
  84. package/dist/components/Panel/Panel.js.map +1 -0
  85. package/dist/components/Panel/Panel.test.js +74 -1
  86. package/dist/components/Panel/Panel.test.js.map +1 -0
  87. package/dist/components/Panel/PanelContent.d.ts +5 -4
  88. package/dist/components/Panel/PanelContent.d.ts.map +1 -1
  89. package/dist/components/Panel/PanelContent.js +41 -1
  90. package/dist/components/Panel/PanelContent.js.map +1 -0
  91. package/dist/components/Panel/index.js +15 -1
  92. package/dist/components/Panel/index.js.map +1 -0
  93. package/dist/components/PanelDrawer/PanelDrawer.d.ts +4 -2
  94. package/dist/components/PanelDrawer/PanelDrawer.d.ts.map +1 -1
  95. package/dist/components/PanelDrawer/PanelDrawer.js +96 -1
  96. package/dist/components/PanelDrawer/PanelDrawer.js.map +1 -0
  97. package/dist/components/PanelDrawer/PanelDrawer.test.js +99 -1
  98. package/dist/components/PanelDrawer/PanelDrawer.test.js.map +1 -0
  99. package/dist/components/PanelDrawer/PanelEditorForm.d.ts +12 -0
  100. package/dist/components/PanelDrawer/PanelEditorForm.d.ts.map +1 -0
  101. package/dist/components/PanelDrawer/PanelEditorForm.js +184 -0
  102. package/dist/components/PanelDrawer/PanelEditorForm.js.map +1 -0
  103. package/dist/components/PanelDrawer/PanelPreview.d.ts +4 -0
  104. package/dist/components/PanelDrawer/PanelPreview.d.ts.map +1 -0
  105. package/dist/components/PanelDrawer/PanelPreview.js +42 -0
  106. package/dist/components/PanelDrawer/PanelPreview.js.map +1 -0
  107. package/dist/components/PanelDrawer/index.d.ts +2 -0
  108. package/dist/components/PanelDrawer/index.d.ts.map +1 -0
  109. package/dist/components/PanelDrawer/index.js +15 -0
  110. package/dist/components/PanelDrawer/index.js.map +1 -0
  111. package/dist/components/PanelGroupDialog/DeletePanelGroupDialog.d.ts +4 -0
  112. package/dist/components/PanelGroupDialog/DeletePanelGroupDialog.d.ts.map +1 -0
  113. package/dist/components/PanelGroupDialog/DeletePanelGroupDialog.js +75 -0
  114. package/dist/components/PanelGroupDialog/DeletePanelGroupDialog.js.map +1 -0
  115. package/dist/components/PanelGroupDialog/PanelGroupDialog.d.ts.map +1 -1
  116. package/dist/components/PanelGroupDialog/PanelGroupDialog.js +131 -1
  117. package/dist/components/PanelGroupDialog/PanelGroupDialog.js.map +1 -0
  118. package/dist/components/PanelGroupDialog/PanelGroupDialog.test.js +74 -1
  119. package/dist/components/PanelGroupDialog/PanelGroupDialog.test.js.map +1 -0
  120. package/dist/components/TimeRangeControls/TimeRangeControls.js +137 -1
  121. package/dist/components/TimeRangeControls/TimeRangeControls.js.map +1 -0
  122. package/dist/components/TimeRangeControls/TimeRangeControls.test.js +59 -1
  123. package/dist/components/TimeRangeControls/TimeRangeControls.test.js.map +1 -0
  124. package/dist/components/TimeRangeControls/index.js +15 -1
  125. package/dist/components/TimeRangeControls/index.js.map +1 -0
  126. package/dist/components/Variables/Variable.d.ts.map +1 -1
  127. package/dist/components/Variables/Variable.js +210 -1
  128. package/dist/components/Variables/Variable.js.map +1 -0
  129. package/dist/components/Variables/VariableEditor.d.ts +8 -0
  130. package/dist/components/Variables/VariableEditor.d.ts.map +1 -0
  131. package/dist/components/Variables/VariableEditor.js +196 -0
  132. package/dist/components/Variables/VariableEditor.js.map +1 -0
  133. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.d.ts +8 -0
  134. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.d.ts.map +1 -0
  135. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js +225 -0
  136. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js.map +1 -0
  137. package/dist/components/Variables/VariableEditorForm/index.d.ts +2 -0
  138. package/dist/components/Variables/VariableEditorForm/index.d.ts.map +1 -0
  139. package/dist/components/Variables/VariableEditorForm/index.js +15 -0
  140. package/dist/components/Variables/VariableEditorForm/index.js.map +1 -0
  141. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.d.ts +21 -0
  142. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.d.ts.map +1 -0
  143. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.js +76 -0
  144. package/dist/components/Variables/VariableEditorForm/variable-editor-form-model.js.map +1 -0
  145. package/dist/components/Variables/VariableList.d.ts.map +1 -1
  146. package/dist/components/Variables/VariableList.js +84 -1
  147. package/dist/components/Variables/VariableList.js.map +1 -0
  148. package/dist/components/Variables/index.d.ts +1 -0
  149. package/dist/components/Variables/index.d.ts.map +1 -1
  150. package/dist/components/Variables/index.js +17 -1
  151. package/dist/components/Variables/index.js.map +1 -0
  152. package/dist/components/index.d.ts +1 -0
  153. package/dist/components/index.d.ts.map +1 -1
  154. package/dist/components/index.js +20 -1
  155. package/dist/components/index.js.map +1 -0
  156. package/dist/context/DashboardProvider/DashboardProvider.d.ts +23 -0
  157. package/dist/context/DashboardProvider/DashboardProvider.d.ts.map +1 -0
  158. package/dist/context/DashboardProvider/DashboardProvider.js +52 -0
  159. package/dist/context/DashboardProvider/DashboardProvider.js.map +1 -0
  160. package/dist/context/DashboardProvider/common.d.ts +5 -0
  161. package/dist/context/DashboardProvider/common.d.ts.map +1 -0
  162. package/dist/context/DashboardProvider/common.js +17 -0
  163. package/dist/context/DashboardProvider/common.js.map +1 -0
  164. package/dist/context/DashboardProvider/dashboard-provider-api.d.ts +32 -0
  165. package/dist/context/DashboardProvider/dashboard-provider-api.d.ts.map +1 -0
  166. package/dist/context/DashboardProvider/dashboard-provider-api.js +56 -0
  167. package/dist/context/DashboardProvider/dashboard-provider-api.js.map +1 -0
  168. package/dist/context/DashboardProvider/index.d.ts +3 -0
  169. package/dist/context/DashboardProvider/index.d.ts.map +1 -0
  170. package/dist/context/DashboardProvider/index.js +16 -0
  171. package/dist/context/DashboardProvider/index.js.map +1 -0
  172. package/dist/context/DashboardProvider/layout-slice.d.ts +57 -0
  173. package/dist/context/DashboardProvider/layout-slice.d.ts.map +1 -0
  174. package/dist/context/DashboardProvider/layout-slice.js +196 -0
  175. package/dist/context/DashboardProvider/layout-slice.js.map +1 -0
  176. package/dist/context/DashboardProvider/panel-editing-slice.d.ts +70 -0
  177. package/dist/context/DashboardProvider/panel-editing-slice.d.ts.map +1 -0
  178. package/dist/context/DashboardProvider/panel-editing-slice.js +152 -0
  179. package/dist/context/DashboardProvider/panel-editing-slice.js.map +1 -0
  180. package/dist/context/DashboardProvider/panel-group-slice.d.ts +15 -0
  181. package/dist/context/DashboardProvider/panel-group-slice.d.ts.map +1 -0
  182. package/dist/context/DashboardProvider/panel-group-slice.js +32 -0
  183. package/dist/context/DashboardProvider/panel-group-slice.js.map +1 -0
  184. package/dist/context/DatasourceStoreProvider.d.ts +24 -0
  185. package/dist/context/DatasourceStoreProvider.d.ts.map +1 -0
  186. package/dist/context/DatasourceStoreProvider.js +166 -0
  187. package/dist/context/DatasourceStoreProvider.js.map +1 -0
  188. package/dist/context/QueryStringProvider.js +40 -1
  189. package/dist/context/QueryStringProvider.js.map +1 -0
  190. package/dist/context/TemplateVariableProvider.d.ts +8 -3
  191. package/dist/context/TemplateVariableProvider.d.ts.map +1 -1
  192. package/dist/context/TemplateVariableProvider.js +199 -1
  193. package/dist/context/TemplateVariableProvider.js.map +1 -0
  194. package/dist/context/TimeRangeProvider.js +72 -1
  195. package/dist/context/TimeRangeProvider.js.map +1 -0
  196. package/dist/context/index.d.ts +3 -4
  197. package/dist/context/index.d.ts.map +1 -1
  198. package/dist/context/index.js +19 -1
  199. package/dist/context/index.js.map +1 -0
  200. package/dist/css/styles.js +186 -1
  201. package/dist/css/styles.js.map +1 -0
  202. package/dist/index.js +17 -1
  203. package/dist/index.js.map +1 -0
  204. package/dist/test/dashboard-provider.d.ts +19 -0
  205. package/dist/test/dashboard-provider.d.ts.map +1 -0
  206. package/dist/test/dashboard-provider.js +40 -0
  207. package/dist/test/dashboard-provider.js.map +1 -0
  208. package/dist/test/index.d.ts +1 -0
  209. package/dist/test/index.d.ts.map +1 -1
  210. package/dist/test/index.js +17 -1
  211. package/dist/test/index.js.map +1 -0
  212. package/dist/test/plugin-registry.d.ts +3 -4
  213. package/dist/test/plugin-registry.d.ts.map +1 -1
  214. package/dist/test/plugin-registry.js +74 -1
  215. package/dist/test/plugin-registry.js.map +1 -0
  216. package/dist/test/render.d.ts +1 -2
  217. package/dist/test/render.d.ts.map +1 -1
  218. package/dist/test/render.js +34 -1
  219. package/dist/test/render.js.map +1 -0
  220. package/dist/test/setup-tests.js +18 -1
  221. package/dist/test/setup-tests.js.map +1 -0
  222. package/dist/test/testDashboard.d.ts.map +1 -1
  223. package/dist/test/testDashboard.js +286 -1
  224. package/dist/test/testDashboard.js.map +1 -0
  225. package/dist/utils/functions.js +17 -1
  226. package/dist/utils/functions.js.map +1 -0
  227. package/dist/views/ViewDashboard/DashboardApp.d.ts.map +1 -1
  228. package/dist/views/ViewDashboard/DashboardApp.js +55 -1
  229. package/dist/views/ViewDashboard/DashboardApp.js.map +1 -0
  230. package/dist/views/ViewDashboard/ViewDashboard.d.ts +2 -0
  231. package/dist/views/ViewDashboard/ViewDashboard.d.ts.map +1 -1
  232. package/dist/views/ViewDashboard/ViewDashboard.js +79 -1
  233. package/dist/views/ViewDashboard/ViewDashboard.js.map +1 -0
  234. package/dist/views/ViewDashboard/index.js +15 -1
  235. package/dist/views/ViewDashboard/index.js.map +1 -0
  236. package/dist/views/ViewDashboard/tests/panelGroups.test.d.ts +2 -0
  237. package/dist/views/ViewDashboard/tests/panelGroups.test.d.ts.map +1 -0
  238. package/dist/views/ViewDashboard/tests/panelGroups.test.js +158 -0
  239. package/dist/views/ViewDashboard/tests/panelGroups.test.js.map +1 -0
  240. package/dist/views/index.js +15 -1
  241. package/dist/views/index.js.map +1 -0
  242. package/package.json +13 -7
  243. package/dist/cjs/components/PanelDrawer/PanelOptionsEditor.js +0 -19
  244. package/dist/cjs/context/DashboardAppSlice.js +0 -45
  245. package/dist/cjs/context/DashboardProvider.js +0 -98
  246. package/dist/cjs/context/LayoutsSlice.js +0 -42
  247. package/dist/components/PanelDrawer/PanelOptionsEditor.d.ts +0 -9
  248. package/dist/components/PanelDrawer/PanelOptionsEditor.d.ts.map +0 -1
  249. package/dist/components/PanelDrawer/PanelOptionsEditor.js +0 -1
  250. package/dist/context/DashboardAppSlice.d.ts +0 -26
  251. package/dist/context/DashboardAppSlice.d.ts.map +0 -1
  252. package/dist/context/DashboardAppSlice.js +0 -1
  253. package/dist/context/DashboardProvider.d.ts +0 -34
  254. package/dist/context/DashboardProvider.d.ts.map +0 -1
  255. package/dist/context/DashboardProvider.js +0 -1
  256. package/dist/context/LayoutsSlice.d.ts +0 -12
  257. package/dist/context/LayoutsSlice.d.ts.map +0 -1
  258. package/dist/context/LayoutsSlice.js +0 -1
@@ -0,0 +1,166 @@
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 { jsx as _jsx } from "react/jsx-runtime";
14
+ import { useCallback, useMemo } from 'react';
15
+ import { useEvent } from '@perses-dev/core';
16
+ import { DatasourceStoreContext, usePluginRegistry } from '@perses-dev/plugin-system';
17
+ /**
18
+ * A `DatasourceContext` provider that uses an external API to resolve datasource selectors.
19
+ */ export function DatasourceStoreProvider(props) {
20
+ const { dashboardResource , datasourceApi , children } = props;
21
+ const { project } = dashboardResource.metadata;
22
+ const { getPlugin , listPluginMetadata } = usePluginRegistry();
23
+ const findDatasource = useEvent(async (selector)=>{
24
+ // Try to find it in dashboard spec
25
+ const { datasources } = dashboardResource.spec;
26
+ const dashboardDatasource = findDashboardDatasource(datasources, selector);
27
+ if (dashboardDatasource !== undefined) {
28
+ return {
29
+ spec: dashboardDatasource,
30
+ proxyUrl: undefined
31
+ };
32
+ }
33
+ // Try to find it at the project level as a Datasource resource
34
+ const datasource = await datasourceApi.getDatasource(project, selector);
35
+ if (datasource !== undefined) {
36
+ return {
37
+ spec: datasource.resource.spec,
38
+ proxyUrl: datasource.proxyUrl
39
+ };
40
+ }
41
+ // Try to find it at the global level as a GlobalDatasource resource
42
+ const globalDatasource = await datasourceApi.getGlobalDatasource(selector);
43
+ if (globalDatasource !== undefined) {
44
+ return {
45
+ spec: globalDatasource.resource.spec,
46
+ proxyUrl: globalDatasource.proxyUrl
47
+ };
48
+ }
49
+ throw new Error(`No datasource found for kind '${selector.kind}' and name '${selector.name}'`);
50
+ });
51
+ // Gets a datasource spec for a given selector
52
+ const getDatasource = useCallback(async (selector)=>{
53
+ const { spec } = await findDatasource(selector);
54
+ return spec;
55
+ }, [
56
+ findDatasource
57
+ ]);
58
+ // Given a Datasource selector, finds the spec for it and then uses its corresponding plugin the create a client
59
+ const getDatasourceClient = useCallback(async function getClient(selector) {
60
+ const { kind } = selector;
61
+ const [{ spec , proxyUrl }, plugin] = await Promise.all([
62
+ findDatasource(selector),
63
+ getPlugin('Datasource', kind)
64
+ ]);
65
+ return plugin.createClient(spec.plugin.spec, {
66
+ proxyUrl
67
+ });
68
+ }, [
69
+ findDatasource,
70
+ getPlugin
71
+ ]);
72
+ const listDatasourceMetadata = useEvent(async (datasourcePluginKind)=>{
73
+ const [pluginMetadata, datasources, globalDatasources] = await Promise.all([
74
+ listPluginMetadata('Datasource'),
75
+ datasourceApi.listDatasources(project, datasourcePluginKind),
76
+ datasourceApi.listGlobalDatasources(datasourcePluginKind)
77
+ ]);
78
+ // Find the metadata for the plugin type they asked for so we can use it for the name of the default datasource
79
+ const datasourcePluginMetadata = pluginMetadata.find((metadata)=>metadata.kind === datasourcePluginKind);
80
+ if (datasourcePluginMetadata === undefined) {
81
+ throw new Error(`Could not find a Datasource plugin with kind '${datasourcePluginKind}'`);
82
+ }
83
+ // Get helper for de-duping results properly
84
+ const { results , addResult } = buildListDatasourceMetadataResults(datasourcePluginMetadata.display.name);
85
+ // Start with dashboard datasources that have highest precedence
86
+ if (dashboardResource.spec.datasources !== undefined) {
87
+ for(const selectorName in dashboardResource.spec.datasources){
88
+ const spec = dashboardResource.spec.datasources[selectorName];
89
+ if (spec === undefined || spec.plugin.kind !== datasourcePluginKind) continue;
90
+ addResult(spec, selectorName);
91
+ }
92
+ }
93
+ // Now look at project-level datasources
94
+ for (const datasource of datasources){
95
+ const selectorName1 = datasource.metadata.name;
96
+ addResult(datasource.spec, selectorName1);
97
+ }
98
+ // And finally global datasources
99
+ for (const globalDatasource of globalDatasources){
100
+ const selectorName2 = globalDatasource.metadata.name;
101
+ addResult(globalDatasource.spec, selectorName2);
102
+ }
103
+ return results;
104
+ });
105
+ const ctxValue = useMemo(()=>({
106
+ getDatasource,
107
+ getDatasourceClient,
108
+ listDatasourceMetadata
109
+ }), [
110
+ getDatasource,
111
+ getDatasourceClient,
112
+ listDatasourceMetadata
113
+ ]);
114
+ return /*#__PURE__*/ _jsx(DatasourceStoreContext.Provider, {
115
+ value: ctxValue,
116
+ children: children
117
+ });
118
+ }
119
+ // Helper to find a datasource in the list embedded in a dashboard spec
120
+ function findDashboardDatasource(dashboardDatasources, selector) {
121
+ if (dashboardDatasources === undefined) return undefined;
122
+ // If using a name in the selector...
123
+ if (selector.name !== undefined) {
124
+ const named = dashboardDatasources[selector.name];
125
+ if (named === undefined) return undefined;
126
+ return named.plugin.kind === selector.kind ? named : undefined;
127
+ }
128
+ // If only using a kind, try to find one with that kind that is the default
129
+ return Object.values(dashboardDatasources).find((ds)=>ds.plugin.kind === selector.kind && ds.default === true);
130
+ }
131
+ // Helper for building a list of DatasourceMetadata results that will take care of de-duping already used selectors
132
+ function buildListDatasourceMetadataResults(pluginDisplayName) {
133
+ const results = [];
134
+ const usedNames = new Set();
135
+ let defaultAdded = false;
136
+ const addResult = (spec, selectorName)=>{
137
+ var ref;
138
+ // If we haven't added a default yet and this is a default, add default option to the beginning of the results
139
+ if (spec.default && defaultAdded === false) {
140
+ results.unshift({
141
+ name: `Default ${pluginDisplayName}`,
142
+ selector: {
143
+ kind: spec.plugin.kind
144
+ }
145
+ });
146
+ defaultAdded = true;
147
+ }
148
+ // If we already have a datasource with this selector name, ignore it, otherwise add to end of list
149
+ if (usedNames.has(selectorName)) return;
150
+ var ref1;
151
+ results.push({
152
+ name: (ref1 = (ref = spec.display) === null || ref === void 0 ? void 0 : ref.name) !== null && ref1 !== void 0 ? ref1 : selectorName,
153
+ selector: {
154
+ kind: spec.plugin.kind,
155
+ name: selectorName
156
+ }
157
+ });
158
+ usedNames.add(selectorName);
159
+ };
160
+ return {
161
+ results,
162
+ addResult
163
+ };
164
+ }
165
+
166
+ //# sourceMappingURL=DatasourceStoreProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/context/DatasourceStoreProvider.tsx"],"sourcesContent":["// Copyright 2022 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { useCallback, useMemo } from 'react';\nimport {\n DashboardResource,\n DashboardSpec,\n Datasource,\n DatasourceSelector,\n DatasourceSpec,\n GlobalDatasource,\n useEvent,\n} from '@perses-dev/core';\nimport {\n DatasourceStoreContext,\n DatasourceStore,\n usePluginRegistry,\n DatasourceMetadata,\n} from '@perses-dev/plugin-system';\n\nexport interface DatasourceStoreProviderProps {\n dashboardResource: DashboardResource;\n datasourceApi: DatasourceApi;\n children?: React.ReactNode;\n}\n\n// The external API for fetching datasource resources\nexport interface DatasourceApi {\n getDatasource: (\n project: string,\n selector: DatasourceSelector\n ) => Promise<{ resource: Datasource; proxyUrl: string } | undefined>;\n\n getGlobalDatasource: (\n selector: DatasourceSelector\n ) => Promise<{ resource: GlobalDatasource; proxyUrl: string } | undefined>;\n\n listDatasources: (project: string, pluginKind?: string) => Promise<Datasource[]>;\n\n listGlobalDatasources: (pluginKind?: string) => Promise<GlobalDatasource[]>;\n}\n\n/**\n * A `DatasourceContext` provider that uses an external API to resolve datasource selectors.\n */\nexport function DatasourceStoreProvider(props: DatasourceStoreProviderProps) {\n const { dashboardResource, datasourceApi, children } = props;\n const { project } = dashboardResource.metadata;\n\n const { getPlugin, listPluginMetadata } = usePluginRegistry();\n\n const findDatasource = useEvent(async (selector: DatasourceSelector) => {\n // Try to find it in dashboard spec\n const { datasources } = dashboardResource.spec;\n const dashboardDatasource = findDashboardDatasource(datasources, selector);\n if (dashboardDatasource !== undefined) {\n return { spec: dashboardDatasource, proxyUrl: undefined };\n }\n\n // Try to find it at the project level as a Datasource resource\n const datasource = await datasourceApi.getDatasource(project, selector);\n if (datasource !== undefined) {\n return { spec: datasource.resource.spec, proxyUrl: datasource.proxyUrl };\n }\n\n // Try to find it at the global level as a GlobalDatasource resource\n const globalDatasource = await datasourceApi.getGlobalDatasource(selector);\n if (globalDatasource !== undefined) {\n return { spec: globalDatasource.resource.spec, proxyUrl: globalDatasource.proxyUrl };\n }\n\n throw new Error(`No datasource found for kind '${selector.kind}' and name '${selector.name}'`);\n });\n\n // Gets a datasource spec for a given selector\n const getDatasource = useCallback(\n async (selector: DatasourceSelector): Promise<DatasourceSpec> => {\n const { spec } = await findDatasource(selector);\n return spec;\n },\n [findDatasource]\n );\n\n // Given a Datasource selector, finds the spec for it and then uses its corresponding plugin the create a client\n const getDatasourceClient = useCallback(\n async function getClient<Client>(selector: DatasourceSelector): Promise<Client> {\n const { kind } = selector;\n const [{ spec, proxyUrl }, plugin] = await Promise.all([findDatasource(selector), getPlugin('Datasource', kind)]);\n return plugin.createClient(spec.plugin.spec, { proxyUrl }) as Client;\n },\n [findDatasource, getPlugin]\n );\n\n const listDatasourceMetadata = useEvent(async (datasourcePluginKind: string): Promise<DatasourceMetadata[]> => {\n const [pluginMetadata, datasources, globalDatasources] = await Promise.all([\n listPluginMetadata('Datasource'),\n datasourceApi.listDatasources(project, datasourcePluginKind),\n datasourceApi.listGlobalDatasources(datasourcePluginKind),\n ]);\n\n // Find the metadata for the plugin type they asked for so we can use it for the name of the default datasource\n const datasourcePluginMetadata = pluginMetadata.find((metadata) => metadata.kind === datasourcePluginKind);\n if (datasourcePluginMetadata === undefined) {\n throw new Error(`Could not find a Datasource plugin with kind '${datasourcePluginKind}'`);\n }\n\n // Get helper for de-duping results properly\n const { results, addResult } = buildListDatasourceMetadataResults(datasourcePluginMetadata.display.name);\n\n // Start with dashboard datasources that have highest precedence\n if (dashboardResource.spec.datasources !== undefined) {\n for (const selectorName in dashboardResource.spec.datasources) {\n const spec = dashboardResource.spec.datasources[selectorName];\n if (spec === undefined || spec.plugin.kind !== datasourcePluginKind) continue;\n addResult(spec, selectorName);\n }\n }\n\n // Now look at project-level datasources\n for (const datasource of datasources) {\n const selectorName = datasource.metadata.name;\n addResult(datasource.spec, selectorName);\n }\n\n // And finally global datasources\n for (const globalDatasource of globalDatasources) {\n const selectorName = globalDatasource.metadata.name;\n addResult(globalDatasource.spec, selectorName);\n }\n\n return results;\n });\n\n const ctxValue: DatasourceStore = useMemo(\n () => ({\n getDatasource,\n getDatasourceClient,\n listDatasourceMetadata,\n }),\n [getDatasource, getDatasourceClient, listDatasourceMetadata]\n );\n\n return <DatasourceStoreContext.Provider value={ctxValue}>{children}</DatasourceStoreContext.Provider>;\n}\n\n// Helper to find a datasource in the list embedded in a dashboard spec\nfunction findDashboardDatasource(dashboardDatasources: DashboardSpec['datasources'], selector: DatasourceSelector) {\n if (dashboardDatasources === undefined) return undefined;\n\n // If using a name in the selector...\n if (selector.name !== undefined) {\n const named = dashboardDatasources[selector.name];\n if (named === undefined) return undefined;\n return named.plugin.kind === selector.kind ? named : undefined;\n }\n\n // If only using a kind, try to find one with that kind that is the default\n return Object.values(dashboardDatasources).find((ds) => ds.plugin.kind === selector.kind && ds.default === true);\n}\n\n// Helper for building a list of DatasourceMetadata results that will take care of de-duping already used selectors\nfunction buildListDatasourceMetadataResults(pluginDisplayName: string) {\n const results: DatasourceMetadata[] = [];\n const usedNames = new Set<string>();\n let defaultAdded = false;\n const addResult = (spec: DatasourceSpec, selectorName: string) => {\n // If we haven't added a default yet and this is a default, add default option to the beginning of the results\n if (spec.default && defaultAdded === false) {\n results.unshift({\n name: `Default ${pluginDisplayName}`,\n selector: {\n kind: spec.plugin.kind,\n },\n });\n defaultAdded = true;\n }\n\n // If we already have a datasource with this selector name, ignore it, otherwise add to end of list\n if (usedNames.has(selectorName)) return;\n\n results.push({\n name: spec.display?.name ?? selectorName,\n selector: {\n kind: spec.plugin.kind,\n name: selectorName,\n },\n });\n usedNames.add(selectorName);\n };\n\n return { results, addResult };\n}\n"],"names":["useCallback","useMemo","useEvent","DatasourceStoreContext","usePluginRegistry","DatasourceStoreProvider","props","dashboardResource","datasourceApi","children","project","metadata","getPlugin","listPluginMetadata","findDatasource","selector","datasources","spec","dashboardDatasource","findDashboardDatasource","undefined","proxyUrl","datasource","getDatasource","resource","globalDatasource","getGlobalDatasource","Error","kind","name","getDatasourceClient","getClient","plugin","Promise","all","createClient","listDatasourceMetadata","datasourcePluginKind","pluginMetadata","globalDatasources","listDatasources","listGlobalDatasources","datasourcePluginMetadata","find","results","addResult","buildListDatasourceMetadataResults","display","selectorName","ctxValue","Provider","value","dashboardDatasources","named","Object","values","ds","default","pluginDisplayName","usedNames","Set","defaultAdded","unshift","has","push","add"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;AAAA,SAASA,WAAW,EAAEC,OAAO,QAAQ,OAAO,CAAC;AAC7C,SAOEC,QAAQ,QACH,kBAAkB,CAAC;AAC1B,SACEC,sBAAsB,EAEtBC,iBAAiB,QAEZ,2BAA2B,CAAC;AAwBnC;;CAEC,GACD,OAAO,SAASC,uBAAuB,CAACC,KAAmC,EAAE;IAC3E,MAAM,EAAEC,iBAAiB,CAAA,EAAEC,aAAa,CAAA,EAAEC,QAAQ,CAAA,EAAE,GAAGH,KAAK,AAAC;IAC7D,MAAM,EAAEI,OAAO,CAAA,EAAE,GAAGH,iBAAiB,CAACI,QAAQ,AAAC;IAE/C,MAAM,EAAEC,SAAS,CAAA,EAAEC,kBAAkB,CAAA,EAAE,GAAGT,iBAAiB,EAAE,AAAC;IAE9D,MAAMU,cAAc,GAA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a,IAAI,EAAEX,IAAI,CAACe,MAAM,CAACJ,IAAI;iBACvB;aACF,CAAC,CAAC;YACHiC,YAAY,GAAG,IAAI,CAAC;QACtB,CAAC;QAED,mGAAmG;QACnG,IAAIF,SAAS,CAACI,GAAG,CAACf,YAAY,CAAC,EAAE,OAAO;YAGhC/B,IAAkB;QAD1B2B,OAAO,CAACoB,IAAI,CAAC;YACXnC,IAAI,EAAEZ,CAAAA,IAAkB,GAAlBA,CAAAA,GAAY,GAAZA,IAAI,CAAC8B,OAAO,cAAZ9B,GAAY,WAAM,GAAlBA,KAAAA,CAAkB,GAAlBA,GAAY,CAAEY,IAAI,cAAlBZ,IAAkB,cAAlBA,IAAkB,GAAI+B,YAAY;YACxCjC,QAAQ,EAAE;gBACRa,IAAI,EAAEX,IAAI,CAACe,MAAM,CAACJ,IAAI;gBACtBC,IAAI,EAAEmB,YAAY;aACnB;SACF,CAAC,CAAC;QACHW,SAAS,CAACM,GAAG,CAACjB,YAAY,CAAC,CAAC;IAC9B,CAAC,AAAC;IAEF,OAAO;QAAEJ,OAAO;QAAEC,SAAS;KAAE,CAAC;AAChC,CAAC"}
@@ -1 +1,40 @@
1
- import{jsx as _jsx}from"react/jsx-runtime";import{useContext,useMemo}from"react";import{QueryStringContext}from"@perses-dev/plugin-system";export function QueryStringProvider(r){const{queryString:e,setQueryString:t,children:n}=r,o=useMemo((()=>({queryString:e,setQueryString:t})),[e,t]);return _jsx(QueryStringContext.Provider,{value:o,children:n})}export function useQueryString(){const r=useContext(QueryStringContext);if(void 0===r)throw new Error("No QueryStringContext found. Did you forget a Provider?");return r}
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 { jsx as _jsx } from "react/jsx-runtime";
14
+ import React, { useContext, useMemo } from 'react';
15
+ import { QueryStringContext } from '@perses-dev/plugin-system';
16
+ /**
17
+ * Allows apps to provide their own query string implementations
18
+ */ export function QueryStringProvider(props) {
19
+ const { queryString , setQueryString , children } = props;
20
+ const ctx = useMemo(()=>({
21
+ queryString,
22
+ setQueryString
23
+ }), [
24
+ queryString,
25
+ setQueryString
26
+ ]);
27
+ return /*#__PURE__*/ _jsx(QueryStringContext.Provider, {
28
+ value: ctx,
29
+ children: children
30
+ });
31
+ }
32
+ export function useQueryString() {
33
+ const ctx = useContext(QueryStringContext);
34
+ if (ctx === undefined) {
35
+ throw new Error('No QueryStringContext found. Did you forget a Provider?');
36
+ }
37
+ return ctx;
38
+ }
39
+
40
+ //# sourceMappingURL=QueryStringProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/context/QueryStringProvider.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 React, { useContext, useMemo } from 'react';\nimport { QueryString, QueryStringContext } from '@perses-dev/plugin-system';\n\nexport interface QueryStringProviderProps {\n queryString: URLSearchParams;\n setQueryString?: (queryString: URLSearchParams) => void;\n children?: React.ReactNode;\n}\n\n/**\n * Allows apps to provide their own query string implementations\n */\nexport function QueryStringProvider(props: QueryStringProviderProps) {\n const { queryString, setQueryString, children } = props;\n\n const ctx = useMemo(() => ({ queryString, setQueryString }), [queryString, setQueryString]);\n\n return <QueryStringContext.Provider value={ctx}>{children}</QueryStringContext.Provider>;\n}\n\nexport function useQueryString(): QueryString {\n const ctx = useContext(QueryStringContext);\n if (ctx === undefined) {\n throw new Error('No QueryStringContext found. Did you forget a Provider?');\n }\n return ctx;\n}\n"],"names":["React","useContext","useMemo","QueryStringContext","QueryStringProvider","props","queryString","setQueryString","children","ctx","Provider","value","useQueryString","undefined","Error"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;AAAA,OAAOA,KAAK,IAAIC,UAAU,EAAEC,OAAO,QAAQ,OAAO,CAAC;AACnD,SAAsBC,kBAAkB,QAAQ,2BAA2B,CAAC;AAQ5E;;CAEC,GACD,OAAO,SAASC,mBAAmB,CAACC,KAA+B,EAAE;IACnE,MAAM,EAAEC,WAAW,CAAA,EAAEC,cAAc,CAAA,EAAEC,QAAQ,CAAA,EAAE,GAAGH,KAAK,AAAC;IAExD,MAAMI,GAAG,GAAGP,OAAO,CAAC,IAAO,CAAA;YAAEI,WAAW;YAAEC,cAAc;SAAE,CAAA,AAAC,EAAE;QAACD,WAAW;QAAEC,cAAc;KAAC,CAAC,AAAC;IAE5F,qBAAO,KAACJ,kBAAkB,CAACO,QAAQ;QAACC,KAAK,EAAEF,GAAG;kBAAGD,QAAQ;MAA+B,CAAC;AAC3F,CAAC;AAED,OAAO,SAASI,cAAc,GAAgB;IAC5C,MAAMH,GAAG,GAAGR,UAAU,CAACE,kBAAkB,CAAC,AAAC;IAC3C,IAAIM,GAAG,KAAKI,SAAS,EAAE;QACrB,MAAM,IAAIC,KAAK,CAAC,yDAAyD,CAAC,CAAC;IAC7E,CAAC;IACD,OAAOL,GAAG,CAAC;AACb,CAAC"}
@@ -1,10 +1,13 @@
1
1
  /// <reference types="react" />
2
- import { VariableStateMap, VariableState, VariableName, VariableValue, VariableDefinition } from '@perses-dev/core';
2
+ import { VariableStateMap, VariableState, VariableOption } from '@perses-dev/plugin-system';
3
+ import { VariableName, VariableValue, VariableDefinition } from '@perses-dev/core';
3
4
  declare type TemplateVariableStore = {
4
5
  variableDefinitions: VariableDefinition[];
5
6
  variableState: VariableStateMap;
6
7
  setVariableValue: (variableName: VariableName, value: VariableValue) => void;
7
- loadTemplateVariable: (name: VariableName) => Promise<void>;
8
+ setVariableOptions: (name: VariableName, options: VariableOption[]) => void;
9
+ setVariableLoading: (name: VariableName, loading: boolean) => void;
10
+ setVariableDefinitions: (definitions: VariableDefinition[]) => void;
8
11
  };
9
12
  export declare function useTemplateVariableValues(variableNames?: string[]): VariableStateMap;
10
13
  export declare function useTemplateVariable(name: string): {
@@ -13,7 +16,9 @@ export declare function useTemplateVariable(name: string): {
13
16
  };
14
17
  export declare function useTemplateVariableActions(): {
15
18
  setVariableValue: (variableName: string, value: VariableValue) => void;
16
- loadTemplateVariable: (name: string) => Promise<void>;
19
+ setVariableLoading: (name: string, loading: boolean) => void;
20
+ setVariableOptions: (name: string, options: VariableOption[]) => void;
21
+ setVariableDefinitions: (definitions: VariableDefinition[]) => void;
17
22
  };
18
23
  export declare function useTemplateVariableDefinitions(): VariableDefinition[];
19
24
  export declare function useTemplateVariableStore(): TemplateVariableStore;
@@ -1 +1 @@
1
- {"version":3,"file":"TemplateVariableProvider.d.ts","sourceRoot":"","sources":["../../src/context/TemplateVariableProvider.tsx"],"names":[],"mappings":";AAkBA,OAAO,EACL,gBAAgB,EAChB,aAAa,EACb,YAAY,EACZ,aAAa,EAEb,kBAAkB,EAEnB,MAAM,kBAAkB,CAAC;AAE1B,aAAK,qBAAqB,GAAG;IAC3B,mBAAmB,EAAE,kBAAkB,EAAE,CAAC;IAC1C,aAAa,EAAE,gBAAgB,CAAC;IAChC,gBAAgB,EAAE,CAAC,YAAY,EAAE,YAAY,EAAE,KAAK,EAAE,aAAa,KAAK,IAA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,EAAE,MAAM,EAAE,oBAqBjE;AAED,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM;;;EAU/C;AAED,wBAAgB,0BAA0B;;;;;EAUzC;AAED,wBAAgB,8BAA8B,yBAG7C;AAED,wBAAgB,wBAAwB,0BAGvC;AAoFD,wBAAgB,wBAAwB,CAAC,EACvC,QAAQ,EACR,0BAA+B,GAChC,EAAE;IACD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,0BAA0B,CAAC,EAAE,kBAAkB,EAAE,CAAC;CACnD,eAQA"}
@@ -1 +1,199 @@
1
- import{jsx as _jsx}from"react/jsx-runtime";import{createContext,useContext}from"react";import{createStore,useStore}from"zustand";import{immer}from"zustand/middleware/immer";import{devtools}from"zustand/middleware";import{TemplateVariableContext}from"@perses-dev/plugin-system";import{DEFAULT_ALL_VALUE as ALL_VALUE}from"@perses-dev/core";const TemplateVariableStoreContext=createContext(void 0);function useTemplateVariableStoreCtx(){const e=useContext(TemplateVariableStoreContext);if(!e)throw new Error("TemplateVariableStoreContext not initialized");return e}export function useTemplateVariableValues(e){const t=useTemplateVariableStoreCtx();return useStore(t,(t=>{const a=null!=e?e:Object.keys(t.variableState),r={};return a.forEach((e=>{const a=t.variableState[e];a&&(r[e]=a)})),r}),((e,t)=>JSON.stringify(e)===JSON.stringify(t)))}export function useTemplateVariable(e){const t=useTemplateVariableStoreCtx();return useStore(t,(t=>({state:t.variableState[e],definition:t.variableDefinitions.find((t=>t.name===e))})))}export function useTemplateVariableActions(){const e=useTemplateVariableStoreCtx();return useStore(e,(e=>({setVariableValue:e.setVariableValue,loadTemplateVariable:e.loadTemplateVariable})))}export function useTemplateVariableDefinitions(){const e=useTemplateVariableStoreCtx();return useStore(e,(e=>e.variableDefinitions))}export function useTemplateVariableStore(){const e=useTemplateVariableStoreCtx();return useStore(e)}function PluginProvider({children:e}){const t=useTemplateVariableValues();return _jsx(TemplateVariableContext.Provider,{value:{state:t},children:e})}function createTemplateVariableSrvStore({initialVariableDefinitions:e=[]}){return createStore()(devtools(immer(((t,a)=>({variableState:hydrateTemplateVariableStates(e),variableDefinitions:e,loadTemplateVariable:async e=>{const r=a().variableDefinitions.find((t=>t.name===e));if(!r)return;t((t=>{const a=t.variableState[e];a&&(a.loading=!0)}));const{data:i}=await loadTemplateVariables();r.options.allowAllValue&&i.unshift(getAllOption()),t((t=>{const a=t.variableState[e];a&&(a.options=i,a.loading=!1)}))},setVariableValue:(e,a)=>t((t=>{let r=a;const i=t.variableState[e];i&&(Array.isArray(r)&&r.includes(ALL_VALUE)&&(r=r.at(-1)===ALL_VALUE?[ALL_VALUE]:r.filter((e=>e!==ALL_VALUE))),i.value=r)}))})))))}export function TemplateVariableProvider({children:e,initialVariableDefinitions:t=[]}){const a=createTemplateVariableSrvStore({initialVariableDefinitions:t});return _jsx(TemplateVariableStoreContext.Provider,{value:a,children:_jsx(PluginProvider,{children:e})})}async function loadTemplateVariables(){const e=Math.floor(1e4*Math.random())+1e3;return await new Promise((t=>setTimeout(t,e))),{data:["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"].map((e=>({label:e,value:e})))}}function getAllOption(){return{label:"All",value:ALL_VALUE}}function hydrateTemplateVariableState(e){var t,a;const r=e,i={value:null,loading:!1};switch(r.kind){case"TextVariable":i.value=r.options.value;break;case"ListVariable":if(i.options=[],r.options.allowAllValue&&i.options.unshift({label:"All",value:ALL_VALUE}),i.options.length>0&&!i.value){const e=null!==(a=null===(t=i.options[0])||void 0===t?void 0:t.value)&&void 0!==a?a:null;null!==e&&(i.value=r.options.allowMultiple?[e]:e)}}return i}function hydrateTemplateVariableStates(e){const t={};return e.forEach((e=>{t[e.name]=hydrateTemplateVariableState(e)})),t}
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 { jsx as _jsx } from "react/jsx-runtime";
14
+ import { createContext, useContext, useMemo, useState } from 'react';
15
+ import { createStore, useStore } from 'zustand';
16
+ import { immer } from 'zustand/middleware/immer';
17
+ import { devtools } from 'zustand/middleware';
18
+ import { TemplateVariableContext, DEFAULT_ALL_VALUE as ALL_VALUE } from '@perses-dev/plugin-system';
19
+ const TemplateVariableStoreContext = /*#__PURE__*/ createContext(undefined);
20
+ function useTemplateVariableStoreCtx() {
21
+ const context = useContext(TemplateVariableStoreContext);
22
+ if (!context) {
23
+ throw new Error('TemplateVariableStoreContext not initialized');
24
+ }
25
+ return context;
26
+ }
27
+ export function useTemplateVariableValues(variableNames) {
28
+ const store = useTemplateVariableStoreCtx();
29
+ const state = useStore(store, (s)=>{
30
+ const names = variableNames !== null && variableNames !== void 0 ? variableNames : Object.keys(s.variableState);
31
+ const vars = {};
32
+ names.forEach((name)=>{
33
+ const varState = s.variableState[name];
34
+ if (!varState) {
35
+ return;
36
+ }
37
+ vars[name] = varState;
38
+ });
39
+ return vars;
40
+ }, (left, right)=>{
41
+ return JSON.stringify(left) === JSON.stringify(right);
42
+ });
43
+ return state;
44
+ }
45
+ export function useTemplateVariable(name) {
46
+ const store = useTemplateVariableStoreCtx();
47
+ return useStore(store, (s)=>{
48
+ const variableState = s.variableState[name];
49
+ const definition = s.variableDefinitions.find((v)=>v.spec.name === name);
50
+ return {
51
+ state: variableState,
52
+ definition
53
+ };
54
+ });
55
+ }
56
+ export function useTemplateVariableActions() {
57
+ const store = useTemplateVariableStoreCtx();
58
+ return useStore(store, (s)=>{
59
+ return {
60
+ setVariableValue: s.setVariableValue,
61
+ setVariableLoading: s.setVariableLoading,
62
+ setVariableOptions: s.setVariableOptions,
63
+ setVariableDefinitions: s.setVariableDefinitions
64
+ };
65
+ });
66
+ }
67
+ export function useTemplateVariableDefinitions() {
68
+ const store = useTemplateVariableStoreCtx();
69
+ return useStore(store, (s)=>s.variableDefinitions);
70
+ }
71
+ export function useTemplateVariableStore() {
72
+ const store = useTemplateVariableStoreCtx();
73
+ return useStore(store);
74
+ }
75
+ function PluginProvider({ children }) {
76
+ const originalValues = useTemplateVariableValues();
77
+ const values = useMemo(()=>{
78
+ const contextValues = {};
79
+ // This will loop through all the current variables values
80
+ // and update any variables that have ALL_VALUE as their current value
81
+ // to include all options.
82
+ Object.keys(originalValues).forEach((name)=>{
83
+ const v = {
84
+ ...originalValues[name]
85
+ };
86
+ if (v.value === ALL_VALUE) {
87
+ var ref;
88
+ var ref1;
89
+ v.value = (ref1 = (ref = v.options) === null || ref === void 0 ? void 0 : ref.map((o)=>o.value)) !== null && ref1 !== void 0 ? ref1 : null;
90
+ }
91
+ contextValues[name] = v;
92
+ });
93
+ return contextValues;
94
+ }, [
95
+ originalValues
96
+ ]);
97
+ return /*#__PURE__*/ _jsx(TemplateVariableContext.Provider, {
98
+ value: {
99
+ state: values
100
+ },
101
+ children: children
102
+ });
103
+ }
104
+ function createTemplateVariableSrvStore({ initialVariableDefinitions =[] }) {
105
+ const store = createStore()(devtools(immer((set)=>({
106
+ variableState: hydrateTemplateVariableStates(initialVariableDefinitions),
107
+ variableDefinitions: initialVariableDefinitions,
108
+ setVariableDefinitions (definitions) {
109
+ set((state)=>{
110
+ state.variableDefinitions = definitions;
111
+ state.variableState = hydrateTemplateVariableStates(definitions);
112
+ });
113
+ },
114
+ setVariableOptions (name, options) {
115
+ set((state)=>{
116
+ const varState = state.variableState[name];
117
+ if (!varState) {
118
+ return;
119
+ }
120
+ varState.options = options;
121
+ });
122
+ },
123
+ setVariableLoading (name, loading) {
124
+ set((state)=>{
125
+ const varState = state.variableState[name];
126
+ if (!varState) {
127
+ return;
128
+ }
129
+ varState.loading = loading;
130
+ });
131
+ },
132
+ setVariableValue: (name, value)=>set((state)=>{
133
+ let val = value;
134
+ const varState = state.variableState[name];
135
+ if (!varState) {
136
+ return;
137
+ }
138
+ // Make sure there is only one all value
139
+ if (Array.isArray(val) && val.includes(ALL_VALUE)) {
140
+ if (val.at(-1) === ALL_VALUE) {
141
+ val = ALL_VALUE;
142
+ } else {
143
+ val = val.filter((v)=>v !== ALL_VALUE);
144
+ }
145
+ }
146
+ varState.value = val;
147
+ })
148
+ }))));
149
+ return store;
150
+ }
151
+ export function TemplateVariableProvider({ children , initialVariableDefinitions =[] }) {
152
+ const [store] = useState(createTemplateVariableSrvStore({
153
+ initialVariableDefinitions
154
+ }));
155
+ return /*#__PURE__*/ _jsx(TemplateVariableStoreContext.Provider, {
156
+ value: store,
157
+ children: /*#__PURE__*/ _jsx(PluginProvider, {
158
+ children: children
159
+ })
160
+ });
161
+ }
162
+ /** Helpers */ function hydrateTemplateVariableState(definition) {
163
+ const v = definition;
164
+ var _default_value;
165
+ const varState = {
166
+ value: (_default_value = v.spec.default_value) !== null && _default_value !== void 0 ? _default_value : null,
167
+ loading: false
168
+ };
169
+ switch(v.kind){
170
+ case 'TextVariable':
171
+ varState.value = v.spec.value;
172
+ break;
173
+ case 'ListVariable':
174
+ varState.options = [];
175
+ if (varState.options.length > 0 && !varState.value) {
176
+ var ref;
177
+ var ref1;
178
+ const firstOptionValue = (ref1 = (ref = varState.options[0]) === null || ref === void 0 ? void 0 : ref.value) !== null && ref1 !== void 0 ? ref1 : null;
179
+ if (firstOptionValue !== null) {
180
+ varState.value = v.spec.allow_multiple ? [
181
+ firstOptionValue
182
+ ] : firstOptionValue;
183
+ }
184
+ }
185
+ break;
186
+ default:
187
+ break;
188
+ }
189
+ return varState;
190
+ }
191
+ function hydrateTemplateVariableStates(definitions) {
192
+ const state = {};
193
+ definitions.forEach((v)=>{
194
+ state[v.spec.name] = hydrateTemplateVariableState(v);
195
+ });
196
+ return state;
197
+ }
198
+
199
+ //# sourceMappingURL=TemplateVariableProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/context/TemplateVariableProvider.tsx"],"sourcesContent":["// Copyright 2022 The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { createContext, useContext, useMemo, useState } from 'react';\nimport { createStore, useStore } from 'zustand';\nimport { immer } from 'zustand/middleware/immer';\nimport { devtools } from 'zustand/middleware';\nimport {\n TemplateVariableContext,\n VariableStateMap,\n VariableState,\n VariableOption,\n DEFAULT_ALL_VALUE as ALL_VALUE,\n} from '@perses-dev/plugin-system';\nimport { VariableName, VariableValue, VariableDefinition } from '@perses-dev/core';\n\ntype TemplateVariableStore = {\n variableDefinitions: VariableDefinition[];\n variableState: VariableStateMap;\n setVariableValue: (variableName: VariableName, value: VariableValue) => void;\n setVariableOptions: (name: VariableName, options: VariableOption[]) => void;\n setVariableLoading: (name: VariableName, loading: boolean) => void;\n setVariableDefinitions: (definitions: VariableDefinition[]) => void;\n};\n\nconst TemplateVariableStoreContext = createContext<ReturnType<typeof createTemplateVariableSrvStore> | undefined>(\n undefined\n);\nfunction useTemplateVariableStoreCtx() {\n const context = useContext(TemplateVariableStoreContext);\n if (!context) {\n throw new Error('TemplateVariableStoreContext not initialized');\n }\n return context;\n}\n\nexport function useTemplateVariableValues(variableNames?: string[]) {\n const store = useTemplateVariableStoreCtx();\n const state = useStore(\n store,\n (s) => {\n const names = variableNames ?? Object.keys(s.variableState);\n const vars: VariableStateMap = {};\n names.forEach((name) => {\n const varState = s.variableState[name];\n if (!varState) {\n return;\n }\n vars[name] = varState;\n });\n return vars;\n },\n (left, right) => {\n return JSON.stringify(left) === JSON.stringify(right);\n }\n );\n return state;\n}\n\nexport function useTemplateVariable(name: string) {\n const store = useTemplateVariableStoreCtx();\n return useStore(store, (s) => {\n const variableState = s.variableState[name];\n const definition = s.variableDefinitions.find((v) => v.spec.name === name);\n return {\n state: variableState,\n definition,\n };\n });\n}\n\nexport function useTemplateVariableActions() {\n const store = useTemplateVariableStoreCtx();\n return useStore(store, (s) => {\n return {\n setVariableValue: s.setVariableValue,\n setVariableLoading: s.setVariableLoading,\n setVariableOptions: s.setVariableOptions,\n setVariableDefinitions: s.setVariableDefinitions,\n };\n });\n}\n\nexport function useTemplateVariableDefinitions() {\n const store = useTemplateVariableStoreCtx();\n return useStore(store, (s) => s.variableDefinitions);\n}\n\nexport function useTemplateVariableStore() {\n const store = useTemplateVariableStoreCtx();\n return useStore(store);\n}\n\nfunction PluginProvider({ children }: { children: React.ReactNode }) {\n const originalValues = useTemplateVariableValues();\n\n const values = useMemo(() => {\n const contextValues: VariableStateMap = {};\n\n // This will loop through all the current variables values\n // and update any variables that have ALL_VALUE as their current value\n // to include all options.\n Object.keys(originalValues).forEach((name) => {\n const v = { ...originalValues[name] } as VariableState;\n if (v.value === ALL_VALUE) {\n v.value = v.options?.map((o: { value: string }) => o.value) ?? null;\n }\n contextValues[name] = v;\n });\n return contextValues;\n }, [originalValues]);\n\n return <TemplateVariableContext.Provider value={{ state: values }}>{children}</TemplateVariableContext.Provider>;\n}\n\ninterface TemplateVariableSrvArgs {\n initialVariableDefinitions?: VariableDefinition[];\n}\n\nfunction createTemplateVariableSrvStore({ initialVariableDefinitions = [] }: TemplateVariableSrvArgs) {\n const store = createStore<TemplateVariableStore>()(\n devtools(\n immer((set) => ({\n variableState: hydrateTemplateVariableStates(initialVariableDefinitions),\n variableDefinitions: initialVariableDefinitions,\n setVariableDefinitions(definitions: VariableDefinition[]) {\n set((state) => {\n state.variableDefinitions = definitions;\n state.variableState = hydrateTemplateVariableStates(definitions);\n });\n },\n setVariableOptions(name, options) {\n set((state) => {\n const varState = state.variableState[name];\n if (!varState) {\n return;\n }\n varState.options = options;\n });\n },\n setVariableLoading(name, loading) {\n set((state) => {\n const varState = state.variableState[name];\n if (!varState) {\n return;\n }\n varState.loading = loading;\n });\n },\n\n setVariableValue: (name, value) =>\n set((state) => {\n let val = value;\n const varState = state.variableState[name];\n if (!varState) {\n return;\n }\n\n // Make sure there is only one all value\n if (Array.isArray(val) && val.includes(ALL_VALUE)) {\n if (val.at(-1) === ALL_VALUE) {\n val = ALL_VALUE;\n } else {\n val = val.filter((v) => v !== ALL_VALUE);\n }\n }\n varState.value = val;\n }),\n }))\n )\n );\n\n return store;\n}\n\nexport function TemplateVariableProvider({\n children,\n initialVariableDefinitions = [],\n}: {\n children: React.ReactNode;\n initialVariableDefinitions?: VariableDefinition[];\n}) {\n const [store] = useState(createTemplateVariableSrvStore({ initialVariableDefinitions }));\n\n return (\n <TemplateVariableStoreContext.Provider value={store}>\n <PluginProvider>{children}</PluginProvider>\n </TemplateVariableStoreContext.Provider>\n );\n}\n\n/** Helpers */\n\nfunction hydrateTemplateVariableState(definition: VariableDefinition) {\n const v = definition;\n const varState: VariableState = {\n value: v.spec.default_value ?? null,\n loading: false,\n };\n switch (v.kind) {\n case 'TextVariable':\n varState.value = v.spec.value;\n break;\n case 'ListVariable':\n varState.options = [];\n if (varState.options.length > 0 && !varState.value) {\n const firstOptionValue = varState.options[0]?.value ?? null;\n if (firstOptionValue !== null) {\n varState.value = v.spec.allow_multiple ? [firstOptionValue] : firstOptionValue;\n }\n }\n break;\n default:\n break;\n }\n return varState;\n}\n\nfunction hydrateTemplateVariableStates(definitions: VariableDefinition[]): VariableStateMap {\n const state: VariableStateMap = {};\n definitions.forEach((v) => {\n state[v.spec.name] = hydrateTemplateVariableState(v);\n });\n return state;\n}\n"],"names":["createContext","useContext","useMemo","useState","createStore","useStore","immer","devtools","TemplateVariableContext","DEFAULT_ALL_VALUE","ALL_VALUE","TemplateVariableStoreContext","undefined","useTemplateVariableStoreCtx","context","Error","useTemplateVariableValues","variableNames","store","state","s","names","Object","keys","variableState","vars","forEach","name","varState","left","right","JSON","stringify","useTemplateVariable","definition","variableDefinitions","find","v","spec","useTemplateVariableActions","setVariableValue","setVariableLoading","setVariableOptions","setVariableDefinitions","useTemplateVariableDefinitions","useTemplateVariableStore","PluginProvider","children","originalValues","values","contextValues","value","options","map","o","Provider","createTemplateVariableSrvStore","initialVariableDefinitions","set","hydrateTemplateVariableStates","definitions","loading","val","Array","isArray","includes","at","filter","TemplateVariableProvider","hydrateTemplateVariableState","default_value","kind","length","firstOptionValue","allow_multiple"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;AAAA,SAASA,aAAa,EAAEC,UAAU,EAAEC,OAAO,EAAEC,QAAQ,QAAQ,OAAO,CAAC;AACrE,SAASC,WAAW,EAAEC,QAAQ,QAAQ,SAAS,CAAC;AAChD,SAASC,KAAK,QAAQ,0BAA0B,CAAC;AACjD,SAASC,QAAQ,QAAQ,oBAAoB,CAAC;AAC9C,SACEC,uBAAuB,EAIvBC,iBAAiB,IAAIC,SAAS,QACzB,2BAA2B,CAAC;AAYnC,MAAMC,4BAA4B,iBAAGX,aAAa,CAChDY,SAAS,CACV,AAAC;AACF,SAASC,2BAA2B,GAAG;IACrC,MAAMC,OAAO,GAAGb,UAAU,CAACU,4BAA4B,CAAC,AAAC;IACzD,IAAI,CAACG,OAAO,EAAE;QACZ,MAAM,IAAIC,KAAK,CAAC,8CAA8C,CAAC,CAAC;IAClE,CAAC;IACD,OAAOD,OAAO,CAAC;AACjB,CAAC;AAED,OAAO,SAASE,yBAAyB,CAACC,aAAwB,EAAE;IAClE,MAAMC,KAAK,GAAGL,2BAA2B,EAAE,AAAC;IAC5C,MAAMM,KAAK,GAAGd,QAAQ,CACpBa,K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YACE,MAAM;KACT;IACD,OAAO7C,QAAQ,CAAC;AAClB,CAAC;AAED,SAAS+B,6BAA6B,CAACC,WAAiC,EAAoB;IAC1F,MAAMzC,KAAK,GAAqB,EAAE,AAAC;IACnCyC,WAAW,CAAClC,OAAO,CAAC,CAACW,CAAC,GAAK;QACzBlB,KAAK,CAACkB,CAAC,CAACC,IAAI,CAACX,IAAI,CAAC,GAAG0C,4BAA4B,CAAChC,CAAC,CAAC,CAAC;IACvD,CAAC,CAAC,CAAC;IACH,OAAOlB,KAAK,CAAC;AACf,CAAC"}
@@ -1 +1,72 @@
1
- import{jsx as _jsx}from"react/jsx-runtime";import{useState,useMemo,useCallback}from"react";import{getUnixTime}from"date-fns";import{toAbsoluteTimeRange,isRelativeTimeRange}from"@perses-dev/core";import{TimeRangeContext,useQueryString}from"@perses-dev/plugin-system";export function TimeRangeProvider(e){const{initialTimeRange:t,children:i,onTimeRangeChange:n}=e,{queryString:s,setQueryString:r}=useQueryString(),a=isRelativeTimeRange(t)?toAbsoluteTimeRange(t):t,[o,m]=useState(a),g=useCallback((e=>{if(void 0===n)if(isRelativeTimeRange(e))r?(s.set("start",e.pastDuration),s.delete("end"),r(s)):m(toAbsoluteTimeRange(e));else if(r){const t=1e3*getUnixTime(e.start),i=1e3*getUnixTime(e.end);s.set("start",t.toString()),s.set("end",i.toString()),r(s)}else m(e);else n(e)}),[s,r,n]),u=useMemo((()=>({timeRange:o,setTimeRange:g})),[o,g]);return _jsx(TimeRangeContext.Provider,{value:u,children:i})}
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 { jsx as _jsx } from "react/jsx-runtime";
14
+ import React, { useState, useMemo, useCallback } from 'react';
15
+ import { getUnixTime } from 'date-fns';
16
+ import { toAbsoluteTimeRange, isRelativeTimeRange } from '@perses-dev/core';
17
+ import { TimeRangeContext, useQueryString } from '@perses-dev/plugin-system';
18
+ /**
19
+ * Provider implementation that supplies the time range state at runtime.
20
+ */ export function TimeRangeProvider(props) {
21
+ const { initialTimeRange , children , onTimeRangeChange } = props;
22
+ const { queryString , setQueryString } = useQueryString();
23
+ const defaultTimeRange = isRelativeTimeRange(initialTimeRange) ? toAbsoluteTimeRange(initialTimeRange) : initialTimeRange;
24
+ const [timeRange, setActiveTimeRange] = useState(defaultTimeRange);
25
+ const setTimeRange = useCallback((value)=>{
26
+ if (onTimeRangeChange !== undefined) {
27
+ // optional callback to override default behavior
28
+ onTimeRangeChange(value);
29
+ return;
30
+ }
31
+ if (isRelativeTimeRange(value)) {
32
+ if (setQueryString) {
33
+ queryString.set('start', value.pastDuration);
34
+ // end not required for relative time but may have been set by AbsoluteTimePicker or zoom
35
+ queryString.delete('end');
36
+ setQueryString(queryString);
37
+ } else {
38
+ setActiveTimeRange(toAbsoluteTimeRange(value));
39
+ }
40
+ return;
41
+ }
42
+ // allows app to specify whether query params should be source of truth for active time range
43
+ if (setQueryString) {
44
+ // Absolute URL example) ?start=1663707045000&end=1663713330000
45
+ // currently set from ViewDashboard initial queryString, AbsoluteTimePicker, or LineChart panel onDataZoom
46
+ const startUnixMs = getUnixTime(value.start) * 1000;
47
+ const endUnixMs = getUnixTime(value.end) * 1000;
48
+ queryString.set('start', startUnixMs.toString());
49
+ queryString.set('end', endUnixMs.toString());
50
+ setQueryString(queryString);
51
+ } else {
52
+ setActiveTimeRange(value);
53
+ }
54
+ }, [
55
+ queryString,
56
+ setQueryString,
57
+ onTimeRangeChange
58
+ ]);
59
+ const ctx = useMemo(()=>({
60
+ timeRange,
61
+ setTimeRange
62
+ }), [
63
+ timeRange,
64
+ setTimeRange
65
+ ]);
66
+ return /*#__PURE__*/ _jsx(TimeRangeContext.Provider, {
67
+ value: ctx,
68
+ children: children
69
+ });
70
+ }
71
+
72
+ //# sourceMappingURL=TimeRangeProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/context/TimeRangeProvider.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 React, { useState, useMemo, useCallback } from 'react';\nimport { getUnixTime } from 'date-fns';\nimport { TimeRangeValue, AbsoluteTimeRange, toAbsoluteTimeRange, isRelativeTimeRange } from '@perses-dev/core';\nimport { TimeRange, TimeRangeContext, useQueryString } from '@perses-dev/plugin-system';\n\nexport interface TimeRangeProviderProps {\n initialTimeRange: TimeRangeValue;\n children?: React.ReactNode;\n onTimeRangeChange?: (e: TimeRangeValue) => void;\n}\n\n/**\n * Provider implementation that supplies the time range state at runtime.\n */\nexport function TimeRangeProvider(props: TimeRangeProviderProps) {\n const { initialTimeRange, children, onTimeRangeChange } = props;\n\n const { queryString, setQueryString } = useQueryString();\n\n const defaultTimeRange: AbsoluteTimeRange = isRelativeTimeRange(initialTimeRange)\n ? toAbsoluteTimeRange(initialTimeRange)\n : initialTimeRange;\n\n const [timeRange, setActiveTimeRange] = useState<AbsoluteTimeRange>(defaultTimeRange);\n\n const setTimeRange: TimeRange['setTimeRange'] = useCallback(\n (value: TimeRangeValue) => {\n if (onTimeRangeChange !== undefined) {\n // optional callback to override default behavior\n onTimeRangeChange(value);\n return;\n }\n\n if (isRelativeTimeRange(value)) {\n if (setQueryString) {\n queryString.set('start', value.pastDuration);\n // end not required for relative time but may have been set by AbsoluteTimePicker or zoom\n queryString.delete('end');\n setQueryString(queryString);\n } else {\n setActiveTimeRange(toAbsoluteTimeRange(value));\n }\n return;\n }\n\n // allows app to specify whether query params should be source of truth for active time range\n if (setQueryString) {\n // Absolute URL example) ?start=1663707045000&end=1663713330000\n // currently set from ViewDashboard initial queryString, AbsoluteTimePicker, or LineChart panel onDataZoom\n const startUnixMs = getUnixTime(value.start) * 1000;\n const endUnixMs = getUnixTime(value.end) * 1000;\n queryString.set('start', startUnixMs.toString());\n queryString.set('end', endUnixMs.toString());\n setQueryString(queryString);\n } else {\n setActiveTimeRange(value);\n }\n },\n [queryString, setQueryString, onTimeRangeChange]\n );\n\n const ctx = useMemo(() => ({ timeRange, setTimeRange }), [timeRange, setTimeRange]);\n\n return <TimeRangeContext.Provider value={ctx}>{children}</TimeRangeContext.Provider>;\n}\n"],"names":["React","useState","useMemo","useCallback","getUnixTime","toAbsoluteTimeRange","isRelativeTimeRange","TimeRangeContext","useQueryString","TimeRangeProvider","props","initialTimeRange","children","onTimeRangeChange","queryString","setQueryString","defaultTimeRange","timeRange","setActiveTimeRange","setTimeRange","value","undefined","set","pastDuration","delete","startUnixMs","start","endUnixMs","end","toString","ctx","Provider"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC;AAAA,OAAOA,KAAK,IAAIC,QAAQ,EAAEC,OAAO,EAAEC,WAAW,QAAQ,OAAO,CAAC;AAC9D,SAASC,WAAW,QAAQ,UAAU,CAAC;AACvC,SAA4CC,mBAAmB,EAAEC,mBAAmB,QAAQ,kBAAkB,CAAC;AAC/G,SAAoBC,gBAAgB,EAAEC,cAAc,QAAQ,2BAA2B,CAAC;AAQxF;;CAEC,GACD,OAAO,SAASC,i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AC,OAAO,EAAEG,WAAW,CAACI,QAAQ,EAAE,CAAC,CAAC;YACjDf,WAAW,CAACQ,GAAG,CAAC,KAAK,EAAEK,SAAS,CAACE,QAAQ,EAAE,CAAC,CAAC;YAC7Cd,cAAc,CAACD,WAAW,CAAC,CAAC;QAC9B,OAAO;YACLI,kBAAkB,CAACE,KAAK,CAAC,CAAC;QAC5B,CAAC;IACH,CAAC,EACD;QAACN,WAAW;QAAEC,cAAc;QAAEF,iBAAiB;KAAC,CACjD,AAAC;IAEF,MAAMiB,GAAG,GAAG5B,OAAO,CAAC,IAAO,CAAA;YAAEe,SAAS;YAAEE,YAAY;SAAE,CAAA,AAAC,EAAE;QAACF,SAAS;QAAEE,YAAY;KAAC,CAAC,AAAC;IAEpF,qBAAO,KAACZ,gBAAgB,CAACwB,QAAQ;QAACX,KAAK,EAAEU,GAAG;kBAAGlB,QAAQ;MAA6B,CAAC;AACvF,CAAC"}
@@ -1,7 +1,6 @@
1
- export * from './DashboardAppSlice';
2
- export * from './LayoutsSlice';
3
- export * from './TemplateVariableProvider';
4
1
  export * from './DashboardProvider';
5
- export * from './TimeRangeProvider';
2
+ export * from './DatasourceStoreProvider';
6
3
  export * from './QueryStringProvider';
4
+ export * from './TemplateVariableProvider';
5
+ export * from './TimeRangeProvider';
7
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qBAAqB,CAAC;AACpC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAaA,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,uBAAuB,CAAC;AACtC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qBAAqB,CAAC"}