@malloy-publisher/sdk 0.2.7 → 0.3.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 (221) hide show
  1. package/README.md +180 -204
  2. package/dist/DocumentStorageProvider-BeBD0uWc.cjs.js +134 -0
  3. package/dist/DocumentStorageProvider-tGvodI8f.es.js +24268 -0
  4. package/dist/RenderedResult-CPi6uQWq.cjs.js +115 -0
  5. package/dist/RenderedResult-mDE-4u8S.es.js +542 -0
  6. package/dist/{ServerProvider-xXW1HJOJ.cjs.js → ServerProvider-CH71GsoL.cjs.js} +1 -1
  7. package/dist/{ServerProvider-ksC5Su_x.es.js → ServerProvider-X2Ma0HRs.es.js} +324 -359
  8. package/dist/builder/index.cjs.js +105 -0
  9. package/dist/builder/index.d.ts +2 -0
  10. package/dist/builder/index.es.js +9561 -0
  11. package/dist/builder-entry.d.ts +8 -0
  12. package/dist/client/api.d.ts +13 -1
  13. package/dist/client/index.cjs.js +1 -1
  14. package/dist/client/index.es.js +1 -1
  15. package/dist/components/ApiErrorDisplay.d.ts +1 -2
  16. package/dist/components/Dashboard/Dashboard.d.ts +23 -16
  17. package/dist/components/Dashboard/DashboardGrid.d.ts +67 -0
  18. package/dist/components/Dashboard/DashboardTile.d.ts +6 -1
  19. package/dist/components/Dashboard/ExploreDialog.d.ts +18 -0
  20. package/dist/components/Dashboard/RowsDialog.d.ts +47 -0
  21. package/dist/components/Dashboard/TileCard.d.ts +44 -0
  22. package/dist/components/Dashboard/index.d.ts +2 -0
  23. package/dist/components/Dashboard/promoteMeasureRow.d.ts +40 -0
  24. package/dist/components/Dashboard/telemetry.d.ts +43 -0
  25. package/dist/components/DashboardBuilder/AddTileDialog.d.ts +35 -0
  26. package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +29 -0
  27. package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +112 -0
  28. package/dist/components/DashboardBuilder/DashboardEditor.d.ts +32 -0
  29. package/dist/components/DashboardBuilder/DiffDialog.d.ts +15 -0
  30. package/dist/components/DashboardBuilder/DrillDialog.d.ts +15 -0
  31. package/dist/components/DashboardBuilder/FilterDialog.d.ts +54 -0
  32. package/dist/components/DashboardBuilder/FilterStrip.d.ts +17 -0
  33. package/dist/components/DashboardBuilder/SettingsPopover.d.ts +20 -0
  34. package/dist/components/DashboardBuilder/TileFrame.d.ts +42 -0
  35. package/dist/components/DashboardBuilder/TileMenu.d.ts +24 -0
  36. package/dist/components/DashboardBuilder/catalog.d.ts +62 -0
  37. package/dist/components/DashboardBuilder/controls.d.ts +157 -0
  38. package/dist/components/DashboardBuilder/diff.d.ts +35 -0
  39. package/dist/components/DashboardBuilder/document.d.ts +218 -0
  40. package/dist/components/DashboardBuilder/layout.d.ts +71 -0
  41. package/dist/components/DashboardBuilder/malloyText.d.ts +45 -0
  42. package/dist/components/DashboardBuilder/preview.d.ts +60 -0
  43. package/dist/components/DashboardBuilder/readDocument.d.ts +106 -0
  44. package/dist/components/DashboardBuilder/sortable.d.ts +51 -0
  45. package/dist/components/DashboardBuilder/spliceDocument.d.ts +44 -0
  46. package/dist/components/DashboardBuilder/testing/fixtures.d.ts +10 -0
  47. package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +22 -0
  48. package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +63 -0
  49. package/dist/components/DashboardBuilder/useDraft.d.ts +13 -0
  50. package/dist/components/DashboardBuilder/useFilterForm.d.ts +65 -0
  51. package/dist/components/DashboardBuilder/useTileReorder.d.ts +22 -0
  52. package/dist/components/DashboardBuilder/useTileResize.d.ts +29 -0
  53. package/dist/components/DocumentStorage/BrowserDocumentStorage.d.ts +14 -0
  54. package/dist/components/DocumentStorage/DocumentStorage.d.ts +58 -0
  55. package/dist/components/DocumentStorage/DocumentStorageProvider.d.ts +25 -0
  56. package/dist/components/DocumentStorage/index.d.ts +3 -0
  57. package/dist/components/FloatingIconButton.d.ts +16 -0
  58. package/dist/components/Model/index.d.ts +0 -1
  59. package/dist/components/Prose.d.ts +46 -0
  60. package/dist/components/RenderedResult/RenderedResult.d.ts +12 -0
  61. package/dist/components/RenderedResult/ResultContainer.d.ts +7 -1
  62. package/dist/components/RenderedResult/ResultPanel.d.ts +30 -0
  63. package/dist/components/RenderedResult/index.d.ts +2 -0
  64. package/dist/components/RenderedResult/resultSizing.d.ts +183 -0
  65. package/dist/components/drill/index.d.ts +2 -1
  66. package/dist/components/drill/resolveDrill.d.ts +22 -0
  67. package/dist/components/drill/useDrill.d.ts +11 -2
  68. package/dist/components/drill/useDrillSelf.d.ts +46 -0
  69. package/dist/components/given/filterValue.d.ts +63 -0
  70. package/dist/components/given/index.d.ts +1 -0
  71. package/dist/components/given/useGivenUrlParams.d.ts +39 -0
  72. package/dist/components/index.d.ts +3 -3
  73. package/dist/components/styles.d.ts +3 -7
  74. package/dist/core-8NVBCLZh.cjs.js +148 -0
  75. package/dist/{core-BY7vB5gN.es.js → core-B8oMW4sm.es.js} +3540 -2335
  76. package/dist/hooks/index.d.ts +3 -1
  77. package/dist/hooks/useDocumentControls.d.ts +70 -0
  78. package/dist/hooks/useQueryResult.d.ts +40 -0
  79. package/dist/hooks/useSuggestOptions.d.ts +28 -12
  80. package/dist/index.cjs.js +34 -1
  81. package/dist/index.es.js +21487 -79
  82. package/dist/sdk.css +1 -1
  83. package/dist/styles.css +0 -1
  84. package/dist/utils/malloyLiteral.d.ts +11 -0
  85. package/package.json +25 -14
  86. package/src/builder-entry.ts +55 -0
  87. package/src/components/ApiErrorDisplay.tsx +1 -1
  88. package/src/components/Connections/EditConnectionDialog.tsx +96 -86
  89. package/src/components/Dashboard/Dashboard.spec.tsx +7 -10
  90. package/src/components/Dashboard/Dashboard.tsx +184 -229
  91. package/src/components/Dashboard/DashboardGrid.tsx +124 -0
  92. package/src/components/Dashboard/DashboardTile.spec.tsx +30 -6
  93. package/src/components/Dashboard/DashboardTile.tsx +67 -101
  94. package/src/components/Dashboard/ExploreDialog.tsx +84 -0
  95. package/src/components/Dashboard/RowsDialog.spec.ts +42 -0
  96. package/src/components/Dashboard/RowsDialog.tsx +234 -0
  97. package/src/components/Dashboard/TileCard.tsx +120 -0
  98. package/src/components/Dashboard/index.ts +2 -0
  99. package/src/components/Dashboard/promoteMeasureRow.spec.ts +207 -0
  100. package/src/components/Dashboard/promoteMeasureRow.ts +160 -0
  101. package/src/components/Dashboard/telemetry.ts +44 -0
  102. package/src/components/Dashboard/tileGridColumn.spec.ts +1 -1
  103. package/src/components/DashboardBuilder/AddTileDialog.tsx +249 -0
  104. package/src/components/DashboardBuilder/BuilderToolbar.tsx +270 -0
  105. package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +858 -0
  106. package/src/components/DashboardBuilder/DashboardBuilder.tsx +653 -0
  107. package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +211 -0
  108. package/src/components/DashboardBuilder/DashboardEditor.tsx +493 -0
  109. package/src/components/DashboardBuilder/DiffDialog.tsx +129 -0
  110. package/src/components/DashboardBuilder/DrillDialog.tsx +281 -0
  111. package/src/components/DashboardBuilder/FilterDialog.tsx +581 -0
  112. package/src/components/DashboardBuilder/FilterStrip.tsx +125 -0
  113. package/src/components/DashboardBuilder/SettingsPopover.tsx +168 -0
  114. package/src/components/DashboardBuilder/TileFrame.tsx +380 -0
  115. package/src/components/DashboardBuilder/TileMenu.tsx +188 -0
  116. package/src/components/DashboardBuilder/catalog.spec.ts +215 -0
  117. package/src/components/DashboardBuilder/catalog.ts +225 -0
  118. package/src/components/DashboardBuilder/controls.spec.ts +243 -0
  119. package/src/components/DashboardBuilder/controls.ts +398 -0
  120. package/src/components/DashboardBuilder/diff.spec.ts +44 -0
  121. package/src/components/DashboardBuilder/diff.ts +93 -0
  122. package/src/components/DashboardBuilder/document.ts +209 -0
  123. package/src/components/DashboardBuilder/keepRowStructure.spec.ts +84 -0
  124. package/src/components/DashboardBuilder/layout.ts +143 -0
  125. package/src/components/DashboardBuilder/malloyText.spec.ts +84 -0
  126. package/src/components/DashboardBuilder/malloyText.ts +151 -0
  127. package/src/components/DashboardBuilder/moveIntoGap.spec.ts +90 -0
  128. package/src/components/DashboardBuilder/preview.spec.ts +149 -0
  129. package/src/components/DashboardBuilder/preview.ts +172 -0
  130. package/src/components/DashboardBuilder/readDocument.spec.ts +375 -0
  131. package/src/components/DashboardBuilder/readDocument.ts +467 -0
  132. package/src/components/DashboardBuilder/sortable.tsx +151 -0
  133. package/src/components/DashboardBuilder/spliceDocument.spec.ts +1108 -0
  134. package/src/components/DashboardBuilder/spliceDocument.ts +1098 -0
  135. package/src/components/DashboardBuilder/testing/fixtures.ts +48 -0
  136. package/src/components/DashboardBuilder/useBuilderShortcuts.ts +88 -0
  137. package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +255 -0
  138. package/src/components/DashboardBuilder/useDashboardEditor.ts +193 -0
  139. package/src/components/DashboardBuilder/useDraft.ts +43 -0
  140. package/src/components/DashboardBuilder/useFilterForm.spec.tsx +144 -0
  141. package/src/components/DashboardBuilder/useFilterForm.ts +352 -0
  142. package/src/components/DashboardBuilder/useTileReorder.spec.tsx +97 -0
  143. package/src/components/DashboardBuilder/useTileReorder.ts +115 -0
  144. package/src/components/DashboardBuilder/useTileResize.spec.tsx +121 -0
  145. package/src/components/DashboardBuilder/useTileResize.ts +110 -0
  146. package/src/components/DashboardBuilder/withGaps.spec.ts +96 -0
  147. package/src/components/DocumentStorage/BrowserDocumentStorage.spec.ts +81 -0
  148. package/src/components/DocumentStorage/BrowserDocumentStorage.ts +105 -0
  149. package/src/components/DocumentStorage/DocumentStorage.ts +73 -0
  150. package/src/components/DocumentStorage/DocumentStorageProvider.tsx +58 -0
  151. package/src/components/DocumentStorage/index.ts +16 -0
  152. package/src/components/Environment/About.tsx +4 -36
  153. package/src/components/FloatingIconButton.tsx +41 -0
  154. package/src/components/Model/Model.tsx +6 -23
  155. package/src/components/Model/ModelCell.tsx +34 -72
  156. package/src/components/Model/index.ts +0 -1
  157. package/src/components/Notebook/Notebook.tsx +27 -156
  158. package/src/components/Notebook/NotebookCell.tsx +37 -191
  159. package/src/components/Prose.spec.tsx +85 -0
  160. package/src/components/Prose.tsx +179 -0
  161. package/src/components/QueryResult/QueryResult.tsx +15 -43
  162. package/src/components/RenderedResult/RenderedResult.tsx +86 -76
  163. package/src/components/RenderedResult/ResultContainer.tsx +25 -36
  164. package/src/components/RenderedResult/ResultPanel.spec.tsx +49 -0
  165. package/src/components/RenderedResult/ResultPanel.tsx +73 -0
  166. package/src/components/RenderedResult/index.ts +16 -0
  167. package/src/components/RenderedResult/resultSizing.spec.ts +249 -0
  168. package/src/components/RenderedResult/resultSizing.ts +308 -0
  169. package/src/components/ResultsDialog.tsx +2 -1
  170. package/src/components/drill/index.ts +6 -0
  171. package/src/components/drill/resolveDrill.ts +22 -1
  172. package/src/components/drill/useDrill.spec.tsx +94 -0
  173. package/src/components/drill/useDrill.tsx +52 -12
  174. package/src/components/drill/useDrillSelf.spec.tsx +64 -0
  175. package/src/components/drill/useDrillSelf.ts +110 -0
  176. package/src/components/given/GivenInput.spec.tsx +196 -2
  177. package/src/components/given/GivenInput.tsx +300 -25
  178. package/src/components/given/GivensPanel.tsx +1 -1
  179. package/src/components/given/filterValue.spec.ts +112 -0
  180. package/src/components/given/filterValue.ts +188 -0
  181. package/src/components/given/index.ts +4 -0
  182. package/src/components/given/useGivenUrlParams.spec.tsx +74 -0
  183. package/src/components/given/useGivenUrlParams.ts +87 -0
  184. package/src/components/index.ts +11 -3
  185. package/src/components/styles.ts +0 -37
  186. package/src/hooks/index.ts +13 -1
  187. package/src/hooks/useDocumentControls.ts +170 -0
  188. package/src/hooks/useGivensState.spec.ts +158 -0
  189. package/src/hooks/useGivensState.ts +114 -23
  190. package/src/hooks/useQueryResult.ts +82 -0
  191. package/src/hooks/useSuggestOptions.dom.spec.ts +60 -1
  192. package/src/hooks/useSuggestOptions.ts +94 -51
  193. package/src/utils/malloyLiteral.spec.ts +43 -0
  194. package/src/utils/malloyLiteral.ts +76 -0
  195. package/vite.config.ts +5 -1
  196. package/dist/components/AnalyzePackageButton.d.ts +0 -6
  197. package/dist/components/Workbook/BrowserWorkbookStorage.d.ts +0 -9
  198. package/dist/components/Workbook/EditableMalloyCell.d.ts +0 -11
  199. package/dist/components/Workbook/ModelPicker.d.ts +0 -11
  200. package/dist/components/Workbook/MutableCell.d.ts +0 -17
  201. package/dist/components/Workbook/Workbook.d.ts +0 -7
  202. package/dist/components/Workbook/WorkbookList.d.ts +0 -7
  203. package/dist/components/Workbook/WorkbookManager.d.ts +0 -84
  204. package/dist/components/Workbook/WorkbookStorage.d.ts +0 -17
  205. package/dist/components/Workbook/WorkbookStorageProvider.d.ts +0 -12
  206. package/dist/components/Workbook/index.d.ts +0 -7
  207. package/dist/core-CWKmWEg2.cjs.js +0 -148
  208. package/dist/index-B0WyZAUA.es.js +0 -66412
  209. package/dist/index-kxVD1RjY.cjs.js +0 -356
  210. package/dist/markdown-editor.css +0 -341
  211. package/src/components/AnalyzePackageButton.tsx +0 -319
  212. package/src/components/Workbook/BrowserWorkbookStorage.ts +0 -64
  213. package/src/components/Workbook/EditableMalloyCell.tsx +0 -49
  214. package/src/components/Workbook/ModelPicker.tsx +0 -134
  215. package/src/components/Workbook/MutableCell.tsx +0 -458
  216. package/src/components/Workbook/Workbook.tsx +0 -501
  217. package/src/components/Workbook/WorkbookList.tsx +0 -110
  218. package/src/components/Workbook/WorkbookManager.ts +0 -214
  219. package/src/components/Workbook/WorkbookStorage.ts +0 -35
  220. package/src/components/Workbook/WorkbookStorageProvider.tsx +0 -40
  221. package/src/components/Workbook/index.ts +0 -10
@@ -0,0 +1,581 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import {
5
+ Autocomplete,
6
+ Box,
7
+ Button,
8
+ Checkbox,
9
+ Dialog,
10
+ DialogActions,
11
+ DialogContent,
12
+ DialogTitle,
13
+ MenuItem,
14
+ Stack,
15
+ TextField,
16
+ ToggleButton,
17
+ ToggleButtonGroup,
18
+ Tooltip,
19
+ Typography,
20
+ type SxProps,
21
+ type Theme,
22
+ } from "@mui/material";
23
+ import { useMemo } from "react";
24
+ import { titleCase, useFilterForm } from "./useFilterForm";
25
+ import { usePublisherTheme } from "../../theme/ThemeContext";
26
+ import type { CatalogField } from "./catalog";
27
+ import {
28
+ CONTROL_KINDS,
29
+ OPERATORS,
30
+ type BuilderControl,
31
+ type ControlKind,
32
+ type MappingRow,
33
+ } from "./controls";
34
+ import type { DashboardDocument, DashboardTile, LocalGiven } from "./document";
35
+
36
+ /**
37
+ * A filter control: what it is, and which tiles it drives.
38
+ *
39
+ * Two questions, asked in that order and nothing else. WHAT: its label, and
40
+ * the field it filters on — one field, because a control almost always filters
41
+ * the same field on every tile, and the case where it does not is one click
42
+ * away ("Adjust per tile"), not five text boxes on every open. WHICH: a list
43
+ * of the dashboard's tiles with a checkbox each, and one checkbox over them
44
+ * all. A tile that cannot take a filter says why in place.
45
+ *
46
+ * The field is SEARCHED, not typed blind, when the host has handed over the
47
+ * source's fields: the picker offers the dimensions the tiles' source has,
48
+ * an unknown name is marked as such where it stands, and Apply waits until
49
+ * every ticked tile's field resolves. A binding to a field that does not exist
50
+ * fails at package load, which is the worst place for it to fail; here it
51
+ * cannot be written.
52
+ *
53
+ * The same window adds, edits and removes. It opens on the mapping the control
54
+ * has NOW, so unticking a tile unbinds it, and a control this dashboard
55
+ * declares can be taken off the page from here.
56
+ *
57
+ * What it writes is the convention {@link LocalGiven} describes: a new control
58
+ * is a `given:` declared in this file, bound on each ticked tile as a
59
+ * `+ { where: field ~ $GIVEN }` refinement. A control the model declares can be
60
+ * bound here but not changed, since the builder never edits model files.
61
+ */
62
+ export interface FilterDialogProps {
63
+ open: boolean;
64
+ document: DashboardDocument;
65
+ /** The control being edited, or undefined to add one. */
66
+ control?: BuilderControl;
67
+ /** Model givens not yet bound anywhere, offered as "from the model". */
68
+ available: readonly BuilderControl[];
69
+ /**
70
+ * The fields a binding on THIS tile may name: the dimensions of the source
71
+ * it reads. Per tile, because a composite dashboard exists to span sources,
72
+ * and a field one source has another may not. Undefined for a tile whose
73
+ * source the host has no catalog for, in which case any name is accepted
74
+ * for it and nothing is searched.
75
+ */
76
+ fieldsFor?: (tile: DashboardTile) => readonly CatalogField[] | undefined;
77
+ onClose: () => void;
78
+ /**
79
+ * Bind a control. `declare` is set when the control is new to this file, or
80
+ * when its declaration changed, and is the declaration to write.
81
+ */
82
+ onApply: (given: string, rows: MappingRow[], declare?: LocalGiven) => void;
83
+ /** Take a control off the dashboard; offered for one this file declares. */
84
+ onRemove: (given: string) => void;
85
+ }
86
+
87
+ /** `BRAND` -> `Brand`: the label a new control gets if the author types none. */
88
+ /**
89
+ * A field name, searched from the source's dimensions and still free to type:
90
+ * the catalog may lag a model edit, and a host without one has no list at all.
91
+ */
92
+ function FieldPicker({
93
+ value,
94
+ onChange,
95
+ fields,
96
+ accepts,
97
+ label,
98
+ ariaLabel,
99
+ error,
100
+ helperText,
101
+ disabled,
102
+ autoFocus,
103
+ placeholder,
104
+ sx,
105
+ }: {
106
+ value: string;
107
+ onChange: (next: string) => void;
108
+ fields: readonly CatalogField[] | undefined;
109
+ /** Which of the fields to offer; the rest are still accepted if typed. */
110
+ accepts?: (field: CatalogField) => boolean;
111
+ label: string;
112
+ ariaLabel: string;
113
+ error?: boolean;
114
+ helperText?: string;
115
+ disabled?: boolean;
116
+ autoFocus?: boolean;
117
+ placeholder?: string;
118
+ sx?: SxProps<Theme>;
119
+ }) {
120
+ const options = useMemo(
121
+ () =>
122
+ (fields ?? [])
123
+ .filter((field) => accepts?.(field) ?? true)
124
+ .map((field) => field.name),
125
+ [fields, accepts],
126
+ );
127
+ const types = useMemo(
128
+ () => new Map((fields ?? []).map((field) => [field.name, field.type])),
129
+ [fields],
130
+ );
131
+ return (
132
+ <Autocomplete
133
+ freeSolo
134
+ size="small"
135
+ options={options}
136
+ inputValue={value}
137
+ onInputChange={(_, next) => onChange(next)}
138
+ value={value}
139
+ onChange={(_, next) => onChange(typeof next === "string" ? next : "")}
140
+ disabled={disabled}
141
+ disableClearable
142
+ renderOption={(props, option) => (
143
+ <li {...props} key={option}>
144
+ <Typography variant="body2" sx={{ flex: 1 }}>
145
+ {option}
146
+ </Typography>
147
+ {types.get(option) && (
148
+ <Typography variant="caption" sx={{ opacity: 0.6 }}>
149
+ {types.get(option)?.replace(/_type$/, "")}
150
+ </Typography>
151
+ )}
152
+ </li>
153
+ )}
154
+ renderInput={(params) => (
155
+ <TextField
156
+ {...params}
157
+ label={label}
158
+ placeholder={placeholder}
159
+ autoFocus={autoFocus}
160
+ error={error}
161
+ helperText={helperText}
162
+ inputProps={{ ...params.inputProps, "aria-label": ariaLabel }}
163
+ />
164
+ )}
165
+ sx={sx}
166
+ />
167
+ );
168
+ }
169
+
170
+ export function FilterDialog({
171
+ open,
172
+ document,
173
+ control,
174
+ available,
175
+ fieldsFor,
176
+ onClose,
177
+ onApply,
178
+ onRemove,
179
+ }: FilterDialogProps) {
180
+ const { theme } = usePublisherTheme();
181
+ const {
182
+ editing,
183
+ shown,
184
+ fromModel,
185
+ source,
186
+ setSource,
187
+ label,
188
+ setLabel,
189
+ kind,
190
+ dateDefault,
191
+ setDateDefault,
192
+ field,
193
+ rows,
194
+ setRows,
195
+ perTile,
196
+ setPerTile,
197
+ newName,
198
+ target,
199
+ valueTyped,
200
+ commonOp,
201
+ setRow,
202
+ setCommonOp,
203
+ pickExisting,
204
+ pickKind,
205
+ bindable,
206
+ bindableCount,
207
+ included,
208
+ commonFields,
209
+ setAll,
210
+ accepts,
211
+ pickField,
212
+ rowProblems,
213
+ commonProblem,
214
+ canApply,
215
+ apply,
216
+ } = useFilterForm({
217
+ open,
218
+ document,
219
+ control,
220
+ available,
221
+ fieldsFor,
222
+ onApply,
223
+ });
224
+
225
+ const operatorField = (
226
+ value: string | undefined,
227
+ onChange: (op: string) => void,
228
+ ariaLabel: string,
229
+ ) => (
230
+ <TextField
231
+ select
232
+ size="small"
233
+ label="Compares"
234
+ value={value ?? ">="}
235
+ onChange={(event) => onChange(event.target.value)}
236
+ inputProps={{ "aria-label": ariaLabel }}
237
+ sx={{ width: 130 }}
238
+ >
239
+ {OPERATORS.filter((o) => o.op !== "~").map((o) => (
240
+ <MenuItem key={o.op} value={o.op}>
241
+ {o.label}
242
+ </MenuItem>
243
+ ))}
244
+ </TextField>
245
+ );
246
+
247
+ return (
248
+ <Dialog open={open} onClose={onClose} maxWidth="sm" fullWidth>
249
+ <DialogTitle sx={{ pb: 0.5 }}>
250
+ {editing ? (shown.label ?? shown.name) : "Add a filter"}
251
+ {editing && (
252
+ <Typography
253
+ component="div"
254
+ variant="caption"
255
+ sx={{ color: theme.tileTitle, mt: 0.25 }}
256
+ >
257
+ <Box
258
+ component="span"
259
+ sx={{ fontFamily: "ui-monospace, monospace" }}
260
+ >
261
+ ${shown.name}
262
+ </Box>
263
+ {fromModel
264
+ ? " · declared in the model, so its label and options are set there"
265
+ : " · declared in this dashboard"}
266
+ </Typography>
267
+ )}
268
+ </DialogTitle>
269
+ <DialogContent>
270
+ {/* Top margin on the stack, not padding on the content: MUI zeroes
271
+ a DialogContent's top padding after a DialogTitle with a rule
272
+ that outranks `sx`, and the fields' floating labels need the
273
+ room or they clip. */}
274
+ <Stack sx={{ gap: 2.5, pt: 1 }}>
275
+ {/* WHAT the filter is. */}
276
+ {!editing && available.length > 0 && (
277
+ <ToggleButtonGroup
278
+ exclusive
279
+ size="small"
280
+ value={source.kind}
281
+ onChange={(_, value: "new" | "existing" | null) => {
282
+ if (value === "new") setSource({ kind: "new" });
283
+ else if (value === "existing" && available[0])
284
+ pickExisting(available[0]);
285
+ }}
286
+ aria-label="Where the filter comes from"
287
+ >
288
+ <ToggleButton value="new">
289
+ New in this dashboard
290
+ </ToggleButton>
291
+ <ToggleButton value="existing">
292
+ From the model
293
+ </ToggleButton>
294
+ </ToggleButtonGroup>
295
+ )}
296
+
297
+ {!editing && source.kind === "existing" ? (
298
+ <TextField
299
+ select
300
+ size="small"
301
+ label="Control"
302
+ value={source.name}
303
+ onChange={(event) => {
304
+ const picked = available.find(
305
+ (g) => g.name === event.target.value,
306
+ );
307
+ if (picked) pickExisting(picked);
308
+ }}
309
+ >
310
+ {available.map((given) => (
311
+ <MenuItem key={given.name} value={given.name}>
312
+ {given.label ?? given.name}
313
+ <Typography
314
+ component="span"
315
+ variant="caption"
316
+ sx={{ ml: 1, opacity: 0.6 }}
317
+ >
318
+ ${given.name}
319
+ </Typography>
320
+ </MenuItem>
321
+ ))}
322
+ </TextField>
323
+ ) : null}
324
+
325
+ <Stack direction="row" sx={{ gap: 1.5, flexWrap: "wrap" }}>
326
+ {/* Hidden while per-tile fields are open: they speak for
327
+ themselves then, and a box above them that applied to
328
+ nothing would read as one more thing to fill. */}
329
+ {!perTile && (
330
+ <FieldPicker
331
+ value={field}
332
+ onChange={pickField}
333
+ fields={commonFields}
334
+ accepts={accepts}
335
+ label="Field to filter"
336
+ ariaLabel="Field to filter"
337
+ placeholder="category"
338
+ autoFocus={!editing}
339
+ error={commonProblem !== undefined}
340
+ helperText={
341
+ commonProblem ??
342
+ (!editing && newName
343
+ ? `Declared as $${newName}`
344
+ : undefined)
345
+ }
346
+ sx={{ flex: 1, minWidth: 200 }}
347
+ />
348
+ )}
349
+ {!fromModel && (
350
+ <TextField
351
+ size="small"
352
+ label="Label"
353
+ placeholder={newName ? titleCase(newName) : ""}
354
+ value={label}
355
+ onChange={(event) => setLabel(event.target.value)}
356
+ inputProps={{ "aria-label": "Control label" }}
357
+ sx={{ flex: 1, minWidth: 160 }}
358
+ />
359
+ )}
360
+ {valueTyped &&
361
+ !perTile &&
362
+ operatorField(commonOp, setCommonOp, "Comparison")}
363
+ </Stack>
364
+ {!editing && source.kind === "new" && (
365
+ <Stack direction="row" sx={{ gap: 1.5 }}>
366
+ <TextField
367
+ select
368
+ size="small"
369
+ label="Control"
370
+ value={kind}
371
+ onChange={(event) =>
372
+ pickKind(event.target.value as ControlKind)
373
+ }
374
+ inputProps={{ "aria-label": "Kind of control" }}
375
+ slotProps={{
376
+ select: {
377
+ renderValue: (value) =>
378
+ CONTROL_KINDS.find((k) => k.kind === value)
379
+ ?.label ?? String(value),
380
+ },
381
+ }}
382
+ sx={{ flex: 1 }}
383
+ >
384
+ {CONTROL_KINDS.map((option) => (
385
+ <MenuItem key={option.kind} value={option.kind}>
386
+ {option.label}
387
+ <Typography
388
+ component="span"
389
+ variant="caption"
390
+ sx={{ ml: 1, opacity: 0.6 }}
391
+ >
392
+ {option.hint}
393
+ </Typography>
394
+ </MenuItem>
395
+ ))}
396
+ </TextField>
397
+ {kind === "date" && (
398
+ <TextField
399
+ size="small"
400
+ type="date"
401
+ label="Starts at"
402
+ value={dateDefault}
403
+ onChange={(event) =>
404
+ setDateDefault(event.target.value)
405
+ }
406
+ InputLabelProps={{ shrink: true }}
407
+ sx={{ flex: 1 }}
408
+ />
409
+ )}
410
+ </Stack>
411
+ )}
412
+
413
+ {/* WHICH tiles. */}
414
+ <Box>
415
+ <Stack
416
+ direction="row"
417
+ sx={{ alignItems: "center", gap: 0.5 }}
418
+ >
419
+ <Checkbox
420
+ size="small"
421
+ checked={
422
+ bindableCount > 0 && included === bindableCount
423
+ }
424
+ indeterminate={included > 0 && included < bindableCount}
425
+ disabled={bindableCount === 0}
426
+ onChange={(event) => setAll(event.target.checked)}
427
+ inputProps={{ "aria-label": "All tiles" }}
428
+ />
429
+ <Typography variant="subtitle2" sx={{ flex: 1 }}>
430
+ Applies to {included} of {document.tiles.length} tiles
431
+ </Typography>
432
+ <Button
433
+ size="small"
434
+ onClick={() => {
435
+ // Opening per-tile starts every row from the common
436
+ // field; closing it goes back to one box for all.
437
+ if (!perTile)
438
+ setRows((previous) =>
439
+ previous.map((row) => ({ ...row, field })),
440
+ );
441
+ setPerTile((was) => !was);
442
+ }}
443
+ >
444
+ {perTile ? "Same field for all" : "Adjust per tile"}
445
+ </Button>
446
+ </Stack>
447
+ <Stack sx={{ gap: perTile ? 1 : 0 }}>
448
+ {document.tiles.map((tile, index) => {
449
+ const row = rows[index];
450
+ const title = tile.label ?? tile.name;
451
+ const problem = perTile
452
+ ? rowProblems[index]
453
+ : undefined;
454
+ return (
455
+ <Stack
456
+ key={`${tile.source}.${tile.name}`}
457
+ direction="row"
458
+ sx={{
459
+ gap: 1,
460
+ alignItems: perTile ? "flex-start" : "center",
461
+ opacity: bindable[index] ? 1 : 0.6,
462
+ }}
463
+ >
464
+ <Checkbox
465
+ size="small"
466
+ disabled={!bindable[index]}
467
+ checked={
468
+ (row?.include ?? false) && bindable[index]
469
+ }
470
+ inputProps={{
471
+ "aria-label": `Filter ${title}`,
472
+ }}
473
+ onChange={(event) =>
474
+ setRow(index, {
475
+ include: event.target.checked,
476
+ })
477
+ }
478
+ />
479
+ <Typography
480
+ variant="body2"
481
+ sx={{
482
+ flex: 1,
483
+ minWidth: 0,
484
+ pt: perTile ? 1 : 0,
485
+ }}
486
+ noWrap
487
+ >
488
+ {title}
489
+ </Typography>
490
+ {!bindable[index] ? (
491
+ <Tooltip
492
+ title={
493
+ tile.declaration.kind === "inherited"
494
+ ? "Declared on its source, which this dashboard does not write."
495
+ : "Its query is written out here rather than named, so a filter cannot be added to it."
496
+ }
497
+ >
498
+ <Typography
499
+ variant="caption"
500
+ sx={{ color: theme.tileTitle }}
501
+ >
502
+ {tile.declaration.kind === "inherited"
503
+ ? "From the model"
504
+ : "Inline query"}
505
+ </Typography>
506
+ </Tooltip>
507
+ ) : (
508
+ perTile && (
509
+ <>
510
+ {valueTyped &&
511
+ operatorField(
512
+ row?.op,
513
+ (op) => setRow(index, { op }),
514
+ `Comparison for ${title}`,
515
+ )}
516
+ <FieldPicker
517
+ value={row?.field ?? ""}
518
+ onChange={(next) =>
519
+ setRow(index, { field: next })
520
+ }
521
+ fields={fieldsFor?.(tile)}
522
+ accepts={accepts}
523
+ label="Field"
524
+ ariaLabel={`Field for ${title}`}
525
+ disabled={!row?.include}
526
+ error={problem !== undefined}
527
+ helperText={problem}
528
+ sx={{ width: 220 }}
529
+ />
530
+ </>
531
+ )
532
+ )}
533
+ </Stack>
534
+ );
535
+ })}
536
+ </Stack>
537
+ {included === 0 && (
538
+ <Typography
539
+ variant="caption"
540
+ sx={{ color: "warning.main", display: "block", mt: 1 }}
541
+ >
542
+ No tile is ticked, so this control will not appear on
543
+ the dashboard.
544
+ </Typography>
545
+ )}
546
+ </Box>
547
+ </Stack>
548
+ </DialogContent>
549
+ <DialogActions sx={{ px: 3, py: 1.5 }}>
550
+ {editing && (
551
+ <Button
552
+ color="error"
553
+ onClick={() => onRemove(shown.name)}
554
+ aria-label={`Remove control ${shown.name}`}
555
+ sx={{ mr: "auto" }}
556
+ >
557
+ Remove from dashboard
558
+ </Button>
559
+ )}
560
+ <Button onClick={onClose}>Cancel</Button>
561
+ <Tooltip
562
+ title={
563
+ canApply || target === undefined
564
+ ? ""
565
+ : "A ticked tile has no field, or names one its source does not have."
566
+ }
567
+ >
568
+ <span>
569
+ <Button
570
+ variant="contained"
571
+ onClick={apply}
572
+ disabled={!canApply}
573
+ >
574
+ {editing ? "Apply" : "Add filter"}
575
+ </Button>
576
+ </span>
577
+ </Tooltip>
578
+ </DialogActions>
579
+ </Dialog>
580
+ );
581
+ }