@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,801 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import ClearIcon from "@mui/icons-material/Clear";
5
+ import {
6
+ Autocomplete,
7
+ Box,
8
+ Checkbox,
9
+ CircularProgress,
10
+ FormControl,
11
+ FormControlLabel,
12
+ FormHelperText,
13
+ IconButton,
14
+ InputAdornment,
15
+ Slider,
16
+ Stack,
17
+ TextField,
18
+ Typography,
19
+ } from "@mui/material";
20
+ import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";
21
+ import { DatePicker } from "@mui/x-date-pickers/DatePicker";
22
+ import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
23
+ import dayjs from "dayjs";
24
+ import utc from "dayjs/plugin/utc";
25
+ import { Given } from "../../client";
26
+ import { paramToGiven, pickedDayToUtc } from "./paramCodec";
27
+ import { GivenValue } from "../../hooks/givenValue";
28
+ import {
29
+ decodeAtLeast,
30
+ decodeFilterList,
31
+ encodeAtLeast,
32
+ encodeFilterList,
33
+ filterInnerType,
34
+ isFilterType,
35
+ isPlainFilterList,
36
+ } from "./filterValue";
37
+ import { renderGivenDefault } from "./utils";
38
+
39
+ dayjs.extend(utc);
40
+
41
+ /**
42
+ * Metrics of MUI's small outlined input, which the box-less controls match so a
43
+ * row of mixed widgets lines up: the horizontal inset of an outlined field's
44
+ * content and helper text, and the height of the field itself.
45
+ */
46
+ const INPUT_CONTENT_INSET = 14;
47
+ const SLIDER_FIELD_HEIGHT = 42;
48
+
49
+ export interface GivenInputProps {
50
+ /**
51
+ * The declaration, which carries its own presentation (`label`, `control`,
52
+ * `rangeMin`, `rangeMax`) from the control tags on it
53
+ * (`# label="Brand" control=select range_min=0`).
54
+ *
55
+ * Presentation never changes what the given means, just which widget stands
56
+ * in for typing the value by hand. An untagged given falls back to the
57
+ * widget its type implies, which is what notebooks have always rendered.
58
+ */
59
+ given: Given;
60
+ value: GivenValue | undefined;
61
+ onChange: (next: GivenValue) => void;
62
+ /**
63
+ * Options for a `select`/`multiselect`, already resolved from the given's
64
+ * `suggest` query. Resolved by the caller rather than fetched here so this
65
+ * stays a presentational component: the suggest query is an ordinary query
66
+ * against the host's model, which the widget has no business knowing about.
67
+ */
68
+ options?: string[];
69
+ optionsLoading?: boolean;
70
+ /**
71
+ * The `suggest` query for this given failed. Distinguished from an empty
72
+ * `options` because a dropdown looks identical either way, and the two mean
73
+ * opposite things: no values in the data, versus no answer from the server.
74
+ * Free text still works, so the control degrades rather than locking up.
75
+ */
76
+ optionsFailed?: boolean;
77
+ }
78
+
79
+ /**
80
+ * Distill a given's `#(...)` annotation list into helper text for the UI.
81
+ * If an annotation includes `description="..."` (a Malloy convention), the
82
+ * quoted value is surfaced verbatim. Otherwise the annotation contents
83
+ * inside `#(...)` are joined as-is so model authors still see something
84
+ * recognizable. Returns undefined when nothing is renderable.
85
+ */
86
+ function annotationHelperText(given: Given): string | undefined {
87
+ const visible = (given.annotations ?? []).filter((a) =>
88
+ a.trim().startsWith("#("),
89
+ );
90
+ if (visible.length === 0) return undefined;
91
+
92
+ const rendered: string[] = [];
93
+ for (const raw of visible) {
94
+ const trimmed = raw.trim();
95
+ const descriptionMatch = trimmed.match(/description="([^"]*)"/);
96
+ if (descriptionMatch) {
97
+ rendered.push(descriptionMatch[1]);
98
+ continue;
99
+ }
100
+ // Strip leading `#(` and trailing `)`, then push the inner content
101
+ const inner = trimmed
102
+ .replace(/^#\(/, "")
103
+ .replace(/\)\s*$/, "")
104
+ .trim();
105
+ if (inner) rendered.push(inner);
106
+ }
107
+ return rendered.length > 0 ? rendered.join("\n") : undefined;
108
+ }
109
+
110
+ /**
111
+ * Renders an input widget appropriate for the declared given type.
112
+ * Unknown / unrecognized types fall back to a plain text input.
113
+ *
114
+ * Three states, distinguished so a deliberate empty/false override is not
115
+ * confused with "use the model default":
116
+ * - unset (`value === undefined`) → the given is omitted from the request and
117
+ * the server applies the model default. Text widgets show the default as a
118
+ * ghost placeholder; the boolean checkbox reflects the default's value.
119
+ * - explicit override (any concrete value, INCLUDING `""` and `false`) → sent
120
+ * verbatim. A clear (×) affordance appears whenever a value is overridden —
121
+ * including an empty string — so typing the field empty (a deliberate `""`)
122
+ * is distinguishable from unset by the × being present.
123
+ * - revert → the × affordance calls `onChange(null)`, which drops the override
124
+ * (useGivensState deletes the key) and returns the widget to its unset state.
125
+ *
126
+ * A given's model default (if any) is also surfaced as an always-visible
127
+ * `Default: …` helper line on every widget — including the boolean checkbox,
128
+ * which gets a wrapping FormControl for the slot.
129
+ */
130
+ export function GivenInput({
131
+ given,
132
+ value,
133
+ onChange,
134
+ options,
135
+ optionsLoading,
136
+ optionsFailed,
137
+ }: GivenInputProps) {
138
+ const label = given.label ?? given.name ?? "";
139
+ const type = given.type ?? "string";
140
+ const helperText = annotationHelperText(given);
141
+ const defaultDisplay = renderGivenDefault(type, given.default);
142
+ // Always-visible default caption. Test `=== undefined`, not truthiness: an
143
+ // explicit empty-string default (`is ''`) renders as "" and must still show
144
+ // (as `(empty)`), not be mistaken for "no default".
145
+ const defaultLine =
146
+ defaultDisplay !== undefined
147
+ ? `Default: ${defaultDisplay === "" ? "(empty)" : defaultDisplay}`
148
+ : undefined;
149
+ // Render annotation and default on separate lines via an explicit <br/>
150
+ // rather than a \n + `white-space: pre-line`: the latter doesn't reach the
151
+ // TextField nested inside MUI's DatePicker, so the date helper ran together.
152
+ // A ReactNode helperText works uniformly across every widget.
153
+ const helperNode =
154
+ helperText || defaultLine ? (
155
+ <>
156
+ {helperText}
157
+ {helperText && defaultLine ? <br /> : null}
158
+ {defaultLine}
159
+ </>
160
+ ) : undefined;
161
+
162
+ // A picker, when the declaration asked for one. Placed ahead of the
163
+ // type branches because `control=` is an explicit instruction and the type
164
+ // is only an inference.
165
+ //
166
+ // But this picker only speaks two languages: plain strings, and the STRING
167
+ // filter grammar. It commits through `encodeFilterList`, which is
168
+ // `StringFilterExpression.unparse`. So `control=select` on anything else is
169
+ // refused rather than honoured, because honouring it corrupts the value:
170
+ // a `filter<date>` or `filter<number>` would receive string-grammar escaping
171
+ // that the temporal and number parsers reject outright, and a `number`,
172
+ // `date` or `boolean` given would receive a string that `givensToRequest`
173
+ // forwards verbatim for Malloy to refuse. Falling through gives the given the
174
+ // widget its type implies, which is the behaviour before `control=` existed.
175
+ // `control=` IS populated now, by `readGivenControlSpec` on the server, so
176
+ // this guard is live rather than pre-emptive.
177
+ const pickableType = type === "string" || filterInnerType(type) === "string";
178
+ const filterIsPickable =
179
+ !isFilterType(type) ||
180
+ typeof value !== "string" ||
181
+ isPlainFilterList(value);
182
+ // And multi-pick only for a `filter<…>`, which is the one type that can carry
183
+ // several values in one string. A plain `string` given cannot hold a list at
184
+ // all: `GivenValue` admits no array, and `givenToParam` refuses one rather
185
+ // than joining it, because the codec cannot read a join back. So a
186
+ // `multiselect` on a plain string given renders as a single-pick instead of
187
+ // emitting something that cannot survive the address bar.
188
+ /**
189
+ * A value that is in force but that this control cannot render.
190
+ *
191
+ * Every typed branch has the same hazard: the control substitutes a blank
192
+ * for a value of the wrong shape, so the value goes on filtering every cell
193
+ * while the box looks empty. Shown here rather than per branch, because
194
+ * handling it three different ways is how the date branch ended up with a
195
+ * revert and the number branch without one.
196
+ *
197
+ * Read-only on purpose: an editable version swapped itself for the real
198
+ * control the moment the typed text first parsed, taking the cursor with it.
199
+ */
200
+ const unrepresentable = (what: string) => (
201
+ <TextField
202
+ label={label}
203
+ value={String(value)}
204
+ error
205
+ inputProps={{ readOnly: true }}
206
+ helperText={
207
+ <>
208
+ {`Not a ${what}. Clear it to pick one.`}
209
+ {helperNode ? <br /> : null}
210
+ {helperNode}
211
+ </>
212
+ }
213
+ slotProps={{
214
+ input: {
215
+ endAdornment: <ClearAdornment onClear={() => onChange(null)} />,
216
+ },
217
+ }}
218
+ fullWidth
219
+ size="small"
220
+ />
221
+ );
222
+
223
+ const multiplePickable = isFilterType(type);
224
+ const multiple = given.control === "multiselect" && multiplePickable;
225
+ const filtered = isFilterType(type);
226
+ const pickerControl =
227
+ given.control === "select" || given.control === "multiselect";
228
+ // A filter carries its selection as one string ("Nike, Levi's"); a plain
229
+ // array-typed given carries a real array.
230
+ //
231
+ // Guarded by `pickerControl` because it is read only by the picker below and
232
+ // by the condition guarding it. Computing it for every given would decode a
233
+ // filter on every render of a control that never shows one.
234
+ const selected: string[] = !pickerControl
235
+ ? []
236
+ : filtered
237
+ ? typeof value === "string"
238
+ ? decodeFilterList(value)
239
+ : []
240
+ : typeof value === "string" && value !== ""
241
+ ? [value]
242
+ : [];
243
+
244
+ if (
245
+ pickerControl &&
246
+ pickableType &&
247
+ // A picker that cannot represent the current filter would silently
248
+ // rewrite it on the next edit: `-Nike` decodes to one opaque chip and
249
+ // re-encodes as a literal, inverting what the filter means. Fall through
250
+ // to the text box instead, where the author's filter stays visible and
251
+ // editable as written.
252
+ filterIsPickable &&
253
+ // Same reasoning, different cause: a single-pick control has room for one
254
+ // value and the model's filter holds several. It rendered the first and
255
+ // dropped the rest as soon as the reader picked anything.
256
+ !(!multiple && selected.length > 1)
257
+ ) {
258
+ const commit = (next: string[]) => {
259
+ // ASK the encoder what survived; do not predict it from `next.length`.
260
+ // `encodeFilterList` drops any value with no non-whitespace character,
261
+ // so a single blank-looking pick (a run of spaces, a tab, an NBSP, all
262
+ // ordinary in scraped data and passed through untouched by
263
+ // `readOptionValues`) has length 1, skipped the revert arm below, and
264
+ // encoded to `""`: the EMPTY filter, sent as an explicit "match
265
+ // everything" override while the control redrew showing the model
266
+ // default as a ghost. This is the identical drift that made a blank
267
+ // cell drill to every row, at a second call site: `drillValueToFilter`
268
+ // was fixed to ask the encoder and this one still predicted it.
269
+ const encoded = filtered ? encodeFilterList(next) : undefined;
270
+ if (next.length === 0 || encoded === "") {
271
+ // `null` for every type, which is what the × means everywhere else
272
+ // in this file: drop the override and let the model's own value
273
+ // stand. This arm used to send `""` for a filter, the empty filter,
274
+ // meaning "match everything" as an explicit override. Same button,
275
+ // same given, opposite result set from the text box's ×, and
276
+ // nothing on screen said which one you were about to get.
277
+ //
278
+ // FORECLOSED by choosing revert: a reader cannot override a
279
+ // DECLARED default with "match everything", because the only
280
+ // gesture for it now means revert. The honest fix is a separate
281
+ // affordance rather than two meanings for one ×. This is now
282
+ // REACHABLE: `control=` is populated server-side and pickers render,
283
+ // so a given with a declared default and a picker hits it.
284
+ onChange(null);
285
+ return;
286
+ }
287
+ // One value for a non-filter given, never the array. `GivenValue`
288
+ // admits no array at all now, and the URL codec could not round-trip
289
+ // one anyway: `givenToParam` refuses it rather than joining on `,`,
290
+ // because reading it back yields the joined string, not the list.
291
+ onChange(filtered ? (encoded as string) : next[0]);
292
+ };
293
+
294
+ return (
295
+ <Autocomplete
296
+ multiple={multiple}
297
+ // Picking one value out of several should not put the list away;
298
+ // MUI closes on select by default, which makes "and also Aurora"
299
+ // cost a second click to reopen.
300
+ disableCloseOnSelect={multiple}
301
+ // Free text keeps the control from being a cage: the suggest query
302
+ // returns the common values, not necessarily every legal one.
303
+ freeSolo
304
+ // MUI hides the dropdown arrow whenever `freeSolo` is set, which
305
+ // left a control that HAS a list of values looking exactly like a
306
+ // plain text box: nothing on it said the list existed, so the only
307
+ // way to find the options was to guess that clicking would reveal
308
+ // them. Reported independently by a reader as "the controls render
309
+ // as text boxes rather than dropdowns"; the options were being
310
+ // fetched and offered correctly the whole time, and only the
311
+ // affordance was missing. Same principle as `markDrillableCells`:
312
+ // something you can act on has to look like it.
313
+ forcePopupIcon
314
+ // Keeps text the reader typed but did not pick from the list. A
315
+ // `freeSolo` Autocomplete without this discards it on blur, so a
316
+ // value that is legal but absent from `suggest` could be typed and
317
+ // then silently vanish.
318
+ autoSelect
319
+ options={options ?? []}
320
+ loading={optionsLoading}
321
+ noOptionsText={
322
+ optionsFailed ? "Options unavailable" : "No matching values"
323
+ }
324
+ value={multiple ? selected : (selected[0] ?? null)}
325
+ onChange={(_event, next) =>
326
+ commit(
327
+ next === null
328
+ ? []
329
+ : Array.isArray(next)
330
+ ? (next as string[])
331
+ : [next as string],
332
+ )
333
+ }
334
+ renderInput={(params) => (
335
+ <TextField
336
+ {...params}
337
+ label={label}
338
+ size="small"
339
+ // The model's own default when it has one, as a ghost, the
340
+ // way every other unset control shows it. A hardcoded "All"
341
+ // told the reader nothing would be filtered while the query
342
+ // was about to run on the declared default.
343
+ //
344
+ // Except when an explicit `""` is in force. That is the EMPTY
345
+ // filter, so the default is precisely what is NOT running,
346
+ // and showing it as a ghost made the control assert one
347
+ // filter while the query used another.
348
+ placeholder={
349
+ selected.length !== 0
350
+ ? undefined
351
+ : value === ""
352
+ ? "All"
353
+ : (defaultDisplay ?? "All")
354
+ }
355
+ error={optionsFailed}
356
+ helperText={
357
+ optionsFailed ? (
358
+ <>
359
+ Could not load the options for this control. Type a
360
+ value to filter anyway.
361
+ {helperNode ? <br /> : null}
362
+ {helperNode}
363
+ </>
364
+ ) : (
365
+ helperNode
366
+ )
367
+ }
368
+ slotProps={{
369
+ input: {
370
+ ...params.InputProps,
371
+ endAdornment: (
372
+ <>
373
+ {optionsLoading && <CircularProgress size={16} />}
374
+ {params.InputProps.endAdornment}
375
+ </>
376
+ ),
377
+ },
378
+ }}
379
+ />
380
+ )}
381
+ fullWidth
382
+ />
383
+ );
384
+ }
385
+
386
+ // A slider, when the declaration bounded the range. Only a lower bound is
387
+ // expressible as a filter here (see `encodeAtLeast`), so this reads as
388
+ // "at least", which is what a threshold control is for.
389
+ const numericFilter =
390
+ isFilterType(type) && filterInnerType(type) === "number";
391
+ const { rangeMin, rangeMax } = given;
392
+ // A slider can only represent `>= N`, so a `filter<number>` carrying anything
393
+ // else (a range, an upper bound, a negation) falls through to the text box.
394
+ // Otherwise `decodeAtLeast` returns undefined, the slider claims "Any" while a
395
+ // filter is in force, and the first drag silently replaces the author's filter
396
+ // with a threshold. Same principle as the picker and the date guards above.
397
+ const sliderIsRepresentable =
398
+ !numericFilter ||
399
+ value === undefined ||
400
+ value === null ||
401
+ value === "" ||
402
+ (typeof value === "string" && decodeAtLeast(value) !== undefined);
403
+ if (
404
+ (numericFilter || type === "number") &&
405
+ rangeMin !== undefined &&
406
+ rangeMax !== undefined &&
407
+ sliderIsRepresentable
408
+ ) {
409
+ const current = numericFilter
410
+ ? typeof value === "string"
411
+ ? decodeAtLeast(value)
412
+ : undefined
413
+ : typeof value === "number"
414
+ ? value
415
+ : undefined;
416
+ // Same hazard as the number and date branches: a value that cannot be
417
+ // placed on the track would otherwise read as "Any" while still being
418
+ // sent. Live rather than latent: `readGivenControlSpec` populates
419
+ // `rangeMin`/`rangeMax` from the declaration's own tags.
420
+ if (
421
+ current === undefined &&
422
+ value !== undefined &&
423
+ value !== null &&
424
+ value !== ""
425
+ ) {
426
+ return unrepresentable("number");
427
+ }
428
+ // An unset control rests at the low end, which is the no-op threshold.
429
+ const position = current ?? rangeMin;
430
+ // `""` on a filter given is an explicit "no threshold" OVERRIDE, not an
431
+ // absence: `paramToGiven` keeps it because the empty string is a value a
432
+ // filter can mean, and `givensToRequest` sends it. Counting it as unset
433
+ // drew "Any" with no clear affordance, so the reader could see neither
434
+ // that an override was in force nor any way back to the model's default.
435
+ // An explicit empty filter is an override with NO threshold, which is a
436
+ // third state: not unset, and not `≥ position` either. Printing
437
+ // `≥ rangeMin` for it asserted a lower bound nobody had chosen, just as
438
+ // counting it unset had hidden that an override was in force at all.
439
+ const emptyFilterOverride = numericFilter && value === "";
440
+ const isOverridden = current !== undefined || emptyFilterOverride;
441
+ // TWO LIMITATIONS here, both reachable as soon as a given carries
442
+ // `rangeMin`/`rangeMax`, and both about the plain `number` arm rather than
443
+ // the filter arm. Written when no server populated those fields; slice 6
444
+ // now does, so treat these as live rather than theoretical.
445
+ // An unset control rests its thumb at `rangeMin` while the
446
+ // readout shows the model default, so the two disagree whenever the
447
+ // default is not the floor; and MUI fires no onChange for a click at the
448
+ // thumb's existing position, so the floor cannot be selected from the
449
+ // resting state. Fixing either means seeding the thumb from the default,
450
+ // which needs a default that parses as a number and a decision about what
451
+ // to show when it does not.
452
+ return (
453
+ <FormControl fullWidth>
454
+ {/* A slider is the one control with no box around it, so it has to
455
+ borrow the outlined inputs' metrics to sit in a row with them:
456
+ their content is inset 14px and their field is 42px tall, and
457
+ a control that matches both puts its helper text on the same
458
+ line as its neighbours'. Left to itself the label started at
459
+ the column edge and the helper sat 10px lower than the rest,
460
+ which is most of what made a mixed control row look crooked. */}
461
+ <Box
462
+ sx={{
463
+ height: SLIDER_FIELD_HEIGHT,
464
+ px: `${INPUT_CONTENT_INSET}px`,
465
+ display: "flex",
466
+ flexDirection: "column",
467
+ justifyContent: "center",
468
+ }}
469
+ >
470
+ <Stack direction="row" alignItems="center" spacing={1}>
471
+ <Typography variant="body2" color="text.secondary" noWrap>
472
+ {label}
473
+ </Typography>
474
+ <Typography variant="body2" sx={{ fontWeight: 600 }}>
475
+ {/* `≥ N` only for a FILTER, whose value IS a lower
476
+ bound. A plain `number` given is an ordinary value, and
477
+ the onChange below says so by committing `picked`
478
+ rather than a threshold, so labelling it `≥` described
479
+ a query nobody was running. */}
480
+ {!isOverridden
481
+ ? numericFilter
482
+ ? "Any"
483
+ : (defaultDisplay ?? "Unset")
484
+ : emptyFilterOverride
485
+ ? "Any"
486
+ : numericFilter
487
+ ? `≥ ${position}`
488
+ : String(position)}
489
+ </Typography>
490
+ {isOverridden && (
491
+ <IconButton
492
+ size="small"
493
+ aria-label="clear value"
494
+ // `null`, like every other × in this file: revert the
495
+ // override. It used to send `""` for a filter, which
496
+ // `decodeAtLeast` reads back as undefined, so the
497
+ // control redrew as "Any" and hid this button while the
498
+ // given was still explicitly set to the empty filter.
499
+ // The reader could neither see the override nor undo it.
500
+ onClick={() => onChange(null)}
501
+ sx={{ p: 0 }}
502
+ >
503
+ <ClearIcon fontSize="small" />
504
+ </IconButton>
505
+ )}
506
+ </Stack>
507
+ <Slider
508
+ size="small"
509
+ min={rangeMin}
510
+ max={rangeMax}
511
+ value={position}
512
+ valueLabelDisplay="auto"
513
+ aria-label={label}
514
+ // MUI pads a slider vertically for a touch target, which is
515
+ // 26px this control cannot spare; the row above already
516
+ // gives the thumb somewhere to be.
517
+ sx={{ py: 0, mt: 0.5 }}
518
+ onChange={(_event, next) => {
519
+ const picked = Array.isArray(next) ? next[0] : next;
520
+ // Back at the floor is no threshold at all for a FILTER,
521
+ // not `>= min`, so dragging left the whole way clears
522
+ // rather than leaving a filter that reads as a constraint.
523
+ //
524
+ // A plain `number` given is not a threshold, so its floor
525
+ // is an ordinary value and clearing there made the minimum
526
+ // the one number on the scale the reader could not pick.
527
+ if (numericFilter && picked <= rangeMin) {
528
+ onChange(null);
529
+ return;
530
+ }
531
+ onChange(numericFilter ? encodeAtLeast(picked) : picked);
532
+ }}
533
+ />
534
+ </Box>
535
+ {helperNode && (
536
+ <FormHelperText sx={{ mx: `${INPUT_CONTENT_INSET}px` }}>
537
+ {helperNode}
538
+ </FormHelperText>
539
+ )}
540
+ </FormControl>
541
+ );
542
+ }
543
+
544
+ // A date-typed filter gets the same picker a plain date given gets. The
545
+ // value is filter syntax, but a bare ISO date is valid filter syntax for
546
+ // "on that day", so the round trip is direct.
547
+ const dateFilterInner = isFilterType(type)
548
+ ? filterInnerType(type)
549
+ : undefined;
550
+ const isDateFilter =
551
+ dateFilterInner === "date" ||
552
+ dateFilterInner === "timestamp" ||
553
+ dateFilterInner === "timestamptz";
554
+ // Only a bare `YYYY-MM-DD` is the picker's own spelling; anything else is a
555
+ // filter expression the picker cannot represent. Unset is fine, since the
556
+ // picker simply shows nothing.
557
+ //
558
+ // An explicit `""` is NOT fine and used to be allowed here. It is the empty
559
+ // filter, a live override that `givensToRequest` sends, and a DatePicker
560
+ // renders it byte-identical to unset: blank field, `Default: …` helper, and
561
+ // no clear button, because MUI emits none for a field with no value. So the
562
+ // screen said the model default was in force while the query matched every
563
+ // row, and there was no way back. Falling through to the text box is the
564
+ // remedy this file already uses for a filter a control cannot represent:
565
+ // there the value is visible as written and the × reverts it.
566
+ const bareDate =
567
+ typeof value === "string" && /^\d{4}-\d{2}-\d{2}$/.test(value);
568
+ const dateIsPickable = value === undefined || value === null || bareDate;
569
+ // An unrepresentable date filter falls through to the text box below, for the
570
+ // same reason a non-list string filter does: a range like
571
+ // `2024-01-01 to 2024-02-01` handed whole to `dayjs.utc` parses as its
572
+ // leading date, so the picker would claim a single wrong day while a range
573
+ // was in force. The text box shows the filter as written, and editable.
574
+ if (isDateFilter && dateIsPickable) {
575
+ const parsed = bareDate ? dayjs.utc(value as string) : null;
576
+ return (
577
+ <LocalizationProvider dateAdapter={AdapterDayjs}>
578
+ <DatePicker
579
+ label={label}
580
+ value={parsed?.isValid() ? parsed : null}
581
+ // Cleared means UNSET, `null`, not an empty filter expression.
582
+ // `""` is a value for a filter given (the empty string is
583
+ // meaningful there), so it reached the wire as a filter of no
584
+ // characters instead of dropping the override.
585
+ onChange={(next) =>
586
+ onChange(next ? next.format("YYYY-MM-DD") : null)
587
+ }
588
+ slotProps={{
589
+ textField: {
590
+ fullWidth: true,
591
+ size: "small",
592
+ helperText: helperNode,
593
+ },
594
+ field: { clearable: true, onClear: () => onChange(null) },
595
+ }}
596
+ />
597
+ </LocalizationProvider>
598
+ );
599
+ }
600
+
601
+ if (type === "boolean") {
602
+ // Three states for a boolean. When unset, reflect the model DEFAULT so the
603
+ // box shows what the query will actually run with (not a misleading
604
+ // unchecked). A toggle is an explicit true/false override; the revert (×)
605
+ // — shown only when overridden — drops the override back to the default.
606
+ const isOverridden = typeof value === "boolean";
607
+ // A link carrying `?FLAG=yes` reaches here as a string, which `paramToGiven`
608
+ // passes through on purpose. Without this the checkbox rendered the model
609
+ // DEFAULT for it, so the box said one thing while `givensToRequest` sent
610
+ // "yes" and every cell failed, with nothing on screen to explain why.
611
+ // No `value !== ""` carve-out here, unlike the number branch it was copied
612
+ // from. `paramToGiven` returns null for an empty `boolean` parameter, so
613
+ // `""` cannot reach a checkbox from a URL or a host; if one ever did, a
614
+ // checkbox has no empty state to show it in, and saying so beats drawing
615
+ // the model default over a value that is still being sent.
616
+ if (!isOverridden && value !== undefined && value !== null) {
617
+ return unrepresentable("true or false");
618
+ }
619
+ const defaultChecked = given.default?.trim() === "true";
620
+ const checked = isOverridden ? value : defaultChecked;
621
+ return (
622
+ <FormControl>
623
+ <Stack direction="row" alignItems="center">
624
+ <FormControlLabel
625
+ control={
626
+ <Checkbox
627
+ checked={checked}
628
+ onChange={(e) => onChange(e.target.checked)}
629
+ />
630
+ }
631
+ label={label}
632
+ />
633
+ {isOverridden && (
634
+ <IconButton
635
+ size="small"
636
+ aria-label="clear value"
637
+ onClick={() => onChange(null)}
638
+ edge="end"
639
+ >
640
+ <ClearIcon fontSize="small" />
641
+ </IconButton>
642
+ )}
643
+ </Stack>
644
+ {helperNode && <FormHelperText>{helperNode}</FormHelperText>}
645
+ </FormControl>
646
+ );
647
+ }
648
+
649
+ if (type === "number") {
650
+ const num = typeof value === "number" ? value : "";
651
+ // Track whether a value is OVERRIDDEN, the way the string branch below
652
+ // does, rather than whether the box happens to render non-empty. A
653
+ // non-number renders blank (see `num` above), and keying the revert off
654
+ // that blank left the user no way to clear it.
655
+ const isOverridden = value !== undefined && value !== null;
656
+ // `""` is an empty box, not an unreadable value. `paramToGiven` now reads
657
+ // `?N=` as UNSET so it no longer arrives here at all, but a host passing
658
+ // values in directly still can, and calling that "Not a number" would put
659
+ // a red error over a field showing nothing.
660
+ if (isOverridden && value !== "" && typeof value !== "number") {
661
+ return unrepresentable("number");
662
+ }
663
+ return (
664
+ <TextField
665
+ label={label}
666
+ type="number"
667
+ value={num}
668
+ onChange={(e) => {
669
+ const v = e.target.value;
670
+ onChange(v === "" ? null : Number(v));
671
+ }}
672
+ placeholder={defaultDisplay}
673
+ helperText={helperNode}
674
+ slotProps={{
675
+ input: {
676
+ endAdornment: isOverridden && (
677
+ <ClearAdornment onClear={() => onChange(null)} />
678
+ ),
679
+ },
680
+ }}
681
+ fullWidth
682
+ size="small"
683
+ />
684
+ );
685
+ }
686
+
687
+ if (type === "date" || type === "timestamp" || type === "timestamptz") {
688
+ // A string is read as a date first. `# drill { to=self }` onto a date
689
+ // given hands over a bare `YYYY-MM-DD` string, not a Date, so treating
690
+ // any non-Date as unreadable painted an error over a value the product
691
+ // itself had just produced.
692
+ const asDate =
693
+ value instanceof Date
694
+ ? value
695
+ : typeof value === "string"
696
+ ? ((parsed) => (parsed instanceof Date ? parsed : null))(
697
+ paramToGiven(type, value),
698
+ )
699
+ : null;
700
+ const dateValue = asDate ? dayjs.utc(asDate) : null;
701
+ // Same lesson as `number` above, and it bit here too. A value the codec
702
+ // cannot read (a shared link carrying `?ORDER_DATE=last month`) is still
703
+ // filtering every cell, but a DatePicker can only render a date, so it
704
+ // showed blank, and the picker's own `clearable` appears only when it
705
+ // holds one: the state was invisible AND had no revert.
706
+ //
707
+ // `""` is an empty control, not an unreadable value; see the number
708
+ // branch above for the same distinction.
709
+ const unreadable =
710
+ value !== undefined &&
711
+ value !== null &&
712
+ value !== "" &&
713
+ asDate === null;
714
+
715
+ // Its own field rather than a badged DatePicker, which was tried twice.
716
+ // Putting the revert in `slotProps.textField.InputProps` REPLACES the
717
+ // picker's own "Choose date" button, because MUI merges that object
718
+ // shallowly and ours wins outright. And passing `error` through the same
719
+ // slot pins it: MUI's `useField` returns any defined `error` ahead of its
720
+ // own validation, so `error: false` suppressed the picker's real errors.
721
+ if (unreadable) return unrepresentable("date");
722
+
723
+ // The date picker shows a format mask, not a placeholder, so the default
724
+ // rides on the shared helper line.
725
+ return (
726
+ <LocalizationProvider dateAdapter={AdapterDayjs}>
727
+ <DatePicker
728
+ label={label}
729
+ value={dateValue}
730
+ // The clicked day rebuilt in UTC, never `next.toDate()`, which
731
+ // is local-mode and lands a day out. See `pickedDayToUtc`.
732
+ onChange={(next) =>
733
+ onChange(
734
+ next ? pickedDayToUtc(next, asDate ?? undefined) : null,
735
+ )
736
+ }
737
+ slotProps={{
738
+ textField: {
739
+ fullWidth: true,
740
+ size: "small",
741
+ helperText: helperNode,
742
+ },
743
+ field: { clearable: true, onClear: () => onChange(null) },
744
+ }}
745
+ />
746
+ </LocalizationProvider>
747
+ );
748
+ }
749
+
750
+ // No `array<…>` branch on purpose. `malloyGivenToApi` renders a non-filter
751
+ // given as the bare `type.type`, so the widest thing the server could ever
752
+ // send is `"array"`; `"array<…>"` is a spelling nothing produces. Malloy's
753
+ // grammar emits only scalar parameter types for givens today anyway
754
+ // (`service/given.ts`), so the case is doubly unreachable.
755
+
756
+ // Default: string, filter<...>, or unknown types — plain text input.
757
+ // An empty field is a deliberate `""` override, NOT a revert: typing the
758
+ // field empty sends "" (so gates like `$region != ''` are expressible). Only
759
+ // the × reverts to the model default. The default ghost shows just for the
760
+ // unset state, so an empty override (× present, no ghost) reads differently.
761
+ const str = typeof value === "string" ? value : "";
762
+ const isOverridden = value !== undefined && value !== null;
763
+ return (
764
+ <TextField
765
+ label={label}
766
+ value={str}
767
+ onChange={(e) => onChange(e.target.value)}
768
+ placeholder={
769
+ isOverridden
770
+ ? undefined
771
+ : (defaultDisplay ??
772
+ (type.startsWith("filter<") ? type : undefined))
773
+ }
774
+ helperText={helperNode}
775
+ slotProps={{
776
+ input: {
777
+ endAdornment: isOverridden && (
778
+ <ClearAdornment onClear={() => onChange(null)} />
779
+ ),
780
+ },
781
+ }}
782
+ fullWidth
783
+ size="small"
784
+ />
785
+ );
786
+ }
787
+
788
+ function ClearAdornment({ onClear }: { onClear: () => void }) {
789
+ return (
790
+ <InputAdornment position="end">
791
+ <IconButton
792
+ size="small"
793
+ aria-label="clear value"
794
+ onClick={onClear}
795
+ edge="end"
796
+ >
797
+ <ClearIcon fontSize="small" />
798
+ </IconButton>
799
+ </InputAdornment>
800
+ );
801
+ }