@malloy-publisher/sdk 0.9.2 → 0.9.4

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 (264) hide show
  1. package/dist/{RenderedResult-CBLHr32C.es.js → RenderedResult-DwB0-9Yx.es.js} +118 -119
  2. package/dist/{RenderedResult-B48wAjM8.cjs.js → RenderedResult-hKjFEqaN.cjs.js} +2 -2
  3. package/dist/{ServerProvider-DCV8BUWD.es.js → ServerProvider-CcSrGaRN.es.js} +1281 -1142
  4. package/dist/ServerProvider-Gbf1YPOP.cjs.js +1 -0
  5. package/dist/builder/index.cjs.js +112 -115
  6. package/dist/builder/index.es.js +11766 -11502
  7. package/dist/builder-entry.d.ts +1 -1
  8. package/dist/client/api.d.ts +170 -9
  9. package/dist/client/index.cjs.js +1 -1
  10. package/dist/client/index.es.js +93 -89
  11. package/dist/client-entry.d.ts +2 -0
  12. package/dist/components/ApiErrorDisplay.d.ts +11 -0
  13. package/dist/components/AppDialog.d.ts +6 -6
  14. package/dist/components/Dashboard/DashboardGrid.d.ts +18 -3
  15. package/dist/components/Dashboard/DashboardTile.d.ts +12 -2
  16. package/dist/components/Dashboard/DashboardView.d.ts +38 -3
  17. package/dist/components/Dashboard/TileCard.d.ts +16 -2
  18. package/dist/components/Dashboard/dropValueUrlTags.d.ts +6 -0
  19. package/dist/components/Dashboard/index.d.ts +3 -2
  20. package/dist/components/Dashboard/telemetry.d.ts +2 -1
  21. package/dist/components/Dashboard/textSource.d.ts +19 -0
  22. package/dist/components/DashboardBuilder/AddTileDialog.d.ts +7 -8
  23. package/dist/components/DashboardBuilder/BuilderDialogs.d.ts +25 -0
  24. package/dist/components/DashboardBuilder/BuilderGrid.d.ts +31 -0
  25. package/dist/components/DashboardBuilder/BuilderHeader.d.ts +17 -0
  26. package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +14 -17
  27. package/dist/components/DashboardBuilder/ChartPicker.d.ts +3 -1
  28. package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +15 -7
  29. package/dist/components/DashboardBuilder/DashboardEditor.d.ts +28 -2
  30. package/dist/components/DashboardBuilder/EditorSurface.d.ts +33 -0
  31. package/dist/components/DashboardBuilder/FilterStrip.d.ts +6 -4
  32. package/dist/components/DashboardBuilder/InlineText.d.ts +1 -1
  33. package/dist/components/DashboardBuilder/NotebookEditor.d.ts +1 -1
  34. package/dist/components/DashboardBuilder/TextTileBody.d.ts +4 -1
  35. package/dist/components/DashboardBuilder/TileFrame.d.ts +43 -6
  36. package/dist/components/DashboardBuilder/TileMenu.d.ts +6 -8
  37. package/dist/components/DashboardBuilder/builderReport.d.ts +13 -0
  38. package/dist/components/DashboardBuilder/catalog.d.ts +0 -18
  39. package/dist/components/DashboardBuilder/documentSession.d.ts +2 -2
  40. package/dist/components/DashboardBuilder/imports.d.ts +19 -0
  41. package/dist/components/DashboardBuilder/inlineEdit.d.ts +18 -0
  42. package/dist/components/DashboardBuilder/readDocument.d.ts +3 -1
  43. package/dist/components/DashboardBuilder/readForEditor.d.ts +1 -1
  44. package/dist/components/DashboardBuilder/sortable.d.ts +2 -0
  45. package/dist/components/DashboardBuilder/spliceDocument.d.ts +2 -0
  46. package/dist/components/DashboardBuilder/testing/resize.d.ts +7 -0
  47. package/dist/components/DashboardBuilder/tileDisplayTitle.d.ts +7 -0
  48. package/dist/components/DashboardBuilder/useBuilderSelection.d.ts +53 -0
  49. package/dist/components/DashboardBuilder/useBuilderSession.d.ts +4 -7
  50. package/dist/components/DashboardBuilder/useControlBindings.d.ts +30 -0
  51. package/dist/components/DashboardBuilder/useConversionConfirm.d.ts +20 -0
  52. package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +2 -0
  53. package/dist/components/DashboardBuilder/useDraft.d.ts +3 -1
  54. package/dist/components/DashboardBuilder/useHostCopy.d.ts +26 -0
  55. package/dist/components/DashboardBuilder/useOpenDraftCommit.d.ts +17 -0
  56. package/dist/components/DashboardBuilder/useOpenedDocument.d.ts +71 -0
  57. package/dist/components/DashboardBuilder/useSaveChannel.d.ts +50 -0
  58. package/dist/components/DashboardBuilder/useTileEditing.d.ts +33 -0
  59. package/dist/components/DashboardBuilder/useTileResize.d.ts +32 -0
  60. package/dist/components/Loading.d.ts +13 -0
  61. package/dist/components/Notebook/NotebookCell.d.ts +1 -5
  62. package/dist/components/Package/Package.d.ts +16 -1
  63. package/dist/components/Package/index.d.ts +1 -1
  64. package/dist/components/Prose.d.ts +1 -1
  65. package/dist/components/RenderedResult/loadRenderer.d.ts +7 -0
  66. package/dist/components/given/paramCodec.d.ts +8 -1
  67. package/dist/components/index.d.ts +1 -2
  68. package/dist/components/styles.d.ts +4 -4
  69. package/dist/hooks/givenValue.d.ts +6 -0
  70. package/dist/hooks/index.d.ts +1 -0
  71. package/dist/hooks/useCompiledDocument.d.ts +23 -0
  72. package/dist/hooks/useDocumentControls.d.ts +6 -1
  73. package/dist/hooks/useNearViewport.d.ts +16 -0
  74. package/dist/hooks/useQueryWithApiError.d.ts +7 -2
  75. package/dist/hooks/useSuggestOptions.d.ts +9 -2
  76. package/dist/index.cjs.js +14 -14
  77. package/dist/index.es.js +9760 -10363
  78. package/dist/text/index.es.js +1 -1
  79. package/dist/{text-entry-CbbBcZl8.es.js → text-entry-CA0LZ1xL.es.js} +8 -8
  80. package/dist/text-entry.d.ts +1 -1
  81. package/dist/theme/accent.d.ts +29 -0
  82. package/dist/theme/buildVegaThemeOverride.d.ts +3 -3
  83. package/dist/theme/index.d.ts +3 -0
  84. package/dist/theme/keys.d.ts +1 -1
  85. package/dist/theme/motion.d.ts +31 -0
  86. package/dist/theme/readChartAnnotations.d.ts +1 -1
  87. package/dist/theme/resolveTheme.d.ts +4 -4
  88. package/dist/theme/types.d.ts +33 -5
  89. package/dist/{DashboardTile-BFcxdv7R.es.js → useCompiledDocument-BU6d8FPp.es.js} +6978 -6289
  90. package/dist/useCompiledDocument-pOg2W-Yt.cjs.js +142 -0
  91. package/package.json +11 -11
  92. package/src/builder-entry.ts +1 -0
  93. package/src/client-entry.ts +8 -0
  94. package/src/clientEntry.spec.ts +17 -0
  95. package/src/components/ApiErrorDisplay.tsx +37 -18
  96. package/src/components/AppDialog.tsx +8 -7
  97. package/src/components/Dashboard/Dashboard.spec.tsx +3 -32
  98. package/src/components/Dashboard/Dashboard.tsx +2 -2
  99. package/src/components/Dashboard/DashboardGrid.tsx +23 -5
  100. package/src/components/Dashboard/DashboardTile.spec.tsx +97 -2
  101. package/src/components/Dashboard/DashboardTile.tsx +95 -31
  102. package/src/components/Dashboard/DashboardView.spec.tsx +156 -0
  103. package/src/components/Dashboard/DashboardView.tsx +165 -89
  104. package/src/components/Dashboard/RowsDialog.tsx +3 -2
  105. package/src/components/Dashboard/TileCard.tsx +17 -2
  106. package/src/components/Dashboard/dropValueUrlTags.spec.ts +177 -0
  107. package/src/components/Dashboard/dropValueUrlTags.ts +108 -0
  108. package/src/components/Dashboard/index.ts +11 -2
  109. package/src/components/Dashboard/telemetry.ts +2 -1
  110. package/src/components/Dashboard/textSource.spec.ts +36 -0
  111. package/src/components/Dashboard/textSource.ts +39 -0
  112. package/src/components/DashboardBuilder/AddTileDialog.tsx +16 -56
  113. package/src/components/DashboardBuilder/BuilderDialogs.tsx +162 -0
  114. package/src/components/DashboardBuilder/BuilderGrid.tsx +245 -0
  115. package/src/components/DashboardBuilder/BuilderHeader.tsx +107 -0
  116. package/src/components/DashboardBuilder/BuilderToolbar.tsx +86 -113
  117. package/src/components/DashboardBuilder/ChartPicker.tsx +4 -1
  118. package/src/components/DashboardBuilder/DashboardBuilder.charts.spec.tsx +5 -5
  119. package/src/components/DashboardBuilder/DashboardBuilder.exit.spec.tsx +74 -83
  120. package/src/components/DashboardBuilder/DashboardBuilder.inline.spec.tsx +62 -18
  121. package/src/components/DashboardBuilder/DashboardBuilder.notebook.spec.tsx +55 -108
  122. package/src/components/DashboardBuilder/DashboardBuilder.openDraft.spec.tsx +10 -62
  123. package/src/components/DashboardBuilder/DashboardBuilder.sources.spec.tsx +50 -57
  124. package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +53 -40
  125. package/src/components/DashboardBuilder/DashboardBuilder.textTiles.spec.tsx +10 -8
  126. package/src/components/DashboardBuilder/DashboardBuilder.tsx +227 -704
  127. package/src/components/DashboardBuilder/DashboardEditor.authoritative.spec.tsx +32 -52
  128. package/src/components/DashboardBuilder/DashboardEditor.emptyStart.spec.tsx +1 -50
  129. package/src/components/DashboardBuilder/DashboardEditor.notebook.spec.tsx +12 -18
  130. package/src/components/DashboardBuilder/DashboardEditor.package.spec.tsx +4 -24
  131. package/src/components/DashboardBuilder/DashboardEditor.resourceUri.spec.tsx +4 -4
  132. package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +8 -13
  133. package/src/components/DashboardBuilder/DashboardEditor.textSource.spec.tsx +317 -0
  134. package/src/components/DashboardBuilder/DashboardEditor.tsx +142 -820
  135. package/src/components/DashboardBuilder/DrillDialog.tsx +2 -2
  136. package/src/components/DashboardBuilder/EditorSurface.tsx +471 -0
  137. package/src/components/DashboardBuilder/FilterDialog.tsx +13 -8
  138. package/src/components/DashboardBuilder/FilterStrip.tsx +117 -79
  139. package/src/components/DashboardBuilder/InlineMarkdown.tsx +7 -9
  140. package/src/components/DashboardBuilder/InlineText.tsx +23 -17
  141. package/src/components/DashboardBuilder/NotebookEditor.tsx +3 -3
  142. package/src/components/DashboardBuilder/TextTileBody.tsx +5 -2
  143. package/src/components/DashboardBuilder/TileFrame.tsx +255 -90
  144. package/src/components/DashboardBuilder/TileMenu.tsx +41 -91
  145. package/src/components/DashboardBuilder/artifactBlock.spec.ts +1 -1
  146. package/src/components/DashboardBuilder/artifactTagParity.spec.ts +9 -4
  147. package/src/components/DashboardBuilder/builderReport.ts +74 -0
  148. package/src/components/DashboardBuilder/catalog.spec.ts +0 -44
  149. package/src/components/DashboardBuilder/catalog.ts +0 -36
  150. package/src/components/DashboardBuilder/controls.spec.ts +1 -1
  151. package/src/components/DashboardBuilder/controls.ts +1 -1
  152. package/src/components/DashboardBuilder/documentSession.ts +3 -2
  153. package/src/components/DashboardBuilder/imports.spec.ts +61 -0
  154. package/src/components/DashboardBuilder/imports.ts +93 -0
  155. package/src/components/DashboardBuilder/inlineEdit.tsx +78 -0
  156. package/src/components/DashboardBuilder/legacyNotebook.ts +1 -1
  157. package/src/components/DashboardBuilder/malloyTree.ts +5 -1
  158. package/src/components/DashboardBuilder/newDashboard.ts +1 -1
  159. package/src/components/DashboardBuilder/preview.ts +1 -1
  160. package/src/components/DashboardBuilder/readDocument.spec.ts +36 -0
  161. package/src/components/DashboardBuilder/readDocument.ts +10 -4
  162. package/src/components/DashboardBuilder/readForEditor.ts +7 -2
  163. package/src/components/DashboardBuilder/sortable.spec.ts +25 -1
  164. package/src/components/DashboardBuilder/sortable.tsx +11 -0
  165. package/src/components/DashboardBuilder/spliceDocument.ts +8 -3
  166. package/src/components/DashboardBuilder/testing/artifactTagParity.json +62 -6
  167. package/src/components/DashboardBuilder/testing/documentKindRule.json +40 -0
  168. package/src/components/DashboardBuilder/testing/inline.ts +1 -1
  169. package/src/components/DashboardBuilder/testing/resize.ts +48 -0
  170. package/src/components/DashboardBuilder/testing/urlRenderTags.json +62 -0
  171. package/src/components/DashboardBuilder/textTiles.spec.ts +43 -1
  172. package/src/components/DashboardBuilder/tileDisplayTitle.ts +13 -0
  173. package/src/components/DashboardBuilder/useBuilderSelection.ts +121 -0
  174. package/src/components/DashboardBuilder/useBuilderSession.spec.ts +0 -85
  175. package/src/components/DashboardBuilder/useBuilderSession.ts +6 -43
  176. package/src/components/DashboardBuilder/useBuilderShortcuts.overlay.spec.tsx +32 -0
  177. package/src/components/DashboardBuilder/useBuilderShortcuts.ts +11 -1
  178. package/src/components/DashboardBuilder/useControlBindings.ts +152 -0
  179. package/src/components/DashboardBuilder/useConversionConfirm.ts +55 -0
  180. package/src/components/DashboardBuilder/useDashboardEditor.ts +5 -2
  181. package/src/components/DashboardBuilder/useDraft.ts +15 -2
  182. package/src/components/DashboardBuilder/useHostCopy.ts +104 -0
  183. package/src/components/DashboardBuilder/useOpenDraftCommit.ts +47 -0
  184. package/src/components/DashboardBuilder/useOpenedDocument.ts +314 -0
  185. package/src/components/DashboardBuilder/useSaveChannel.ts +275 -0
  186. package/src/components/DashboardBuilder/useTileEditing.ts +141 -0
  187. package/src/components/DashboardBuilder/useTileResize.spec.tsx +146 -0
  188. package/src/components/DashboardBuilder/useTileResize.ts +133 -0
  189. package/src/components/DataAppViewer/DataAppViewer.tsx +7 -4
  190. package/src/components/DocumentCreate/guards.ts +1 -1
  191. package/src/components/Environment/Environment.tsx +0 -10
  192. package/src/components/Loading.tsx +14 -0
  193. package/src/components/Model/Model.tsx +16 -9
  194. package/src/components/Model/ModelExplorer.tsx +2 -2
  195. package/src/components/Notebook/Notebook.tsx +6 -2
  196. package/src/components/Notebook/NotebookCell.tsx +90 -215
  197. package/src/components/Package/Package.artifacts.spec.tsx +8 -12
  198. package/src/components/Package/Package.create.spec.tsx +22 -1
  199. package/src/components/Package/Package.tsx +51 -27
  200. package/src/components/Package/index.ts +1 -1
  201. package/src/components/Prose.spec.tsx +37 -0
  202. package/src/components/Prose.tsx +35 -5
  203. package/src/components/RenderedResult/RenderedResult.tsx +5 -7
  204. package/src/components/RenderedResult/ResultContainer.tsx +7 -2
  205. package/src/components/RenderedResult/ResultPanel.tsx +20 -5
  206. package/src/components/RenderedResult/loadRenderer.ts +42 -0
  207. package/src/components/given/GivenInput.spec.tsx +5 -5
  208. package/src/components/given/GivenInput.tsx +46 -38
  209. package/src/components/given/GivensPanel.tsx +2 -8
  210. package/src/components/given/paramCodec.ts +27 -1
  211. package/src/components/index.ts +1 -2
  212. package/src/components/styles.ts +4 -5
  213. package/src/hooks/givenValue.ts +9 -0
  214. package/src/hooks/index.ts +4 -0
  215. package/src/hooks/useCompiledDocument.spec.tsx +86 -0
  216. package/src/hooks/useCompiledDocument.ts +59 -0
  217. package/src/hooks/useDimensionalFilterRangeData.ts +3 -1
  218. package/src/hooks/useDocumentControls.ts +13 -1
  219. package/src/hooks/useNearViewport.spec.tsx +150 -0
  220. package/src/hooks/useNearViewport.ts +102 -0
  221. package/src/hooks/useQueryResult.ts +7 -1
  222. package/src/hooks/useQueryWithApiError.ts +10 -3
  223. package/src/hooks/useRawQueryData.ts +3 -1
  224. package/src/hooks/useSuggestOptions.dom.spec.ts +40 -1
  225. package/src/hooks/useSuggestOptions.ts +42 -10
  226. package/src/text-entry.ts +1 -1
  227. package/src/textEntry.spec.ts +2 -8
  228. package/src/theme/accent.spec.ts +116 -0
  229. package/src/theme/accent.ts +148 -0
  230. package/src/theme/buildTableCssVars.spec.ts +24 -0
  231. package/src/theme/buildTableCssVars.ts +1 -2
  232. package/src/theme/buildVegaThemeOverride.spec.ts +38 -2
  233. package/src/theme/buildVegaThemeOverride.ts +5 -5
  234. package/src/theme/defaults.ts +34 -0
  235. package/src/theme/index.ts +9 -0
  236. package/src/theme/keys.ts +6 -0
  237. package/src/theme/motion.ts +44 -0
  238. package/src/theme/readChartAnnotations.spec.ts +18 -0
  239. package/src/theme/readChartAnnotations.ts +1 -1
  240. package/src/theme/resolveTheme.spec.ts +73 -0
  241. package/src/theme/resolveTheme.ts +61 -20
  242. package/src/theme/types.ts +30 -5
  243. package/src/utils/queryClient.ts +8 -0
  244. package/dist/DashboardTile-sg8LjUk8.cjs.js +0 -137
  245. package/dist/ServerProvider-HRqT3w1C.cjs.js +0 -1
  246. package/dist/components/Dashboard/DashboardBar.d.ts +0 -21
  247. package/dist/components/Dashboard/ExploreDialog.d.ts +0 -20
  248. package/dist/components/DashboardBuilder/DiffDialog.d.ts +0 -7
  249. package/dist/components/DashboardBuilder/SaveNotice.d.ts +0 -27
  250. package/dist/components/DashboardBuilder/SettingsPopover.d.ts +0 -32
  251. package/dist/components/DashboardBuilder/diff.d.ts +0 -35
  252. package/src/components/Dashboard/DashboardBar.tsx +0 -88
  253. package/src/components/Dashboard/ExploreDialog.tsx +0 -89
  254. package/src/components/DashboardBuilder/DashboardBuilder.notice.spec.tsx +0 -194
  255. package/src/components/DashboardBuilder/DiffDialog.spec.tsx +0 -25
  256. package/src/components/DashboardBuilder/DiffDialog.tsx +0 -103
  257. package/src/components/DashboardBuilder/SaveNotice.tsx +0 -113
  258. package/src/components/DashboardBuilder/SettingsPopover.spec.tsx +0 -87
  259. package/src/components/DashboardBuilder/SettingsPopover.tsx +0 -326
  260. package/src/components/DashboardBuilder/diff.spec.ts +0 -44
  261. package/src/components/DashboardBuilder/diff.ts +0 -93
  262. /package/dist/{components/DashboardBuilder → utils}/malloyText.d.ts +0 -0
  263. /package/src/{components/DashboardBuilder → utils}/malloyText.spec.ts +0 -0
  264. /package/src/{components/DashboardBuilder → utils}/malloyText.ts +0 -0
@@ -0,0 +1,55 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { useCallback, useRef, useState } from "react";
5
+
6
+ /** A save waiting on the reader's answer: go ahead, or stop without writing. */
7
+ export interface ConversionConfirm {
8
+ go: () => void;
9
+ stop: () => void;
10
+ }
11
+
12
+ /**
13
+ * Asking before the save that converts a cell-format notebook.
14
+ *
15
+ * `prepareSave` is `prepare` behind that question. While `confirmConversion`
16
+ * is set the question is open; answering it runs the save, or drops it.
17
+ */
18
+ export function useConversionConfirm({
19
+ pendingOpen,
20
+ prepare,
21
+ }: {
22
+ /** Whether the document opened unsaved, as a conversion does. */
23
+ pendingOpen: boolean | undefined;
24
+ /** What runs before every save once the reader has said yes. */
25
+ prepare: (run: () => Promise<void> | void) => Promise<void> | void;
26
+ }) {
27
+ // A save that would convert a cell-format notebook, waiting on the reader's yes.
28
+ const [confirmConversion, setConfirmConversion] = useState<
29
+ ConversionConfirm | undefined
30
+ >(undefined);
31
+ // The first save of a cell-format notebook rewrites the file in the tile
32
+ // layout, which the builder cannot take back: it asks before it writes.
33
+ const pendingOpenRef = useRef(pendingOpen);
34
+ pendingOpenRef.current = pendingOpen;
35
+ const prepareSave = useCallback(
36
+ (run: () => Promise<void> | void): Promise<void> | void => {
37
+ if (!pendingOpenRef.current) return prepare(run);
38
+ return new Promise<void>((resolve) =>
39
+ setConfirmConversion({
40
+ go: () => {
41
+ setConfirmConversion(undefined);
42
+ resolve(prepare(run) as Promise<void> | undefined);
43
+ },
44
+ stop: () => {
45
+ setConfirmConversion(undefined);
46
+ resolve();
47
+ },
48
+ }),
49
+ );
50
+ },
51
+ [prepare],
52
+ );
53
+
54
+ return { confirmConversion, prepareSave };
55
+ }
@@ -48,8 +48,10 @@ export function useDashboardEditor(options: {
48
48
  replaces?: string;
49
49
  /** The file's path within the package. */
50
50
  modelPath?: string;
51
+ /** The document is held as text: a kind switch always writes `kind=`. */
52
+ explicitKind?: boolean;
51
53
  }): DashboardEditor {
52
- const { conversion, modelPath, ...rest } = options;
54
+ const { conversion, modelPath, explicitKind, ...rest } = options;
53
55
  const from = conversion?.from;
54
56
  const to = conversion?.to;
55
57
  const splice = useCallback(
@@ -61,9 +63,10 @@ export function useDashboardEditor(options: {
61
63
  {
62
64
  changeKind: true,
63
65
  ...(modelPath !== undefined ? { modelPath } : {}),
66
+ ...(explicitKind ? { explicitKind } : {}),
64
67
  },
65
68
  ),
66
- [from, to, modelPath],
69
+ [from, to, modelPath, explicitKind],
67
70
  );
68
71
  return useDocumentEditor({
69
72
  ...rest,
@@ -5,7 +5,8 @@ import { useEffect, useState } from "react";
5
5
 
6
6
  /**
7
7
  * A copy of `value` to edit while a window is open, committed ONCE when it
8
- * closes, and only if something changed.
8
+ * closes, and only if something changed — or at once, through `apply`, for a
9
+ * change that should show while the window is open.
9
10
  *
10
11
  * Per-keystroke commits would put a document in the undo stack for every
11
12
  * letter typed into a title, and undo would then walk back through the word.
@@ -37,7 +38,19 @@ export function useDraft<T>(
37
38
  change(next);
38
39
  return next;
39
40
  });
41
+ /**
42
+ * Change the draft AND commit it at once, for a choice whose effect should
43
+ * show while the window is still open (a tile's chart). One choice, one
44
+ * history entry; the close then finds nothing left to commit.
45
+ */
46
+ const apply = (change: (draft: T) => void) => {
47
+ if (draft === undefined) return;
48
+ const next = structuredClone(draft);
49
+ change(next);
50
+ setDraft(next);
51
+ if (JSON.stringify(next) !== JSON.stringify(value)) onCommit(next);
52
+ };
40
53
  /** Drop the draft without committing: for an action that supersedes it. */
41
54
  const discard = () => setDraft(undefined);
42
- return { draft, patch, close, discard };
55
+ return { draft, patch, apply, close, discard };
43
56
  }
@@ -0,0 +1,104 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { useEffect, useState } from "react";
5
+ import {
6
+ isDocumentNotFound,
7
+ type DocumentStorage,
8
+ type Workspace,
9
+ } from "../DocumentStorage";
10
+ import { locatorFor } from "../DocumentCreate/documentPath";
11
+ import type { DocumentKind } from "./document";
12
+ import { chooseWorkspace, storageErrorMessage } from "./documentSession";
13
+
14
+ /**
15
+ * The host's copy of the document, if one was saved earlier: which workspace
16
+ * holds it, its text, and whether the read has answered yet.
17
+ *
18
+ * Read once per document, from the host's {@link DocumentStorage}. A host
19
+ * without one has nothing to read, so the answer is in at once.
20
+ */
21
+ export function useHostCopy({
22
+ storage,
23
+ kind,
24
+ notebook,
25
+ environmentName,
26
+ packageName,
27
+ modelPath,
28
+ }: {
29
+ storage: DocumentStorage | undefined;
30
+ kind: DocumentKind;
31
+ notebook: boolean;
32
+ environmentName: string;
33
+ packageName: string;
34
+ modelPath: string;
35
+ }) {
36
+ // The host's copy, if one was saved earlier.
37
+ const [workspace, setWorkspace] = useState<Workspace | undefined>(undefined);
38
+ const [draft, setDraft] = useState<string | undefined>(undefined);
39
+ const [draftChecked, setDraftChecked] = useState(storage === undefined);
40
+ // Whether a copy was there when the editor opened: only that one is
41
+ // offered. A copy this session saves is not "edits from an earlier visit".
42
+ const [offered, setOffered] = useState(false);
43
+ // A read the backend could not answer, which is not the same as no
44
+ // document. Held rather than swallowed, because "there is nothing saved"
45
+ // is what makes overwriting the record look safe.
46
+ const [readFailure, setReadFailure] = useState<string | undefined>(
47
+ undefined,
48
+ );
49
+ useEffect(() => {
50
+ if (!storage) return;
51
+ let stale = false;
52
+ setReadFailure(undefined);
53
+ (async () => {
54
+ try {
55
+ // Every workspace, not only the writeable ones: a reader who
56
+ // cannot write to the record still has to be shown the record.
57
+ // Asking for writeable only would hide it and fall back to the
58
+ // package, which on a server that takes writes means publishing a
59
+ // deploy of the record over the record.
60
+ const chosen = chooseWorkspace(await storage.listWorkspaces(false));
61
+ if (stale) return;
62
+ setWorkspace(chosen);
63
+ // Only a notebook's record is read: a copy beside the package is never offered back.
64
+ if (chosen !== undefined && (!notebook || chosen.authoritative)) {
65
+ const text = await storage
66
+ .getDocument(
67
+ locatorFor(
68
+ kind,
69
+ chosen.name,
70
+ environmentName,
71
+ packageName,
72
+ modelPath,
73
+ ),
74
+ )
75
+ // Absence is the ordinary first visit, not a failure.
76
+ .catch((error: unknown) => {
77
+ if (isDocumentNotFound(error)) return undefined;
78
+ throw error;
79
+ });
80
+ if (stale) return;
81
+ setDraft(text);
82
+ setOffered(text !== undefined);
83
+ }
84
+ } catch (error) {
85
+ if (stale) return;
86
+ setReadFailure(storageErrorMessage(error));
87
+ }
88
+ if (!stale) setDraftChecked(true);
89
+ })();
90
+ return () => {
91
+ stale = true;
92
+ };
93
+ }, [storage, kind, notebook, environmentName, packageName, modelPath]);
94
+
95
+ return {
96
+ workspace,
97
+ draft,
98
+ setDraft,
99
+ draftChecked,
100
+ offered,
101
+ setOffered,
102
+ readFailure,
103
+ };
104
+ }
@@ -0,0 +1,47 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
5
+ import type { OpenDraftSink } from "./openDraft";
6
+
7
+ /**
8
+ * The text field open somewhere on the page, as the builder's save sees it.
9
+ *
10
+ * An inline field reports itself through `openDraft` (provided as
11
+ * `OpenDraftContext`): whether it holds typing not yet in the document, and
12
+ * how to commit it. `draftDirty` counts that typing as unsaved work, and
13
+ * `prepare` commits it before a save reads the document.
14
+ */
15
+ export function useOpenDraftCommit({
16
+ editorDirty,
17
+ }: {
18
+ /** The editor's own dirty flag: a commit landing changes it, which is when the waiting save runs. */
19
+ editorDirty: boolean;
20
+ }) {
21
+ const [draftDirty, setDraftDirty] = useState(false);
22
+ const draftCommit = useRef<(() => boolean) | undefined>(undefined);
23
+ const afterCommit = useRef<(() => void) | undefined>(undefined);
24
+ const openDraft = useMemo<OpenDraftSink>(
25
+ () => ({ setDirty: setDraftDirty, commitRef: draftCommit }),
26
+ [],
27
+ );
28
+ // A save reads the committed document, so an open draft is committed first and the save runs once that has rendered.
29
+ const prepare = useCallback(
30
+ (run: () => Promise<void> | void): Promise<void> | void => {
31
+ if (!draftCommit.current) return run();
32
+ if (!draftCommit.current()) return;
33
+ return new Promise<void>((resolve) => {
34
+ afterCommit.current = () => resolve(run());
35
+ });
36
+ },
37
+ [],
38
+ );
39
+ useEffect(() => {
40
+ if (draftDirty || !afterCommit.current) return;
41
+ const run = afterCommit.current;
42
+ afterCommit.current = undefined;
43
+ run();
44
+ }, [draftDirty, editorDirty]);
45
+
46
+ return { draftDirty, openDraft, prepare };
47
+ }
@@ -0,0 +1,314 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { useEffect, useRef, useState, type MutableRefObject } from "react";
5
+ import type { Workspace } from "../DocumentStorage";
6
+ import { now } from "../../utils/clock";
7
+ import type { DashboardDocument } from "./document";
8
+ import { readForEditor } from "./readForEditor";
9
+ import type { BuilderEvent } from "./telemetry";
10
+
11
+ export const LEGACY_REFUSAL =
12
+ "a .malloynb notebook is read, not edited. Fix: rewrite it as a `.malloy` notebook under notebooks/ to edit it here.";
13
+
14
+ const WITHHELD_REFUSAL =
15
+ "the server did not send this notebook's text, so there is nothing here to edit. Fix: edit the file in the package.";
16
+
17
+ /** The document the builder is on, and what it was opened from. */
18
+ export interface OpenedDocument {
19
+ source: string;
20
+ document: DashboardDocument;
21
+ conversion?: { from: string; to: string };
22
+ /** The package file when this opened, which a draft's save overwrites. */
23
+ packageText?: string;
24
+ generation: number;
25
+ }
26
+
27
+ /**
28
+ * Which text the editor opens, and when a newer one is held back.
29
+ *
30
+ * Three channels can hold the document — the host's record, a copy the reader
31
+ * resumes, the package file — and each can move under an open editor. This
32
+ * decides which one the builder is on, opens it (reporting the open or the
33
+ * refusal), and keeps a newer version behind a banner rather than loading it
34
+ * over work the reader has not saved.
35
+ */
36
+ export function useOpenedDocument({
37
+ workspace,
38
+ draft,
39
+ draftChecked,
40
+ readFailure,
41
+ packageText,
42
+ fetchedAt,
43
+ packageSettled,
44
+ modelPath,
45
+ notebook,
46
+ noun,
47
+ refusedEvent,
48
+ legacyFormat,
49
+ textHeld,
50
+ startedAt,
51
+ onEventRef,
52
+ }: {
53
+ workspace: Workspace | undefined;
54
+ /** The host's copy, if one was read. */
55
+ draft: string | undefined;
56
+ /** Whether the host's store has answered. */
57
+ draftChecked: boolean;
58
+ readFailure: string | undefined;
59
+ /** The file as the package's last fetch has it. */
60
+ packageText: string | undefined;
61
+ /** When that fetch landed. */
62
+ fetchedAt: number;
63
+ /** Whether the package's fetch has succeeded and is not refetching. */
64
+ packageSettled: boolean;
65
+ modelPath: string;
66
+ notebook: boolean;
67
+ noun: string;
68
+ refusedEvent: "notebook.open_refused" | "dashboard.open_refused";
69
+ legacyFormat: boolean;
70
+ /** The document is held as text by the host: there is no package file to fall back on. */
71
+ textHeld: boolean;
72
+ /** When the open was asked for, so "opened" can say how long it took. */
73
+ startedAt: MutableRefObject<number>;
74
+ onEventRef: MutableRefObject<((event: BuilderEvent) => void) | undefined>;
75
+ }) {
76
+ // The host's copy is the record: the editor opens it, writes back to it,
77
+ // and never offers to "resume" it, because a record is not a pending edit.
78
+ const authoritative = workspace?.authoritative === true;
79
+ // The package file is a deploy of the record, so opening it when the record
80
+ // itself could not be read would put a reader on the wrong document.
81
+ const blockedOnRecord = authoritative && readFailure !== undefined;
82
+
83
+ // What the editor opens: the record when the host keeps one, the copy when
84
+ // the reader chose to resume it, the package file otherwise. `generation`
85
+ // remounts the builder for a fresh history when that choice changes.
86
+ const [resume, setResume] = useState<boolean | undefined>(undefined);
87
+ const fromDraft = authoritative
88
+ ? draft !== undefined
89
+ : resume === true && draft !== undefined;
90
+ const [opened, setOpened] = useState<OpenedDocument | undefined>(undefined);
91
+ const [openError, setOpenError] = useState<string | undefined>(
92
+ legacyFormat ? LEGACY_REFUSAL : undefined,
93
+ );
94
+ // The package's hash after this editor's last write, as the server computed
95
+ // it: the base the next save is spliced against.
96
+ const savedHashRef = useRef<string | undefined>(undefined);
97
+ // The package file as it stood when the editor last opened a document. The
98
+ // base a save is spliced against is the package's, which is NOT the text
99
+ // the builder holds: a resumed copy was opened from the store, and a
100
+ // version held back while the reader keeps editing has moved the fetch
101
+ // past the file the reader is answering for.
102
+ const packageBaseRef = useRef<string | undefined>(undefined);
103
+ // What this editor last wrote into the package, and the fetch it was
104
+ // written on top of. The write is in the file before the fetch behind
105
+ // `packageText` catches up, so until a fetch actually lands `packageText`
106
+ // is the text the save replaced rather than a version to open. Tied to the
107
+ // fetch and not to the text, so the NEXT fetch speaks for the file whether
108
+ // it carries this editor's write or someone else's.
109
+ const [wrote, setWrote] = useState<
110
+ { text: string; onFetch: number } | undefined
111
+ >(undefined);
112
+ const fetchedAtRef = useRef(fetchedAt);
113
+ fetchedAtRef.current = fetchedAt;
114
+ const packageNow =
115
+ wrote !== undefined && wrote.onFetch === fetchedAt
116
+ ? wrote.text
117
+ : packageText;
118
+ const packageNowRef = useRef(packageNow);
119
+ packageNowRef.current = packageNow;
120
+ const latest = fromDraft ? draft : packageNow;
121
+
122
+ // Whether the builder holds edits the record does not have, and the version
123
+ // being held back because of them.
124
+ const [dirty, setDirty] = useState(false);
125
+ const [accepted, setAccepted] = useState<string | undefined>(undefined);
126
+ // The text on this channel the editor has already reckoned with: what it
127
+ // opened, and what it wrote. Compared against the CHANNEL rather than
128
+ // against the builder, because the two diverge legitimately — a copy saved
129
+ // beside the package leaves the builder ahead of a package that has not
130
+ // moved, and reading that as an incoming version would offer a reader
131
+ // their own work back forever.
132
+ const [seen, setSeen] = useState<string | undefined>(undefined);
133
+ const incoming = latest !== undefined && latest !== seen;
134
+ // What the builder is on. A save does not remount it, so this follows the
135
+ // save rather than the text the builder was opened with.
136
+ const current = opened?.source;
137
+ // Work the package does not have: unsaved edits, or a copy saved beside the
138
+ // package that differs from it. Either way a newer version is held back
139
+ // behind the banner rather than loaded over what the reader made. Only a
140
+ // copy the builder is ON counts: one the reader passed over for the package
141
+ // file is not work in front of them, and holding a version back for it
142
+ // would tell them "your edits are still here" about edits they never opened.
143
+ const aheadOfPackage =
144
+ !authoritative &&
145
+ draft !== undefined &&
146
+ current === draft &&
147
+ draft !== packageNow;
148
+ const holding =
149
+ incoming &&
150
+ (dirty || aheadOfPackage) &&
151
+ current !== undefined &&
152
+ latest !== accepted;
153
+ const opening = holding ? current : incoming ? latest : (current ?? latest);
154
+ const held = holding ? latest : undefined;
155
+
156
+ // Read by the open effect so it keeps its single dependency: the guard must
157
+ // not re-run the effect when what it compares against changes.
158
+ const openedSourceRef = useRef(current);
159
+ openedSourceRef.current = current;
160
+ const latestRef = useRef(latest);
161
+ latestRef.current = latest;
162
+ const fromRef = useRef<"package" | "draft" | "record">("package");
163
+ fromRef.current = fromDraft
164
+ ? authoritative
165
+ ? "record"
166
+ : "draft"
167
+ : "package";
168
+ useEffect(() => {
169
+ // Not until the storage answer is in. Opening on the package file while
170
+ // the editor still has no idea whether the host keeps the record would
171
+ // open the wrong document, and report an open of it.
172
+ if (opening === undefined || !draftChecked || blockedOnRecord) return;
173
+ if (opening === openedSourceRef.current) return;
174
+ let stale = false;
175
+ const packageAtOpen = packageNowRef.current;
176
+ const latestAtOpen = latestRef.current;
177
+ void readForEditor(opening, modelPath, textHeld)
178
+ .then((result) => {
179
+ if (stale) return;
180
+ if (result.ok === false) {
181
+ setOpenError(result.reason);
182
+ onEventRef.current?.({
183
+ type: refusedEvent,
184
+ reason: result.reason,
185
+ });
186
+ return;
187
+ }
188
+ setOpenError(undefined);
189
+ // A different document is open, so what this editor wrote before is
190
+ // no longer the base anything is spliced against; the package file
191
+ // the reader is now answering for is the one current at this open.
192
+ savedHashRef.current = undefined;
193
+ packageBaseRef.current = packageAtOpen;
194
+ setWrote(undefined);
195
+ setAccepted(undefined);
196
+ setSeen(latestAtOpen);
197
+ setOpened((previous) => ({
198
+ source: opening,
199
+ document: result.document,
200
+ ...(result.conversion ? { conversion: result.conversion } : {}),
201
+ ...(packageAtOpen !== undefined
202
+ ? { packageText: packageAtOpen }
203
+ : {}),
204
+ generation: (previous?.generation ?? 0) + 1,
205
+ }));
206
+ onEventRef.current?.(
207
+ notebook || result.document.kind === "notebook"
208
+ ? {
209
+ type: "notebook.opened",
210
+ from:
211
+ fromRef.current === "package" ? "package" : "record",
212
+ cells: result.document.tiles.length,
213
+ durationMs: now() - startedAt.current,
214
+ }
215
+ : {
216
+ type: "dashboard.opened",
217
+ from: fromRef.current,
218
+ tiles: result.document.tiles.length,
219
+ durationMs: now() - startedAt.current,
220
+ },
221
+ );
222
+ })
223
+ .catch((error: unknown) => {
224
+ if (stale) return;
225
+ const reason = `Could not read the ${noun}: ${error instanceof Error ? error.message : String(error)}`;
226
+ setOpenError(reason);
227
+ onEventRef.current?.({ type: refusedEvent, reason });
228
+ });
229
+ return () => {
230
+ stale = true;
231
+ };
232
+ }, [
233
+ opening,
234
+ modelPath,
235
+ draftChecked,
236
+ blockedOnRecord,
237
+ notebook,
238
+ noun,
239
+ refusedEvent,
240
+ textHeld,
241
+ // Refs, stable: read when the open lands, never a reason to open again.
242
+ startedAt,
243
+ onEventRef,
244
+ ]);
245
+
246
+ useEffect(() => {
247
+ if (legacyFormat)
248
+ onEventRef.current?.({
249
+ type: "notebook.open_refused",
250
+ reason: LEGACY_REFUSAL,
251
+ });
252
+ }, [legacyFormat, onEventRef]);
253
+
254
+ // The record is the only document a text source has, so it must exist and be one the host calls authoritative.
255
+ useEffect(() => {
256
+ if (!textHeld || !draftChecked || opened || readFailure !== undefined)
257
+ return;
258
+ if (!authoritative)
259
+ setOpenError(
260
+ "a document held as text needs a storage whose workspace is authoritative. Fix: mark the workspace that keeps it `authoritative`.",
261
+ );
262
+ else if (draft === undefined)
263
+ setOpenError("the host's storage has no document at this location.");
264
+ }, [textHeld, draftChecked, opened, readFailure, authoritative, draft]);
265
+
266
+ const withheld =
267
+ notebook &&
268
+ !textHeld &&
269
+ !opened &&
270
+ draftChecked &&
271
+ !fromDraft &&
272
+ packageSettled &&
273
+ packageText === undefined;
274
+ useEffect(() => {
275
+ if (!withheld) return;
276
+ setOpenError(WITHHELD_REFUSAL);
277
+ onEventRef.current?.({
278
+ type: "notebook.open_refused",
279
+ reason: WITHHELD_REFUSAL,
280
+ });
281
+ }, [withheld, onEventRef]);
282
+
283
+ // An explicit choice is a choice about the very text that would otherwise
284
+ // be held back, so it is never held.
285
+ const choose = (resumeDraft: boolean) => {
286
+ startedAt.current = now();
287
+ setAccepted(resumeDraft ? draft : packageNow);
288
+ setResume(resumeDraft);
289
+ };
290
+ const acceptHeld = () => {
291
+ startedAt.current = now();
292
+ setAccepted(held);
293
+ };
294
+
295
+ return {
296
+ authoritative,
297
+ blockedOnRecord,
298
+ resume,
299
+ setResume,
300
+ fromDraft,
301
+ opened,
302
+ setOpened,
303
+ openError,
304
+ savedHashRef,
305
+ packageBaseRef,
306
+ setWrote,
307
+ fetchedAtRef,
308
+ setDirty,
309
+ setSeen,
310
+ held,
311
+ choose,
312
+ acceptHeld,
313
+ };
314
+ }