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