@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,666 @@
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
+ * Where `onSave` puts the file, for the event it reports. The builder
151
+ * cannot tell — it is handed a function — and a save into the package and a
152
+ * save into this browser are not the same event.
153
+ */
154
+ savesTo?: "package" | "browser";
155
+ /**
156
+ * The host's own actions for the edit bar — Done — rendered beside
157
+ * undo, redo and save. The builder owns the edits; where the file goes
158
+ * afterwards is the host's, so its buttons sit in the host's slot.
159
+ */
160
+ toolbar?: ReactNode;
161
+ }
162
+
163
+ export function DashboardBuilder({
164
+ source,
165
+ document,
166
+ onSave,
167
+ onChange,
168
+ renderTile,
169
+ controls,
170
+ givens,
171
+ catalog,
172
+ toolbar,
173
+ dashboards,
174
+ onEvent,
175
+ savesTo = "package",
176
+ }: DashboardBuilderProps) {
177
+ const editor = useDashboardEditor({
178
+ source,
179
+ document,
180
+ ...(onSave ? { onSave } : {}),
181
+ });
182
+ const [selected, setSelected] = useState<number | undefined>(undefined);
183
+ const [saving, setSaving] = useState(false);
184
+ const columns = editor.document.columns ?? DEFAULT_COLUMNS;
185
+ const { resize, gridBox, startResize, onResize, endResize } = useTileResize({
186
+ tiles: editor.document.tiles,
187
+ columns,
188
+ onStart: setSelected,
189
+ commit: (index, span) =>
190
+ editor.update((draft) => {
191
+ draft.tiles[index].colspan = span;
192
+ }),
193
+ });
194
+ const { dragging, preview, onDragStart, onDragOver, onDragEnd } =
195
+ useTileReorder({
196
+ tiles: editor.document.tiles,
197
+ commit: (next) =>
198
+ editor.update((draft) => {
199
+ draft.tiles = next;
200
+ }),
201
+ onLanded: setSelected,
202
+ });
203
+ // The filter window: open on a control, or open to add one.
204
+ const [filterDialog, setFilterDialog] = useState<
205
+ { control?: BuilderControl } | undefined
206
+ >(undefined);
207
+ // A tile's menu, anchored to the button that opened it.
208
+ const [menu, setMenu] = useState<
209
+ { anchor: HTMLElement; index: number } | undefined
210
+ >(undefined);
211
+ // The add-tile picker, and the diff a structural save shows first.
212
+ const [addingTile, setAddingTile] = useState(false);
213
+ // The clickable-cells window, for one tile's source.
214
+ const [drillSource, setDrillSource] = useState<string | undefined>(
215
+ undefined,
216
+ );
217
+ // The page's settings, anchored to the toolbar button that opened them.
218
+ const [settingsAnchor, setSettingsAnchor] = useState<HTMLElement | null>(
219
+ null,
220
+ );
221
+ const [pendingSave, setPendingSave] = useState<
222
+ { before: string; after: string } | undefined
223
+ >(undefined);
224
+
225
+ useEffect(() => {
226
+ onChange?.(editor.document);
227
+ }, [editor.document, onChange]);
228
+ // One object per document, or the popover's draft would reset on every
229
+ // render of the builder while it is open.
230
+ const settings = useMemo(
231
+ () => settingsOf(editor.document),
232
+ [editor.document],
233
+ );
234
+
235
+ // The givens the MODEL offers: the caller's list, less any the opened file
236
+ // declared itself. A caller gets that list from the server's manifest, which
237
+ // resolves givens across the file and its imports without saying which is
238
+ // which — so it names this file's own declarations too, and keeps naming a
239
+ // control after this document removes it, until a save is written and the
240
+ // package reloads. Without this, "Remove from dashboard" took the chip off
241
+ // and it came straight back, faint, labelled "from the model".
242
+ const modelGivens = useMemo(() => {
243
+ const ownDeclarations = new Set(
244
+ (document.localGivens ?? []).map((given) => given.name),
245
+ );
246
+ return (givens ?? []).filter((given) => !ownDeclarations.has(given.name));
247
+ }, [document, givens]);
248
+ // Every control the builder can offer: this file's own, then the model's.
249
+ const controlList = useMemo(
250
+ () => controlsOf(editor.document, modelGivens),
251
+ [editor.document, modelGivens],
252
+ );
253
+ // The fields a binding may name, PER SOURCE: the dimensions of the model
254
+ // source each of this file's extensions is built on, when the host knows
255
+ // them. A composite spans sources, so one list would call a field the second
256
+ // source has "unknown" and block the window on it. Undefined for a source
257
+ // the catalog does not have, and nothing checks that source's tiles.
258
+ const fieldsBySource = useMemo(
259
+ () =>
260
+ new Map(
261
+ editor.document.sources.map((source) => [
262
+ source.name,
263
+ filterableFields(catalog, source.base),
264
+ ]),
265
+ ),
266
+ [catalog, editor.document.sources],
267
+ );
268
+ const fieldsFor = useCallback(
269
+ (tile: DashboardTile) =>
270
+ fieldsBySource.get(tile.source) ??
271
+ // A tile on an imported source with no extension of its own: its
272
+ // source IS a model source, and may be in the catalog directly.
273
+ filterableFields(catalog, tile.source),
274
+ [fieldsBySource, catalog],
275
+ );
276
+ // Bindings a tile's source cannot take, per control — a field it does not
277
+ // have, or one of a type the given cannot compare: marked on the chip, so a
278
+ // broken binding is seen before the package refuses it.
279
+ const unknownFieldsOf = (
280
+ name: string,
281
+ type: string | undefined,
282
+ ): string[] => {
283
+ const out: string[] = [];
284
+ for (const tile of editor.document.tiles) {
285
+ const known = fieldsFor(tile);
286
+ if (!known) continue;
287
+ const types = new Map(known.map((field) => [field.name, field.type]));
288
+ for (const filter of tile.filters ?? []) {
289
+ if (filter.given !== name) continue;
290
+ if (
291
+ !types.has(filter.field) ||
292
+ !acceptsField(type, types.get(filter.field))
293
+ )
294
+ out.push(
295
+ `${filter.field} on ${tile.label ?? tile.name} (${
296
+ editor.document.sources.find((s) => s.name === tile.source)
297
+ ?.base ?? tile.source
298
+ })`,
299
+ );
300
+ }
301
+ }
302
+ return out;
303
+ };
304
+ // Model givens nothing binds yet: what "From the model" offers.
305
+ const available = useMemo(
306
+ () =>
307
+ controlList.filter((c) => c.origin === "model" && c.boundTiles === 0),
308
+ [controlList],
309
+ );
310
+
311
+ const commitSave = useCallback(() => {
312
+ setPendingSave(undefined);
313
+ setSaving(true);
314
+ const started = now();
315
+ const { structural } = editor;
316
+ const tiles = editor.document.tiles.length;
317
+ void editor
318
+ .save()
319
+ .then((outcome) => {
320
+ if (outcome.ok === true)
321
+ onEvent?.({
322
+ type: "dashboard.saved",
323
+ tiles,
324
+ structural,
325
+ where: savesTo,
326
+ durationMs: now() - started,
327
+ });
328
+ else
329
+ onEvent?.({
330
+ type: "dashboard.save_refused",
331
+ reason: outcome.reason,
332
+ });
333
+ })
334
+ .finally(() => setSaving(false));
335
+ }, [editor, onEvent, savesTo]);
336
+ const save = useCallback(() => {
337
+ if (!onSave || !editor.dirty || saving) return;
338
+ if (!editor.structural) {
339
+ commitSave();
340
+ return;
341
+ }
342
+ // A tile was added or removed: show what that does to the file first.
343
+ void editor.preview().then((result) => {
344
+ if (result.ok)
345
+ setPendingSave({ before: editor.source, after: result.source });
346
+ // A refusal surfaces through the same path a save's would.
347
+ else commitSave();
348
+ });
349
+ }, [onSave, editor, saving, commitSave]);
350
+
351
+ /** A tile from the picker: on the extension of its source, or a new one. */
352
+ const addTile = (tile: NewTile) => {
353
+ setAddingTile(false);
354
+ editor.update((draft) => {
355
+ let extension = draft.sources.find((s) => s.base === tile.base);
356
+ if (!extension) {
357
+ // A name of the file's own: the base's, suffixed, since an
358
+ // extension cannot share its base's name.
359
+ const taken = new Set(draft.sources.map((s) => s.name));
360
+ let name = `${tile.base}_tiles`;
361
+ for (let n = 2; taken.has(name); n++)
362
+ name = `${tile.base}_tiles_${n}`;
363
+ extension = { name, base: tile.base };
364
+ draft.sources.push(extension);
365
+ }
366
+ // The view's name in the extension: the base view's, suffixed,
367
+ // because an extension inherits its base's views and cannot redeclare
368
+ // one under the same name; then kept distinct from its siblings.
369
+ const used = new Set(
370
+ draft.tiles
371
+ .filter((t) => t.source === extension!.name)
372
+ .map((t) => t.name),
373
+ );
374
+ let name = `${tile.view}_tile`;
375
+ for (let n = 2; used.has(name); n++) name = `${tile.view}_tile_${n}`;
376
+ draft.tiles.push({
377
+ name,
378
+ source: extension.name,
379
+ declaration: { kind: "reference", from: tile.view },
380
+ colspan: tile.colspan,
381
+ ...(tile.label ? { label: tile.label } : {}),
382
+ });
383
+ });
384
+ setSelected(editor.document.tiles.length);
385
+ };
386
+
387
+ const removeTile = (index: number) => {
388
+ setMenu(undefined);
389
+ setSelected(undefined);
390
+ editor.update((draft) => {
391
+ draft.tiles.splice(index, 1);
392
+ });
393
+ };
394
+
395
+ useBuilderShortcuts(
396
+ // One handlers object per change of what they read, so the key listener
397
+ // is not torn down and re-bound on every render.
398
+ useMemo(
399
+ () => ({
400
+ undo: editor.undo,
401
+ redo: editor.redo,
402
+ ...(onSave ? { save } : {}),
403
+ // Escape drops the selection — unless the menu or the filter
404
+ // window is open, in which case the key is theirs and they close
405
+ // on it themselves.
406
+ escape: () => {
407
+ if (!menu && !filterDialog) setSelected(undefined);
408
+ },
409
+ nudge: (delta: 1 | -1) => {
410
+ if (selected === undefined) return;
411
+ const tile = editor.document.tiles[selected];
412
+ if (!tile || tile.declaration.kind === "inherited") return;
413
+ const span = Math.min(
414
+ Math.max((tile.colspan ?? 1) + delta, 1),
415
+ columns,
416
+ );
417
+ if (span === (tile.colspan ?? 1)) return;
418
+ editor.update((draft) => {
419
+ draft.tiles[selected].colspan = span;
420
+ });
421
+ },
422
+ }),
423
+ [editor, onSave, save, menu, filterDialog, selected, columns],
424
+ ),
425
+ );
426
+
427
+ /** The filter window's result: bind, and declare when it is new or retagged. */
428
+ const applyFilter = (
429
+ given: string,
430
+ rows: MappingRow[],
431
+ declare?: LocalGiven,
432
+ ) => {
433
+ setFilterDialog(undefined);
434
+ // One history entry for the whole filter, however many tiles it touches.
435
+ editor.update((draft) => {
436
+ if (declare) declareControl(draft, declare);
437
+ applyMapping(draft, given, rows);
438
+ });
439
+ };
440
+
441
+ /** Take a control off the dashboard: its declaration, if ours, and every binding. */
442
+ const dropControl = (given: string) => {
443
+ setFilterDialog(undefined);
444
+ editor.update((draft) => removeControl(draft, given));
445
+ };
446
+
447
+ // What the grid lays out: the document, except mid-gesture, where it is
448
+ // the preview — the tile being resized at its previewed width, or the tiles
449
+ // in their previewed order. So the row reflows under the pointer exactly as
450
+ // it will once the edit lands.
451
+ const shown = (() => {
452
+ const tiles = editor.document.tiles;
453
+ if (resize !== undefined)
454
+ return tiles.map((each, index) =>
455
+ index === resize.index ? { ...each, colspan: resize.span } : each,
456
+ );
457
+ return preview ?? tiles;
458
+ })();
459
+ // And, while a drag is live, the empty end of every row as a drop target.
460
+ // Not otherwise: a gap is only a place to land while something is in hand.
461
+ const entries = dragging ? withGaps(shown, columns) : shown.map(tileEntry);
462
+ return (
463
+ // The padding is for the SELECTION RING. An outline is painted outside
464
+ // the element's border box, so a selected tile's ring lands beyond the
465
+ // grid — and a host that scrolls this surface clips it: `overflow-y:
466
+ // auto` computes `overflow-x` to `auto` as well, so the ring's left and
467
+ // right edges were cut off against the scroll box while its top and
468
+ // bottom showed. Reserved HERE, on the whole surface, rather than on the
469
+ // grid alone, so the header, the control row and the tiles all inset
470
+ // together and stay aligned with each other. 4px = the ring's 2px offset
471
+ // plus its 2px width.
472
+ // The bar carries its own gap to what it sits over, so the surface adds
473
+ // none between the two.
474
+ <Stack sx={{ gap: 0 }}>
475
+ {/* Outside the ring's inset, so the bar lines up to the pixel with the
476
+ reader's — the whole point of it being the same bar. */}
477
+ <BuilderToolbar
478
+ canUndo={editor.canUndo}
479
+ canRedo={editor.canRedo}
480
+ onUndo={editor.undo}
481
+ onRedo={editor.redo}
482
+ dirty={editor.dirty}
483
+ saving={saving}
484
+ {...(onSave ? { onSave: save } : {})}
485
+ {...(toolbar ? { actions: toolbar } : {})}
486
+ {...(catalog ? { onAddTile: () => setAddingTile(true) } : {})}
487
+ onSettings={setSettingsAnchor}
488
+ />
489
+
490
+ {/* The ring's inset, minus the top: nothing at the top of this stack
491
+ can be selected (the prose and the control row are not tiles), and
492
+ 4px there would put the title 4px further from the bar than the
493
+ reader's is. */}
494
+ <Stack sx={{ gap: 2, px: "4px", pb: "4px" }}>
495
+ <DashboardProse
496
+ title={editor.document.title || "Untitled dashboard"}
497
+ {...(editor.document.description
498
+ ? { description: editor.document.description }
499
+ : {})}
500
+ />
501
+
502
+ <FilterStrip
503
+ controls={controlList}
504
+ tileCount={editor.document.tiles.length}
505
+ unknownFieldsOf={unknownFieldsOf}
506
+ onEdit={(control) => setFilterDialog({ control })}
507
+ onAdd={() => setFilterDialog({})}
508
+ >
509
+ {controls}
510
+ </FilterStrip>
511
+
512
+ {editor.error && (
513
+ // The edit is still here; the message says what stopped it reaching
514
+ // the file, which is a different thing from losing the work.
515
+ <Alert severity="warning">{editor.error}</Alert>
516
+ )}
517
+
518
+ <DragDropProvider
519
+ sensors={builderSensors}
520
+ onDragStart={onDragStart}
521
+ onDragOver={onDragOver}
522
+ onDragEnd={onDragEnd}
523
+ >
524
+ <Box ref={gridBox} sx={{ position: "relative" }}>
525
+ {(resize !== undefined || dragging) && (
526
+ <GridGuides columns={columns} />
527
+ )}
528
+
529
+ <DashboardGrid
530
+ tiles={entries}
531
+ columns={columns}
532
+ keyOf={(entry) =>
533
+ entry.kind === "gap"
534
+ ? gapId(entry.after)
535
+ : tileKey(entry.tile)
536
+ }
537
+ renderTile={(entry) => {
538
+ if (entry.kind === "gap")
539
+ return <GapTarget after={entry.after} />;
540
+ const { tile: each, index } = entry;
541
+ return (
542
+ <TileFrame
543
+ tile={each}
544
+ index={index}
545
+ selected={index === selected}
546
+ menuOpen={menu?.index === index}
547
+ resizeSpan={
548
+ resize?.index === index
549
+ ? resize.span
550
+ : undefined
551
+ }
552
+ columns={columns}
553
+ onSelect={() => setSelected(index)}
554
+ onOpenMenu={(anchor) => {
555
+ setSelected(index);
556
+ setMenu({ anchor, index });
557
+ }}
558
+ onResizeStart={(event) =>
559
+ startResize(event, index)
560
+ }
561
+ onResizeMove={onResize}
562
+ onResizeEnd={endResize}
563
+ >
564
+ {renderTile ? (
565
+ renderTile(each)
566
+ ) : (
567
+ <TilePlaceholder tile={each} />
568
+ )}
569
+ </TileFrame>
570
+ );
571
+ }}
572
+ />
573
+ </Box>
574
+ </DragDropProvider>
575
+ <FilterDialog
576
+ open={filterDialog !== undefined}
577
+ document={editor.document}
578
+ {...(filterDialog?.control
579
+ ? { control: filterDialog.control }
580
+ : {})}
581
+ available={available}
582
+ {...(catalog ? { fieldsFor } : {})}
583
+ onClose={() => setFilterDialog(undefined)}
584
+ onApply={applyFilter}
585
+ onRemove={dropControl}
586
+ />
587
+ <TileMenu
588
+ anchor={menu?.anchor ?? null}
589
+ tile={
590
+ menu === undefined
591
+ ? undefined
592
+ : editor.document.tiles[menu.index]
593
+ }
594
+ onClose={() => setMenu(undefined)}
595
+ onCommit={(next) => {
596
+ const at = menu?.index;
597
+ if (at === undefined) return;
598
+ editor.update((draft) => {
599
+ draft.tiles[at] = next;
600
+ });
601
+ }}
602
+ onRemove={() => {
603
+ if (menu !== undefined) removeTile(menu.index);
604
+ }}
605
+ columns={columns}
606
+ onDrills={() => {
607
+ if (menu !== undefined)
608
+ setDrillSource(editor.document.tiles[menu.index]?.source);
609
+ }}
610
+ />
611
+ <DrillDialog
612
+ open={drillSource !== undefined}
613
+ document={editor.document}
614
+ source={editor.document.sources.find(
615
+ (s) => s.name === drillSource,
616
+ )}
617
+ givenNames={controlList.map((c) => c.name)}
618
+ dashboards={dashboards ?? []}
619
+ onClose={() => setDrillSource(undefined)}
620
+ onApply={(drills) =>
621
+ editor.update((draft) => {
622
+ const kept = (draft.drills ?? []).filter(
623
+ (d) => d.source !== drillSource,
624
+ );
625
+ const next = [...kept, ...drills];
626
+ if (next.length === 0) delete draft.drills;
627
+ else draft.drills = next;
628
+ })
629
+ }
630
+ />
631
+ <SettingsPopover
632
+ anchor={settingsAnchor}
633
+ settings={settings}
634
+ onClose={() => setSettingsAnchor(null)}
635
+ onCommit={(next) =>
636
+ editor.update((draft) => {
637
+ draft.title = next.title;
638
+ if (next.description === undefined)
639
+ delete draft.description;
640
+ else draft.description = next.description;
641
+ if (next.columns === undefined) delete draft.columns;
642
+ else draft.columns = next.columns;
643
+ if (next.autorun === undefined) delete draft.autorun;
644
+ else draft.autorun = next.autorun;
645
+ })
646
+ }
647
+ />
648
+ <AddTileDialog
649
+ open={addingTile}
650
+ document={editor.document}
651
+ catalog={catalog}
652
+ columns={columns}
653
+ onClose={() => setAddingTile(false)}
654
+ onAdd={addTile}
655
+ />
656
+ <DiffDialog
657
+ open={pendingSave !== undefined}
658
+ before={pendingSave?.before ?? ""}
659
+ after={pendingSave?.after ?? ""}
660
+ onConfirm={commitSave}
661
+ onClose={() => setPendingSave(undefined)}
662
+ />
663
+ </Stack>
664
+ </Stack>
665
+ );
666
+ }