@malloy-publisher/sdk 0.0.382-dev → 0.2.1

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 (284) hide show
  1. package/README.md +115 -95
  2. package/TREE_SHAKING.md +16 -11
  3. package/bunfig.toml +7 -0
  4. package/dist/ServerProvider-Cn_LhKda.cjs.js +1 -0
  5. package/dist/ServerProvider-sfabtriY.es.js +4076 -0
  6. package/dist/client/api.d.ts +3773 -1515
  7. package/dist/client/configuration.d.ts +1 -1
  8. package/dist/client/index.cjs.js +1 -1
  9. package/dist/client/index.d.ts +1 -1
  10. package/dist/client/index.es.js +86 -59
  11. package/dist/components/Connections/EditConnectionDialog.d.ts +2 -1
  12. package/dist/components/Connections/common.d.ts +8 -2
  13. package/dist/components/Dashboard/Dashboard.d.ts +62 -0
  14. package/dist/components/Dashboard/DashboardTile.d.ts +57 -0
  15. package/dist/components/Dashboard/index.d.ts +2 -0
  16. package/dist/components/DataAppViewer/DataAppViewer.d.ts +23 -0
  17. package/dist/components/DataAppViewer/index.d.ts +1 -0
  18. package/dist/components/Environment/Environment.d.ts +6 -0
  19. package/dist/components/Environment/index.d.ts +2 -0
  20. package/dist/components/Home/AddEnvironmentDialog.d.ts +1 -0
  21. package/dist/components/Home/DeleteEnvironmentDialog.d.ts +5 -0
  22. package/dist/components/Home/EditEnvironmentDialog.d.ts +7 -0
  23. package/dist/components/Home/Home.d.ts +2 -2
  24. package/dist/components/Materializations/CreateEnvMaterializationDialog.d.ts +14 -0
  25. package/dist/components/Materializations/CreateMaterializationDialog.d.ts +10 -0
  26. package/dist/components/Materializations/DeleteMaterializationDialog.d.ts +7 -0
  27. package/dist/components/Materializations/EnvironmentMaterializations.d.ts +7 -0
  28. package/dist/components/Materializations/ManifestView.d.ts +13 -0
  29. package/dist/components/Materializations/MaterializationDetailDialog.d.ts +9 -0
  30. package/dist/components/Materializations/MaterializationRunsList.d.ts +15 -0
  31. package/dist/components/Materializations/Materializations.d.ts +7 -0
  32. package/dist/components/Materializations/ScheduleCard.d.ts +20 -0
  33. package/dist/components/Materializations/SectionLabel.d.ts +10 -0
  34. package/dist/components/Materializations/SetScheduleDialog.d.ts +11 -0
  35. package/dist/components/Materializations/TriggerChip.d.ts +10 -0
  36. package/dist/components/Materializations/cron.d.ts +23 -0
  37. package/dist/components/Materializations/index.d.ts +2 -0
  38. package/dist/components/Materializations/utils.d.ts +46 -0
  39. package/dist/components/Notebook/Notebook.d.ts +47 -4
  40. package/dist/components/Notebook/NotebookCell.d.ts +37 -1
  41. package/dist/components/Notebook/runPlan.d.ts +29 -0
  42. package/dist/components/Notebook/types.d.ts +10 -0
  43. package/dist/components/Package/ContentTypeIcon.d.ts +31 -0
  44. package/dist/components/Package/Package.d.ts +1 -4
  45. package/dist/components/RenderedResult/RenderedResult.d.ts +7 -1
  46. package/dist/components/RenderedResult/ResultContainer.d.ts +5 -1
  47. package/dist/components/RenderedResult/renderLogs.d.ts +6 -0
  48. package/dist/components/ResultsDialog.d.ts +6 -1
  49. package/dist/components/ServerProvider.d.ts +28 -4
  50. package/dist/components/click_helper.d.ts +10 -1
  51. package/dist/components/drill/index.d.ts +3 -0
  52. package/dist/components/drill/markDrillableCells.d.ts +55 -0
  53. package/dist/components/drill/resolveDrill.d.ts +139 -0
  54. package/dist/components/drill/useDrill.d.ts +63 -0
  55. package/dist/components/given/GivenInput.d.ts +52 -0
  56. package/dist/components/given/GivensPanel.d.ts +47 -0
  57. package/dist/components/given/filterValue.d.ts +97 -0
  58. package/dist/components/given/index.d.ts +2 -0
  59. package/dist/components/given/paramCodec.d.ts +60 -0
  60. package/dist/components/given/utils.d.ts +17 -0
  61. package/dist/components/highlighter.d.ts +6 -1
  62. package/dist/components/index.d.ts +7 -2
  63. package/dist/components/styles.d.ts +47 -0
  64. package/dist/constants/docLinks.d.ts +12 -0
  65. package/dist/{core-YNpOLuB1.cjs.js → core-CqRKhtoy.cjs.js} +1 -1
  66. package/dist/{core-CjeTkq8O.es.js → core-DyU47reh.es.js} +1 -1
  67. package/dist/github-dark-BKL9xNgN.cjs.js +1 -0
  68. package/dist/github-dark-DenFmJkN.es.js +4 -0
  69. package/dist/hooks/givenValue.d.ts +10 -0
  70. package/dist/hooks/index.d.ts +4 -0
  71. package/dist/hooks/useDimensionFiltersFromSpec.d.ts +3 -3
  72. package/dist/hooks/useDimensionFiltersQuery.d.ts +3 -3
  73. package/dist/hooks/useDimensionalFilterRangeData.d.ts +4 -4
  74. package/dist/hooks/useGivensState.d.ts +64 -0
  75. package/dist/hooks/useModelGivens.d.ts +10 -0
  76. package/dist/hooks/useSuggestOptions.d.ts +78 -0
  77. package/dist/{index-DRDu9kIV.es.js → index-CAkFE4Ol.es.js} +41278 -28504
  78. package/dist/index-DD4b_bt0.cjs.js +356 -0
  79. package/dist/index.cjs.js +1 -1
  80. package/dist/index.d.ts +4 -1
  81. package/dist/index.es.js +79 -48
  82. package/dist/malloy-explorer.css +12 -1
  83. package/dist/sdk.css +1 -1
  84. package/dist/theme/ThemeContext.d.ts +45 -0
  85. package/dist/theme/buildMalloyExplicitTheme.d.ts +34 -0
  86. package/dist/theme/buildTableCssVars.d.ts +41 -0
  87. package/dist/theme/buildVegaThemeOverride.d.ts +16 -0
  88. package/dist/theme/defaults.d.ts +7 -0
  89. package/dist/theme/index.d.ts +8 -0
  90. package/dist/theme/keys.d.ts +20 -0
  91. package/dist/theme/readChartAnnotations.d.ts +23 -0
  92. package/dist/theme/resolveTheme.d.ts +22 -0
  93. package/dist/theme/types.d.ts +70 -0
  94. package/dist/utils/dataAppEmbed.d.ts +44 -0
  95. package/dist/utils/formatting.d.ts +1 -1
  96. package/dist/utils/malloyString.d.ts +7 -0
  97. package/dist/utils/parsing.d.ts +2 -2
  98. package/dist/utils/queryClient.d.ts +5 -0
  99. package/package.json +19 -8
  100. package/src/client-entry.ts +3 -0
  101. package/src/components/AnalyzePackageButton.tsx +9 -3
  102. package/src/components/ApiErrorDisplay.tsx +3 -0
  103. package/src/components/Connections/AddConnectionDialog.tsx +132 -25
  104. package/src/components/Connections/DeleteConnectionDialog.tsx +14 -3
  105. package/src/components/Connections/EditConnectionDialog.tsx +167 -37
  106. package/src/components/Connections/common.ts +94 -3
  107. package/src/components/DOMElement/DOMElement.tsx +3 -0
  108. package/src/components/DOMElement/index.ts +3 -0
  109. package/src/components/Dashboard/Dashboard.spec.tsx +216 -0
  110. package/src/components/Dashboard/Dashboard.tsx +458 -0
  111. package/src/components/Dashboard/DashboardTile.spec.tsx +81 -0
  112. package/src/components/Dashboard/DashboardTile.tsx +199 -0
  113. package/src/components/Dashboard/index.ts +5 -0
  114. package/src/components/Dashboard/tileGridColumn.spec.ts +35 -0
  115. package/src/components/DataAppViewer/DataAppViewer.tsx +214 -0
  116. package/src/components/DataAppViewer/index.ts +4 -0
  117. package/src/components/{Project → Environment}/About.tsx +11 -4
  118. package/src/components/Environment/AddPackageDialog.tsx +202 -0
  119. package/src/components/{Project → Environment}/ConnectionExplorer.tsx +17 -14
  120. package/src/components/Environment/Connections.tsx +391 -0
  121. package/src/components/{Project → Environment}/DeletePackageDialog.tsx +8 -4
  122. package/src/components/{Project → Environment}/EditPackageDialog.tsx +21 -13
  123. package/src/components/Environment/Environment.tsx +89 -0
  124. package/src/components/Environment/Packages.tsx +206 -0
  125. package/src/components/Environment/index.ts +5 -0
  126. package/src/components/Home/{AddProjectDialog.tsx → AddEnvironmentDialog.tsx} +25 -19
  127. package/src/components/Home/{DeleteProjectDialog.tsx → DeleteEnvironmentDialog.tsx} +20 -15
  128. package/src/components/Home/{EditProjectDialog.tsx → EditEnvironmentDialog.tsx} +35 -28
  129. package/src/components/Home/Home.tsx +386 -408
  130. package/src/components/Home/index.ts +3 -0
  131. package/src/components/Loading.tsx +3 -0
  132. package/src/components/Materializations/CreateEnvMaterializationDialog.tsx +179 -0
  133. package/src/components/Materializations/CreateMaterializationDialog.tsx +112 -0
  134. package/src/components/Materializations/DeleteMaterializationDialog.tsx +109 -0
  135. package/src/components/Materializations/EnvironmentMaterializations.tsx +267 -0
  136. package/src/components/Materializations/ManifestView.tsx +87 -0
  137. package/src/components/Materializations/MaterializationDetailDialog.tsx +259 -0
  138. package/src/components/Materializations/MaterializationRunsList.tsx +256 -0
  139. package/src/components/Materializations/Materializations.tsx +355 -0
  140. package/src/components/Materializations/ScheduleCard.tsx +231 -0
  141. package/src/components/Materializations/SectionLabel.tsx +30 -0
  142. package/src/components/Materializations/SetScheduleDialog.tsx +162 -0
  143. package/src/components/Materializations/TriggerChip.tsx +44 -0
  144. package/src/components/Materializations/cron.ts +124 -0
  145. package/src/components/Materializations/index.ts +5 -0
  146. package/src/components/Materializations/utils.ts +145 -0
  147. package/src/components/Model/Model.tsx +44 -2
  148. package/src/components/Model/ModelCell.tsx +14 -5
  149. package/src/components/Model/ModelExplorer.tsx +3 -0
  150. package/src/components/Model/ModelExplorerDialog.tsx +4 -1
  151. package/src/components/Model/SourcesExplorer.tsx +9 -6
  152. package/src/components/Model/index.ts +3 -0
  153. package/src/components/Model/useModelData.ts +6 -3
  154. package/src/components/Notebook/Notebook.spec.tsx +88 -0
  155. package/src/components/Notebook/Notebook.tsx +476 -339
  156. package/src/components/Notebook/NotebookCell.tsx +264 -25
  157. package/src/components/Notebook/index.ts +3 -0
  158. package/src/components/Notebook/runPlan.spec.ts +155 -0
  159. package/src/components/Notebook/runPlan.ts +67 -0
  160. package/src/components/Notebook/types.ts +14 -0
  161. package/src/components/Package/ContentTypeIcon.spec.ts +91 -0
  162. package/src/components/Package/ContentTypeIcon.tsx +177 -0
  163. package/src/components/Package/FileTreeView.tsx +3 -0
  164. package/src/components/Package/Models.tsx +7 -4
  165. package/src/components/Package/Package.spec.tsx +113 -0
  166. package/src/components/Package/Package.tsx +692 -59
  167. package/src/components/Package/index.ts +3 -0
  168. package/src/components/QueryResult/QueryResult.tsx +17 -8
  169. package/src/components/QueryResult/index.ts +3 -0
  170. package/src/components/RenderedResult/RenderedResult.tsx +725 -67
  171. package/src/components/RenderedResult/ResultContainer.tsx +71 -2
  172. package/src/components/RenderedResult/index.ts +3 -0
  173. package/src/components/RenderedResult/renderLogs.spec.ts +58 -0
  174. package/src/components/RenderedResult/renderLogs.ts +43 -0
  175. package/src/components/ResultsDialog.tsx +13 -1
  176. package/src/components/ServerProvider.tsx +175 -51
  177. package/src/components/Workbook/BrowserWorkbookStorage.ts +3 -0
  178. package/src/components/Workbook/EditableMalloyCell.tsx +3 -0
  179. package/src/components/Workbook/ModelPicker.tsx +7 -4
  180. package/src/components/Workbook/MutableCell.tsx +10 -5
  181. package/src/components/Workbook/Workbook.tsx +8 -5
  182. package/src/components/Workbook/WorkbookList.tsx +3 -0
  183. package/src/components/Workbook/WorkbookManager.ts +3 -0
  184. package/src/components/Workbook/WorkbookStorage.ts +3 -0
  185. package/src/components/Workbook/WorkbookStorageProvider.tsx +3 -0
  186. package/src/components/Workbook/index.ts +3 -0
  187. package/src/components/click_helper.ts +17 -1
  188. package/src/components/drill/index.ts +28 -0
  189. package/src/components/drill/markDrillableCells.dom.spec.ts +313 -0
  190. package/src/components/drill/markDrillableCells.spec.ts +109 -0
  191. package/src/components/drill/markDrillableCells.ts +314 -0
  192. package/src/components/drill/resolveDrill.spec.ts +449 -0
  193. package/src/components/drill/resolveDrill.ts +375 -0
  194. package/src/components/drill/useDrill.tsx +203 -0
  195. package/src/components/filter/DimensionFilter.tsx +3 -0
  196. package/src/components/filter/index.ts +3 -0
  197. package/src/components/filter/utils.ts +16 -5
  198. package/src/components/given/GivenInput.spec.tsx +689 -0
  199. package/src/components/given/GivenInput.tsx +801 -0
  200. package/src/components/given/GivensPanel.spec.tsx +175 -0
  201. package/src/components/given/GivensPanel.tsx +195 -0
  202. package/src/components/given/filterValue.spec.ts +364 -0
  203. package/src/components/given/filterValue.ts +207 -0
  204. package/src/components/given/index.ts +9 -0
  205. package/src/components/given/paramCodec.spec.ts +857 -0
  206. package/src/components/given/paramCodec.ts +483 -0
  207. package/src/components/given/utils.spec.ts +60 -0
  208. package/src/components/given/utils.ts +79 -0
  209. package/src/components/highlighter.ts +22 -5
  210. package/src/components/index.ts +10 -2
  211. package/src/components/styles.ts +82 -33
  212. package/src/constants/docLinks.spec.ts +54 -0
  213. package/src/constants/docLinks.ts +46 -0
  214. package/src/hooks/givenValue.ts +13 -0
  215. package/src/hooks/index.ts +13 -0
  216. package/src/hooks/useDimensionFilters.ts +4 -7
  217. package/src/hooks/useDimensionFiltersFromSpec.ts +8 -5
  218. package/src/hooks/useDimensionFiltersQuery.ts +10 -13
  219. package/src/hooks/useDimensionalFilterRangeData.ts +11 -14
  220. package/src/hooks/useGivensState.spec.ts +449 -0
  221. package/src/hooks/useGivensState.ts +340 -0
  222. package/src/hooks/useModelGivens.ts +29 -0
  223. package/src/hooks/useQueryWithApiError.ts +3 -0
  224. package/src/hooks/useRawQueryData.ts +6 -3
  225. package/src/hooks/useSuggestOptions.dom.spec.ts +91 -0
  226. package/src/hooks/useSuggestOptions.spec.ts +112 -0
  227. package/src/hooks/useSuggestOptions.ts +268 -0
  228. package/src/index.ts +10 -1
  229. package/src/react-app-env.d.ts +3 -0
  230. package/src/theme/ThemeContext.tsx +86 -0
  231. package/src/theme/buildMalloyExplicitTheme.ts +61 -0
  232. package/src/theme/buildTableCssVars.spec.ts +86 -0
  233. package/src/theme/buildTableCssVars.ts +98 -0
  234. package/src/theme/buildVegaThemeOverride.spec.ts +56 -0
  235. package/src/theme/buildVegaThemeOverride.ts +47 -0
  236. package/src/theme/defaults.ts +78 -0
  237. package/src/theme/index.ts +11 -0
  238. package/src/theme/keys.ts +32 -0
  239. package/src/theme/readChartAnnotations.spec.ts +58 -0
  240. package/src/theme/readChartAnnotations.ts +69 -0
  241. package/src/theme/resolveTheme.spec.ts +160 -0
  242. package/src/theme/resolveTheme.ts +121 -0
  243. package/src/theme/types.ts +79 -0
  244. package/src/utils/dataAppEmbed.spec.ts +63 -0
  245. package/src/utils/dataAppEmbed.ts +84 -0
  246. package/src/utils/formatting.spec.ts +25 -21
  247. package/src/utils/formatting.ts +11 -7
  248. package/src/utils/malloyString.spec.ts +24 -0
  249. package/src/utils/malloyString.ts +12 -0
  250. package/src/utils/parsing.spec.ts +26 -19
  251. package/src/utils/parsing.ts +11 -6
  252. package/src/utils/queryClient.ts +17 -0
  253. package/test/README.md +185 -0
  254. package/test/serverProvider.tsx +72 -0
  255. package/test/setup.ts +29 -0
  256. package/tsconfig.json +12 -1
  257. package/tsconfig.spec.json +22 -0
  258. package/vite.config.ts +3 -0
  259. package/dist/ServerProvider-lOQXDlqB.cjs.js +0 -1
  260. package/dist/ServerProvider-on-8FH5Z.es.js +0 -3060
  261. package/dist/components/Home/AddProjectDialog.d.ts +0 -1
  262. package/dist/components/Home/DeleteProjectDialog.d.ts +0 -5
  263. package/dist/components/Home/EditProjectDialog.d.ts +0 -7
  264. package/dist/components/Package/Config.d.ts +0 -5
  265. package/dist/components/Package/Databases.d.ts +0 -5
  266. package/dist/components/Package/Notebooks.d.ts +0 -6
  267. package/dist/components/Project/Project.d.ts +0 -6
  268. package/dist/components/Project/index.d.ts +0 -2
  269. package/dist/index-BOLBP6_i.cjs.js +0 -233
  270. package/src/components/Package/Config.tsx +0 -97
  271. package/src/components/Package/Connections.tsx +0 -373
  272. package/src/components/Package/Databases.tsx +0 -228
  273. package/src/components/Package/Notebooks.tsx +0 -77
  274. package/src/components/Project/AddPackageDialog.tsx +0 -159
  275. package/src/components/Project/Packages.tsx +0 -191
  276. package/src/components/Project/Project.tsx +0 -52
  277. package/src/components/Project/index.ts +0 -2
  278. /package/dist/components/{Project → Environment}/About.d.ts +0 -0
  279. /package/dist/components/{Project → Environment}/AddPackageDialog.d.ts +0 -0
  280. /package/dist/components/{Project → Environment}/ConnectionExplorer.d.ts +0 -0
  281. /package/dist/components/{Package → Environment}/Connections.d.ts +0 -0
  282. /package/dist/components/{Project → Environment}/DeletePackageDialog.d.ts +0 -0
  283. /package/dist/components/{Project → Environment}/EditPackageDialog.d.ts +0 -0
  284. /package/dist/components/{Project → Environment}/Packages.d.ts +0 -0
@@ -0,0 +1,175 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { fireEvent, render, screen } from "@testing-library/react";
5
+ import { describe, expect, it } from "bun:test";
6
+ import { useState } from "react";
7
+ import type { Given } from "../../client";
8
+ import { useGivensState } from "../../hooks/useGivensState";
9
+ import { decodeFilterList } from "./filterValue";
10
+ import { GivensPanel } from "./GivensPanel";
11
+
12
+ const TYPES = new Map<string, string | undefined>([
13
+ ["BRAND", "filter<string>"],
14
+ ]);
15
+
16
+ const MULTISELECT: Given = {
17
+ name: "BRAND",
18
+ type: "filter<string>",
19
+ control: "multiselect",
20
+ };
21
+
22
+ let lastDraft: string | undefined;
23
+
24
+ /**
25
+ * The panel wired to the state hook through a host that keeps applied values in
26
+ * its "URL" and feeds them straight back in, which is what `NotebookPage` does
27
+ * through react-router. The loop is the point: a control's next value is
28
+ * computed from the value it is currently rendering, so anything that delays
29
+ * that value getting back to it is where a lost click would come from.
30
+ */
31
+ function Harness({ options }: { options?: Map<string, string[]> }) {
32
+ const [params, setParams] = useState<Record<string, string>>({});
33
+ const { draft, setGiven } = useGivensState({
34
+ declaredTypes: TYPES,
35
+ params,
36
+ onParamsChange: setParams,
37
+ autorun: true,
38
+ });
39
+ lastDraft = draft.get("BRAND") as string | undefined;
40
+ return (
41
+ <GivensPanel
42
+ givens={[MULTISELECT]}
43
+ values={draft}
44
+ onChange={setGiven}
45
+ onReset={() => {}}
46
+ options={options ?? new Map([["BRAND", ["Loft", "Aurora", "Nike"]]])}
47
+ />
48
+ );
49
+ }
50
+
51
+ // The combobox by role, not by label: once a value is picked the control also
52
+ // renders a chip carrying the same accessible name, and getByLabelText then
53
+ // matches two elements.
54
+ const combobox = () => screen.getByRole("combobox");
55
+
56
+ function pick(label: string) {
57
+ fireEvent.keyDown(combobox(), { key: "ArrowDown" });
58
+ fireEvent.click(screen.getByRole("option", { name: label }));
59
+ }
60
+
61
+ describe("GivensPanel: multiselect", () => {
62
+ it("joins a second pick onto the first rather than replacing it", () => {
63
+ // A Playwright spec on the originating branch documented the opposite,
64
+ // "a second click sent immediately lands on the pre-first-pick selection
65
+ // and replaces Loft instead of joining it", and worked around it by
66
+ // awaiting a URL change between the clicks. It does not reproduce here,
67
+ // with the host's round trip in place and no await between the clicks, so
68
+ // this pins the behaviour that is actually observable at this level
69
+ // rather than encoding a race nobody can currently demonstrate.
70
+ render(<Harness />);
71
+
72
+ pick("Loft");
73
+ expect(decodeFilterList(lastDraft ?? "")).toEqual(["Loft"]);
74
+
75
+ pick("Aurora");
76
+ expect(decodeFilterList(lastDraft ?? "")).toEqual(["Loft", "Aurora"]);
77
+ });
78
+
79
+ it("encodes the selection as a filter Malloy reads as 'any of these'", () => {
80
+ render(<Harness options={new Map([["BRAND", ["Ben & Jerry, Inc"]]])} />);
81
+
82
+ pick("Ben & Jerry, Inc");
83
+
84
+ // Escaped, so the comma stays inside one value instead of splitting it
85
+ // into two brands.
86
+ expect(lastDraft).toBe("Ben\\ &\\ Jerry\\,\\ Inc");
87
+ expect(decodeFilterList(lastDraft ?? "")).toEqual(["Ben & Jerry, Inc"]);
88
+ });
89
+ });
90
+
91
+ describe("GivensPanel: a suggest query that failed", () => {
92
+ it("says so, instead of showing an empty dropdown", () => {
93
+ // An empty option list and a failed query render identically, and mean
94
+ // opposite things. Without this the reader sees a picker with nothing in
95
+ // it and concludes the data has no values.
96
+ render(
97
+ <GivensPanel
98
+ givens={[MULTISELECT]}
99
+ values={new Map()}
100
+ onChange={() => {}}
101
+ onReset={() => {}}
102
+ options={new Map()}
103
+ optionsFailed={new Set(["BRAND"])}
104
+ />,
105
+ );
106
+
107
+ expect(
108
+ screen.getByText(/Could not load the options for this control/),
109
+ ).toBeDefined();
110
+ });
111
+
112
+ it("says nothing when the dimension simply has no values", () => {
113
+ render(
114
+ <GivensPanel
115
+ givens={[MULTISELECT]}
116
+ values={new Map()}
117
+ onChange={() => {}}
118
+ onReset={() => {}}
119
+ options={new Map([["BRAND", []]])}
120
+ />,
121
+ );
122
+
123
+ expect(screen.queryAllByText(/Could not load the options/)).toHaveLength(
124
+ 0,
125
+ );
126
+ });
127
+ });
128
+
129
+ describe("layout and title", () => {
130
+ // Both props exist for the dashboard surface, which does not land in this
131
+ // slice, so nothing in the repo passes either one. Exercised here so the
132
+ // first render of the `bar` branch is not an external consumer's.
133
+ const props = {
134
+ givens: [MULTISELECT],
135
+ values: new Map(),
136
+ onChange: () => {},
137
+ onReset: () => {},
138
+ options: new Map([["BRAND", ["Nike"]]]),
139
+ };
140
+
141
+ it('titles the panel "Parameters" by default', () => {
142
+ render(<GivensPanel {...props} />);
143
+ expect(screen.getByText("Parameters")).toBeDefined();
144
+ });
145
+
146
+ it("uses a given title in place of the default", () => {
147
+ render(<GivensPanel {...props} title="Filters" />);
148
+ expect(screen.getByText("Filters")).toBeDefined();
149
+ expect(screen.queryAllByText("Parameters")).toHaveLength(0);
150
+ });
151
+
152
+ it("renders the bar layout with its controls and no heading", () => {
153
+ render(<GivensPanel {...props} layout="bar" />);
154
+ // The bar is meant to sit inline above a dashboard, so it carries the
155
+ // controls but drops the heading the panel layout shows.
156
+ expect(screen.getByRole("combobox")).toBeDefined();
157
+ expect(screen.queryAllByText("Parameters")).toHaveLength(0);
158
+ });
159
+
160
+ it("still offers Reset from the bar layout", () => {
161
+ let reset = 0;
162
+ render(
163
+ <GivensPanel
164
+ {...props}
165
+ layout="bar"
166
+ values={new Map([["BRAND", "Nike"]])}
167
+ onReset={() => {
168
+ reset += 1;
169
+ }}
170
+ />,
171
+ );
172
+ fireEvent.click(screen.getByRole("button", { name: "Reset" }));
173
+ expect(reset).toBe(1);
174
+ });
175
+ });
@@ -0,0 +1,195 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { Box, Button, Paper, Stack, Typography } from "@mui/material";
5
+ import { Given } from "../../client";
6
+ import { GivenValue } from "../../hooks/givenValue";
7
+ import { GivenInput } from "./GivenInput";
8
+
9
+ /**
10
+ * How the control row is laid out.
11
+ *
12
+ * - `panel`: the vertical "Parameters" block a notebook shows above its cells.
13
+ * - `bar`: a horizontal filter bar above a dashboard grid.
14
+ *
15
+ * Two presentations of one control implementation, rather than two
16
+ * implementations: a control gains a behavior (a suggest dropdown, an Apply
17
+ * button) on both surfaces at once.
18
+ */
19
+ export type GivensLayout = "panel" | "bar";
20
+
21
+ export interface GivensPanelProps {
22
+ givens: Given[];
23
+ values: Map<string, GivenValue>;
24
+ onChange: (name: string, value: GivenValue) => void;
25
+ onReset: () => void;
26
+ layout?: GivensLayout;
27
+ /** Resolved `suggest` options per given name. */
28
+ options?: Map<string, string[]>;
29
+ optionsLoading?: boolean;
30
+ /** Givens whose `suggest` query failed, from `useSuggestOptions.failed`. */
31
+ optionsFailed?: ReadonlySet<string>;
32
+ /**
33
+ * Set to render an Apply button, for `autorun=false`. Absent means controls
34
+ * take effect as they change, which is the default everywhere.
35
+ */
36
+ apply?: {
37
+ onApply: () => void;
38
+ /** Whether anything has changed since the last apply. */
39
+ pending: boolean;
40
+ };
41
+ /** Overrides the `panel` layout's "Parameters" heading. */
42
+ title?: string;
43
+ }
44
+
45
+ /**
46
+ * Renders the control row: one input per declared `given:`.
47
+ * Returns null when the model declares no givens, so callers can drop
48
+ * `<GivensPanel ... />` unconditionally without a length guard.
49
+ *
50
+ * A "Reset" button appears in the header when at least one value is set; it
51
+ * fires `onReset`, which the parent should wire to `useGivensState.reset`. That
52
+ * puts the document's starting values back rather than emptying the row, so the
53
+ * button and the state agree on what "Reset" means.
54
+ */
55
+ export function GivensPanel({
56
+ givens,
57
+ values,
58
+ onChange,
59
+ onReset,
60
+ layout = "panel",
61
+ options,
62
+ optionsLoading,
63
+ optionsFailed,
64
+ apply,
65
+ title,
66
+ }: GivensPanelProps) {
67
+ if (givens.length === 0) return null;
68
+ // Some value actually SET, not merely some entry present. `paramsToGivens`
69
+ // records `?X=` as a null entry, so counting entries offered Reset for a
70
+ // control that is unset, and pressing it changed nothing and left the empty
71
+ // parameter in the URL.
72
+ const hasValues = Array.from(values.values()).some((v) => v !== null);
73
+
74
+ const inputs = givens.map((given) => (
75
+ <Box
76
+ key={given.name}
77
+ sx={
78
+ layout === "bar"
79
+ ? { minWidth: 200, flex: "1 1 200px", maxWidth: 320 }
80
+ : undefined
81
+ }
82
+ >
83
+ <GivenInput
84
+ given={given}
85
+ value={given.name ? values.get(given.name) : undefined}
86
+ onChange={(next) => given.name && onChange(given.name, next)}
87
+ options={given.name ? options?.get(given.name) : undefined}
88
+ optionsLoading={optionsLoading}
89
+ optionsFailed={
90
+ given.name ? optionsFailed?.has(given.name) : undefined
91
+ }
92
+ />
93
+ </Box>
94
+ ));
95
+
96
+ const actions = (
97
+ <Stack direction="row" alignItems="center" spacing={1}>
98
+ {apply && (
99
+ <Button
100
+ variant="contained"
101
+ size="small"
102
+ disableElevation
103
+ onClick={apply.onApply}
104
+ disabled={!apply.pending}
105
+ sx={{ textTransform: "none" }}
106
+ >
107
+ Apply
108
+ </Button>
109
+ )}
110
+ {hasValues && (
111
+ <Button
112
+ variant="text"
113
+ size="small"
114
+ onClick={onReset}
115
+ sx={{ textTransform: "none" }}
116
+ >
117
+ Reset
118
+ </Button>
119
+ )}
120
+ </Stack>
121
+ );
122
+
123
+ if (layout === "bar") {
124
+ return (
125
+ <Paper
126
+ elevation={0}
127
+ sx={{
128
+ p: 2,
129
+ border: 1,
130
+ borderColor: "divider",
131
+ borderRadius: 1,
132
+ backgroundColor: "background.paper",
133
+ }}
134
+ >
135
+ <Stack
136
+ direction="row"
137
+ alignItems="flex-start"
138
+ spacing={2}
139
+ useFlexGap
140
+ sx={{ flexWrap: "wrap" }}
141
+ >
142
+ {inputs}
143
+ <Box sx={{ flex: "0 0 auto", ml: "auto", pt: 0.5 }}>
144
+ {actions}
145
+ </Box>
146
+ </Stack>
147
+ </Paper>
148
+ );
149
+ }
150
+
151
+ return (
152
+ <Paper
153
+ elevation={0}
154
+ sx={{
155
+ p: 3,
156
+ backgroundColor: "transparent",
157
+ border: "none",
158
+ boxShadow: "none",
159
+ }}
160
+ >
161
+ <Stack
162
+ direction="row"
163
+ alignItems="center"
164
+ justifyContent="space-between"
165
+ sx={{ mb: 2 }}
166
+ >
167
+ <Typography
168
+ variant="subtitle2"
169
+ sx={{ fontWeight: 600, color: "#333" }}
170
+ >
171
+ {title ?? "Parameters"}
172
+ </Typography>
173
+ {actions}
174
+ </Stack>
175
+ <Box
176
+ sx={{
177
+ display: "grid",
178
+ // Equal columns, so controls line up in a grid rather than
179
+ // packing by their own widths. 220px is a deliberate floor: at
180
+ // 250 a four-given notebook came out three-across with a lone
181
+ // control stranded on a second row at the common page width.
182
+ gridTemplateColumns: "repeat(auto-fill, minmax(220px, 1fr))",
183
+ // Each control is label + field + a helper line or two, so rows
184
+ // are top-aligned; a taller helper must not push its neighbours'
185
+ // fields down.
186
+ alignItems: "start",
187
+ columnGap: 3,
188
+ rowGap: 2.5,
189
+ }}
190
+ >
191
+ {inputs}
192
+ </Box>
193
+ </Paper>
194
+ );
195
+ }