@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,653 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { DragDropProvider } from "@dnd-kit/react";
5
+ import { Alert, Box, Stack } from "@mui/material";
6
+ import {
7
+ useCallback,
8
+ useEffect,
9
+ useMemo,
10
+ useState,
11
+ type ReactNode,
12
+ } from "react";
13
+ import { DashboardProse } from "../Dashboard/Dashboard";
14
+ import { DashboardGrid, DEFAULT_COLUMNS } from "../Dashboard/DashboardGrid";
15
+ import { now, type DashboardEventHandler } from "../Dashboard/telemetry";
16
+ import {
17
+ acceptsField,
18
+ applyMapping,
19
+ controlsOf,
20
+ declareControl,
21
+ removeControl,
22
+ type BuilderControl,
23
+ type BuilderGiven,
24
+ type MappingRow,
25
+ } from "./controls";
26
+ import { BuilderToolbar } from "./BuilderToolbar";
27
+ import { filterableFields, type PackageCatalog } from "./catalog";
28
+ import {
29
+ tileKey,
30
+ type DashboardDocument,
31
+ type DashboardTile,
32
+ type LocalGiven,
33
+ } from "./document";
34
+ import { AddTileDialog, type NewTile } from "./AddTileDialog";
35
+ import { DiffDialog } from "./DiffDialog";
36
+ import { DrillDialog } from "./DrillDialog";
37
+ import { FilterDialog } from "./FilterDialog";
38
+ import { SettingsPopover, settingsOf } from "./SettingsPopover";
39
+ import { FilterStrip } from "./FilterStrip";
40
+ import { gapId, tileEntry, withGaps } from "./layout";
41
+ import { builderSensors } from "./sortable";
42
+ import { GapTarget, GridGuides, TileFrame, TilePlaceholder } from "./TileFrame";
43
+ import { useTileReorder } from "./useTileReorder";
44
+ import { useTileResize } from "./useTileResize";
45
+ import { TileMenu } from "./TileMenu";
46
+ import { useBuilderShortcuts } from "./useBuilderShortcuts";
47
+ import { useDashboardEditor } from "./useDashboardEditor";
48
+
49
+ export type { BuilderGiven } from "./controls";
50
+
51
+ /**
52
+ * The dashboard builder's editing surface.
53
+ *
54
+ * Lays the tiles out on the SAME grid the dashboard renders on — literally the
55
+ * same component, `DashboardGrid`, rather than a grid restated here to match.
56
+ * What you arrange is what a reader will see, and the two cannot drift.
57
+ *
58
+ * The TILE ITSELF is the caller's business, through `renderTile`. A tile's
59
+ * result comes from running a query, and where that query runs differs between
60
+ * a saved package dashboard and an unsaved draft; keeping it out here means the
61
+ * editing surface can be mounted, tested and reviewed without a server.
62
+ *
63
+ * `renderTile` replaces the tile rather than filling it, because `DashboardTile`
64
+ * draws its own card AND its own heading. Nesting one inside a card of ours
65
+ * would show a card in a card under two titles — which is precisely not what a
66
+ * reader sees. So every affordance is drawn AROUND whatever the caller renders:
67
+ * an outline for selection, a grip and a menu in the corners, a handle at the
68
+ * edge.
69
+ *
70
+ * **Everything here is on the thing it changes.** Drag a tile's right edge to
71
+ * set its width, and the tile itself to reorder — into the empty end of a row
72
+ * to change which row it is in. The tile's own menu holds its title and
73
+ * subtitle. FILTERS are configured in exactly one place, the strip under the
74
+ * header: each chip opens the tiles-to-update window for that control,
75
+ * which is where a tile is bound or unbound, and "Add filter" declares a new
76
+ * one in this file — the convention {@link LocalGiven} describes — or binds one
77
+ * the model offers. Nothing about filters is on the tiles themselves: a second
78
+ * place to edit the same binding is a second place for it to be wrong.
79
+ *
80
+ * Tiles are added from the package's catalog and removed from their menu; a
81
+ * save that adds or removes one shows the file's diff first, because those
82
+ * moves relocate declarations and the comments beside them.
83
+ */
84
+ export interface DashboardBuilderProps {
85
+ /** The file being edited. */
86
+ source: string;
87
+ /** The document that file produced. */
88
+ document: DashboardDocument;
89
+ /** Persist the patched file. Left out, the builder edits without saving. */
90
+ onSave?: (source: string) => Promise<void> | void;
91
+ /**
92
+ * The document as it stands, on every edit — including the first render.
93
+ *
94
+ * For the host's LIVE VIEW. A tile's result and the control row are the
95
+ * host's to render (`renderTile`, `controls`), and both have to follow the
96
+ * document rather than the saved file, or an edit looks like it did
97
+ * nothing: unbinding a tile left it filtering, removing a control left it
98
+ * in the row. `preview.ts` turns the document handed here into what a reader
99
+ * would see.
100
+ */
101
+ onChange?: (document: DashboardDocument) => void;
102
+ /**
103
+ * Renders a tile, card and heading included — this is where a real
104
+ * `DashboardTile` goes. Without it, tiles show what they will run.
105
+ */
106
+ renderTile?: (tile: DashboardTile) => ReactNode;
107
+ /**
108
+ * The control row, in the slot the reader puts it — this is where a real
109
+ * `GivensPanel` goes.
110
+ *
111
+ * A seam for the same reason as `renderTile`, and not an oversight: a
112
+ * control's SPEC (its type, its control tag, its suggestions) is resolved by
113
+ * the server across the model and everything it imports, and a
114
+ * `DashboardDocument` holds only what this one file declares. The storefront
115
+ * dashboard is the ordinary case — it imports every given from
116
+ * `../givens.malloy` — so a bar built from the document alone would come up
117
+ * empty and claim the dashboard has no filters. Better to let a caller that
118
+ * has the manifest hand over the real one.
119
+ *
120
+ * It matters to LAYOUT, not just to fidelity: the bar takes vertical space
121
+ * above the grid, so an author arranging tiles without it is arranging
122
+ * against a page the reader never sees.
123
+ */
124
+ controls?: ReactNode;
125
+ /**
126
+ * Givens a tile can be bound to, for the palette above the grid.
127
+ *
128
+ * A prop for the same reason as `controls`: which givens exist is resolved
129
+ * by the server across the model and its imports, and a `DashboardDocument`
130
+ * knows only what its own file declares.
131
+ */
132
+ givens?: BuilderGiven[];
133
+ /**
134
+ * What the package offers, for the filter window to SEARCH a field rather
135
+ * than take one on trust: the dimensions of the source the tiles read are
136
+ * offered, an unknown name is marked where it stands, and a binding to a
137
+ * field the source does not have cannot be applied. Absent, any name is
138
+ * accepted — a binding to a missing field then fails at package load, which
139
+ * is the worst place for it.
140
+ */
141
+ catalog?: PackageCatalog;
142
+ /**
143
+ * The other dashboards in the package, by slug — where a clicked cell can
144
+ * go. Absent, a drill can only filter this dashboard.
145
+ */
146
+ dashboards?: string[];
147
+ /** Saves and refusals, for the host to log; see `DashboardEvent`. */
148
+ onEvent?: DashboardEventHandler;
149
+ /**
150
+ * The host's own actions for the edit bar — Export, Done — rendered beside
151
+ * undo, redo and save. The builder owns the edits; where the file goes
152
+ * afterwards is the host's, so its buttons sit in the host's slot.
153
+ */
154
+ toolbar?: ReactNode;
155
+ }
156
+
157
+ export function DashboardBuilder({
158
+ source,
159
+ document,
160
+ onSave,
161
+ onChange,
162
+ renderTile,
163
+ controls,
164
+ givens,
165
+ catalog,
166
+ toolbar,
167
+ dashboards,
168
+ onEvent,
169
+ }: DashboardBuilderProps) {
170
+ const editor = useDashboardEditor({
171
+ source,
172
+ document,
173
+ ...(onSave ? { onSave } : {}),
174
+ });
175
+ const [selected, setSelected] = useState<number | undefined>(undefined);
176
+ const [saving, setSaving] = useState(false);
177
+ const columns = editor.document.columns ?? DEFAULT_COLUMNS;
178
+ const { resize, gridBox, startResize, onResize, endResize } = useTileResize({
179
+ tiles: editor.document.tiles,
180
+ columns,
181
+ onStart: setSelected,
182
+ commit: (index, span) =>
183
+ editor.update((draft) => {
184
+ draft.tiles[index].colspan = span;
185
+ }),
186
+ });
187
+ const { dragging, preview, onDragStart, onDragOver, onDragEnd } =
188
+ useTileReorder({
189
+ tiles: editor.document.tiles,
190
+ commit: (next) =>
191
+ editor.update((draft) => {
192
+ draft.tiles = next;
193
+ }),
194
+ onLanded: setSelected,
195
+ });
196
+ // The filter window: open on a control, or open to add one.
197
+ const [filterDialog, setFilterDialog] = useState<
198
+ { control?: BuilderControl } | undefined
199
+ >(undefined);
200
+ // A tile's menu, anchored to the button that opened it.
201
+ const [menu, setMenu] = useState<
202
+ { anchor: HTMLElement; index: number } | undefined
203
+ >(undefined);
204
+ // The add-tile picker, and the diff a structural save shows first.
205
+ const [addingTile, setAddingTile] = useState(false);
206
+ // The clickable-cells window, for one tile's source.
207
+ const [drillSource, setDrillSource] = useState<string | undefined>(
208
+ undefined,
209
+ );
210
+ // The page's settings, anchored to the toolbar button that opened them.
211
+ const [settingsAnchor, setSettingsAnchor] = useState<HTMLElement | null>(
212
+ null,
213
+ );
214
+ const [pendingSave, setPendingSave] = useState<
215
+ { before: string; after: string } | undefined
216
+ >(undefined);
217
+
218
+ useEffect(() => {
219
+ onChange?.(editor.document);
220
+ }, [editor.document, onChange]);
221
+ // One object per document, or the popover's draft would reset on every
222
+ // render of the builder while it is open.
223
+ const settings = useMemo(
224
+ () => settingsOf(editor.document),
225
+ [editor.document],
226
+ );
227
+
228
+ // The givens the MODEL offers: the caller's list, less any the opened file
229
+ // declared itself. A caller gets that list from the server's manifest, which
230
+ // resolves givens across the file and its imports without saying which is
231
+ // which — so it names this file's own declarations too, and keeps naming a
232
+ // control after this document removes it, until a save is written and the
233
+ // package reloads. Without this, "Remove from dashboard" took the chip off
234
+ // and it came straight back, faint, labelled "from the model".
235
+ const modelGivens = useMemo(() => {
236
+ const ownDeclarations = new Set(
237
+ (document.localGivens ?? []).map((given) => given.name),
238
+ );
239
+ return (givens ?? []).filter((given) => !ownDeclarations.has(given.name));
240
+ }, [document, givens]);
241
+ // Every control the builder can offer: this file's own, then the model's.
242
+ const controlList = useMemo(
243
+ () => controlsOf(editor.document, modelGivens),
244
+ [editor.document, modelGivens],
245
+ );
246
+ // The fields a binding may name, PER SOURCE: the dimensions of the model
247
+ // source each of this file's extensions is built on, when the host knows
248
+ // them. A composite spans sources, so one list would call a field the second
249
+ // source has "unknown" and block the window on it. Undefined for a source
250
+ // the catalog does not have, and nothing checks that source's tiles.
251
+ const fieldsBySource = useMemo(
252
+ () =>
253
+ new Map(
254
+ editor.document.sources.map((source) => [
255
+ source.name,
256
+ filterableFields(catalog, source.base),
257
+ ]),
258
+ ),
259
+ [catalog, editor.document.sources],
260
+ );
261
+ const fieldsFor = useCallback(
262
+ (tile: DashboardTile) =>
263
+ fieldsBySource.get(tile.source) ??
264
+ // A tile on an imported source with no extension of its own: its
265
+ // source IS a model source, and may be in the catalog directly.
266
+ filterableFields(catalog, tile.source),
267
+ [fieldsBySource, catalog],
268
+ );
269
+ // Bindings a tile's source cannot take, per control — a field it does not
270
+ // have, or one of a type the given cannot compare: marked on the chip, so a
271
+ // broken binding is seen before the package refuses it.
272
+ const unknownFieldsOf = (
273
+ name: string,
274
+ type: string | undefined,
275
+ ): string[] => {
276
+ const out: string[] = [];
277
+ for (const tile of editor.document.tiles) {
278
+ const known = fieldsFor(tile);
279
+ if (!known) continue;
280
+ const types = new Map(known.map((field) => [field.name, field.type]));
281
+ for (const filter of tile.filters ?? []) {
282
+ if (filter.given !== name) continue;
283
+ if (
284
+ !types.has(filter.field) ||
285
+ !acceptsField(type, types.get(filter.field))
286
+ )
287
+ out.push(
288
+ `${filter.field} on ${tile.label ?? tile.name} (${
289
+ editor.document.sources.find((s) => s.name === tile.source)
290
+ ?.base ?? tile.source
291
+ })`,
292
+ );
293
+ }
294
+ }
295
+ return out;
296
+ };
297
+ // Model givens nothing binds yet: what "From the model" offers.
298
+ const available = useMemo(
299
+ () =>
300
+ controlList.filter((c) => c.origin === "model" && c.boundTiles === 0),
301
+ [controlList],
302
+ );
303
+
304
+ const commitSave = useCallback(() => {
305
+ setPendingSave(undefined);
306
+ setSaving(true);
307
+ const started = now();
308
+ const { structural } = editor;
309
+ const tiles = editor.document.tiles.length;
310
+ void editor
311
+ .save()
312
+ .then((outcome) => {
313
+ if (outcome.ok === true)
314
+ onEvent?.({
315
+ type: "dashboard.saved",
316
+ tiles,
317
+ structural,
318
+ durationMs: now() - started,
319
+ });
320
+ else
321
+ onEvent?.({
322
+ type: "dashboard.save_refused",
323
+ reason: outcome.reason,
324
+ });
325
+ })
326
+ .finally(() => setSaving(false));
327
+ }, [editor, onEvent]);
328
+ const save = useCallback(() => {
329
+ if (!onSave || !editor.dirty || saving) return;
330
+ if (!editor.structural) {
331
+ commitSave();
332
+ return;
333
+ }
334
+ // A tile was added or removed: show what that does to the file first.
335
+ void editor.preview().then((result) => {
336
+ if (result.ok)
337
+ setPendingSave({ before: editor.source, after: result.source });
338
+ // A refusal surfaces through the same path a save's would.
339
+ else commitSave();
340
+ });
341
+ }, [onSave, editor, saving, commitSave]);
342
+
343
+ /** A tile from the picker: on the extension of its source, or a new one. */
344
+ const addTile = (tile: NewTile) => {
345
+ setAddingTile(false);
346
+ editor.update((draft) => {
347
+ let extension = draft.sources.find((s) => s.base === tile.base);
348
+ if (!extension) {
349
+ // A name of the file's own: the base's, suffixed, since an
350
+ // extension cannot share its base's name.
351
+ const taken = new Set(draft.sources.map((s) => s.name));
352
+ let name = `${tile.base}_tiles`;
353
+ for (let n = 2; taken.has(name); n++)
354
+ name = `${tile.base}_tiles_${n}`;
355
+ extension = { name, base: tile.base };
356
+ draft.sources.push(extension);
357
+ }
358
+ // The view's name in the extension: the base view's, suffixed,
359
+ // because an extension inherits its base's views and cannot redeclare
360
+ // one under the same name; then kept distinct from its siblings.
361
+ const used = new Set(
362
+ draft.tiles
363
+ .filter((t) => t.source === extension!.name)
364
+ .map((t) => t.name),
365
+ );
366
+ let name = `${tile.view}_tile`;
367
+ for (let n = 2; used.has(name); n++) name = `${tile.view}_tile_${n}`;
368
+ draft.tiles.push({
369
+ name,
370
+ source: extension.name,
371
+ declaration: { kind: "reference", from: tile.view },
372
+ colspan: tile.colspan,
373
+ ...(tile.label ? { label: tile.label } : {}),
374
+ });
375
+ });
376
+ setSelected(editor.document.tiles.length);
377
+ };
378
+
379
+ /** Every tile this file owns, to one fraction of the grid. */
380
+ const quickLayout = (share: 1 | 2 | 3 | 4) => {
381
+ const span = Math.max(1, Math.round(columns / share));
382
+ editor.update((draft) => {
383
+ for (const tile of draft.tiles)
384
+ if (tile.declaration.kind !== "inherited") tile.colspan = span;
385
+ });
386
+ };
387
+
388
+ const removeTile = (index: number) => {
389
+ setMenu(undefined);
390
+ setSelected(undefined);
391
+ editor.update((draft) => {
392
+ draft.tiles.splice(index, 1);
393
+ });
394
+ };
395
+
396
+ useBuilderShortcuts(
397
+ // One handlers object per change of what they read, so the key listener
398
+ // is not torn down and re-bound on every render.
399
+ useMemo(
400
+ () => ({
401
+ undo: editor.undo,
402
+ redo: editor.redo,
403
+ ...(onSave ? { save } : {}),
404
+ // Escape drops the selection — unless the menu or the filter
405
+ // window is open, in which case the key is theirs and they close
406
+ // on it themselves.
407
+ escape: () => {
408
+ if (!menu && !filterDialog) setSelected(undefined);
409
+ },
410
+ nudge: (delta: 1 | -1) => {
411
+ if (selected === undefined) return;
412
+ const tile = editor.document.tiles[selected];
413
+ if (!tile || tile.declaration.kind === "inherited") return;
414
+ const span = Math.min(
415
+ Math.max((tile.colspan ?? 1) + delta, 1),
416
+ columns,
417
+ );
418
+ if (span === (tile.colspan ?? 1)) return;
419
+ editor.update((draft) => {
420
+ draft.tiles[selected].colspan = span;
421
+ });
422
+ },
423
+ }),
424
+ [editor, onSave, save, menu, filterDialog, selected, columns],
425
+ ),
426
+ );
427
+
428
+ /** The filter window's result: bind, and declare when it is new or retagged. */
429
+ const applyFilter = (
430
+ given: string,
431
+ rows: MappingRow[],
432
+ declare?: LocalGiven,
433
+ ) => {
434
+ setFilterDialog(undefined);
435
+ // One history entry for the whole filter, however many tiles it touches.
436
+ editor.update((draft) => {
437
+ if (declare) declareControl(draft, declare);
438
+ applyMapping(draft, given, rows);
439
+ });
440
+ };
441
+
442
+ /** Take a control off the dashboard: its declaration, if ours, and every binding. */
443
+ const dropControl = (given: string) => {
444
+ setFilterDialog(undefined);
445
+ editor.update((draft) => removeControl(draft, given));
446
+ };
447
+
448
+ // What the grid lays out: the document, except mid-gesture, where it is
449
+ // the preview — the tile being resized at its previewed width, or the tiles
450
+ // in their previewed order. So the row reflows under the pointer exactly as
451
+ // it will once the edit lands.
452
+ const shown = (() => {
453
+ const tiles = editor.document.tiles;
454
+ if (resize !== undefined)
455
+ return tiles.map((each, index) =>
456
+ index === resize.index ? { ...each, colspan: resize.span } : each,
457
+ );
458
+ return preview ?? tiles;
459
+ })();
460
+ // And, while a drag is live, the empty end of every row as a drop target.
461
+ // Not otherwise: a gap is only a place to land while something is in hand.
462
+ const entries = dragging ? withGaps(shown, columns) : shown.map(tileEntry);
463
+ return (
464
+ // The padding is for the SELECTION RING. An outline is painted outside
465
+ // the element's border box, so a selected tile's ring lands beyond the
466
+ // grid — and a host that scrolls this surface clips it: `overflow-y:
467
+ // auto` computes `overflow-x` to `auto` as well, so the ring's left and
468
+ // right edges were cut off against the scroll box while its top and
469
+ // bottom showed. Reserved HERE, on the whole surface, rather than on the
470
+ // grid alone, so the header, the control row and the tiles all inset
471
+ // together and stay aligned with each other. 4px = the ring's 2px offset
472
+ // plus its 2px width.
473
+ <Stack sx={{ gap: 2, p: "4px" }}>
474
+ {/* The reader's own header, from the component the dashboard renders:
475
+ the surface whose job is showing the author what a reader gets. */}
476
+ <BuilderToolbar
477
+ canUndo={editor.canUndo}
478
+ canRedo={editor.canRedo}
479
+ onUndo={editor.undo}
480
+ onRedo={editor.redo}
481
+ dirty={editor.dirty}
482
+ saving={saving}
483
+ {...(onSave ? { onSave: save } : {})}
484
+ {...(toolbar ? { actions: toolbar } : {})}
485
+ {...(catalog ? { onAddTile: () => setAddingTile(true) } : {})}
486
+ onSettings={setSettingsAnchor}
487
+ onQuickLayout={quickLayout}
488
+ />
489
+
490
+ <DashboardProse
491
+ title={editor.document.title || "Untitled dashboard"}
492
+ {...(editor.document.description
493
+ ? { description: editor.document.description }
494
+ : {})}
495
+ />
496
+
497
+ <FilterStrip
498
+ controls={controlList}
499
+ tileCount={editor.document.tiles.length}
500
+ unknownFieldsOf={unknownFieldsOf}
501
+ onEdit={(control) => setFilterDialog({ control })}
502
+ onAdd={() => setFilterDialog({})}
503
+ >
504
+ {controls}
505
+ </FilterStrip>
506
+
507
+ {editor.error && (
508
+ // The edit is still here; the message says what stopped it reaching
509
+ // the file, which is a different thing from losing the work.
510
+ <Alert severity="warning">{editor.error}</Alert>
511
+ )}
512
+
513
+ <DragDropProvider
514
+ sensors={builderSensors}
515
+ onDragStart={onDragStart}
516
+ onDragOver={onDragOver}
517
+ onDragEnd={onDragEnd}
518
+ >
519
+ <Box ref={gridBox} sx={{ position: "relative" }}>
520
+ {(resize !== undefined || dragging) && (
521
+ <GridGuides columns={columns} />
522
+ )}
523
+
524
+ <DashboardGrid
525
+ tiles={entries}
526
+ columns={columns}
527
+ keyOf={(entry) =>
528
+ entry.kind === "gap"
529
+ ? gapId(entry.after)
530
+ : tileKey(entry.tile)
531
+ }
532
+ renderTile={(entry) => {
533
+ if (entry.kind === "gap")
534
+ return <GapTarget after={entry.after} />;
535
+ const { tile: each, index } = entry;
536
+ return (
537
+ <TileFrame
538
+ tile={each}
539
+ index={index}
540
+ selected={index === selected}
541
+ menuOpen={menu?.index === index}
542
+ resizeSpan={
543
+ resize?.index === index ? resize.span : undefined
544
+ }
545
+ columns={columns}
546
+ onSelect={() => setSelected(index)}
547
+ onOpenMenu={(anchor) => {
548
+ setSelected(index);
549
+ setMenu({ anchor, index });
550
+ }}
551
+ onResizeStart={(event) => startResize(event, index)}
552
+ onResizeMove={onResize}
553
+ onResizeEnd={endResize}
554
+ >
555
+ {renderTile ? (
556
+ renderTile(each)
557
+ ) : (
558
+ <TilePlaceholder tile={each} />
559
+ )}
560
+ </TileFrame>
561
+ );
562
+ }}
563
+ />
564
+ </Box>
565
+ </DragDropProvider>
566
+ <FilterDialog
567
+ open={filterDialog !== undefined}
568
+ document={editor.document}
569
+ {...(filterDialog?.control
570
+ ? { control: filterDialog.control }
571
+ : {})}
572
+ available={available}
573
+ {...(catalog ? { fieldsFor } : {})}
574
+ onClose={() => setFilterDialog(undefined)}
575
+ onApply={applyFilter}
576
+ onRemove={dropControl}
577
+ />
578
+ <TileMenu
579
+ anchor={menu?.anchor ?? null}
580
+ tile={
581
+ menu === undefined
582
+ ? undefined
583
+ : editor.document.tiles[menu.index]
584
+ }
585
+ onClose={() => setMenu(undefined)}
586
+ onCommit={(next) => {
587
+ const at = menu?.index;
588
+ if (at === undefined) return;
589
+ editor.update((draft) => {
590
+ draft.tiles[at] = next;
591
+ });
592
+ }}
593
+ onRemove={() => {
594
+ if (menu !== undefined) removeTile(menu.index);
595
+ }}
596
+ columns={columns}
597
+ onDrills={() => {
598
+ if (menu !== undefined)
599
+ setDrillSource(editor.document.tiles[menu.index]?.source);
600
+ }}
601
+ />
602
+ <DrillDialog
603
+ open={drillSource !== undefined}
604
+ document={editor.document}
605
+ source={editor.document.sources.find((s) => s.name === drillSource)}
606
+ givenNames={controlList.map((c) => c.name)}
607
+ dashboards={dashboards ?? []}
608
+ onClose={() => setDrillSource(undefined)}
609
+ onApply={(drills) =>
610
+ editor.update((draft) => {
611
+ const kept = (draft.drills ?? []).filter(
612
+ (d) => d.source !== drillSource,
613
+ );
614
+ const next = [...kept, ...drills];
615
+ if (next.length === 0) delete draft.drills;
616
+ else draft.drills = next;
617
+ })
618
+ }
619
+ />
620
+ <SettingsPopover
621
+ anchor={settingsAnchor}
622
+ settings={settings}
623
+ onClose={() => setSettingsAnchor(null)}
624
+ onCommit={(next) =>
625
+ editor.update((draft) => {
626
+ draft.title = next.title;
627
+ if (next.description === undefined) delete draft.description;
628
+ else draft.description = next.description;
629
+ if (next.columns === undefined) delete draft.columns;
630
+ else draft.columns = next.columns;
631
+ if (next.autorun === undefined) delete draft.autorun;
632
+ else draft.autorun = next.autorun;
633
+ })
634
+ }
635
+ />
636
+ <AddTileDialog
637
+ open={addingTile}
638
+ document={editor.document}
639
+ catalog={catalog}
640
+ columns={columns}
641
+ onClose={() => setAddingTile(false)}
642
+ onAdd={addTile}
643
+ />
644
+ <DiffDialog
645
+ open={pendingSave !== undefined}
646
+ before={pendingSave?.before ?? ""}
647
+ after={pendingSave?.after ?? ""}
648
+ onConfirm={commitSave}
649
+ onClose={() => setPendingSave(undefined)}
650
+ />
651
+ </Stack>
652
+ );
653
+ }