@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,115 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { move } from "@dnd-kit/helpers";
5
+ import type { DragEndEvent, DragOverEvent } from "@dnd-kit/react";
6
+ import { useRef, useState } from "react";
7
+ import { tileKey, type DashboardTile } from "./document";
8
+ import { keepRowStructure, moveIntoGap } from "./layout";
9
+ import { GAP_TYPE, type GapData } from "./sortable";
10
+
11
+ /**
12
+ * Dragging a tile to reorder it. The gesture is `@dnd-kit/react`'s (see
13
+ * `sortable.tsx`); what is decided here is what a drop means for the file,
14
+ * through `keepRowStructure` for a drop onto a tile and `moveIntoGap` for a
15
+ * drop into the empty end of a row. The preview is rebuilt on each report and
16
+ * written ONCE, on release: one history entry for the whole drag.
17
+ */
18
+ export function useTileReorder({
19
+ tiles,
20
+ commit,
21
+ onLanded,
22
+ }: {
23
+ tiles: DashboardTile[];
24
+ /** The order to write when the drag ends somewhere new. */
25
+ commit: (next: DashboardTile[]) => void;
26
+ /** Where the dragged tile came to rest, which the caller selects. */
27
+ onLanded: (index: number) => void;
28
+ }) {
29
+ // A move in flight: the tiles as they will stand if the drag ends now.
30
+ // CUMULATIVE — each report moves the tile from where the last report left
31
+ // it, which is the sortable convention and what the library's optimistic
32
+ // sorting assumes; rebuilt from the document on every report, the target
33
+ // flips back and forth as the row reflows under the pointer. State for the
34
+ // render, and a ref for `onDragEnd`, which can fire before React has
35
+ // committed it.
36
+ const [preview, setPreview] = useState<DashboardTile[] | undefined>(
37
+ undefined,
38
+ );
39
+ const previewRef = useRef<DashboardTile[] | undefined>(undefined);
40
+ // Whether a drag is live at all: what turns the row-end gaps into drop
41
+ // targets and draws the grid guides.
42
+ const [dragging, setDragging] = useState(false);
43
+
44
+ /**
45
+ * Reordering, unlike resizing, is offered on EVERY tile — an inherited one
46
+ * included.
47
+ *
48
+ * The two edits touch different parts of the file. A width is a `# colspan`
49
+ * tag on the view, so a tile whose view lives in the model cannot be
50
+ * resized here; but order is the `tiles=[…]` array on this file's own
51
+ * `## artifact` tag, which this file always owns. So a tile the properties
52
+ * panel refuses can still be moved.
53
+ *
54
+ * The gesture itself is `@dnd-kit/react`'s — see `sortable.tsx`. What is
55
+ * decided HERE is what a drop means for the file. The library reports which
56
+ * target the tile is over; these handlers turn that into an order and a set
57
+ * of row starts, through `keepRowStructure` for a drop onto a tile and
58
+ * `moveIntoGap` for a drop into the empty end of a row. The preview is
59
+ * rebuilt from the document on each report and written once, on release:
60
+ * ONE history entry for the whole drag, however far it wandered.
61
+ */
62
+ const onDragStart = () => {
63
+ setDragging(true);
64
+ previewRef.current = tiles;
65
+ };
66
+
67
+ const onDragOver = (event: DragOverEvent) => {
68
+ const { source, target } = event.operation;
69
+ if (!source || !target) return;
70
+ const current = previewRef.current ?? tiles;
71
+ let next: DashboardTile[];
72
+ if (target.type === GAP_TYPE) {
73
+ const { after } = target.data as GapData;
74
+ const from = current.findIndex((tile) => tileKey(tile) === source.id);
75
+ // The gap right after the dragged tile is the one it is already
76
+ // previewed in; there is nothing to change.
77
+ if (from < 0 || after === source.id) return;
78
+ const rest = current.filter((_, index) => index !== from);
79
+ const to = rest.findIndex((tile) => tileKey(tile) === after) + 1;
80
+ next = moveIntoGap(current, from, to);
81
+ } else {
82
+ // Onto a tile: the library's own `move` — the same arithmetic every
83
+ // sortable list built on it uses, over the order as it stands. Then
84
+ // the row starts re-applied by position, so the rows keep their shape
85
+ // and the tiles flow through them.
86
+ const keys = current.map(tileKey);
87
+ const reordered = move(keys, event);
88
+ if (reordered.every((key, index) => key === keys[index])) return;
89
+ const byKey = new Map(current.map((tile) => [tileKey(tile), tile]));
90
+ next = keepRowStructure(
91
+ reordered.map((key) => byKey.get(key) as DashboardTile),
92
+ tiles.map((tile) => tile.break ?? false),
93
+ );
94
+ }
95
+ previewRef.current = next;
96
+ setPreview(next);
97
+ };
98
+
99
+ const onDragEnd = (event: DragEndEvent) => {
100
+ setDragging(false);
101
+ const next = previewRef.current;
102
+ previewRef.current = undefined;
103
+ setPreview(undefined);
104
+ if (event.canceled || !next) return;
105
+ // ONE history entry for the whole drag; a drag that ends where it began
106
+ // changes nothing and makes none.
107
+ commit(next);
108
+ const landing = next.findIndex(
109
+ (tile) => tileKey(tile) === event.operation.source?.id,
110
+ );
111
+ if (landing >= 0) onLanded(landing);
112
+ };
113
+
114
+ return { dragging, preview, onDragStart, onDragOver, onDragEnd };
115
+ }
@@ -0,0 +1,121 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { act, renderHook } from "@testing-library/react";
5
+ import { describe, expect, it, mock } from "bun:test";
6
+ import type { PointerEvent } from "react";
7
+ import { GRID_GAP_PX } from "../Dashboard/DashboardGrid";
8
+ import type { DashboardTile } from "./document";
9
+ import { useTileResize } from "./useTileResize";
10
+
11
+ const COLUMNS = 12;
12
+ const GRID_WIDTH = 1200 + GRID_GAP_PX * (COLUMNS - 1);
13
+ const TRACK = 100;
14
+
15
+ const tiles: DashboardTile[] = [
16
+ {
17
+ name: "a",
18
+ source: "s",
19
+ declaration: { kind: "reference", from: "x" },
20
+ colspan: 6,
21
+ },
22
+ { name: "b", source: "s", declaration: { kind: "inherited" } },
23
+ ];
24
+
25
+ /** A pointer event on a handle whose tile starts at `left`, with the grid measured. */
26
+ const pointer = (clientX: number, left = 0) => {
27
+ const handle = document.createElement("div");
28
+ const tile = document.createElement("div");
29
+ tile.appendChild(handle);
30
+ tile.getBoundingClientRect = () =>
31
+ ({ left, width: 600, top: 0, height: 100 }) as DOMRect;
32
+ let captured = false;
33
+ Object.assign(handle, {
34
+ setPointerCapture: () => {
35
+ captured = true;
36
+ },
37
+ hasPointerCapture: () => captured,
38
+ releasePointerCapture: () => {
39
+ captured = false;
40
+ },
41
+ });
42
+ return {
43
+ clientX,
44
+ pointerId: 1,
45
+ currentTarget: handle,
46
+ stopPropagation: mock(() => {}),
47
+ preventDefault: mock(() => {}),
48
+ } as unknown as PointerEvent<HTMLDivElement>;
49
+ };
50
+
51
+ const mount = () => {
52
+ const commit = mock((_index: number, _span: number) => {});
53
+ const onStart = mock((_index: number) => {});
54
+ const view = renderHook(() =>
55
+ useTileResize({ tiles, columns: COLUMNS, onStart, commit }),
56
+ );
57
+ const grid = document.createElement("div");
58
+ grid.getBoundingClientRect = () =>
59
+ ({ left: 0, width: GRID_WIDTH, top: 0, height: 400 }) as DOMRect;
60
+ (view.result.current.gridBox as { current: HTMLDivElement | null }).current =
61
+ grid;
62
+ return { view, commit, onStart };
63
+ };
64
+
65
+ describe("useTileResize", () => {
66
+ it("previews the width in whole columns as the edge moves, and writes it once on release", () => {
67
+ const { view, commit, onStart } = mount();
68
+ act(() => view.result.current.startResize(pointer(600), 0));
69
+ expect(onStart).toHaveBeenCalledWith(0);
70
+ expect(view.result.current.resize).toMatchObject({
71
+ index: 0,
72
+ span: 6,
73
+ track: TRACK,
74
+ });
75
+
76
+ // Eight tracks and seven gutters: the edge sits at exactly eight columns.
77
+ act(() =>
78
+ view.result.current.onResize(pointer(8 * TRACK + 7 * GRID_GAP_PX)),
79
+ );
80
+ expect(view.result.current.resize?.span).toBe(8);
81
+ // Part way into the ninth column still rounds to eight; nothing is
82
+ // written yet.
83
+ act(() =>
84
+ view.result.current.onResize(
85
+ pointer(8 * TRACK + 7 * GRID_GAP_PX + TRACK / 3),
86
+ ),
87
+ );
88
+ expect(view.result.current.resize?.span).toBe(8);
89
+ expect(commit).not.toHaveBeenCalled();
90
+
91
+ act(() => view.result.current.endResize(pointer(0)));
92
+ expect(commit).toHaveBeenCalledTimes(1);
93
+ expect(commit).toHaveBeenCalledWith(0, 8);
94
+ expect(view.result.current.resize).toBeUndefined();
95
+ });
96
+
97
+ it("clamps the preview to the grid, one column to full width", () => {
98
+ const { view } = mount();
99
+ act(() => view.result.current.startResize(pointer(600), 0));
100
+ act(() => view.result.current.onResize(pointer(-500)));
101
+ expect(view.result.current.resize?.span).toBe(1);
102
+ act(() => view.result.current.onResize(pointer(GRID_WIDTH * 3)));
103
+ expect(view.result.current.resize?.span).toBe(COLUMNS);
104
+ });
105
+
106
+ it("does nothing when the grid has not been measured", () => {
107
+ const commit = mock(() => {});
108
+ const view = renderHook(() =>
109
+ useTileResize({
110
+ tiles,
111
+ columns: COLUMNS,
112
+ onStart: () => {},
113
+ commit,
114
+ }),
115
+ );
116
+ act(() => view.result.current.startResize(pointer(600), 0));
117
+ expect(view.result.current.resize).toBeUndefined();
118
+ act(() => view.result.current.endResize(pointer(0)));
119
+ expect(commit).not.toHaveBeenCalled();
120
+ });
121
+ });
@@ -0,0 +1,110 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { useRef, useState } from "react";
5
+ import { GRID_GAP_PX } from "../Dashboard/DashboardGrid";
6
+ import type { DashboardTile } from "./document";
7
+
8
+ /** A resize in flight: the tile, the width it WOULD be, and the geometry. */
9
+ export interface ResizeState {
10
+ index: number;
11
+ span: number;
12
+ left: number;
13
+ track: number;
14
+ }
15
+
16
+ /**
17
+ * While a RESIZE is live, the page has to stop behaving like a document:
18
+ * dragging an edge across a dashboard otherwise selects the text it crosses and
19
+ * leaves the cursor as whatever it was over. (A MOVE gets the same from the
20
+ * library's own plugins.)
21
+ */
22
+ const dragChrome = (on: boolean) => {
23
+ const body = window.document.body;
24
+ body.style.userSelect = on ? "none" : "";
25
+ body.style.cursor = on ? "grabbing" : "";
26
+ };
27
+
28
+ /**
29
+ * Dragging a tile's right edge to set its `colspan`. The width is previewed
30
+ * by the caller from `resize.span` and written ONCE, on release: writing on
31
+ * every pointer move would put a dozen documents in the history for one
32
+ * gesture, and undo would walk back through the drag a column at a time.
33
+ */
34
+ export function useTileResize({
35
+ tiles,
36
+ columns,
37
+ onStart,
38
+ commit,
39
+ }: {
40
+ tiles: readonly DashboardTile[];
41
+ columns: number;
42
+ /** The tile a resize begins on, which the caller selects. */
43
+ onStart: (index: number) => void;
44
+ /** The width to write when the drag ends. */
45
+ commit: (index: number, span: number) => void;
46
+ }) {
47
+ const [resize, setResize] = useState<ResizeState | undefined>(undefined);
48
+ // Wraps the grid, so its content box IS the grid's: what a dragged edge has
49
+ // to be measured against to work out a column count.
50
+ const gridBox = useRef<HTMLDivElement>(null);
51
+
52
+ /**
53
+ * Width is the ONLY thing a resize can change.
54
+ *
55
+ * The format lays tiles out as a flow — `# colspan` for width, `# break` to
56
+ * start a row — with no row index, no column position and no height. So a
57
+ * right edge maps onto `colspan` and persists; a bottom edge has nothing to
58
+ * be written as, and a left edge would mean placing the tile, which the grid
59
+ * cannot express either. Offering those handles would be offering a drag the
60
+ * writer then refuses, which is worse than not offering it.
61
+ */
62
+ const startResize = (
63
+ event: React.PointerEvent<HTMLDivElement>,
64
+ index: number,
65
+ ) => {
66
+ const grid = gridBox.current?.getBoundingClientRect();
67
+ const tile = event.currentTarget.parentElement?.getBoundingClientRect();
68
+ if (!grid || !tile) return;
69
+ // Stops the tile's own click selecting a second time on release.
70
+ event.stopPropagation();
71
+ event.preventDefault();
72
+ event.currentTarget.setPointerCapture(event.pointerId);
73
+ dragChrome(true);
74
+ onStart(index);
75
+ setResize({
76
+ index,
77
+ span: tiles[index]?.colspan ?? 1,
78
+ left: tile.left,
79
+ // One column track: the row's width less every gutter in it.
80
+ track: (grid.width - GRID_GAP_PX * (columns - 1)) / columns,
81
+ });
82
+ };
83
+
84
+ const onResize = (event: React.PointerEvent<HTMLDivElement>) => {
85
+ if (!resize) return;
86
+ const width = event.clientX - resize.left;
87
+ // A tile of N tracks is N tracks plus the N-1 gutters between them, so
88
+ // adding one gutter back makes the division land on whole columns.
89
+ const span = Math.round(
90
+ (width + GRID_GAP_PX) / (resize.track + GRID_GAP_PX),
91
+ );
92
+ const clamped = Math.min(Math.max(span, 1), columns);
93
+ if (clamped !== resize.span) setResize({ ...resize, span: clamped });
94
+ };
95
+
96
+ const endResize = (event: React.PointerEvent<HTMLDivElement>) => {
97
+ if (event.currentTarget.hasPointerCapture(event.pointerId))
98
+ event.currentTarget.releasePointerCapture(event.pointerId);
99
+ dragChrome(false);
100
+ if (!resize) return;
101
+ const { index, span } = resize;
102
+ setResize(undefined);
103
+ // ONE history entry for the whole drag. Writing on every pointer move
104
+ // would put a dozen documents in the stack for one gesture, and undo
105
+ // would walk back through the drag a column at a time.
106
+ commit(index, span);
107
+ };
108
+
109
+ return { resize, gridBox, startResize, onResize, endResize };
110
+ }
@@ -0,0 +1,96 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import { withGaps } from "./layout";
6
+ import type { DashboardTile } from "./document";
7
+
8
+ const tile = (
9
+ name: string,
10
+ colspan: number,
11
+ starts = false,
12
+ ): DashboardTile => ({
13
+ name,
14
+ source: "a",
15
+ declaration: { kind: "reference", from: `${name}_view` },
16
+ colspan,
17
+ ...(starts ? { break: true } : {}),
18
+ });
19
+
20
+ /** The grid's entries as a reader would list them: a name, or a gap and its width. */
21
+ const shape = (entries: ReturnType<typeof withGaps>) =>
22
+ entries.map((entry) =>
23
+ entry.kind === "gap"
24
+ ? `gap(${entry.colspan}) after ${entry.after}`
25
+ : entry.tile.name,
26
+ );
27
+
28
+ describe("withGaps", () => {
29
+ it("lays a gap where a break leaves a row short", () => {
30
+ // The storefront overview with the trend narrowed: half a row open to
31
+ // its right, because `kpis` breaks onto a fresh row.
32
+ const entries = withGaps(
33
+ [tile("trend", 6), tile("kpis", 6, true), tile("map", 6)],
34
+ 12,
35
+ );
36
+ expect(shape(entries)).toEqual([
37
+ "trend",
38
+ "gap(6) after a.trend",
39
+ "kpis",
40
+ "map",
41
+ ]);
42
+ });
43
+
44
+ it("lays a gap where a tile is too wide for what is left of the row", () => {
45
+ // No break anywhere: the wide tile wraps of its own accord, and the
46
+ // space it could not fit into is a gap all the same.
47
+ const entries = withGaps([tile("narrow", 4), tile("wide", 12)], 12);
48
+ expect(shape(entries)).toEqual([
49
+ "narrow",
50
+ "gap(8) after a.narrow",
51
+ "wide",
52
+ ]);
53
+ });
54
+
55
+ it("lays a gap at the end of the last row", () => {
56
+ const entries = withGaps(
57
+ [tile("one", 6), tile("two", 6), tile("three", 6)],
58
+ 12,
59
+ );
60
+ expect(shape(entries)).toEqual([
61
+ "one",
62
+ "two",
63
+ "three",
64
+ "gap(6) after a.three",
65
+ ]);
66
+ });
67
+
68
+ it("lays no gap into a full row", () => {
69
+ const entries = withGaps(
70
+ [tile("one", 6), tile("two", 6), tile("three", 12)],
71
+ 12,
72
+ );
73
+ expect(entries.every((entry) => entry.kind === "tile")).toBe(true);
74
+ });
75
+
76
+ it("numbers tiles by their place among the tiles, not among the entries", () => {
77
+ // The sortable is told a tile's index; a gap between two tiles must not
78
+ // shift the second one's.
79
+ const entries = withGaps([tile("one", 6), tile("two", 6, true)], 12);
80
+ const indices = entries.flatMap((entry) =>
81
+ entry.kind === "tile" ? [entry.index] : [],
82
+ );
83
+ expect(indices).toEqual([0, 1]);
84
+ });
85
+
86
+ it("carries each tile's width and row start through to the grid", () => {
87
+ const [first, , second] = withGaps(
88
+ [tile("one", 6), tile("two", 4, true)],
89
+ 12,
90
+ );
91
+ expect(first.colspan).toBe(6);
92
+ expect(first.break).toBeUndefined();
93
+ expect(second.colspan).toBe(4);
94
+ expect(second.break).toBe(true);
95
+ });
96
+ });
@@ -44,3 +44,15 @@ export function useDocumentStorage(): DocumentStorageContextValue {
44
44
  }
45
45
  return context;
46
46
  }
47
+
48
+ /**
49
+ * The host's storage, or undefined outside a provider. For a component that
50
+ * can do without one — the dashboard editor still edits and exports with no
51
+ * place to save — rather than the throwing form, which is for components that
52
+ * cannot.
53
+ */
54
+ export function useOptionalDocumentStorage():
55
+ | DocumentStorageContextValue
56
+ | undefined {
57
+ return useContext(DocumentStorageContext);
58
+ }
@@ -10,6 +10,7 @@ export type {
10
10
  export {
11
11
  DocumentStorageProvider,
12
12
  useDocumentStorage,
13
+ useOptionalDocumentStorage,
13
14
  type DocumentStorageProviderProps,
14
15
  } from "./DocumentStorageProvider";
15
16
  export { BrowserDocumentStorage } from "./BrowserDocumentStorage";
@@ -1,19 +1,16 @@
1
1
  // Copyright (c) Credible Data Inc.
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
- import { Box, Button } from "@mui/material";
5
- import Markdown from "markdown-to-jsx";
4
+ import { Box } from "@mui/material";
6
5
  import { useState } from "react";
7
6
  import { useQueryWithApiError } from "../../hooks/useQueryWithApiError";
8
7
  import { parseResourceUri } from "../../utils/formatting";
9
8
  import { ApiErrorDisplay } from "../ApiErrorDisplay";
10
9
  import { Loading } from "../Loading";
10
+ import { PackageSection } from "../PackageSection";
11
+ import { Prose } from "../Prose";
12
+ import { SecondaryButton } from "../buttons";
11
13
  import { useServer } from "../ServerProvider";
12
- import {
13
- PackageCard,
14
- PackageCardContent,
15
- PackageSectionTitle,
16
- } from "../styles";
17
14
  interface AboutProps {
18
15
  resourceUri: string;
19
16
  }
@@ -39,62 +36,19 @@ export default function About({ resourceUri }: AboutProps) {
39
36
  <>
40
37
  {!isSuccess && !isError && <Loading text="Fetching About..." />}
41
38
  {isSuccess && readmeContent && (
42
- <PackageCard>
43
- <PackageCardContent>
44
- <PackageSectionTitle>Readme</PackageSectionTitle>
45
- <Box sx={{ mt: 1, fontSize: "0.875rem", lineHeight: 1.6 }}>
46
- <Markdown
47
- options={{
48
- overrides: {
49
- h1: {
50
- component: "p",
51
- props: {
52
- style: {
53
- fontSize: "inherit",
54
- fontWeight: "italic",
55
- },
56
- },
57
- },
58
- h2: {
59
- component: "p",
60
- props: {
61
- style: {
62
- fontSize: "inherit",
63
- fontWeight: "italic",
64
- },
65
- },
66
- },
67
- h3: {
68
- component: "p",
69
- props: {
70
- style: {
71
- fontSize: "inherit",
72
- fontWeight: "italic",
73
- },
74
- },
75
- },
76
- },
77
- }}
78
- >
79
- {expanded || !shouldTruncate ? readmeContent : preview}
80
- </Markdown>
81
-
82
- {shouldTruncate && (
83
- <Box sx={{ mt: 1 }}>
84
- {" "}
85
- {/* separate line */}
86
- <Button
87
- variant="text"
88
- size="small"
89
- onClick={() => setExpanded(!expanded)}
90
- >
91
- {expanded ? "Read less" : "Read more"}
92
- </Button>
93
- </Box>
94
- )}
39
+ <PackageSection title="Readme">
40
+ <Prose variant="caption">
41
+ {expanded || !shouldTruncate ? readmeContent : preview}
42
+ </Prose>
43
+ {shouldTruncate && (
44
+ <Box sx={{ mt: 1 }}>
45
+ <SecondaryButton
46
+ label={expanded ? "Read less" : "Read more"}
47
+ onClick={() => setExpanded(!expanded)}
48
+ />
95
49
  </Box>
96
- </PackageCardContent>
97
- </PackageCard>
50
+ )}
51
+ </PackageSection>
98
52
  )}
99
53
  {isError && (
100
54
  <ApiErrorDisplay