@perses-dev/plugin-system 0.46.0 → 0.47.0-rc0

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 (185) hide show
  1. package/dist/cjs/components/DatasourceEditorForm/DatasourceEditorForm.js +70 -99
  2. package/dist/cjs/components/LegendOptionsEditor/LegendOptionsEditor.js +2 -1
  3. package/dist/cjs/components/MultiQueryEditor/QueryEditorContainer.js +1 -1
  4. package/dist/cjs/components/PanelSpecEditor/PanelSpecEditor.js +49 -19
  5. package/dist/cjs/components/PluginKindSelect/PluginKindSelect.js +1 -0
  6. package/dist/cjs/components/TimeRangeControls/TimeRangeControls.js +10 -71
  7. package/dist/cjs/components/Variables/VariableEditorForm/VariableEditorForm.js +408 -376
  8. package/dist/cjs/components/Variables/VariableEditorForm/VariablePreview.js +2 -2
  9. package/dist/cjs/components/Variables/variable-model.js +24 -22
  10. package/dist/cjs/context/ProjectStoreProvider.js +7 -4
  11. package/dist/cjs/context/ValidationProvider.js +69 -0
  12. package/dist/cjs/context/index.js +1 -0
  13. package/dist/cjs/index.js +0 -1
  14. package/dist/cjs/runtime/QueryCountProvider.js +83 -0
  15. package/dist/cjs/runtime/TimeRangeProvider/TimeRangeSettingsProvider.js +100 -0
  16. package/dist/cjs/runtime/TimeRangeProvider/index.js +2 -1
  17. package/dist/cjs/runtime/builtin-variables.js +1 -1
  18. package/dist/cjs/runtime/datasources.js +22 -13
  19. package/dist/cjs/runtime/index.js +3 -2
  20. package/dist/cjs/runtime/plugin-registry.js +33 -22
  21. package/dist/cjs/runtime/time-series-queries.js +3 -3
  22. package/dist/cjs/runtime/trace-queries.js +5 -1
  23. package/dist/cjs/runtime/{template-variables.js → variables.js} +19 -19
  24. package/dist/cjs/stories/shared-utils/decorators/{WithPluginSystemTemplateVariables.js → WithPluginSystemVariables.js} +8 -8
  25. package/dist/cjs/stories/shared-utils/decorators/index.js +2 -2
  26. package/dist/cjs/test/mock-data.js +25 -4
  27. package/dist/cjs/test/render.js +1 -9
  28. package/dist/cjs/test/test-plugins/bert/index.js +20 -19
  29. package/dist/cjs/utils/variables.js +23 -23
  30. package/dist/components/DatasourceEditorForm/DatasourceEditorForm.d.ts +2 -2
  31. package/dist/components/DatasourceEditorForm/DatasourceEditorForm.d.ts.map +1 -1
  32. package/dist/components/DatasourceEditorForm/DatasourceEditorForm.js +71 -100
  33. package/dist/components/DatasourceEditorForm/DatasourceEditorForm.js.map +1 -1
  34. package/dist/components/LegendOptionsEditor/LegendOptionsEditor.js +4 -3
  35. package/dist/components/LegendOptionsEditor/LegendOptionsEditor.js.map +1 -1
  36. package/dist/components/MultiQueryEditor/MultiQueryEditor.d.ts +1 -1
  37. package/dist/components/MultiQueryEditor/MultiQueryEditor.js +1 -1
  38. package/dist/components/MultiQueryEditor/MultiQueryEditor.js.map +1 -1
  39. package/dist/components/MultiQueryEditor/QueryEditorContainer.js +1 -1
  40. package/dist/components/MultiQueryEditor/QueryEditorContainer.js.map +1 -1
  41. package/dist/components/PanelSpecEditor/PanelSpecEditor.d.ts +3 -1
  42. package/dist/components/PanelSpecEditor/PanelSpecEditor.d.ts.map +1 -1
  43. package/dist/components/PanelSpecEditor/PanelSpecEditor.js +50 -20
  44. package/dist/components/PanelSpecEditor/PanelSpecEditor.js.map +1 -1
  45. package/dist/components/PluginKindSelect/PluginKindSelect.d.ts.map +1 -1
  46. package/dist/components/PluginKindSelect/PluginKindSelect.js +1 -0
  47. package/dist/components/PluginKindSelect/PluginKindSelect.js.map +1 -1
  48. package/dist/components/TimeRangeControls/TimeRangeControls.d.ts +2 -2
  49. package/dist/components/TimeRangeControls/TimeRangeControls.d.ts.map +1 -1
  50. package/dist/components/TimeRangeControls/TimeRangeControls.js +12 -70
  51. package/dist/components/TimeRangeControls/TimeRangeControls.js.map +1 -1
  52. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.d.ts.map +1 -1
  53. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js +411 -379
  54. package/dist/components/Variables/VariableEditorForm/VariableEditorForm.js.map +1 -1
  55. package/dist/components/Variables/VariableEditorForm/VariablePreview.js +2 -2
  56. package/dist/components/Variables/VariableEditorForm/VariablePreview.js.map +1 -1
  57. package/dist/components/Variables/variable-model.d.ts.map +1 -1
  58. package/dist/components/Variables/variable-model.js +25 -23
  59. package/dist/components/Variables/variable-model.js.map +1 -1
  60. package/dist/context/ProjectStoreProvider.d.ts.map +1 -1
  61. package/dist/context/ProjectStoreProvider.js +7 -4
  62. package/dist/context/ProjectStoreProvider.js.map +1 -1
  63. package/dist/context/ValidationProvider.d.ts +19 -0
  64. package/dist/context/ValidationProvider.d.ts.map +1 -0
  65. package/dist/context/ValidationProvider.js +52 -0
  66. package/dist/context/ValidationProvider.js.map +1 -0
  67. package/dist/context/index.d.ts +1 -0
  68. package/dist/context/index.d.ts.map +1 -1
  69. package/dist/context/index.js +1 -0
  70. package/dist/context/index.js.map +1 -1
  71. package/dist/index.d.ts +0 -1
  72. package/dist/index.d.ts.map +1 -1
  73. package/dist/index.js +0 -1
  74. package/dist/index.js.map +1 -1
  75. package/dist/runtime/DataQueriesProvider/model.d.ts +1 -1
  76. package/dist/runtime/DataQueriesProvider/model.d.ts.map +1 -1
  77. package/dist/runtime/DataQueriesProvider/model.js.map +1 -1
  78. package/dist/runtime/QueryCountProvider.d.ts +9 -0
  79. package/dist/runtime/QueryCountProvider.d.ts.map +1 -0
  80. package/dist/{validation/duration.js → runtime/QueryCountProvider.js} +13 -4
  81. package/dist/runtime/QueryCountProvider.js.map +1 -0
  82. package/dist/runtime/TimeRangeProvider/TimeRangeSettingsProvider.d.ts +32 -0
  83. package/dist/runtime/TimeRangeProvider/TimeRangeSettingsProvider.d.ts.map +1 -0
  84. package/dist/runtime/TimeRangeProvider/TimeRangeSettingsProvider.js +82 -0
  85. package/dist/runtime/TimeRangeProvider/TimeRangeSettingsProvider.js.map +1 -0
  86. package/dist/runtime/TimeRangeProvider/index.d.ts +1 -0
  87. package/dist/runtime/TimeRangeProvider/index.d.ts.map +1 -1
  88. package/dist/runtime/TimeRangeProvider/index.js +2 -1
  89. package/dist/runtime/TimeRangeProvider/index.js.map +1 -1
  90. package/dist/runtime/builtin-variables.d.ts +1 -1
  91. package/dist/runtime/builtin-variables.d.ts.map +1 -1
  92. package/dist/runtime/builtin-variables.js +1 -1
  93. package/dist/runtime/builtin-variables.js.map +1 -1
  94. package/dist/runtime/datasources.d.ts.map +1 -1
  95. package/dist/runtime/datasources.js +22 -13
  96. package/dist/runtime/datasources.js.map +1 -1
  97. package/dist/runtime/index.d.ts +2 -1
  98. package/dist/runtime/index.d.ts.map +1 -1
  99. package/dist/runtime/index.js +3 -2
  100. package/dist/runtime/index.js.map +1 -1
  101. package/dist/runtime/plugin-registry.d.ts.map +1 -1
  102. package/dist/runtime/plugin-registry.js +33 -22
  103. package/dist/runtime/plugin-registry.js.map +1 -1
  104. package/dist/runtime/time-series-queries.d.ts +2 -2
  105. package/dist/runtime/time-series-queries.d.ts.map +1 -1
  106. package/dist/runtime/time-series-queries.js +3 -3
  107. package/dist/runtime/time-series-queries.js.map +1 -1
  108. package/dist/runtime/trace-queries.d.ts.map +1 -1
  109. package/dist/runtime/trace-queries.js +5 -1
  110. package/dist/runtime/trace-queries.js.map +1 -1
  111. package/dist/runtime/{template-variables.d.ts → variables.d.ts} +4 -4
  112. package/dist/runtime/variables.d.ts.map +1 -0
  113. package/dist/runtime/{template-variables.js → variables.js} +18 -17
  114. package/dist/runtime/variables.js.map +1 -0
  115. package/dist/stories/shared-utils/decorators/WithPluginSystemVariables.d.ts +12 -0
  116. package/dist/stories/shared-utils/decorators/WithPluginSystemVariables.d.ts.map +1 -0
  117. package/dist/stories/shared-utils/decorators/{WithPluginSystemTemplateVariables.js → WithPluginSystemVariables.js} +9 -9
  118. package/dist/stories/shared-utils/decorators/WithPluginSystemVariables.js.map +1 -0
  119. package/dist/stories/shared-utils/decorators/index.d.ts +1 -1
  120. package/dist/stories/shared-utils/decorators/index.d.ts.map +1 -1
  121. package/dist/stories/shared-utils/decorators/index.js +2 -2
  122. package/dist/stories/shared-utils/decorators/index.js.map +1 -1
  123. package/dist/test/mock-data.d.ts.map +1 -1
  124. package/dist/test/mock-data.js +25 -4
  125. package/dist/test/mock-data.js.map +1 -1
  126. package/dist/test/render.d.ts.map +1 -1
  127. package/dist/test/render.js +1 -9
  128. package/dist/test/render.js.map +1 -1
  129. package/dist/test/test-plugins/bert/index.d.ts.map +1 -1
  130. package/dist/test/test-plugins/bert/index.js +20 -19
  131. package/dist/test/test-plugins/bert/index.js.map +1 -1
  132. package/dist/utils/variables.d.ts +4 -4
  133. package/dist/utils/variables.d.ts.map +1 -1
  134. package/dist/utils/variables.js +18 -18
  135. package/dist/utils/variables.js.map +1 -1
  136. package/package.json +5 -5
  137. package/dist/cjs/validation/datasource.js +0 -30
  138. package/dist/cjs/validation/duration.js +0 -25
  139. package/dist/cjs/validation/index.js +0 -34
  140. package/dist/cjs/validation/resource.js +0 -24
  141. package/dist/cjs/validation/role.js +0 -86
  142. package/dist/cjs/validation/rolebinding.js +0 -55
  143. package/dist/cjs/validation/secret.js +0 -176
  144. package/dist/cjs/validation/user.js +0 -46
  145. package/dist/cjs/validation/variable.js +0 -48
  146. package/dist/runtime/template-variables.d.ts.map +0 -1
  147. package/dist/runtime/template-variables.js.map +0 -1
  148. package/dist/stories/shared-utils/decorators/WithPluginSystemTemplateVariables.d.ts +0 -12
  149. package/dist/stories/shared-utils/decorators/WithPluginSystemTemplateVariables.d.ts.map +0 -1
  150. package/dist/stories/shared-utils/decorators/WithPluginSystemTemplateVariables.js.map +0 -1
  151. package/dist/validation/datasource.d.ts +0 -19
  152. package/dist/validation/datasource.d.ts.map +0 -1
  153. package/dist/validation/datasource.js +0 -22
  154. package/dist/validation/datasource.js.map +0 -1
  155. package/dist/validation/duration.d.ts +0 -3
  156. package/dist/validation/duration.d.ts.map +0 -1
  157. package/dist/validation/duration.js.map +0 -1
  158. package/dist/validation/index.d.ts +0 -6
  159. package/dist/validation/index.d.ts.map +0 -1
  160. package/dist/validation/index.js +0 -19
  161. package/dist/validation/index.js.map +0 -1
  162. package/dist/validation/resource.d.ts +0 -3
  163. package/dist/validation/resource.d.ts.map +0 -1
  164. package/dist/validation/resource.js +0 -16
  165. package/dist/validation/resource.js.map +0 -1
  166. package/dist/validation/role.d.ts +0 -228
  167. package/dist/validation/role.d.ts.map +0 -1
  168. package/dist/validation/role.js +0 -67
  169. package/dist/validation/role.js.map +0 -1
  170. package/dist/validation/rolebinding.d.ts +0 -137
  171. package/dist/validation/rolebinding.d.ts.map +0 -1
  172. package/dist/validation/rolebinding.js +0 -47
  173. package/dist/validation/rolebinding.js.map +0 -1
  174. package/dist/validation/secret.d.ts +0 -964
  175. package/dist/validation/secret.d.ts.map +0 -1
  176. package/dist/validation/secret.js +0 -157
  177. package/dist/validation/secret.js.map +0 -1
  178. package/dist/validation/user.d.ts +0 -93
  179. package/dist/validation/user.d.ts.map +0 -1
  180. package/dist/validation/user.js +0 -38
  181. package/dist/validation/user.js.map +0 -1
  182. package/dist/validation/variable.d.ts +0 -96
  183. package/dist/validation/variable.d.ts.map +0 -1
  184. package/dist/validation/variable.js +0 -40
  185. package/dist/validation/variable.js.map +0 -1
@@ -1,4 +1,4 @@
1
- // Copyright 2023 The Perses Authors
1
+ // Copyright 2024 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -14,28 +14,28 @@
14
14
  Object.defineProperty(exports, "__esModule", {
15
15
  value: true
16
16
  });
17
- Object.defineProperty(exports, "WithPluginSystemTemplateVariables", {
17
+ Object.defineProperty(exports, "WithPluginSystemVariables", {
18
18
  enumerable: true,
19
19
  get: function() {
20
- return WithPluginSystemTemplateVariables;
20
+ return WithPluginSystemVariables;
21
21
  }
22
22
  });
23
23
  const _jsxruntime = require("react/jsx-runtime");
24
24
  const _runtime = require("../../../runtime");
25
25
  // Type guard because storybook types parameters as `any`
26
- function isWithTemplateVariableParameter(parameter) {
26
+ function isWithVariableParameter(parameter) {
27
27
  return !!parameter && typeof parameter === 'object' && 'props' in parameter;
28
28
  }
29
- const WithPluginSystemTemplateVariables = (Story, context)=>{
30
- const initParameter = context.parameters.withPluginSystemTemplateVariables;
29
+ const WithPluginSystemVariables = (Story, context)=>{
30
+ const initParameter = context.parameters.withPluginSystemVariables;
31
31
  const defaultValue = {
32
32
  state: {}
33
33
  };
34
- const parameter = isWithTemplateVariableParameter(initParameter) ? initParameter : {
34
+ const parameter = isWithVariableParameter(initParameter) ? initParameter : {
35
35
  props: defaultValue
36
36
  };
37
37
  const props = parameter === null || parameter === void 0 ? void 0 : parameter.props;
38
- return /*#__PURE__*/ (0, _jsxruntime.jsx)(_runtime.TemplateVariableContext.Provider, {
38
+ return /*#__PURE__*/ (0, _jsxruntime.jsx)(_runtime.VariableContext.Provider, {
39
39
  value: props,
40
40
  children: /*#__PURE__*/ (0, _jsxruntime.jsx)(Story, {})
41
41
  });
@@ -1,4 +1,4 @@
1
- // Copyright 2023 The Perses Authors
1
+ // Copyright 2024 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -18,7 +18,7 @@ _export_star(require("./WithDataQueries"), exports);
18
18
  _export_star(require("./WithPluginRegistry"), exports);
19
19
  _export_star(require("./WithPluginSystemBuiltinVariables"), exports);
20
20
  _export_star(require("./WithPluginSystemDatasourceStore"), exports);
21
- _export_star(require("./WithPluginSystemTemplateVariables"), exports);
21
+ _export_star(require("./WithPluginSystemVariables"), exports);
22
22
  _export_star(require("./WithTimeRange"), exports);
23
23
  function _export_star(from, to) {
24
24
  Object.keys(from).forEach(function(k) {
@@ -64,14 +64,35 @@ const MOCK_TIME_SERIES_DATA = {
64
64
  ]
65
65
  };
66
66
  const MOCK_TRACE_DATA = {
67
- traces: [
67
+ searchResult: [
68
68
  {
69
69
  durationMs: 1120,
70
- errorCount: 0,
71
- spanCount: 10,
70
+ serviceStats: {
71
+ 'shop-backend': {
72
+ spanCount: 4,
73
+ errorCount: 0
74
+ },
75
+ 'cart-service': {
76
+ spanCount: 2,
77
+ errorCount: 0
78
+ },
79
+ 'article-service': {
80
+ spanCount: 2,
81
+ errorCount: 0
82
+ },
83
+ 'auth-service': {
84
+ spanCount: 1,
85
+ errorCount: 0
86
+ },
87
+ postgres: {
88
+ spanCount: 1,
89
+ errorCount: 0
90
+ }
91
+ },
72
92
  startTimeUnixMs: 1699916103945861,
73
93
  traceId: '95ba9202315c29c801b5aa41452aa775',
74
- name: 'rootServiceName="shop-backend", rootTraceName="shop-backend"'
94
+ rootServiceName: 'shop-backend',
95
+ rootTraceName: 'article-to-cart'
75
96
  }
76
97
  ],
77
98
  metadata: {
@@ -25,13 +25,6 @@ const _react = require("@testing-library/react");
25
25
  const _reactquery = require("@tanstack/react-query");
26
26
  const _PluginRegistry = require("../components/PluginRegistry");
27
27
  const _testplugins = require("./test-plugins");
28
- const testLogger = {
29
- log: console.log,
30
- warn: console.warn,
31
- error: ()=>{
32
- // Don't log network errors in tests to the console
33
- }
34
- };
35
28
  function renderWithContext(ui, renderOptions, contextOptions) {
36
29
  // Create a new QueryClient for each test to avoid caching issues
37
30
  const queryClient = new _reactquery.QueryClient({
@@ -40,8 +33,7 @@ function renderWithContext(ui, renderOptions, contextOptions) {
40
33
  refetchOnWindowFocus: false,
41
34
  retry: false
42
35
  }
43
- },
44
- logger: testLogger
36
+ }
45
37
  });
46
38
  var _contextOptions_defaultPluginKinds;
47
39
  return (0, _react.render)(/*#__PURE__*/ (0, _jsxruntime.jsx)(_reactquery.QueryClientProvider, {
@@ -60,30 +60,31 @@ const BertPanel1 = {
60
60
  option1: ''
61
61
  })
62
62
  };
63
+ function BertPanel2Editor({ value, onChange }) {
64
+ return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
65
+ children: [
66
+ /*#__PURE__*/ (0, _jsxruntime.jsx)("label", {
67
+ htmlFor: "editor-input",
68
+ children: "BertPanel2 editor"
69
+ }),
70
+ /*#__PURE__*/ (0, _jsxruntime.jsx)("input", {
71
+ type: "text",
72
+ id: "editor-input",
73
+ value: value.option2,
74
+ onChange: (e)=>onChange({
75
+ ...value,
76
+ option2: e.target.value
77
+ })
78
+ })
79
+ ]
80
+ });
81
+ }
63
82
  const BertPanel2 = {
64
83
  PanelComponent: ()=>null,
65
84
  panelOptionsEditorComponents: [
66
85
  {
67
86
  label: 'Settings',
68
- content: function BertPanel2Editor({ value, onChange }) {
69
- return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
70
- children: [
71
- /*#__PURE__*/ (0, _jsxruntime.jsx)("label", {
72
- htmlFor: "editor-input",
73
- children: "BertPanel2 editor"
74
- }),
75
- /*#__PURE__*/ (0, _jsxruntime.jsx)("input", {
76
- type: "text",
77
- id: "editor-input",
78
- value: value.option2,
79
- onChange: (e)=>onChange({
80
- ...value,
81
- option2: e.target.value
82
- })
83
- })
84
- ]
85
- });
86
- }
87
+ content: BertPanel2Editor
87
88
  },
88
89
  {
89
90
  label: 'Custom Tab',
@@ -1,4 +1,4 @@
1
- // Copyright 2023 The Perses Authors
1
+ // Copyright 2024 The Perses Authors
2
2
  // Licensed under the Apache License, Version 2.0 (the "License");
3
3
  // you may not use this file except in compliance with the License.
4
4
  // You may obtain a copy of the License at
@@ -21,18 +21,18 @@ function _export(target, all) {
21
21
  });
22
22
  }
23
23
  _export(exports, {
24
- parseTemplateVariables: function() {
25
- return parseTemplateVariables;
24
+ parseVariables: function() {
25
+ return parseVariables;
26
26
  },
27
- replaceTemplateVariable: function() {
28
- return replaceTemplateVariable;
27
+ replaceVariable: function() {
28
+ return replaceVariable;
29
29
  },
30
- replaceTemplateVariables: function() {
31
- return replaceTemplateVariables;
30
+ replaceVariables: function() {
31
+ return replaceVariables;
32
32
  }
33
33
  });
34
- function replaceTemplateVariables(text, variableState) {
35
- const variables = parseTemplateVariables(text);
34
+ function replaceVariables(text, variableState) {
35
+ const variables = parseVariables(text);
36
36
  let finalText = text;
37
37
  variables// Sorting variables by their length.
38
38
  // In order to not have a variable name have contained in another variable name.
@@ -40,34 +40,34 @@ function replaceTemplateVariables(text, variableState) {
40
40
  .sort((a, b)=>b.length - a.length).forEach((v)=>{
41
41
  const variable = variableState[v];
42
42
  if (variable && (variable === null || variable === void 0 ? void 0 : variable.value)) {
43
- finalText = replaceTemplateVariable(finalText, v, variable === null || variable === void 0 ? void 0 : variable.value);
43
+ finalText = replaceVariable(finalText, v, variable === null || variable === void 0 ? void 0 : variable.value);
44
44
  }
45
45
  });
46
46
  return finalText;
47
47
  }
48
- function replaceTemplateVariable(text, varName, templateVariableValue) {
49
- const variableTemplate = '$' + varName;
50
- const bracketsVariableTemplate = '${' + varName + '}';
48
+ function replaceVariable(text, varName, variableValue) {
49
+ const variableSyntax = '$' + varName;
50
+ const alternativeVariableSyntax = '${' + varName + '}';
51
51
  let replaceString = '';
52
- if (Array.isArray(templateVariableValue)) {
53
- replaceString = `(${templateVariableValue.join('|')})`; // regex style
52
+ if (Array.isArray(variableValue)) {
53
+ replaceString = `(${variableValue.join('|')})`; // regex style
54
54
  }
55
- if (typeof templateVariableValue === 'string') {
56
- replaceString = templateVariableValue;
55
+ if (typeof variableValue === 'string') {
56
+ replaceString = variableValue;
57
57
  }
58
- text = text.replaceAll(variableTemplate, replaceString);
59
- return text.replaceAll(bracketsVariableTemplate, replaceString);
58
+ text = text.replaceAll(variableSyntax, replaceString);
59
+ return text.replaceAll(alternativeVariableSyntax, replaceString);
60
60
  }
61
- // This regular expression is designed to identify variable references in a template string.
61
+ // This regular expression is designed to identify variable references in a string.
62
62
  // It supports two formats for referencing variables:
63
63
  // 1. $variableName - This is a simpler format, and the regular expression captures the variable name (\w+ matches one or more word characters).
64
64
  // 2. ${variableName} - This is a more complex format and the regular expression captures the variable name (\w+ matches one or more word characters) in the curly braces.
65
65
  // 3. [COMING SOON] ${variableName:value} - This is a more complex format that allows specifying a format function as well.
66
66
  // TODO: Fix this lint error
67
67
  // eslint-disable-next-line no-useless-escape
68
- const TEMPLATE_VARIABLE_REGEX = /\$(\w+)|\${(\w+)(?:\.([^:^\}]+))?(?::([^\}]+))?}/gm;
69
- const parseTemplateVariables = (text)=>{
70
- const regex = TEMPLATE_VARIABLE_REGEX;
68
+ const VARIABLE_REGEX = /\$(\w+)|\${(\w+)(?:\.([^:^\}]+))?(?::([^\}]+))?}/gm;
69
+ const parseVariables = (text)=>{
70
+ const regex = VARIABLE_REGEX;
71
71
  const matches = new Set();
72
72
  let match;
73
73
  while((match = regex.exec(text)) !== null){
@@ -1,11 +1,11 @@
1
- import { Action, DatasourceDefinition, DatasourceSpec } from '@perses-dev/core';
1
+ import { Action, DatasourceDefinition } from '@perses-dev/core';
2
2
  import { DispatchWithoutAction } from 'react';
3
3
  interface DatasourceEditorFormProps {
4
4
  initialDatasourceDefinition: DatasourceDefinition;
5
5
  initialAction: Action;
6
6
  isDraft: boolean;
7
7
  isReadonly?: boolean;
8
- onSave: (name: string, spec: DatasourceSpec) => void;
8
+ onSave: (def: DatasourceDefinition) => void;
9
9
  onClose: DispatchWithoutAction;
10
10
  onDelete?: DispatchWithoutAction;
11
11
  }
@@ -1 +1 @@
1
- {"version":3,"file":"DatasourceEditorForm.d.ts","sourceRoot":"","sources":["../../../src/components/DatasourceEditorForm/DatasourceEditorForm.tsx"],"names":[],"mappings":"AAcA,OAAO,EAAE,MAAM,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEhF,OAAO,EAAE,qBAAqB,EAAyB,MAAM,OAAO,CAAC;AA2BrE,UAAU,yBAAyB;IACjC,2BAA2B,EAAE,oBAAoB,CAAC;IAClD,aAAa,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,KAAK,IAAI,CAAC;IACrD,OAAO,EAAE,qBAAqB,CAAC;IAC/B,QAAQ,CAAC,EAAE,qBAAqB,CAAC;CAClC;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,yBAAyB,2CA+OpE"}
1
+ {"version":3,"file":"DatasourceEditorForm.d.ts","sourceRoot":"","sources":["../../../src/components/DatasourceEditorForm/DatasourceEditorForm.tsx"],"names":[],"mappings":"AAaA,OAAO,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAEhE,OAAO,EAAE,qBAAqB,EAAY,MAAM,OAAO,CAAC;AAQxD,UAAU,yBAAyB;IACjC,2BAA2B,EAAE,oBAAoB,CAAC;IAClD,aAAa,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,MAAM,EAAE,CAAC,GAAG,EAAE,oBAAoB,KAAK,IAAI,CAAC;IAC5C,OAAO,EAAE,qBAAqB,CAAC;IAC/B,QAAQ,CAAC,EAAE,qBAAqB,CAAC;CAClC;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,yBAAyB,2CAuOpE"}
@@ -11,79 +11,52 @@
11
11
  // See the License for the specific language governing permissions and
12
12
  // limitations under the License.
13
13
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
14
- import { useImmer } from 'use-immer';
15
14
  import { Box, Button, Divider, FormControlLabel, Grid, Stack, Switch, TextField, Typography } from '@mui/material';
16
- import { useCallback, useState } from 'react';
15
+ import { useState } from 'react';
17
16
  import { DiscardChangesConfirmationDialog } from '@perses-dev/components';
18
17
  import { Controller, FormProvider, useForm } from 'react-hook-form';
19
18
  import { zodResolver } from '@hookform/resolvers/zod';
20
19
  import { PluginEditor } from '../PluginEditor';
21
20
  import { getSubmitText, getTitleAction } from '../../utils';
22
- import { datasourceEditValidationSchema } from '../../validation';
23
- /**
24
- * This preprocessing ensures that we always have a defined object for the `display` property
25
- * @param datasource
26
- */ function getInitialState(datasourceDefinition) {
27
- var _datasourceDefinition_spec_display, _datasourceDefinition_spec_display1;
28
- var _datasourceDefinition_spec_display_name, _datasourceDefinition_spec_display_description;
29
- const patchedDisplay = {
30
- name: (_datasourceDefinition_spec_display_name = (_datasourceDefinition_spec_display = datasourceDefinition.spec.display) === null || _datasourceDefinition_spec_display === void 0 ? void 0 : _datasourceDefinition_spec_display.name) !== null && _datasourceDefinition_spec_display_name !== void 0 ? _datasourceDefinition_spec_display_name : '',
31
- description: (_datasourceDefinition_spec_display_description = (_datasourceDefinition_spec_display1 = datasourceDefinition.spec.display) === null || _datasourceDefinition_spec_display1 === void 0 ? void 0 : _datasourceDefinition_spec_display1.description) !== null && _datasourceDefinition_spec_display_description !== void 0 ? _datasourceDefinition_spec_display_description : ''
32
- };
33
- return {
34
- name: datasourceDefinition.name,
35
- spec: {
36
- ...datasourceDefinition.spec,
37
- display: patchedDisplay
38
- }
39
- };
40
- }
21
+ import { useValidationSchemas } from '../../context';
41
22
  export function DatasourceEditorForm(props) {
42
- var _state_spec_display, _state_spec_display1;
43
23
  const { initialDatasourceDefinition, initialAction, isDraft, isReadonly, onSave, onClose, onDelete } = props;
44
- const initialState = getInitialState(initialDatasourceDefinition);
45
- const [state, setState] = useImmer(initialState);
46
24
  const [isDiscardDialogOpened, setDiscardDialogOpened] = useState(false);
47
25
  const [action, setAction] = useState(initialAction);
48
26
  const titleAction = getTitleAction(action, isDraft);
49
27
  const submitText = getSubmitText(action, isDraft);
28
+ const { datasourceEditorSchema } = useValidationSchemas();
50
29
  const form = useForm({
51
- resolver: zodResolver(datasourceEditValidationSchema),
30
+ resolver: zodResolver(datasourceEditorSchema),
52
31
  mode: 'onBlur',
53
- defaultValues: {
54
- name: state.name,
55
- title: (_state_spec_display = state.spec.display) === null || _state_spec_display === void 0 ? void 0 : _state_spec_display.name,
56
- description: (_state_spec_display1 = state.spec.display) === null || _state_spec_display1 === void 0 ? void 0 : _state_spec_display1.description,
57
- default: state.spec.default
58
- }
32
+ defaultValues: initialDatasourceDefinition
59
33
  });
60
- const processForm = ()=>{
61
- var _state_spec_display, _state_spec_display1, _state_spec_display2, _state_spec_display3;
62
- // reset display attributes to undefined when empty, because we don't want to save empty strings
63
- onSave(state.name, {
64
- ...state.spec,
65
- display: {
66
- name: ((_state_spec_display = state.spec.display) === null || _state_spec_display === void 0 ? void 0 : _state_spec_display.name) === '' ? undefined : (_state_spec_display1 = state.spec.display) === null || _state_spec_display1 === void 0 ? void 0 : _state_spec_display1.name,
67
- description: ((_state_spec_display2 = state.spec.display) === null || _state_spec_display2 === void 0 ? void 0 : _state_spec_display2.description) === '' ? undefined : (_state_spec_display3 = state.spec.display) === null || _state_spec_display3 === void 0 ? void 0 : _state_spec_display3.description
68
- }
69
- });
34
+ /*
35
+ * Remove empty fields that are optional
36
+ */ function clearFormData(data) {
37
+ var _result_spec_display, _result_spec_display1;
38
+ const result = {
39
+ ...data
40
+ };
41
+ if (((_result_spec_display = result.spec.display) === null || _result_spec_display === void 0 ? void 0 : _result_spec_display.name) === undefined && ((_result_spec_display1 = result.spec.display) === null || _result_spec_display1 === void 0 ? void 0 : _result_spec_display1.description) === undefined) {
42
+ delete result.spec.display;
43
+ }
44
+ return result;
45
+ }
46
+ const processForm = (data)=>{
47
+ onSave(clearFormData(data));
70
48
  };
71
49
  // When user click on cancel, several possibilities:
72
50
  // - create action: ask for discard approval
73
51
  // - update action: ask for discard approval if changed
74
52
  // - read action: don´t ask for discard approval
75
- const handleCancel = useCallback(()=>{
76
- if (JSON.stringify(initialState) !== JSON.stringify(state)) {
53
+ function handleCancel() {
54
+ if (JSON.stringify(initialDatasourceDefinition) !== JSON.stringify(clearFormData(form.getValues()))) {
77
55
  setDiscardDialogOpened(true);
78
56
  } else {
79
57
  onClose();
80
58
  }
81
- }, [
82
- state,
83
- initialState,
84
- setDiscardDialogOpened,
85
- onClose
86
- ]);
59
+ }
87
60
  return /*#__PURE__*/ _jsxs(FormProvider, {
88
61
  ...form,
89
62
  children: [
@@ -175,10 +148,12 @@ export function DatasourceEditorForm(props) {
175
148
  item: true,
176
149
  xs: 4,
177
150
  children: /*#__PURE__*/ _jsx(Controller, {
151
+ control: form.control,
178
152
  name: "name",
179
- render: ({ field, fieldState })=>{
153
+ render: ({ field, fieldState })=>/*#__PURE__*/ {
180
154
  var _fieldState_error;
181
- return /*#__PURE__*/ _jsx(TextField, {
155
+ var _field_value;
156
+ return _jsx(TextField, {
182
157
  ...field,
183
158
  required: true,
184
159
  fullWidth: true,
@@ -193,11 +168,9 @@ export function DatasourceEditorForm(props) {
193
168
  },
194
169
  error: !!fieldState.error,
195
170
  helperText: (_fieldState_error = fieldState.error) === null || _fieldState_error === void 0 ? void 0 : _fieldState_error.message,
171
+ value: (_field_value = field.value) !== null && _field_value !== void 0 ? _field_value : '',
196
172
  onChange: (event)=>{
197
173
  field.onChange(event);
198
- setState((draft)=>{
199
- draft.name = event.target.value;
200
- });
201
174
  }
202
175
  });
203
176
  }
@@ -207,10 +180,12 @@ export function DatasourceEditorForm(props) {
207
180
  item: true,
208
181
  xs: 8,
209
182
  children: /*#__PURE__*/ _jsx(Controller, {
210
- name: "title",
211
- render: ({ field, fieldState })=>{
183
+ control: form.control,
184
+ name: "spec.display.name",
185
+ render: ({ field, fieldState })=>/*#__PURE__*/ {
212
186
  var _fieldState_error;
213
- return /*#__PURE__*/ _jsx(TextField, {
187
+ var _field_value;
188
+ return _jsx(TextField, {
214
189
  ...field,
215
190
  fullWidth: true,
216
191
  name: "title",
@@ -223,13 +198,9 @@ export function DatasourceEditorForm(props) {
223
198
  },
224
199
  error: !!fieldState.error,
225
200
  helperText: (_fieldState_error = fieldState.error) === null || _fieldState_error === void 0 ? void 0 : _fieldState_error.message,
201
+ value: (_field_value = field.value) !== null && _field_value !== void 0 ? _field_value : '',
226
202
  onChange: (event)=>{
227
- setState((draft)=>{
228
- field.onChange(event);
229
- if (draft.spec.display) {
230
- draft.spec.display.name = event.target.value;
231
- }
232
- });
203
+ field.onChange(event);
233
204
  }
234
205
  });
235
206
  }
@@ -239,10 +210,12 @@ export function DatasourceEditorForm(props) {
239
210
  item: true,
240
211
  xs: 12,
241
212
  children: /*#__PURE__*/ _jsx(Controller, {
242
- name: "description",
243
- render: ({ field, fieldState })=>{
213
+ control: form.control,
214
+ name: "spec.display.description",
215
+ render: ({ field, fieldState })=>/*#__PURE__*/ {
244
216
  var _fieldState_error;
245
- return /*#__PURE__*/ _jsx(TextField, {
217
+ var _field_value;
218
+ return _jsx(TextField, {
246
219
  ...field,
247
220
  fullWidth: true,
248
221
  name: "description",
@@ -255,13 +228,9 @@ export function DatasourceEditorForm(props) {
255
228
  },
256
229
  error: !!fieldState.error,
257
230
  helperText: (_fieldState_error = fieldState.error) === null || _fieldState_error === void 0 ? void 0 : _fieldState_error.message,
231
+ value: (_field_value = field.value) !== null && _field_value !== void 0 ? _field_value : '',
258
232
  onChange: (event)=>{
259
233
  field.onChange(event);
260
- setState((draft)=>{
261
- if (draft.spec.display) {
262
- draft.spec.display.description = event.target.value;
263
- }
264
- });
265
234
  }
266
235
  });
267
236
  }
@@ -276,28 +245,26 @@ export function DatasourceEditorForm(props) {
276
245
  children: /*#__PURE__*/ _jsxs(Stack, {
277
246
  children: [
278
247
  /*#__PURE__*/ _jsx(Controller, {
279
- name: "default",
248
+ control: form.control,
249
+ name: "spec.default",
280
250
  render: ({ field })=>/*#__PURE__*/ _jsx(FormControlLabel, {
281
- ...field,
251
+ label: "Set as default",
282
252
  control: /*#__PURE__*/ _jsx(Switch, {
283
- checked: state.spec.default,
253
+ ...field,
254
+ checked: !!field.value,
284
255
  readOnly: action === 'read',
285
256
  onChange: (event)=>{
286
257
  if (action === 'read') return; // ReadOnly prop is not blocking user interaction...
287
258
  field.onChange(event);
288
- setState((draft)=>{
289
- draft.spec.default = event.target.checked;
290
- });
291
259
  }
292
- }),
293
- label: "Set as default"
260
+ })
294
261
  })
295
262
  }),
296
263
  /*#__PURE__*/ _jsxs(Typography, {
297
264
  variant: "caption",
298
265
  children: [
299
266
  "Whether this datasource should be the default ",
300
- state.spec.plugin.kind,
267
+ form.getValues().spec.plugin.kind,
301
268
  " to be used"
302
269
  ]
303
270
  })
@@ -312,26 +279,30 @@ export function DatasourceEditorForm(props) {
312
279
  variant: "h3",
313
280
  children: "Plugin Options"
314
281
  }),
315
- /*#__PURE__*/ _jsx(PluginEditor, {
316
- width: "100%",
317
- pluginTypes: [
318
- 'Datasource'
319
- ],
320
- pluginKindLabel: "Source",
321
- value: {
322
- selection: {
323
- kind: state.spec.plugin.kind,
324
- type: 'Datasource'
325
- },
326
- spec: state.spec.plugin.spec
327
- },
328
- isReadonly: action === 'read',
329
- onChange: (v)=>{
330
- setState((draft)=>{
331
- draft.spec.plugin.kind = v.selection.kind;
332
- draft.spec.plugin.spec = v.spec;
333
- });
334
- }
282
+ /*#__PURE__*/ _jsx(Controller, {
283
+ control: form.control,
284
+ name: "spec.plugin",
285
+ render: ({ field })=>/*#__PURE__*/ _jsx(PluginEditor, {
286
+ width: "100%",
287
+ pluginTypes: [
288
+ 'Datasource'
289
+ ],
290
+ pluginKindLabel: "Source",
291
+ value: {
292
+ selection: {
293
+ type: 'Datasource',
294
+ kind: field.value.kind
295
+ },
296
+ spec: field.value.spec
297
+ },
298
+ isReadonly: action === 'read',
299
+ onChange: (v)=>{
300
+ field.onChange({
301
+ kind: v.selection.kind,
302
+ spec: v.spec
303
+ });
304
+ }
305
+ })
335
306
  })
336
307
  ]
337
308
  }),