@malloy-publisher/sdk 0.0.382-dev → 0.2.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 (277) 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-CTopDUWW.cjs.js +1 -0
  5. package/dist/ServerProvider-DUFPY6Rv.es.js +4072 -0
  6. package/dist/client/api.d.ts +3718 -1507
  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 +85 -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 +49 -0
  14. package/dist/components/Dashboard/DashboardTile.d.ts +48 -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-hLahnv0W.cjs.js} +1 -1
  66. package/dist/{core-CjeTkq8O.es.js → core-lb28vQkR.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-DvEanVgc.cjs.js +339 -0
  78. package/dist/{index-DRDu9kIV.es.js → index-txw04ZbN.es.js} +40531 -27964
  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 +25 -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 +30 -2
  104. package/src/components/Connections/DeleteConnectionDialog.tsx +14 -3
  105. package/src/components/Connections/EditConnectionDialog.tsx +33 -4
  106. package/src/components/Connections/common.ts +64 -1
  107. package/src/components/DOMElement/DOMElement.tsx +3 -0
  108. package/src/components/DOMElement/index.ts +3 -0
  109. package/src/components/Dashboard/Dashboard.tsx +398 -0
  110. package/src/components/Dashboard/DashboardTile.tsx +153 -0
  111. package/src/components/Dashboard/index.ts +5 -0
  112. package/src/components/DataAppViewer/DataAppViewer.tsx +214 -0
  113. package/src/components/DataAppViewer/index.ts +4 -0
  114. package/src/components/{Project → Environment}/About.tsx +11 -4
  115. package/src/components/Environment/AddPackageDialog.tsx +202 -0
  116. package/src/components/{Project → Environment}/ConnectionExplorer.tsx +17 -14
  117. package/src/components/Environment/Connections.tsx +391 -0
  118. package/src/components/{Project → Environment}/DeletePackageDialog.tsx +8 -4
  119. package/src/components/{Project → Environment}/EditPackageDialog.tsx +21 -13
  120. package/src/components/Environment/Environment.tsx +89 -0
  121. package/src/components/Environment/Packages.tsx +206 -0
  122. package/src/components/Environment/index.ts +5 -0
  123. package/src/components/Home/{AddProjectDialog.tsx → AddEnvironmentDialog.tsx} +25 -19
  124. package/src/components/Home/{DeleteProjectDialog.tsx → DeleteEnvironmentDialog.tsx} +20 -15
  125. package/src/components/Home/{EditProjectDialog.tsx → EditEnvironmentDialog.tsx} +35 -28
  126. package/src/components/Home/Home.tsx +386 -408
  127. package/src/components/Home/index.ts +3 -0
  128. package/src/components/Loading.tsx +3 -0
  129. package/src/components/Materializations/CreateEnvMaterializationDialog.tsx +179 -0
  130. package/src/components/Materializations/CreateMaterializationDialog.tsx +112 -0
  131. package/src/components/Materializations/DeleteMaterializationDialog.tsx +109 -0
  132. package/src/components/Materializations/EnvironmentMaterializations.tsx +267 -0
  133. package/src/components/Materializations/ManifestView.tsx +87 -0
  134. package/src/components/Materializations/MaterializationDetailDialog.tsx +259 -0
  135. package/src/components/Materializations/MaterializationRunsList.tsx +256 -0
  136. package/src/components/Materializations/Materializations.tsx +355 -0
  137. package/src/components/Materializations/ScheduleCard.tsx +231 -0
  138. package/src/components/Materializations/SectionLabel.tsx +30 -0
  139. package/src/components/Materializations/SetScheduleDialog.tsx +162 -0
  140. package/src/components/Materializations/TriggerChip.tsx +44 -0
  141. package/src/components/Materializations/cron.ts +124 -0
  142. package/src/components/Materializations/index.ts +5 -0
  143. package/src/components/Materializations/utils.ts +145 -0
  144. package/src/components/Model/Model.tsx +44 -2
  145. package/src/components/Model/ModelCell.tsx +14 -5
  146. package/src/components/Model/ModelExplorer.tsx +3 -0
  147. package/src/components/Model/ModelExplorerDialog.tsx +4 -1
  148. package/src/components/Model/SourcesExplorer.tsx +9 -6
  149. package/src/components/Model/index.ts +3 -0
  150. package/src/components/Model/useModelData.ts +6 -3
  151. package/src/components/Notebook/Notebook.tsx +475 -339
  152. package/src/components/Notebook/NotebookCell.tsx +264 -25
  153. package/src/components/Notebook/index.ts +3 -0
  154. package/src/components/Notebook/runPlan.spec.ts +155 -0
  155. package/src/components/Notebook/runPlan.ts +67 -0
  156. package/src/components/Notebook/types.ts +14 -0
  157. package/src/components/Package/ContentTypeIcon.spec.ts +91 -0
  158. package/src/components/Package/ContentTypeIcon.tsx +177 -0
  159. package/src/components/Package/FileTreeView.tsx +3 -0
  160. package/src/components/Package/Models.tsx +7 -4
  161. package/src/components/Package/Package.tsx +666 -59
  162. package/src/components/Package/index.ts +3 -0
  163. package/src/components/QueryResult/QueryResult.tsx +17 -8
  164. package/src/components/QueryResult/index.ts +3 -0
  165. package/src/components/RenderedResult/RenderedResult.tsx +685 -67
  166. package/src/components/RenderedResult/ResultContainer.tsx +71 -2
  167. package/src/components/RenderedResult/index.ts +3 -0
  168. package/src/components/RenderedResult/renderLogs.spec.ts +58 -0
  169. package/src/components/RenderedResult/renderLogs.ts +43 -0
  170. package/src/components/ResultsDialog.tsx +13 -1
  171. package/src/components/ServerProvider.tsx +175 -51
  172. package/src/components/Workbook/BrowserWorkbookStorage.ts +3 -0
  173. package/src/components/Workbook/EditableMalloyCell.tsx +3 -0
  174. package/src/components/Workbook/ModelPicker.tsx +7 -4
  175. package/src/components/Workbook/MutableCell.tsx +10 -5
  176. package/src/components/Workbook/Workbook.tsx +8 -5
  177. package/src/components/Workbook/WorkbookList.tsx +3 -0
  178. package/src/components/Workbook/WorkbookManager.ts +3 -0
  179. package/src/components/Workbook/WorkbookStorage.ts +3 -0
  180. package/src/components/Workbook/WorkbookStorageProvider.tsx +3 -0
  181. package/src/components/Workbook/index.ts +3 -0
  182. package/src/components/click_helper.ts +17 -1
  183. package/src/components/drill/index.ts +28 -0
  184. package/src/components/drill/markDrillableCells.dom.spec.ts +313 -0
  185. package/src/components/drill/markDrillableCells.spec.ts +109 -0
  186. package/src/components/drill/markDrillableCells.ts +314 -0
  187. package/src/components/drill/resolveDrill.spec.ts +449 -0
  188. package/src/components/drill/resolveDrill.ts +375 -0
  189. package/src/components/drill/useDrill.tsx +203 -0
  190. package/src/components/filter/DimensionFilter.tsx +3 -0
  191. package/src/components/filter/index.ts +3 -0
  192. package/src/components/filter/utils.ts +16 -5
  193. package/src/components/given/GivenInput.spec.tsx +689 -0
  194. package/src/components/given/GivenInput.tsx +801 -0
  195. package/src/components/given/GivensPanel.spec.tsx +175 -0
  196. package/src/components/given/GivensPanel.tsx +195 -0
  197. package/src/components/given/filterValue.spec.ts +364 -0
  198. package/src/components/given/filterValue.ts +207 -0
  199. package/src/components/given/index.ts +9 -0
  200. package/src/components/given/paramCodec.spec.ts +857 -0
  201. package/src/components/given/paramCodec.ts +483 -0
  202. package/src/components/given/utils.spec.ts +60 -0
  203. package/src/components/given/utils.ts +79 -0
  204. package/src/components/highlighter.ts +22 -5
  205. package/src/components/index.ts +10 -2
  206. package/src/components/styles.ts +82 -33
  207. package/src/constants/docLinks.spec.ts +54 -0
  208. package/src/constants/docLinks.ts +46 -0
  209. package/src/hooks/givenValue.ts +13 -0
  210. package/src/hooks/index.ts +13 -0
  211. package/src/hooks/useDimensionFilters.ts +4 -7
  212. package/src/hooks/useDimensionFiltersFromSpec.ts +8 -5
  213. package/src/hooks/useDimensionFiltersQuery.ts +10 -13
  214. package/src/hooks/useDimensionalFilterRangeData.ts +11 -14
  215. package/src/hooks/useGivensState.spec.ts +449 -0
  216. package/src/hooks/useGivensState.ts +340 -0
  217. package/src/hooks/useModelGivens.ts +29 -0
  218. package/src/hooks/useQueryWithApiError.ts +3 -0
  219. package/src/hooks/useRawQueryData.ts +6 -3
  220. package/src/hooks/useSuggestOptions.spec.ts +112 -0
  221. package/src/hooks/useSuggestOptions.ts +263 -0
  222. package/src/index.ts +10 -1
  223. package/src/react-app-env.d.ts +3 -0
  224. package/src/theme/ThemeContext.tsx +86 -0
  225. package/src/theme/buildMalloyExplicitTheme.ts +61 -0
  226. package/src/theme/buildTableCssVars.spec.ts +67 -0
  227. package/src/theme/buildTableCssVars.ts +67 -0
  228. package/src/theme/buildVegaThemeOverride.spec.ts +56 -0
  229. package/src/theme/buildVegaThemeOverride.ts +47 -0
  230. package/src/theme/defaults.ts +78 -0
  231. package/src/theme/index.ts +11 -0
  232. package/src/theme/keys.ts +32 -0
  233. package/src/theme/readChartAnnotations.spec.ts +58 -0
  234. package/src/theme/readChartAnnotations.ts +69 -0
  235. package/src/theme/resolveTheme.spec.ts +160 -0
  236. package/src/theme/resolveTheme.ts +121 -0
  237. package/src/theme/types.ts +79 -0
  238. package/src/utils/dataAppEmbed.spec.ts +63 -0
  239. package/src/utils/dataAppEmbed.ts +84 -0
  240. package/src/utils/formatting.spec.ts +25 -21
  241. package/src/utils/formatting.ts +11 -7
  242. package/src/utils/malloyString.spec.ts +24 -0
  243. package/src/utils/malloyString.ts +12 -0
  244. package/src/utils/parsing.spec.ts +26 -19
  245. package/src/utils/parsing.ts +11 -6
  246. package/src/utils/queryClient.ts +17 -0
  247. package/test/README.md +141 -0
  248. package/test/setup.ts +29 -0
  249. package/tsconfig.json +12 -1
  250. package/tsconfig.spec.json +22 -0
  251. package/vite.config.ts +3 -0
  252. package/dist/ServerProvider-lOQXDlqB.cjs.js +0 -1
  253. package/dist/ServerProvider-on-8FH5Z.es.js +0 -3060
  254. package/dist/components/Home/AddProjectDialog.d.ts +0 -1
  255. package/dist/components/Home/DeleteProjectDialog.d.ts +0 -5
  256. package/dist/components/Home/EditProjectDialog.d.ts +0 -7
  257. package/dist/components/Package/Config.d.ts +0 -5
  258. package/dist/components/Package/Databases.d.ts +0 -5
  259. package/dist/components/Package/Notebooks.d.ts +0 -6
  260. package/dist/components/Project/Project.d.ts +0 -6
  261. package/dist/components/Project/index.d.ts +0 -2
  262. package/dist/index-BOLBP6_i.cjs.js +0 -233
  263. package/src/components/Package/Config.tsx +0 -97
  264. package/src/components/Package/Connections.tsx +0 -373
  265. package/src/components/Package/Databases.tsx +0 -228
  266. package/src/components/Package/Notebooks.tsx +0 -77
  267. package/src/components/Project/AddPackageDialog.tsx +0 -159
  268. package/src/components/Project/Packages.tsx +0 -191
  269. package/src/components/Project/Project.tsx +0 -52
  270. package/src/components/Project/index.ts +0 -2
  271. /package/dist/components/{Project → Environment}/About.d.ts +0 -0
  272. /package/dist/components/{Project → Environment}/AddPackageDialog.d.ts +0 -0
  273. /package/dist/components/{Project → Environment}/ConnectionExplorer.d.ts +0 -0
  274. /package/dist/components/{Package → Environment}/Connections.d.ts +0 -0
  275. /package/dist/components/{Project → Environment}/DeletePackageDialog.d.ts +0 -0
  276. /package/dist/components/{Project → Environment}/EditPackageDialog.d.ts +0 -0
  277. /package/dist/components/{Project → Environment}/Packages.d.ts +0 -0
@@ -0,0 +1,340 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
5
+ import { givensToParams, paramsToGivens } from "../components/given/paramCodec";
6
+ import type { GivenValue } from "./givenValue";
7
+
8
+ /**
9
+ * The state behind a control row: what the controls show, what the queries run
10
+ * with, and how a change gets from one to the other.
11
+ *
12
+ * Those are two different things whenever `autorun=false`. Editing three
13
+ * controls should re-run the queries once, when Apply is pressed, not three
14
+ * times on the way there, which matters most in a notebook, where one control
15
+ * change re-runs every cell in the document.
16
+ */
17
+ export interface UseGivensStateOptions {
18
+ /** Declared type per given name; decides how a URL value is read back. */
19
+ declaredTypes: ReadonlyMap<string, string | undefined>;
20
+ /**
21
+ * Starting values from `# artifact givens {…}`. Overridden by anything in
22
+ * `params`, so a link always wins over the file's defaults: otherwise a
23
+ * shared URL would not show what the sender was looking at.
24
+ */
25
+ startingValues?: Record<string, string>;
26
+ /**
27
+ * URL-carried values from the host. Read on mount and whenever they change
28
+ * beneath us (a drill navigating to a new URL for the same dashboard).
29
+ */
30
+ params?: Record<string, string>;
31
+ /**
32
+ * Called with the applied values whenever they change, for the host to write
33
+ * to the URL. It is the *applied* values, not the draft: a link should
34
+ * reproduce what the sender was looking at, not controls they had not
35
+ * committed yet.
36
+ */
37
+ onParamsChange?: (params: Record<string, string>) => void;
38
+ /**
39
+ * Identifies the document the edits belong to, so they are dropped when the
40
+ * host swaps documents underneath this hook. Without it the edits are keyed
41
+ * by their starting VALUES alone, and two documents whose starting values
42
+ * coincide (the common case: both empty) look like one, so the previous
43
+ * document's applied values keep filtering the new one.
44
+ */
45
+ documentKey?: string;
46
+ /** False batches changes behind Apply. */
47
+ autorun: boolean;
48
+ }
49
+
50
+ export interface UseGivensStateResult {
51
+ /** What the controls show. */
52
+ draft: Map<string, GivenValue>;
53
+ /** What queries should run with. Equal to `draft` when autorun. */
54
+ applied: Map<string, GivenValue>;
55
+ setGiven: (name: string, value: GivenValue) => void;
56
+ /**
57
+ * Back to where the document says the controls start, committed.
58
+ *
59
+ * Committed unconditionally, `autorun` or not: Reset is an explicit action
60
+ * like Apply, and leaving it pending makes the button look broken. And back
61
+ * to the *starting values* rather than to empty, because empty is not a
62
+ * state the document describes: clearing to it only to have the starting
63
+ * values flow back in from the URL is what made Reset run the queries twice.
64
+ */
65
+ reset: () => void;
66
+ /** Commit the draft. A no-op when autorun, where it is already committed. */
67
+ apply: () => void;
68
+ /** Whether the draft differs from what is applied. */
69
+ pending: boolean;
70
+ }
71
+
72
+ /**
73
+ * The user's edits, tagged with the starting point they were made against.
74
+ *
75
+ * The tag is what lets a new starting point take effect without an effect to
76
+ * clear the old edits, because edits whose key no longer matches are simply not
77
+ * read. A new starting point means a different dashboard, or the same one at a
78
+ * URL a drill has just pushed.
79
+ */
80
+ interface Edits {
81
+ key: string;
82
+ draft: Map<string, GivenValue>;
83
+ applied: Map<string, GivenValue>;
84
+ }
85
+
86
+ /** Values for givens the model no longer declares, dropped. */
87
+ function prune(
88
+ values: Map<string, GivenValue>,
89
+ declaredTypes: ReadonlyMap<string, string | undefined>,
90
+ ): Map<string, GivenValue> {
91
+ const stale = Array.from(values.keys()).filter(
92
+ (name) => !declaredTypes.has(name),
93
+ );
94
+ if (stale.length === 0) return values;
95
+ const next = new Map(values);
96
+ for (const name of stale) next.delete(name);
97
+ return next;
98
+ }
99
+
100
+ /**
101
+ * Entries in a fixed order, so the key below depends on the values and not on
102
+ * the order a host happened to build its object in. `URLSearchParams` iterates
103
+ * in the query string's own order, so `?B=2&A=1` and `?A=1&B=2` would otherwise
104
+ * be different starting points and re-key the reader's edits for nothing.
105
+ */
106
+ function stableEntries(source: Record<string, string> | undefined) {
107
+ return Object.entries(source ?? {}).sort(([a], [b]) => (a < b ? -1 : 1));
108
+ }
109
+
110
+ /** Same entries, same values: enough for state whose values are primitives. */
111
+ function sameParams(
112
+ a: Record<string, string>,
113
+ b: Record<string, string>,
114
+ ): boolean {
115
+ const aKeys = Object.keys(a);
116
+ if (aKeys.length !== Object.keys(b).length) return false;
117
+ return aKeys.every((key) => a[key] === b[key]);
118
+ }
119
+
120
+ export function useGivensState({
121
+ declaredTypes,
122
+ startingValues,
123
+ params,
124
+ onParamsChange,
125
+ documentKey,
126
+ autorun,
127
+ }: UseGivensStateOptions): UseGivensStateResult {
128
+ // Where the document says the controls start, with no URL in it. This is
129
+ // what Reset goes back to, so it deliberately ignores `params`: the URL is
130
+ // the thing being reset away from.
131
+ const startingPoint = useMemo(
132
+ () => paramsToGivens(startingValues ?? {}, declaredTypes),
133
+ // eslint-disable-next-line react-hooks/exhaustive-deps
134
+ [declaredTypes, JSON.stringify(startingValues)],
135
+ );
136
+
137
+ // The URL beats the file's starting values, per the precedence above.
138
+ const initial = useMemo(
139
+ () =>
140
+ paramsToGivens(
141
+ { ...(startingValues ?? {}), ...(params ?? {}) },
142
+ declaredTypes,
143
+ ),
144
+ // eslint-disable-next-line react-hooks/exhaustive-deps
145
+ [declaredTypes, JSON.stringify(startingValues), JSON.stringify(params)],
146
+ );
147
+
148
+ // Which starting point the edits below belong to. A change means a different
149
+ // document, or the same one at a different URL after a drill.
150
+ //
151
+ // The document is part of the key, not just the values. Keyed by values
152
+ // alone, navigating between two notebooks that both start empty produced
153
+ // equal keys, so the first notebook's applied values stayed `active` and
154
+ // filtered the second, while `lastReported` (equal to them already)
155
+ // suppressed the report that would have put them in the URL: the address bar
156
+ // and the running cells disagreed, and copying the URL reproduced neither.
157
+ //
158
+ // Built from the INPUTS that define a starting point, not from `initial`,
159
+ // which is those inputs already filtered by `declaredTypes`. Keying on the
160
+ // filtered map made the set of declared givens part of the key, so a model
161
+ // reload that merely dropped one given re-keyed everything and discarded the
162
+ // reader's edits to the givens that SURVIVED, which is the opposite of what
163
+ // `prune` below is written to do. Dropping stale values is `prune`'s job; a
164
+ // declaration change is not a new starting point.
165
+ const initialKey = useMemo(
166
+ () =>
167
+ JSON.stringify([
168
+ documentKey ?? null,
169
+ stableEntries(startingValues),
170
+ stableEntries(params),
171
+ ]),
172
+ // eslint-disable-next-line react-hooks/exhaustive-deps
173
+ [documentKey, JSON.stringify(startingValues), JSON.stringify(params)],
174
+ );
175
+
176
+ // KNOWN LIMITATION, latent today. A given that has a starting value cannot be
177
+ // cleared: `setGiven(name, null)` drops the key, the report omits it, the host
178
+ // removes it from the URL, `initial` recomputes from `startingValues` and
179
+ // still carries it, `initialKey` therefore changes, the edits are discarded as
180
+ // stale, and the control snaps back to the value the reader just cleared.
181
+ //
182
+ // Not reachable yet: no server populates `startingGivens`, and deliberately
183
+ // not fixed here, because the fix has to tell our OWN echo of the URL (which
184
+ // must not discard edits) from an externally pushed URL such as a drill
185
+ // arriving (which must), and that is the one piece of this hook the drill path
186
+ // depends on and is covered by live tests. Getting it wrong breaks something
187
+ // that works to fix something nothing can reach.
188
+ //
189
+ // The intended shape, for whoever makes it reachable: compare the incoming
190
+ // `params` against `lastReported` below, treat a match as our own echo, and
191
+ // keep the edits (and their key) across it. An explicit `cleared` set on
192
+ // `Edits` is the alternative and needs the same echo test to survive.
193
+ //
194
+ // Only the user's edits are state; `initial` is read through, not copied in.
195
+ //
196
+ // Copying it in on arrival cannot be made correct here. The starting values
197
+ // are not known on mount: they arrive with the manifest, a render later,
198
+ // and every way of adopting them afterwards is wrong in its own way. An
199
+ // effect hands the surfaces the empty map first and the real values a commit
200
+ // later, so a notebook link carrying parameters runs every cell bare and
201
+ // then runs the whole document again. A render-phase update avoids the extra
202
+ // commit but does not survive: React can restart a render attempt and drop
203
+ // the queued updates, which left the controls blank while a stale draft won.
204
+ //
205
+ // Reading through has neither problem. The first render that sees the
206
+ // manifest already reports the right values, and there is no second state to
207
+ // fall out of step with the props.
208
+ const [edits, setEdits] = useState<Edits | null>(null);
209
+ const active = edits?.key === initialKey ? edits : null;
210
+
211
+ // Edits whose key no longer matches are DISCARDED, not merely ignored. The
212
+ // line above stops reading them, which is enough until the key comes back:
213
+ // the key space is small (document plus starting values plus params) and is
214
+ // revisited routinely, so `a` -> `b` -> `a` from a clean link matched the
215
+ // abandoned edits again and re-applied a filter the URL did not carry, to a
216
+ // reader who had just opened the notebook fresh. Browser Back and a drill
217
+ // pushing a URL equal to an earlier one do the same thing.
218
+ //
219
+ // In an effect rather than during render: `active` already ignores them, so
220
+ // this cannot change what this render shows, and clearing is idempotent if
221
+ // React runs it more than once.
222
+ useEffect(() => {
223
+ if (edits !== null && edits.key !== initialKey) setEdits(null);
224
+ }, [edits, initialKey]);
225
+
226
+ // Drop values for givens that no longer exist, which is what a model reload
227
+ // can do while the page is open. Sending one the model no longer declares
228
+ // fails the query outright ("unknown given"), so a value outliving its
229
+ // declaration is worse than losing the edit. Values for givens that survive
230
+ // the reload are kept, since re-typing them would be the greater annoyance.
231
+ //
232
+ // `initial` needs none of this: `paramsToGivens` already keeps only declared
233
+ // names, which is why this narrows the edits alone.
234
+ const draft = useMemo(
235
+ () => prune(active?.draft ?? initial, declaredTypes),
236
+ [active, initial, declaredTypes],
237
+ );
238
+ const applied = useMemo(
239
+ () => prune(active?.applied ?? initial, declaredTypes),
240
+ [active, initial, declaredTypes],
241
+ );
242
+
243
+ // Edits are made against whatever is on screen now, so a first edit has to
244
+ // start from `initial` rather than from an empty map: otherwise setting one
245
+ // control would silently drop the starting values of the others.
246
+ const base = useCallback(
247
+ (prev: Edits | null): Edits =>
248
+ prev?.key === initialKey
249
+ ? prev
250
+ : { key: initialKey, draft: initial, applied: initial },
251
+ [initialKey, initial],
252
+ );
253
+
254
+ // Report applied values outward for the host to put in the URL. Skipped when
255
+ // they already match, so this cannot loop against a host that feeds its URL
256
+ // back in through `params`.
257
+ //
258
+ // Starting values are reported too, not just edits: the address bar is meant
259
+ // to show what the reader is looking at, so a dashboard opened at its
260
+ // declared defaults is already a link that reproduces them.
261
+ //
262
+ // The reference starts at the first render's values rather than empty. The
263
+ // control row is empty until the manifest arrives and says which givens
264
+ // exist, and an unseeded reference would report that emptiness on mount,
265
+ // telling the host to clear a URL whose parameters had not been read yet,
266
+ // wiping the state the link was carrying.
267
+ //
268
+ // ACCEPTED COST of that seeding: when the manifest is already cached, the
269
+ // first render knows the types, so the seed equals the URL's values and no
270
+ // initial report fires at all. A host that learns which names are its own
271
+ // FROM the reports (as `NotebookPage` does) therefore never learns them, and
272
+ // if a later model reload drops one of those givens, its parameter is left
273
+ // behind in the address bar. Kept because the alternative is worse: an
274
+ // unseeded reference wipes a shared link's parameters on mount for any
275
+ // consumer that wires this callback before the manifest lands, and a
276
+ // leftover parameter for an undeclared given is inert, since `paramsToGivens`
277
+ // ignores names the model does not declare.
278
+ const appliedParams = useMemo(
279
+ () => givensToParams(applied, declaredTypes),
280
+ [applied, declaredTypes],
281
+ );
282
+ const lastReported = useRef<Record<string, string>>(appliedParams);
283
+ useEffect(() => {
284
+ if (!onParamsChange) return;
285
+ if (sameParams(lastReported.current, appliedParams)) return;
286
+ lastReported.current = appliedParams;
287
+ onParamsChange(appliedParams);
288
+ }, [appliedParams, onParamsChange]);
289
+
290
+ const setGiven = useCallback(
291
+ (name: string, value: GivenValue) => {
292
+ setEdits((prev) => {
293
+ const from = base(prev);
294
+ const next = new Map(from.draft);
295
+ if (value === null) next.delete(name);
296
+ else next.set(name, value);
297
+ return {
298
+ key: from.key,
299
+ draft: next,
300
+ applied: autorun ? next : from.applied,
301
+ };
302
+ });
303
+ },
304
+ [autorun, base],
305
+ );
306
+
307
+ const reset = useCallback(() => {
308
+ setEdits((prev) => ({
309
+ key: base(prev).key,
310
+ draft: startingPoint,
311
+ applied: startingPoint,
312
+ }));
313
+ }, [base, startingPoint]);
314
+
315
+ const apply = useCallback(() => {
316
+ setEdits((prev) => {
317
+ const from = base(prev);
318
+ return { key: from.key, draft: from.draft, applied: from.draft };
319
+ });
320
+ }, [base]);
321
+
322
+ const pending = useMemo(() => {
323
+ if (autorun) return false;
324
+ if (draft.size !== applied.size) return true;
325
+ for (const [name, value] of draft) {
326
+ const other = applied.get(name);
327
+ // Dates are the one non-primitive here, so compare them by instant.
328
+ if (value instanceof Date && other instanceof Date) {
329
+ if (value.getTime() !== other.getTime()) return true;
330
+ } else if (Array.isArray(value) && Array.isArray(other)) {
331
+ if (String(value) !== String(other)) return true;
332
+ } else if (value !== other) {
333
+ return true;
334
+ }
335
+ }
336
+ return false;
337
+ }, [autorun, draft, applied]);
338
+
339
+ return { draft, applied, setGiven, reset, apply, pending };
340
+ }
@@ -0,0 +1,29 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { useMemo } from "react";
5
+ import { Given, RawNotebook } from "../client";
6
+
7
+ /**
8
+ * Extracts the deduplicated list of model-level `given:` declarations from
9
+ * a notebook's sources. The server attaches the same model-level givens to
10
+ * every `Source` for SDK ergonomics (see #761); this hook collapses them
11
+ * back to one entry per name so callers render a single input per given.
12
+ *
13
+ * Returns an empty array when no givens are declared on any source.
14
+ */
15
+ export function useModelGivens(notebook: RawNotebook | undefined): Given[] {
16
+ return useMemo(() => {
17
+ if (!notebook?.sources?.length) return [];
18
+ const byName = new Map<string, Given>();
19
+ for (const source of notebook.sources) {
20
+ if (!source.givens?.length) continue;
21
+ for (const given of source.givens) {
22
+ if (given.name && !byName.has(given.name)) {
23
+ byName.set(given.name, given);
24
+ }
25
+ }
26
+ }
27
+ return Array.from(byName.values());
28
+ }, [notebook]);
29
+ }
@@ -1,3 +1,6 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
1
4
  import {
2
5
  useMutation,
3
6
  UseMutationOptions,
@@ -1,3 +1,6 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
1
4
  import { useServer } from "../components/ServerProvider";
2
5
  import { parseResourceUri } from "../utils/formatting";
3
6
  import { useQueryWithApiError } from "./useQueryWithApiError";
@@ -19,14 +22,14 @@ export function useRawQueryData({
19
22
  enabled = true,
20
23
  resourceUri,
21
24
  }: UseRawQueryDataProps) {
22
- const { projectName, packageName, versionId } =
25
+ const { environmentName, packageName, versionId } =
23
26
  parseResourceUri(resourceUri);
24
27
  const { apiClients } = useServer();
25
28
 
26
29
  const { data, isSuccess, isError, error, isLoading } = useQueryWithApiError({
27
30
  queryKey: [
28
31
  "rawQueryData",
29
- projectName,
32
+ environmentName,
30
33
  packageName,
31
34
  modelPath,
32
35
  versionId,
@@ -36,7 +39,7 @@ export function useRawQueryData({
36
39
  ],
37
40
  queryFn: () =>
38
41
  apiClients.models.executeQueryModel(
39
- projectName,
42
+ environmentName,
40
43
  packageName,
41
44
  modelPath,
42
45
  {
@@ -0,0 +1,112 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import {
6
+ buildSuggestQuery,
7
+ readOptionValues,
8
+ SUGGEST_OPTION_LIMIT,
9
+ } from "./useSuggestOptions";
10
+
11
+ describe("readOptionValues", () => {
12
+ it("takes the named dimension's column", () => {
13
+ const rows = JSON.stringify([
14
+ { region: "us-east", orders: 12 },
15
+ { region: "us-west", orders: 8 },
16
+ ]);
17
+ expect(readOptionValues(rows, "region")).toEqual(["us-east", "us-west"]);
18
+ });
19
+
20
+ it("falls back to the first column", () => {
21
+ // A `suggest { query=… }` names no dimension, and its column can be
22
+ // called anything.
23
+ const rows = JSON.stringify([{ name: "Nike" }, { name: "Levi's" }]);
24
+ expect(readOptionValues(rows, undefined)).toEqual(["Nike", "Levi's"]);
25
+ });
26
+
27
+ it("drops nulls and duplicates", () => {
28
+ // "No value" is expressed by clearing the control, not by picking an
29
+ // empty option.
30
+ const rows = JSON.stringify([
31
+ { region: "us-east" },
32
+ { region: null },
33
+ { region: "us-east" },
34
+ ]);
35
+ expect(readOptionValues(rows, "region")).toEqual(["us-east"]);
36
+ });
37
+
38
+ it("renders a non-string value as text", () => {
39
+ const rows = JSON.stringify([{ year: 2024 }, { year: 2025 }]);
40
+ expect(readOptionValues(rows, "year")).toEqual(["2024", "2025"]);
41
+ });
42
+
43
+ it("is empty for the shapes that legitimately mean no values", () => {
44
+ // `result` is optional in the API contract, so an absent one is a
45
+ // response the server is allowed to send, and an empty list plainly
46
+ // means no values.
47
+ expect(readOptionValues(undefined, undefined)).toEqual([]);
48
+ expect(readOptionValues("[]", undefined)).toEqual([]);
49
+ });
50
+
51
+ it("throws on a response it cannot read, rather than reporting no values", () => {
52
+ // This used to return `[]` for both, which collapsed "the query failed"
53
+ // into "this dimension is empty": the control said "No matching values"
54
+ // about a populated dimension. Throwing is what reaches `failed` and
55
+ // puts "Options unavailable" on the control instead.
56
+ expect(() => readOptionValues("not json", undefined)).toThrow();
57
+ expect(() => readOptionValues('{"not":"an array"}', undefined)).toThrow();
58
+ });
59
+ });
60
+
61
+ describe("readOptionValues takes the OUTPUT column name", () => {
62
+ it("finds a joined dimension's column, which is its last segment", () => {
63
+ // `group_by: products.category` names its column `category`. Looking up
64
+ // the full path finds nothing and falls back to the first column, which is
65
+ // right only by luck when the query selects exactly one.
66
+ const rows = JSON.stringify([
67
+ { state: "CA", category: "Outerwear" },
68
+ { state: "NY", category: "Jeans" },
69
+ ]);
70
+ expect(readOptionValues(rows, "category")).toEqual([
71
+ "Outerwear",
72
+ "Jeans",
73
+ ]);
74
+ // The path spelling misses, and takes the wrong column.
75
+ expect(readOptionValues(rows, "products.category")).toEqual(["CA", "NY"]);
76
+ });
77
+ });
78
+
79
+ describe("the generated suggest query", () => {
80
+ // Asserts the text the hook actually sends, by calling the exported builder
81
+ // rather than a copy declared here. Rebuilding the string alongside the
82
+ // implementation would prove only that the test agrees with itself, which is
83
+ // no use for text that reaches a warehouse.
84
+ it("orders by the output field name, not the field path", () => {
85
+ // `order_by: products.category` is refused by the compiler ("order_by
86
+ // takes the name of a field in the query output, not a path"), which would
87
+ // have failed the option query for every joined dimension. Verified against
88
+ // a real DuckDB-backed model: the path form errors, this form returns the
89
+ // 10 categories.
90
+ expect(buildSuggestQuery("order_items", "products.category")).toBe(
91
+ "run: order_items -> {\n" +
92
+ " group_by: products.category\n" +
93
+ " order_by: category asc\n" +
94
+ ` limit: ${SUGGEST_OPTION_LIMIT}\n` +
95
+ "}",
96
+ );
97
+ });
98
+
99
+ it("is unchanged for a dimension that is already a bare name", () => {
100
+ expect(buildSuggestQuery("products", "category")).toContain(
101
+ "order_by: category asc",
102
+ );
103
+ });
104
+
105
+ it("always carries an explicit limit", () => {
106
+ // Otherwise the server's default row cap truncates in whatever order the
107
+ // warehouse returned, silently and differently each time.
108
+ expect(buildSuggestQuery("products", "category")).toContain(
109
+ `limit: ${SUGGEST_OPTION_LIMIT}`,
110
+ );
111
+ });
112
+ });