@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,375 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ /**
5
+ * Resolving a renderer click into a `# drill` intent.
6
+ *
7
+ * `# drill { to=[…] given=… }` is written on a source *dimension*, and Malloy
8
+ * carries a dimension's annotations through to the query result's output field.
9
+ * So everything a click needs is already on the clicked field, and drill needs
10
+ * no endpoint of its own: the same resolution works for a dashboard tile and a
11
+ * notebook cell, which is the "one drill implementation" the design asks for.
12
+ *
13
+ * That propagation is the premise the whole design rests on, and NOTHING IN THIS
14
+ * PR pins it: the specs here drive a hand-built tag stub, deliberately, because
15
+ * what they test is the grammar. It is pinned against the real compiler by
16
+ * `packages/server/src/service/drill_probe.spec.ts`, which arrives with the
17
+ * dashboard slice of this split rather than here. If a Malloy upgrade stopped
18
+ * propagating dimension annotations, drill would go quiet everywhere at once and
19
+ * this slice's tests would all still pass.
20
+ *
21
+ * This module answers only *what* was asked for, which dashboards, seeding
22
+ * which given to which value. Navigating is the host's job.
23
+ */
24
+
25
+ import {
26
+ encodeFilterList,
27
+ filterInnerType,
28
+ isFilterType,
29
+ } from "../given/filterValue";
30
+
31
+ /**
32
+ * The parsed-tag surface this module reads, structurally typed.
33
+ *
34
+ * The renderer hands over an already-parsed `Tag` from `@malloydata/malloy-tag`,
35
+ * so naming the shape rather than importing the class keeps that package a
36
+ * render-time detail: SDK consumers can bundle without it, the same allowance
37
+ * `RenderedResult` makes for its theme annotations.
38
+ */
39
+ export interface DrillTagReader {
40
+ tag(...at: string[]): DrillTagReader | undefined;
41
+ text(...at: string[]): string | undefined;
42
+ textArray(...at: string[]): string[] | undefined;
43
+ }
44
+
45
+ /** The clicked field, as much of it as drill reads. */
46
+ export interface DrillField {
47
+ name: string;
48
+ tag?: DrillTagReader;
49
+ /**
50
+ * Whether the field was grouped rather than aggregated. Only dimensions
51
+ * drill: a measure's cell has no dimension value to seed a filter with even
52
+ * when the tag reaches it, so filtering by one would filter by something
53
+ * other than what was clicked. Optional because the renderer's click payload
54
+ * and its field metadata are the two callers and only one is guaranteed to
55
+ * carry the method; absent, the field is taken at its word.
56
+ */
57
+ wasDimension?: () => boolean;
58
+ }
59
+
60
+ /**
61
+ * A click from `@malloydata/render`'s `onClick`, narrowed to what drill needs.
62
+ * Mirrors its `MalloyClickEventPayload` without importing it, since the renderer
63
+ * is a lazily-imported dependency here.
64
+ */
65
+ export interface DrillClickPayload {
66
+ field?: DrillField;
67
+ value?: unknown;
68
+ isHeader?: boolean;
69
+ /**
70
+ * The originating click, which the host needs for two things a resolved
71
+ * intent cannot express, where to anchor a menu of destinations, and whether
72
+ * a modifier was held (cmd/ctrl to open in a new tab).
73
+ */
74
+ event?: MouseEvent;
75
+ }
76
+
77
+ /** The literal `to=self`: filter the current view instead of leaving it. */
78
+ export const DRILL_SELF = "self";
79
+
80
+ /**
81
+ * The `to=` destinations a field's `# drill` declares, empty when it declares
82
+ * none or the field cannot drill.
83
+ *
84
+ * Read separately from {@link resolveDrill} because two callers need it: the
85
+ * click, which also needs the clicked value, and the *affordance*, which runs
86
+ * over the result's field metadata before any click to decide which columns read
87
+ * as clickable. Sharing this is what keeps the two honest: a cell that looks
88
+ * clickable is one whose click resolves, because both answers come from here.
89
+ */
90
+ export function drillDestinations(field: DrillField | undefined): string[] {
91
+ if (!field) return [];
92
+ // `wasDimension` is only consulted when the field answers; see DrillField.
93
+ if (field.wasDimension && !field.wasDimension()) return [];
94
+ const drill = field.tag?.tag("drill");
95
+ if (!drill) return [];
96
+ // Malloyyo accepts a lone destination unbracketed (`to=overview`) as well as
97
+ // the list form. The server grows the same leniency in `readDrillTag`
98
+ // (`service/dashboard.ts`) with the dashboard slice of this split; it is not
99
+ // in the tree yet, so keep the two in step when it lands.
100
+ return (
101
+ drill.textArray("to") ?? [drill.text("to")].filter(isNonEmpty)
102
+ ).filter(isNonEmpty);
103
+ }
104
+
105
+ /**
106
+ * A destination slug as a menu label: `category_detail` → `Category detail`.
107
+ *
108
+ * Ported from Malloyyo so the same drill reads the same way in both, rather than
109
+ * naming the file on one and a sentence on the other.
110
+ */
111
+ export function humanizeSlug(slug: string): string {
112
+ const words = slug.replace(/[-_]+/g, " ").trim();
113
+ return words.charAt(0).toUpperCase() + words.slice(1);
114
+ }
115
+
116
+ /**
117
+ * The given a `# drill` on this field would set: `given=` when the tag names
118
+ * one, otherwise the dimension name exactly as the model spells it.
119
+ *
120
+ * Shared with the affordance for the same reason `drillDestinations` is: a cell
121
+ * reads as clickable exactly when clicking it would do something, and deciding
122
+ * "would it" needs the given's name before any click happens.
123
+ */
124
+ export function drillGivenName(field: DrillField): string {
125
+ // The field name verbatim, NOT upper-cased. Given names are stored as the
126
+ // model spells them, so upper-casing meant a `to=self` on a lowercase-named
127
+ // dimension resolved to a given that does not exist and the click was
128
+ // dropped in silence. An upper-case convention is common enough that it hid
129
+ // this: `given: REGION` worked, `given: region` did not.
130
+ //
131
+ // How the name is then MATCHED is the caller's business, and Notebook folds
132
+ // case before looking it up, so a tag naming `region` still finds a declared
133
+ // `REGION`. That is a courtesy at the lookup, not a licence to re-spell the
134
+ // name here: the value has to be set under the name the model declares.
135
+ return field.tag?.tag("drill")?.text("given") ?? field.name;
136
+ }
137
+
138
+ /** Where a `# drill` click wants to go, for the host to route however it routes. */
139
+ export interface DrillNavigation {
140
+ /** The destination dashboard's slug. */
141
+ dashboard: string;
142
+ /**
143
+ * Control values to open it with: the clicked value seeded into the drill's
144
+ * given. Only the seeded given: the source view's other filters are not
145
+ * carried over, since the destination need not bind them.
146
+ */
147
+ givens: Record<string, string>;
148
+ }
149
+
150
+ /** What a click asked for, once the tag on the clicked field is read. */
151
+ export interface DrillIntent {
152
+ /** Destinations in declared order: dashboard slugs and/or {@link DRILL_SELF}. */
153
+ to: string[];
154
+ /** The given to seed: the dimension name as spelled, unless `given=` says otherwise. */
155
+ given: string;
156
+ /**
157
+ * The clicked value as Malloy filter syntax, ready to send to a `filter<…>`
158
+ * given. This is the right encoding for a cross-dashboard drill, whose
159
+ * destination declares its own givens and is conventionally `filter<…>`.
160
+ */
161
+ value: string;
162
+ /**
163
+ * The same value before any encoding.
164
+ *
165
+ * Carried because the filter encoding above is only correct for a
166
+ * `filter<…>` target, and a click cannot know what it is aiming at: the
167
+ * destination may be another dashboard entirely. A host that *does* know,
168
+ * `to=self`, where the target given is one of its own: re-encodes from this
169
+ * with {@link encodeDrillValue}. Without it, drilling into a plain `string`
170
+ * given seeded it with backslash-escaped filter syntax and matched nothing.
171
+ */
172
+ rawValue: unknown;
173
+ /** The clicked value as it read on screen, for labelling the destination menu. */
174
+ label: string;
175
+ }
176
+
177
+ /**
178
+ * The clicked cell's value as filter syntax, or undefined when it cannot be
179
+ * expressed as one.
180
+ *
181
+ * Encoded from the value's own type rather than the given's, which a click does
182
+ * not know (a notebook cell has no control row to consult). Refusing the cases
183
+ * it cannot encode is deliberate: a drill that filtered by something other than
184
+ * the cell the user clicked would be worse than one that does nothing.
185
+ */
186
+ export function drillValueToFilter(value: unknown): string | undefined {
187
+ if (typeof value === "string") {
188
+ // Refused when it ENCODES to nothing, not when it IS the empty string.
189
+ // `encodeFilterList` drops any value with no non-whitespace character,
190
+ // and the empty filter matches every row, so a drill producing one would
191
+ // silently widen instead of narrowing. Testing the input against `""`
192
+ // duplicated the encoder's rule and then fell out of step with it the
193
+ // moment that rule grew to cover whitespace: clicking a blank-looking
194
+ // cell (a space, a tab, an NBSP, all ordinary in pasted data) handed back
195
+ // the whole dataset dressed up as a drilled view. Asking the encoder is
196
+ // the one form of this that cannot drift again.
197
+ const encoded = encodeFilterList([value]);
198
+ return encoded === "" ? undefined : encoded;
199
+ }
200
+ if (typeof value === "boolean") return String(value);
201
+ if (typeof value === "number") {
202
+ // A bare number reads as equality. NaN/Infinity have no filter spelling.
203
+ return Number.isFinite(value) ? String(value) : undefined;
204
+ }
205
+ if (value instanceof Date) {
206
+ // Malloy's date filter literal. Deliberately day-granularity: seeding a
207
+ // timestamp to the millisecond would filter to a single row nobody asked
208
+ // for, and for a `date` column the clicked cell was a truncated date in
209
+ // the first place.
210
+ //
211
+ // KNOWN LIMITATION for a TIMESTAMP column feeding a `filter<…>` given.
212
+ // This truncates on the UTC day, and the renderer prints a timestamp in
213
+ // the query timezone, so the two disagree whenever that zone crosses
214
+ // midnight away from UTC: an instant of `2024-03-06T04:00:00Z` shows as
215
+ // `2024-03-05 20:00` under US-Pacific and seeds `2024-03-06`, the day
216
+ // AFTER the one on screen. Plain `date`/`timestamp` givens route around
217
+ // this entirely (`encodeDrillValue` hands them the Date and lets
218
+ // `givensToRequest` spell it per type); only the filter-typed targets
219
+ // land here, because a filter's value must be text.
220
+ //
221
+ // Not fixed rather than fixed wrongly: the correct day is the one the
222
+ // CELL displayed, which needs the query timezone, and that is not on the
223
+ // click payload or anywhere else this function can see. Guessing the
224
+ // browser's zone would be a second wrong answer that happens to be right
225
+ // more often, which is harder to diagnose than a consistent one.
226
+ const iso = value.toISOString();
227
+ return iso.slice(0, iso.indexOf("T"));
228
+ }
229
+ // null included: "the rows with no value" is expressible in Malloy, but
230
+ // clicking an empty cell is far more likely a misclick than that request.
231
+ return undefined;
232
+ }
233
+
234
+ /** How the clicked value read on screen, for a menu label. */
235
+ function drillValueLabel(value: unknown): string {
236
+ if (value instanceof Date) return drillValueToFilter(value) ?? "";
237
+ return String(value);
238
+ }
239
+
240
+ /**
241
+ * Read the `# drill` intent off a renderer click, or undefined when the click
242
+ * was not on a drillable value.
243
+ *
244
+ * Silent on anything untagged, which is most clicks: this runs on every click
245
+ * the renderer reports, so "not a drill" is the ordinary answer, not a fault.
246
+ */
247
+ export function resolveDrill(
248
+ payload: DrillClickPayload | undefined,
249
+ ): DrillIntent | undefined {
250
+ // A column header names the field rather than carrying a value, so there is
251
+ // nothing to seed even though the tag is on the field the header belongs to.
252
+ if (!payload || payload.isHeader) return undefined;
253
+
254
+ const field = payload.field;
255
+ if (!field) return undefined;
256
+
257
+ const to = drillDestinations(field);
258
+ if (to.length === 0) return undefined;
259
+
260
+ const value = drillValueToFilter(payload.value);
261
+ if (value === undefined) return undefined;
262
+
263
+ return {
264
+ to,
265
+ given: drillGivenName(field),
266
+ value,
267
+ rawValue: payload.value,
268
+ label: drillValueLabel(payload.value),
269
+ };
270
+ }
271
+
272
+ function isNonEmpty(value: string | undefined): value is string {
273
+ return typeof value === "string" && value !== "";
274
+ }
275
+
276
+ /**
277
+ * A clicked value encoded for a given whose declared type is known.
278
+ *
279
+ * `filter<…>` takes filter syntax, so the value is escaped into a one-value
280
+ * filter list. Every other type takes the value itself: a plain `string` given
281
+ * seeded with `Ben\ &\ Jerry` would match nothing, because the escaping is
282
+ * meaningful only to the filter parser.
283
+ *
284
+ * Undefined when the value has no spelling for that type, which the caller
285
+ * should treat the way it treats an unresolvable drill: do nothing.
286
+ */
287
+ export function encodeDrillValue(
288
+ rawValue: unknown,
289
+ targetType: string | undefined,
290
+ ): string | number | boolean | Date | undefined {
291
+ if (targetType !== undefined && !isFilterType(targetType)) {
292
+ // Returned in the given's OWN type, not stringified. `givensToRequest`
293
+ // converts only `Date`, so a string sent for a `number` or `boolean` given
294
+ // travels as a string and Malloy refuses the query.
295
+ if (targetType === "number") {
296
+ // Only a number, or a string that spells one. `Number()` accepts far
297
+ // more than that and every extra it accepts is a wrong filter rather
298
+ // than a refused one: a clicked Date became its epoch milliseconds
299
+ // (1709646300000 for 2024-03-05) and a null cell became 0, so the
300
+ // notebook re-ran filtered to a value nobody clicked and returned
301
+ // nothing, with no error to explain it.
302
+ if (typeof rawValue === "number") {
303
+ return Number.isFinite(rawValue) ? rawValue : undefined;
304
+ }
305
+ if (typeof rawValue !== "string" || rawValue.trim() === "") {
306
+ return undefined;
307
+ }
308
+ const parsed = Number(rawValue);
309
+ return Number.isFinite(parsed) ? parsed : undefined;
310
+ }
311
+ if (
312
+ targetType === "date" ||
313
+ targetType === "timestamp" ||
314
+ targetType === "timestamptz"
315
+ ) {
316
+ // The Date itself, so `givensToRequest` can spell it per type. Falling
317
+ // through to `drillValueToFilter` truncated it to `YYYY-MM-DD` first,
318
+ // which is the right spelling for a `date` given and drops the time of
319
+ // day for a `timestamp` one: the drill then filtered to midnight
320
+ // rather than to the instant in the cell.
321
+ return rawValue instanceof Date ? rawValue : undefined;
322
+ }
323
+ if (targetType === "boolean") {
324
+ if (typeof rawValue === "boolean") return rawValue;
325
+ if (rawValue === "true") return true;
326
+ if (rawValue === "false") return false;
327
+ return undefined;
328
+ }
329
+ // A blank cell is refused here too, for the same reason as in
330
+ // `drillValueToFilter`: on a plain given it is a real but almost certainly
331
+ // unintended value, and clicking a blank cell is far more likely a
332
+ // misclick. Whitespace counts as blank, which is what makes that reason
333
+ // and this test agree: refusing only `""` left a space, a tab or an NBSP
334
+ // drilling to a value that matches essentially nothing, so the reader got
335
+ // an empty notebook rather than the click being declined.
336
+ if (typeof rawValue === "string")
337
+ return rawValue.trim() === "" ? undefined : rawValue;
338
+ if (typeof rawValue === "number") {
339
+ return Number.isFinite(rawValue) ? String(rawValue) : undefined;
340
+ }
341
+ if (typeof rawValue === "boolean") return String(rawValue);
342
+ }
343
+ // A `filter<string>` target takes the STRING grammar, so a bare number is
344
+ // not safe there: `-5` parses as `{operator:"=", values:["5"], not:true}`,
345
+ // a NEGATION, so drilling a negative cell filtered to every row EXCEPT the
346
+ // one clicked, silently. `drillValueToFilter`'s number branch is right for a
347
+ // `filter<number>`, whose grammar reads `-5` as the value. The spec already
348
+ // knew the two grammars disagree here and pinned only the number one.
349
+ if (
350
+ filterInnerType(targetType) === "string" &&
351
+ typeof rawValue === "number"
352
+ ) {
353
+ return Number.isFinite(rawValue)
354
+ ? encodeFilterList([String(rawValue)])
355
+ : undefined;
356
+ }
357
+ // A `filter<number>` takes the NUMBER grammar, so a string belongs to it only
358
+ // if it spells a number. Falling through emitted STRING filter syntax for it:
359
+ // a click on a text cell seeded `filter<number>` with `West`, a boolean with
360
+ // `true`, and a Date with a `YYYY-MM-DD` day, each of which that grammar
361
+ // refuses, so the server failed EVERY cell and the reader got an error
362
+ // notebook rather than a declined click. The non-filter `number` branch above
363
+ // already refuses exactly these; this is the same rule for the filter arm,
364
+ // which is where the two copies had drifted.
365
+ if (filterInnerType(targetType) === "number") {
366
+ if (typeof rawValue === "number") {
367
+ return Number.isFinite(rawValue) ? String(rawValue) : undefined;
368
+ }
369
+ if (typeof rawValue !== "string" || rawValue.trim() === "") {
370
+ return undefined;
371
+ }
372
+ return Number.isFinite(Number(rawValue)) ? rawValue.trim() : undefined;
373
+ }
374
+ return drillValueToFilter(rawValue);
375
+ }
@@ -0,0 +1,203 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { ListSubheader, Menu, MenuItem } from "@mui/material";
5
+ import { useCallback, useMemo, useState, type ReactNode } from "react";
6
+ import type { DrillField } from "./resolveDrill";
7
+ import {
8
+ DRILL_SELF,
9
+ drillDestinations,
10
+ drillGivenName,
11
+ humanizeSlug,
12
+ resolveDrill,
13
+ type DrillClickPayload,
14
+ type DrillIntent,
15
+ type DrillNavigation,
16
+ } from "./resolveDrill";
17
+
18
+ export interface UseDrillOptions {
19
+ /**
20
+ * Open another dashboard, seeded with the clicked value. Omitted on a surface
21
+ * that cannot navigate, which makes those destinations inert rather than
22
+ * dead-ended.
23
+ */
24
+ onNavigate?: (target: DrillNavigation, event?: MouseEvent) => void;
25
+ /**
26
+ * Apply the clicked value to this surface's own control row (`to=self`).
27
+ * Omitted on a surface that has none, such as a notebook cell.
28
+ */
29
+ onSelf?: (given: string, rawValue: unknown) => void;
30
+ /**
31
+ * Whether a `to=self` drill onto this given can actually be honoured: for a
32
+ * notebook, whether it declares that given at all.
33
+ *
34
+ * Without it the affordance says yes to every self-drill and the click is
35
+ * refused afterwards with a console warning, which is the dead link this
36
+ * module exists to avoid. Absent means "any self-drill is fine", the right
37
+ * default for a surface that binds whatever it is handed.
38
+ */
39
+ canSelf?: (given: string) => boolean;
40
+ /**
41
+ * What `to=self` is called in the menu. A notebook filtering itself is not
42
+ * "this dashboard", and the drill tag: written on a shared model dimension,
43
+ * cannot know which document the reader is in, so the surface names itself.
44
+ */
45
+ selfLabel?: string;
46
+ }
47
+
48
+ /**
49
+ * What a surface hands to its result renderer to become drillable: the click
50
+ * handler, and the predicate that decides which cells say so.
51
+ *
52
+ * One object because the two must agree. Passing only the handler is what
53
+ * produced the original asymmetry: clicks worked, but nothing looked clickable.
54
+ */
55
+ export interface DrillBinding {
56
+ /**
57
+ * Every cell click. Most carry no `# drill` and mean nothing, so this
58
+ * tolerates being called constantly.
59
+ */
60
+ onClick: (payload: DrillClickPayload) => void;
61
+ /** Whether this field's cells should read as clickable. */
62
+ canDrill: (field: DrillField) => boolean;
63
+ }
64
+
65
+ export interface UseDrillResult {
66
+ /** Hand to `ResultContainer`'s (or `RenderedResult`'s) `drill` prop. */
67
+ drill: DrillBinding;
68
+ /** Render anywhere in the subtree: the menu a multi-destination drill opens. */
69
+ drillMenu: ReactNode;
70
+ }
71
+
72
+ interface DrillMenuState {
73
+ intent: DrillIntent;
74
+ destinations: string[];
75
+ event?: MouseEvent;
76
+ top: number;
77
+ left: number;
78
+ }
79
+
80
+ /**
81
+ * Click-to-drill for any surface that renders a Malloy result.
82
+ *
83
+ * Shared between the dashboard viewer and notebook cells on purpose: `# drill`
84
+ * is declared on a model *dimension*, so it fires wherever that dimension is
85
+ * grouped, and a notebook can therefore drill into a dashboard with no notebook-
86
+ * specific code. What differs between surfaces is only what they can do about it,
87
+ * which is what the two callbacks express.
88
+ */
89
+ export function useDrill({
90
+ onNavigate,
91
+ onSelf,
92
+ selfLabel = "Filter this view",
93
+ canSelf,
94
+ }: UseDrillOptions): UseDrillResult {
95
+ // A cell inside the renderer's own DOM is the anchor, and this component
96
+ // holds no ref to it, so the menu is positioned from the click coordinates.
97
+ const [menu, setMenu] = useState<DrillMenuState | null>(null);
98
+
99
+ // What this surface can actually do about a destination. Applied to the click
100
+ // and to the affordance from one place, so a cell reads as clickable exactly
101
+ // when clicking it would do something.
102
+ const honorable = useCallback(
103
+ (destinations: string[], given: string) =>
104
+ destinations.filter((destination) =>
105
+ destination === DRILL_SELF
106
+ ? onSelf !== undefined && (canSelf?.(given) ?? true)
107
+ : onNavigate !== undefined,
108
+ ),
109
+ [onNavigate, onSelf, canSelf],
110
+ );
111
+
112
+ const go = useCallback(
113
+ (intent: DrillIntent, destination: string, event?: MouseEvent) => {
114
+ if (destination === DRILL_SELF) {
115
+ // The raw value, not `intent.value`: only this surface knows the
116
+ // declared type of its own given, so only it can encode correctly.
117
+ onSelf?.(intent.given, intent.rawValue);
118
+ return;
119
+ }
120
+ onNavigate?.(
121
+ {
122
+ dashboard: destination,
123
+ givens: { [intent.given]: intent.value },
124
+ },
125
+ event,
126
+ );
127
+ },
128
+ [onNavigate, onSelf],
129
+ );
130
+
131
+ const onClick = useCallback(
132
+ (payload: DrillClickPayload) => {
133
+ // Runs on every cell click, so "not a drill" is the ordinary answer.
134
+ const intent = resolveDrill(payload);
135
+ if (!intent) return;
136
+
137
+ // Offer only what this surface can actually do, so a menu never lists a
138
+ // destination that would be a no-op when picked.
139
+ const destinations = honorable(intent.to, intent.given);
140
+ if (destinations.length === 0) return;
141
+ if (destinations.length === 1) {
142
+ go(intent, destinations[0], payload.event);
143
+ return;
144
+ }
145
+ setMenu({
146
+ intent,
147
+ destinations,
148
+ event: payload.event,
149
+ top: payload.event?.clientY ?? 0,
150
+ left: payload.event?.clientX ?? 0,
151
+ });
152
+ },
153
+ [go, honorable],
154
+ );
155
+
156
+ const canDrill = useCallback(
157
+ (field: DrillField) =>
158
+ honorable(drillDestinations(field), drillGivenName(field)).length > 0,
159
+ [honorable],
160
+ );
161
+
162
+ const drill = useMemo<DrillBinding>(
163
+ () => ({ onClick, canDrill }),
164
+ [onClick, canDrill],
165
+ );
166
+
167
+ const close = useCallback(() => setMenu(null), []);
168
+
169
+ return {
170
+ drill,
171
+ drillMenu: (
172
+ <Menu
173
+ open={menu !== null}
174
+ onClose={close}
175
+ anchorReference="anchorPosition"
176
+ anchorPosition={{ top: menu?.top ?? 0, left: menu?.left ?? 0 }}
177
+ >
178
+ <ListSubheader sx={{ lineHeight: "32px" }}>
179
+ {menu ? `${menu.intent.label} →` : ""}
180
+ </ListSubheader>
181
+ {/* Deduplicated: `# drill { to=[overview, overview] }` is a typo
182
+ rather than a request for two identical rows, and React keys
183
+ these by destination, so the duplicate warned as well as
184
+ rendered twice. */}
185
+ {Array.from(new Set(menu?.destinations ?? [])).map(
186
+ (destination) => (
187
+ <MenuItem
188
+ key={destination}
189
+ onClick={() => {
190
+ close();
191
+ if (menu) go(menu.intent, destination, menu.event);
192
+ }}
193
+ >
194
+ {destination === DRILL_SELF
195
+ ? selfLabel
196
+ : humanizeSlug(destination)}
197
+ </MenuItem>
198
+ ),
199
+ )}
200
+ </Menu>
201
+ ),
202
+ };
203
+ }
@@ -1,3 +1,6 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
1
4
  import Autocomplete from "@mui/material/Autocomplete";
2
5
  import Box from "@mui/material/Box";
3
6
  import CircularProgress from "@mui/material/CircularProgress";
@@ -1,3 +1,6 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
1
4
  export { DimensionFilter } from "./DimensionFilter";
2
5
  export type {
3
6
  DimensionFilterProps,
@@ -1,9 +1,13 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
1
4
  import * as Malloy from "@malloydata/malloy-interfaces";
2
5
  import {
3
6
  DimensionSpec,
4
7
  FilterType,
5
8
  } from "../../hooks/useDimensionalFilterRangeData";
6
9
  import { FilterSelection } from "../../hooks/useDimensionFilters";
10
+ import { escapeMalloyString } from "../../utils/malloyString";
7
11
 
8
12
  /**
9
13
  * A single filter specification with source and dimension
@@ -136,9 +140,16 @@ export function parseAllSourceInfos(
136
140
  if (cell.type === "code") {
137
141
  // Extract model path from the first import statement found
138
142
  if (!modelPath && cell.text) {
139
- const importMatch = cell.text.match(
140
- /import\s+(?:\{[^}]*\}\s+from\s+)?['"]([^'"]+)['"]/,
141
- );
143
+ // The first quoted string on the first `import` line is the
144
+ // module path, for both `import 'x'` and `import {a} from 'x'`.
145
+ // A line scan rather than one regex over the whole cell: the
146
+ // optional `{...} from` group made that regex backtrack
147
+ // polynomially on adversarial input.
148
+ const importLine = cell.text
149
+ .split("\n")
150
+ .map((line) => line.trim())
151
+ .find((line) => line.startsWith("import "));
152
+ const importMatch = importLine?.match(/['"]([^'"\n]+)['"]/);
142
153
  if (importMatch) {
143
154
  modelPath = importMatch[1];
144
155
  }
@@ -295,7 +306,7 @@ export function generateFilterClause(
295
306
  return `@${v.getFullYear()}-${String(v.getMonth() + 1).padStart(2, "0")}-${String(v.getDate()).padStart(2, "0")}`;
296
307
  }
297
308
  if (typeof v === "string") {
298
- return `'${v.replace(/'/g, "\\'")}'`;
309
+ return `'${escapeMalloyString(v)}'`;
299
310
  }
300
311
  if (typeof v === "boolean") return v ? "true" : "false";
301
312
  return String(v);
@@ -314,7 +325,7 @@ export function generateFilterClause(
314
325
 
315
326
  case "Contains":
316
327
  if (typeof value === "string") {
317
- return `${fieldName} ~ f'%${value.replace(/'/g, "\\'")}%'`;
328
+ return `${fieldName} ~ f'%${escapeMalloyString(value)}%'`;
318
329
  }
319
330
  return `${fieldName} = ${formatValue(value)}`;
320
331