@malloy-publisher/sdk 0.9.1 → 0.9.3

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 (410) hide show
  1. package/dist/{RenderedResult-BRgIgxWh.es.js → RenderedResult-DwB0-9Yx.es.js} +219 -210
  2. package/dist/RenderedResult-hKjFEqaN.cjs.js +141 -0
  3. package/dist/{ServerProvider-Boxrv6Ym.es.js → ServerProvider-CcSrGaRN.es.js} +1473 -1326
  4. package/dist/ServerProvider-Gbf1YPOP.cjs.js +1 -0
  5. package/dist/builder/index.cjs.js +143 -58
  6. package/dist/builder/index.es.js +12179 -9318
  7. package/dist/builder-entry.d.ts +5 -1
  8. package/dist/client/api.d.ts +247 -56
  9. package/dist/client/configuration.d.ts +1 -1
  10. package/dist/client/index.cjs.js +1 -1
  11. package/dist/client/index.d.ts +1 -1
  12. package/dist/client/index.es.js +94 -88
  13. package/dist/client-entry.d.ts +2 -0
  14. package/dist/components/ApiErrorDisplay.d.ts +11 -0
  15. package/dist/components/AppDialog.d.ts +6 -6
  16. package/dist/components/Dashboard/Dashboard.d.ts +8 -15
  17. package/dist/components/Dashboard/DashboardGrid.d.ts +38 -6
  18. package/dist/components/Dashboard/DashboardTile.d.ts +21 -2
  19. package/dist/components/Dashboard/DashboardView.d.ts +71 -0
  20. package/dist/components/Dashboard/TileCard.d.ts +30 -5
  21. package/dist/components/Dashboard/TileFilterTag.d.ts +12 -0
  22. package/dist/components/Dashboard/dropValueUrlTags.d.ts +6 -0
  23. package/dist/components/Dashboard/index.d.ts +5 -1
  24. package/dist/components/Dashboard/telemetry.d.ts +16 -3
  25. package/dist/components/Dashboard/textSource.d.ts +19 -0
  26. package/dist/components/DashboardBuilder/AddTileDialog.d.ts +15 -9
  27. package/dist/components/DashboardBuilder/BuilderDialogs.d.ts +25 -0
  28. package/dist/components/DashboardBuilder/BuilderGrid.d.ts +31 -0
  29. package/dist/components/DashboardBuilder/BuilderHeader.d.ts +17 -0
  30. package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +23 -15
  31. package/dist/components/DashboardBuilder/ChartPicker.d.ts +31 -0
  32. package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +56 -21
  33. package/dist/components/DashboardBuilder/DashboardEditor.d.ts +44 -5
  34. package/dist/components/DashboardBuilder/EditorSurface.d.ts +33 -0
  35. package/dist/components/DashboardBuilder/FilterDialog.d.ts +2 -2
  36. package/dist/components/DashboardBuilder/FilterStrip.d.ts +8 -4
  37. package/dist/components/DashboardBuilder/InlineMarkdown.d.ts +18 -0
  38. package/dist/components/DashboardBuilder/InlineText.d.ts +19 -0
  39. package/dist/components/DashboardBuilder/NotebookEditor.d.ts +15 -0
  40. package/dist/components/DashboardBuilder/SourceViewPicker.d.ts +9 -0
  41. package/dist/components/DashboardBuilder/TextTileBody.d.ts +10 -0
  42. package/dist/components/DashboardBuilder/TileFrame.d.ts +51 -12
  43. package/dist/components/DashboardBuilder/TileMenu.d.ts +12 -6
  44. package/dist/components/DashboardBuilder/annotationText.d.ts +4 -0
  45. package/dist/components/DashboardBuilder/builderReport.d.ts +13 -0
  46. package/dist/components/DashboardBuilder/catalog.d.ts +4 -18
  47. package/dist/components/DashboardBuilder/changedTile.d.ts +7 -0
  48. package/dist/components/DashboardBuilder/chartLine.d.ts +33 -0
  49. package/dist/components/DashboardBuilder/controls.d.ts +2 -2
  50. package/dist/components/DashboardBuilder/document.d.ts +42 -26
  51. package/dist/components/DashboardBuilder/documentSession.d.ts +100 -0
  52. package/dist/components/DashboardBuilder/imports.d.ts +19 -0
  53. package/dist/components/DashboardBuilder/inlineEdit.d.ts +18 -0
  54. package/dist/components/DashboardBuilder/legacyNotebook.d.ts +73 -0
  55. package/dist/components/DashboardBuilder/loadMalloy.d.ts +15 -0
  56. package/dist/components/DashboardBuilder/malloyTree.d.ts +119 -0
  57. package/dist/components/DashboardBuilder/newDashboard.d.ts +3 -1
  58. package/dist/components/DashboardBuilder/openDraft.d.ts +10 -0
  59. package/dist/components/DashboardBuilder/preview.d.ts +24 -4
  60. package/dist/components/DashboardBuilder/readDocument.d.ts +6 -1
  61. package/dist/components/DashboardBuilder/readForEditor.d.ts +16 -0
  62. package/dist/components/DashboardBuilder/sortable.d.ts +16 -0
  63. package/dist/components/DashboardBuilder/spliceDocument.d.ts +15 -13
  64. package/dist/components/DashboardBuilder/spliceResult.d.ts +11 -0
  65. package/dist/components/DashboardBuilder/tagParse.d.ts +24 -0
  66. package/dist/components/DashboardBuilder/telemetry.d.ts +43 -0
  67. package/dist/components/DashboardBuilder/testing/fixtures.d.ts +3 -1
  68. package/dist/components/DashboardBuilder/testing/inline.d.ts +4 -0
  69. package/dist/components/DashboardBuilder/testing/resize.d.ts +7 -0
  70. package/dist/components/DashboardBuilder/tileDisplayTitle.d.ts +7 -0
  71. package/dist/components/DashboardBuilder/useBuilderSelection.d.ts +53 -0
  72. package/dist/components/DashboardBuilder/useBuilderSession.d.ts +86 -0
  73. package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +9 -6
  74. package/dist/components/DashboardBuilder/useControlBindings.d.ts +30 -0
  75. package/dist/components/DashboardBuilder/useConversionConfirm.d.ts +20 -0
  76. package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +19 -55
  77. package/dist/components/DashboardBuilder/useDocumentEditor.d.ts +105 -0
  78. package/dist/components/DashboardBuilder/useDraft.d.ts +3 -1
  79. package/dist/components/DashboardBuilder/useExitGuard.d.ts +19 -0
  80. package/dist/components/DashboardBuilder/useFilterForm.d.ts +3 -3
  81. package/dist/components/DashboardBuilder/useHostCopy.d.ts +26 -0
  82. package/dist/components/DashboardBuilder/useOpenDraftCommit.d.ts +17 -0
  83. package/dist/components/DashboardBuilder/useOpenedDocument.d.ts +71 -0
  84. package/dist/components/DashboardBuilder/useSaveChannel.d.ts +50 -0
  85. package/dist/components/DashboardBuilder/useTileEditing.d.ts +33 -0
  86. package/dist/components/DashboardBuilder/useTileResize.d.ts +6 -3
  87. package/dist/components/DocumentCreate/canRetry.d.ts +6 -0
  88. package/dist/components/DocumentCreate/createDocument.d.ts +58 -0
  89. package/dist/components/DocumentCreate/documentPath.d.ts +10 -0
  90. package/dist/components/DocumentCreate/events.d.ts +10 -0
  91. package/dist/components/DocumentCreate/exportedSources.d.ts +7 -0
  92. package/dist/components/DocumentCreate/guards.d.ts +12 -0
  93. package/dist/components/DocumentCreate/index.d.ts +6 -0
  94. package/dist/components/DocumentCreate/newNotebook.d.ts +9 -0
  95. package/dist/components/DocumentCreate/useDocumentChoices.d.ts +32 -0
  96. package/dist/components/DocumentStorage/DocumentStorage.d.ts +2 -1
  97. package/dist/components/Loading.d.ts +13 -0
  98. package/dist/components/NarrowEditGate.d.ts +8 -0
  99. package/dist/components/Notebook/Notebook.d.ts +1 -1
  100. package/dist/components/Notebook/NotebookCell.d.ts +1 -5
  101. package/dist/components/Package/NewDocumentDialog.d.ts +33 -0
  102. package/dist/components/Package/Package.d.ts +17 -1
  103. package/dist/components/Package/documentLocation.d.ts +28 -0
  104. package/dist/components/Package/index.d.ts +5 -1
  105. package/dist/components/Package/useDocumentLocation.d.ts +12 -0
  106. package/dist/components/PackageSection.d.ts +2 -2
  107. package/dist/components/Prose.d.ts +1 -1
  108. package/dist/components/RenderedResult/RenderedResult.d.ts +2 -0
  109. package/dist/components/RenderedResult/ResultContainer.d.ts +8 -1
  110. package/dist/components/RenderedResult/ResultPanel.d.ts +3 -1
  111. package/dist/components/RenderedResult/loadRenderer.d.ts +7 -0
  112. package/dist/components/RenderedResult/resultSizing.d.ts +9 -1
  113. package/dist/components/ServerProvider.d.ts +2 -1
  114. package/dist/components/UnsavedChangesDialog.d.ts +9 -0
  115. package/dist/components/given/GivensPanel.d.ts +2 -2
  116. package/dist/components/given/paramCodec.d.ts +8 -1
  117. package/dist/components/index.d.ts +3 -2
  118. package/dist/components/styles.d.ts +4 -4
  119. package/dist/hooks/givenValue.d.ts +6 -0
  120. package/dist/hooks/index.d.ts +2 -0
  121. package/dist/hooks/useCompiledDocument.d.ts +23 -0
  122. package/dist/hooks/useDocumentControls.d.ts +6 -1
  123. package/dist/hooks/useNarrowScreen.d.ts +4 -0
  124. package/dist/hooks/useNearViewport.d.ts +16 -0
  125. package/dist/hooks/useQueryResult.d.ts +2 -0
  126. package/dist/hooks/useQueryWithApiError.d.ts +7 -2
  127. package/dist/hooks/useSettled.d.ts +14 -0
  128. package/dist/hooks/useSuggestOptions.d.ts +9 -2
  129. package/dist/index.cjs.js +23 -24
  130. package/dist/index.es.js +9041 -7002
  131. package/dist/text/index.cjs.js +1 -0
  132. package/dist/text/index.d.ts +2 -0
  133. package/dist/text/index.es.js +6 -0
  134. package/dist/text-entry-BJwltZiy.cjs.js +7 -0
  135. package/dist/text-entry-CA0LZ1xL.es.js +196 -0
  136. package/dist/text-entry.d.ts +2 -0
  137. package/dist/theme/accent.d.ts +29 -0
  138. package/dist/theme/buildVegaThemeOverride.d.ts +3 -3
  139. package/dist/theme/index.d.ts +3 -0
  140. package/dist/theme/keys.d.ts +1 -1
  141. package/dist/theme/motion.d.ts +31 -0
  142. package/dist/theme/readChartAnnotations.d.ts +1 -1
  143. package/dist/theme/resolveTheme.d.ts +4 -4
  144. package/dist/theme/types.d.ts +33 -5
  145. package/dist/{Dashboard-H1kJlDFo.es.js → useCompiledDocument-BU6d8FPp.es.js} +17904 -19057
  146. package/dist/useCompiledDocument-pOg2W-Yt.cjs.js +142 -0
  147. package/dist/utils/clock.d.ts +2 -0
  148. package/dist/utils/malloyText.d.ts +104 -0
  149. package/package.json +20 -11
  150. package/src/builder-entry.ts +36 -0
  151. package/src/client-entry.ts +8 -0
  152. package/src/clientEntry.spec.ts +17 -0
  153. package/src/components/ApiErrorDisplay.tsx +37 -18
  154. package/src/components/AppDialog.tsx +8 -7
  155. package/src/components/Dashboard/Dashboard.spec.tsx +3 -32
  156. package/src/components/Dashboard/Dashboard.tsx +29 -280
  157. package/src/components/Dashboard/DashboardGrid.tsx +87 -8
  158. package/src/components/Dashboard/DashboardTile.spec.tsx +121 -2
  159. package/src/components/Dashboard/DashboardTile.tsx +121 -30
  160. package/src/components/Dashboard/DashboardView.spec.tsx +368 -0
  161. package/src/components/Dashboard/DashboardView.tsx +428 -0
  162. package/src/components/Dashboard/RowsDialog.tsx +4 -3
  163. package/src/components/Dashboard/TileCard.tsx +60 -11
  164. package/src/components/Dashboard/TileFilterTag.spec.tsx +73 -0
  165. package/src/components/Dashboard/TileFilterTag.tsx +78 -0
  166. package/src/components/Dashboard/defaultTileSpan.spec.ts +32 -0
  167. package/src/components/Dashboard/dropValueUrlTags.spec.ts +177 -0
  168. package/src/components/Dashboard/dropValueUrlTags.ts +108 -0
  169. package/src/components/Dashboard/index.ts +13 -1
  170. package/src/components/Dashboard/telemetry.ts +17 -5
  171. package/src/components/Dashboard/textSource.spec.ts +36 -0
  172. package/src/components/Dashboard/textSource.ts +39 -0
  173. package/src/components/DashboardBuilder/AddTileDialog.spec.tsx +88 -0
  174. package/src/components/DashboardBuilder/AddTileDialog.tsx +124 -138
  175. package/src/components/DashboardBuilder/BuilderDialogs.tsx +162 -0
  176. package/src/components/DashboardBuilder/BuilderGrid.tsx +245 -0
  177. package/src/components/DashboardBuilder/BuilderHeader.tsx +107 -0
  178. package/src/components/DashboardBuilder/BuilderToolbar.tsx +111 -80
  179. package/src/components/DashboardBuilder/ChartPicker.render.spec.tsx +25 -0
  180. package/src/components/DashboardBuilder/ChartPicker.spec.ts +77 -0
  181. package/src/components/DashboardBuilder/ChartPicker.tsx +156 -0
  182. package/src/components/DashboardBuilder/DashboardBuilder.charts.spec.tsx +205 -0
  183. package/src/components/DashboardBuilder/DashboardBuilder.exit.spec.tsx +183 -0
  184. package/src/components/DashboardBuilder/DashboardBuilder.inline.spec.tsx +373 -0
  185. package/src/components/DashboardBuilder/DashboardBuilder.notebook.spec.tsx +310 -0
  186. package/src/components/DashboardBuilder/DashboardBuilder.openDraft.spec.tsx +105 -0
  187. package/src/components/DashboardBuilder/DashboardBuilder.sources.spec.tsx +117 -0
  188. package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +146 -108
  189. package/src/components/DashboardBuilder/DashboardBuilder.textTiles.spec.tsx +179 -0
  190. package/src/components/DashboardBuilder/DashboardBuilder.tsx +330 -512
  191. package/src/components/DashboardBuilder/DashboardEditor.authoritative.spec.tsx +55 -25
  192. package/src/components/DashboardBuilder/DashboardEditor.emptyStart.spec.tsx +142 -0
  193. package/src/components/DashboardBuilder/DashboardEditor.filterTag.spec.tsx +162 -0
  194. package/src/components/DashboardBuilder/DashboardEditor.notebook.spec.tsx +310 -0
  195. package/src/components/DashboardBuilder/DashboardEditor.package.spec.tsx +39 -13
  196. package/src/components/DashboardBuilder/DashboardEditor.resourceUri.spec.tsx +9 -26
  197. package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +102 -25
  198. package/src/components/DashboardBuilder/DashboardEditor.textSource.spec.tsx +317 -0
  199. package/src/components/DashboardBuilder/DashboardEditor.tsx +252 -780
  200. package/src/components/DashboardBuilder/DrillDialog.tsx +2 -2
  201. package/src/components/DashboardBuilder/EditorSurface.tsx +471 -0
  202. package/src/components/DashboardBuilder/FilterDialog.tsx +23 -11
  203. package/src/components/DashboardBuilder/FilterStrip.tsx +130 -70
  204. package/src/components/DashboardBuilder/InlineMarkdown.tsx +182 -0
  205. package/src/components/DashboardBuilder/InlineText.tsx +112 -0
  206. package/src/components/DashboardBuilder/NotebookEditor.tsx +44 -0
  207. package/src/components/DashboardBuilder/SourceViewPicker.tsx +122 -0
  208. package/src/components/DashboardBuilder/TextTileBody.tsx +31 -0
  209. package/src/components/DashboardBuilder/TileFrame.tsx +302 -163
  210. package/src/components/DashboardBuilder/TileMenu.tsx +125 -97
  211. package/src/components/DashboardBuilder/annotationText.spec.ts +33 -0
  212. package/src/components/DashboardBuilder/annotationText.ts +17 -0
  213. package/src/components/DashboardBuilder/artifactBlock.spec.ts +248 -0
  214. package/src/components/DashboardBuilder/artifactTagParity.spec.ts +46 -0
  215. package/src/components/DashboardBuilder/builderReport.ts +74 -0
  216. package/src/components/DashboardBuilder/catalog.aggregate.spec.ts +49 -0
  217. package/src/components/DashboardBuilder/catalog.spec.ts +2 -44
  218. package/src/components/DashboardBuilder/catalog.ts +61 -57
  219. package/src/components/DashboardBuilder/changedTile.spec.ts +46 -0
  220. package/src/components/DashboardBuilder/changedTile.ts +28 -0
  221. package/src/components/DashboardBuilder/chartLine.spec.ts +180 -0
  222. package/src/components/DashboardBuilder/chartLine.ts +121 -0
  223. package/src/components/DashboardBuilder/controls.spec.ts +21 -10
  224. package/src/components/DashboardBuilder/controls.ts +22 -8
  225. package/src/components/DashboardBuilder/document.ts +53 -5
  226. package/src/components/DashboardBuilder/documentSession.spec.ts +209 -0
  227. package/src/components/DashboardBuilder/documentSession.ts +258 -0
  228. package/src/components/DashboardBuilder/emptyStart.spec.ts +131 -0
  229. package/src/components/DashboardBuilder/imports.spec.ts +61 -0
  230. package/src/components/DashboardBuilder/imports.ts +93 -0
  231. package/src/components/DashboardBuilder/inlineEdit.tsx +78 -0
  232. package/src/components/DashboardBuilder/invariants.spec.ts +6 -2
  233. package/src/components/DashboardBuilder/legacyNotebook.chart.spec.ts +77 -0
  234. package/src/components/DashboardBuilder/legacyNotebook.convert.spec.ts +630 -0
  235. package/src/components/DashboardBuilder/legacyNotebook.read.spec.ts +338 -0
  236. package/src/components/DashboardBuilder/legacyNotebook.ts +1117 -0
  237. package/src/components/DashboardBuilder/loadMalloy.spec.ts +80 -0
  238. package/src/components/DashboardBuilder/loadMalloy.ts +39 -0
  239. package/src/components/DashboardBuilder/malloyTree.ts +159 -7
  240. package/src/components/DashboardBuilder/newDashboard.spec.ts +46 -2
  241. package/src/components/DashboardBuilder/newDashboard.ts +30 -3
  242. package/src/components/DashboardBuilder/openDraft.ts +52 -0
  243. package/src/components/DashboardBuilder/preview.shapes.spec.ts +75 -0
  244. package/src/components/DashboardBuilder/preview.spec.ts +159 -12
  245. package/src/components/DashboardBuilder/preview.ts +83 -9
  246. package/src/components/DashboardBuilder/readDocument.spec.ts +174 -26
  247. package/src/components/DashboardBuilder/readDocument.ts +137 -45
  248. package/src/components/DashboardBuilder/readForEditor.spec.ts +80 -0
  249. package/src/components/DashboardBuilder/readForEditor.ts +52 -0
  250. package/src/components/DashboardBuilder/scannerShapes.spec.ts +14 -13
  251. package/src/components/DashboardBuilder/sortable.spec.ts +89 -0
  252. package/src/components/DashboardBuilder/sortable.tsx +11 -10
  253. package/src/components/DashboardBuilder/spliceDocument.chart.spec.ts +340 -0
  254. package/src/components/DashboardBuilder/spliceDocument.imports.spec.ts +167 -0
  255. package/src/components/DashboardBuilder/spliceDocument.spec.ts +229 -94
  256. package/src/components/DashboardBuilder/spliceDocument.ts +768 -131
  257. package/src/components/DashboardBuilder/spliceResult.ts +15 -0
  258. package/src/components/DashboardBuilder/statementExtent.spec.ts +33 -30
  259. package/src/components/DashboardBuilder/tagParse.spec.ts +149 -0
  260. package/src/components/DashboardBuilder/tagParse.ts +103 -0
  261. package/src/components/DashboardBuilder/telemetry.ts +47 -0
  262. package/src/components/DashboardBuilder/testing/artifactTagParity.json +253 -0
  263. package/src/components/DashboardBuilder/testing/documentKindRule.json +40 -0
  264. package/src/components/DashboardBuilder/testing/fixtures.ts +16 -1
  265. package/src/components/DashboardBuilder/testing/inline.ts +25 -0
  266. package/src/components/DashboardBuilder/testing/resize.ts +48 -0
  267. package/src/components/DashboardBuilder/testing/urlRenderTags.json +62 -0
  268. package/src/components/DashboardBuilder/textTiles.spec.ts +760 -0
  269. package/src/components/DashboardBuilder/tileDisplayTitle.ts +13 -0
  270. package/src/components/DashboardBuilder/useBuilderSelection.ts +121 -0
  271. package/src/components/DashboardBuilder/useBuilderSession.spec.ts +250 -0
  272. package/src/components/DashboardBuilder/useBuilderSession.ts +255 -0
  273. package/src/components/DashboardBuilder/useBuilderShortcuts.overlay.spec.tsx +97 -0
  274. package/src/components/DashboardBuilder/useBuilderShortcuts.spec.tsx +60 -0
  275. package/src/components/DashboardBuilder/useBuilderShortcuts.ts +45 -9
  276. package/src/components/DashboardBuilder/useControlBindings.ts +152 -0
  277. package/src/components/DashboardBuilder/useConversionConfirm.ts +55 -0
  278. package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +16 -13
  279. package/src/components/DashboardBuilder/useDashboardEditor.ts +57 -181
  280. package/src/components/DashboardBuilder/useDocumentEditor.spec.ts +565 -0
  281. package/src/components/DashboardBuilder/useDocumentEditor.ts +399 -0
  282. package/src/components/DashboardBuilder/useDraft.ts +15 -2
  283. package/src/components/DashboardBuilder/useExitGuard.spec.ts +159 -0
  284. package/src/components/DashboardBuilder/useExitGuard.ts +89 -0
  285. package/src/components/DashboardBuilder/useFilterForm.existing.spec.tsx +41 -0
  286. package/src/components/DashboardBuilder/useFilterForm.spec.tsx +24 -2
  287. package/src/components/DashboardBuilder/useFilterForm.ts +41 -13
  288. package/src/components/DashboardBuilder/useHostCopy.ts +104 -0
  289. package/src/components/DashboardBuilder/useOpenDraftCommit.ts +47 -0
  290. package/src/components/DashboardBuilder/useOpenedDocument.ts +314 -0
  291. package/src/components/DashboardBuilder/useSaveChannel.ts +275 -0
  292. package/src/components/DashboardBuilder/useTileEditing.ts +141 -0
  293. package/src/components/DashboardBuilder/useTileReorder.spec.tsx +28 -5
  294. package/src/components/DashboardBuilder/useTileReorder.ts +5 -7
  295. package/src/components/DashboardBuilder/useTileResize.spec.tsx +30 -5
  296. package/src/components/DashboardBuilder/useTileResize.ts +31 -8
  297. package/src/components/DataAppViewer/DataAppViewer.tsx +7 -4
  298. package/src/components/DocumentCreate/canRetry.spec.ts +32 -0
  299. package/src/components/DocumentCreate/canRetry.ts +19 -0
  300. package/src/components/DocumentCreate/createDocument.spec.ts +310 -0
  301. package/src/components/DocumentCreate/createDocument.ts +159 -0
  302. package/src/components/DocumentCreate/documentPath.ts +40 -0
  303. package/src/components/DocumentCreate/events.ts +15 -0
  304. package/src/components/DocumentCreate/exportedSources.ts +25 -0
  305. package/src/components/DocumentCreate/guards.ts +45 -0
  306. package/src/components/DocumentCreate/index.ts +27 -0
  307. package/src/components/DocumentCreate/newNotebook.spec.ts +156 -0
  308. package/src/components/DocumentCreate/newNotebook.ts +44 -0
  309. package/src/components/DocumentCreate/useDocumentChoices.spec.tsx +138 -0
  310. package/src/components/DocumentCreate/useDocumentChoices.ts +113 -0
  311. package/src/components/DocumentStorage/DocumentStorage.ts +2 -1
  312. package/src/components/Environment/Environment.tsx +0 -10
  313. package/src/components/Loading.tsx +14 -0
  314. package/src/components/Model/Model.tsx +16 -9
  315. package/src/components/Model/ModelExplorer.spec.tsx +2 -2
  316. package/src/components/Model/ModelExplorer.tsx +2 -2
  317. package/src/components/Model/SourcesExplorer.tsx +2 -4
  318. package/src/components/NarrowEditGate.spec.tsx +73 -0
  319. package/src/components/NarrowEditGate.tsx +29 -0
  320. package/src/components/Notebook/Notebook.layout.spec.tsx +94 -0
  321. package/src/components/Notebook/Notebook.tsx +72 -29
  322. package/src/components/Notebook/NotebookCell.tsx +90 -215
  323. package/src/components/Package/NewDocumentDialog.spec.tsx +749 -0
  324. package/src/components/Package/NewDocumentDialog.tsx +401 -0
  325. package/src/components/Package/Package.artifacts.spec.tsx +186 -0
  326. package/src/components/Package/Package.create.spec.tsx +401 -0
  327. package/src/components/Package/Package.notebooks.spec.tsx +18 -0
  328. package/src/components/Package/Package.tsx +396 -177
  329. package/src/components/Package/documentLocation.spec.ts +81 -0
  330. package/src/components/Package/documentLocation.ts +55 -0
  331. package/src/components/Package/index.ts +14 -1
  332. package/src/components/Package/useDocumentLocation.ts +59 -0
  333. package/src/components/PackageSection.tsx +2 -2
  334. package/src/components/Prose.spec.tsx +37 -0
  335. package/src/components/Prose.tsx +35 -5
  336. package/src/components/RenderedResult/RenderedResult.tsx +23 -8
  337. package/src/components/RenderedResult/ResultContainer.tsx +51 -13
  338. package/src/components/RenderedResult/ResultPanel.tsx +24 -5
  339. package/src/components/RenderedResult/loadRenderer.ts +42 -0
  340. package/src/components/RenderedResult/resultSizing.spec.ts +68 -0
  341. package/src/components/RenderedResult/resultSizing.ts +14 -3
  342. package/src/components/ServerProvider.tsx +2 -1
  343. package/src/components/UnsavedChangesDialog.spec.tsx +70 -0
  344. package/src/components/UnsavedChangesDialog.tsx +49 -0
  345. package/src/components/given/GivenInput.spec.tsx +5 -5
  346. package/src/components/given/GivenInput.tsx +46 -38
  347. package/src/components/given/GivensPanel.spec.tsx +6 -6
  348. package/src/components/given/GivensPanel.tsx +5 -11
  349. package/src/components/given/paramCodec.ts +27 -1
  350. package/src/components/index.ts +3 -2
  351. package/src/components/styles.ts +4 -5
  352. package/src/hooks/givenValue.ts +9 -0
  353. package/src/hooks/index.ts +7 -0
  354. package/src/hooks/useCompiledDocument.spec.tsx +86 -0
  355. package/src/hooks/useCompiledDocument.ts +59 -0
  356. package/src/hooks/useDimensionalFilterRangeData.ts +3 -1
  357. package/src/hooks/useDocumentControls.ts +13 -1
  358. package/src/hooks/useNarrowScreen.ts +21 -0
  359. package/src/hooks/useNearViewport.spec.tsx +150 -0
  360. package/src/hooks/useNearViewport.ts +102 -0
  361. package/src/hooks/useQueryResult.ts +23 -1
  362. package/src/hooks/useQueryWithApiError.ts +10 -3
  363. package/src/hooks/useRawQueryData.ts +3 -1
  364. package/src/hooks/useSettled.spec.ts +41 -0
  365. package/src/hooks/useSettled.ts +27 -0
  366. package/src/hooks/useSuggestOptions.dom.spec.ts +40 -1
  367. package/src/hooks/useSuggestOptions.ts +42 -10
  368. package/src/mainEntry.spec.ts +145 -0
  369. package/src/text-entry.ts +12 -0
  370. package/src/textEntry.spec.ts +112 -0
  371. package/src/theme/accent.spec.ts +116 -0
  372. package/src/theme/accent.ts +148 -0
  373. package/src/theme/buildTableCssVars.spec.ts +24 -0
  374. package/src/theme/buildTableCssVars.ts +1 -2
  375. package/src/theme/buildVegaThemeOverride.spec.ts +38 -2
  376. package/src/theme/buildVegaThemeOverride.ts +5 -5
  377. package/src/theme/defaults.ts +34 -0
  378. package/src/theme/index.ts +9 -0
  379. package/src/theme/keys.ts +6 -0
  380. package/src/theme/motion.ts +44 -0
  381. package/src/theme/readChartAnnotations.spec.ts +18 -0
  382. package/src/theme/readChartAnnotations.ts +1 -1
  383. package/src/theme/resolveTheme.spec.ts +73 -0
  384. package/src/theme/resolveTheme.ts +61 -20
  385. package/src/theme/types.ts +30 -5
  386. package/src/utils/clock.spec.ts +12 -0
  387. package/src/utils/clock.ts +6 -0
  388. package/src/{components/DashboardBuilder → utils}/malloyText.spec.ts +48 -0
  389. package/src/utils/malloyText.ts +368 -0
  390. package/src/utils/queryClient.ts +8 -0
  391. package/vite.config.ts +4 -0
  392. package/dist/Dashboard-ChyylkTD.cjs.js +0 -136
  393. package/dist/RenderedResult-AJPMUkAD.cjs.js +0 -135
  394. package/dist/ServerProvider-CXrOKD6g.cjs.js +0 -1
  395. package/dist/components/Dashboard/DashboardBar.d.ts +0 -21
  396. package/dist/components/Dashboard/ExploreDialog.d.ts +0 -20
  397. package/dist/components/DashboardBuilder/DiffDialog.d.ts +0 -15
  398. package/dist/components/DashboardBuilder/SettingsPopover.d.ts +0 -20
  399. package/dist/components/DashboardBuilder/diff.d.ts +0 -35
  400. package/dist/components/DashboardBuilder/malloyText.d.ts +0 -64
  401. package/dist/components/Package/NewDashboardDialog.d.ts +0 -18
  402. package/src/components/Dashboard/DashboardBar.tsx +0 -88
  403. package/src/components/Dashboard/ExploreDialog.tsx +0 -89
  404. package/src/components/DashboardBuilder/DiffDialog.tsx +0 -122
  405. package/src/components/DashboardBuilder/SettingsPopover.tsx +0 -168
  406. package/src/components/DashboardBuilder/diff.spec.ts +0 -44
  407. package/src/components/DashboardBuilder/diff.ts +0 -93
  408. package/src/components/DashboardBuilder/malloyText.ts +0 -214
  409. package/src/components/Package/NewDashboardDialog.spec.tsx +0 -129
  410. package/src/components/Package/NewDashboardDialog.tsx +0 -265
@@ -0,0 +1,399 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { useCallback, useMemo, useRef, useState } from "react";
5
+ import { type SpliceResult, spliceFailed } from "./spliceResult";
6
+
7
+ /**
8
+ * The editor's state: the document being edited, its history, and saving.
9
+ *
10
+ * Deliberately holds the DOCUMENT and nothing else. Given VALUES — what the
11
+ * preview is running with — are editor state rather than document state and
12
+ * live outside this hook, which is what keeps them out of the undo stack. Undo
13
+ * should take back an edit; it should not rewind the values someone was
14
+ * previewing under.
15
+ *
16
+ * `T` must survive `structuredClone`, and equal documents must `JSON.stringify` equal.
17
+ */
18
+
19
+ export interface DocumentEditor<T> {
20
+ document: T;
21
+ /** The document as of the last save (or open). */
22
+ saved: T;
23
+ /** The file as it currently stands, which is what a save patches. */
24
+ source: string;
25
+ canUndo: boolean;
26
+ canRedo: boolean;
27
+ /** Whether saving would change the file: the document differs from what was saved, or it opened that way ({@link DocumentEditorOptions.opensDirty}). */
28
+ dirty: boolean;
29
+ /** Whether the document differs from what was saved, or an undone save left its draft text in no file: work to lose, leaving out an `opensDirty` open that nothing has touched. */
30
+ edited: boolean;
31
+ /** Whether the document opened unsaved and no save has written it since ({@link DocumentEditorOptions.opensDirty}). */
32
+ pendingOpen: boolean;
33
+ /**
34
+ * Why the last save did not happen. The document is untouched when this is
35
+ * set: a refused write keeps the reader's work and reports a defect in the
36
+ * writer, rather than discarding an edit because a tool could not read back
37
+ * what it produced.
38
+ */
39
+ error?: string;
40
+ /** Apply a change. Receives a copy; mutate it freely. */
41
+ update: (change: (draft: T) => void) => void;
42
+ undo: () => void;
43
+ redo: () => void;
44
+ /** Splice the change into the file, or say why it was refused. */
45
+ save: () => Promise<SaveOutcome>;
46
+ /**
47
+ * The file a save would write, without writing it: what View change
48
+ * shows. The failure arm is the writer's refusal, worded for the author.
49
+ */
50
+ preview: () => Promise<
51
+ { ok: true; source: string } | { ok: false; reason: string }
52
+ >;
53
+ /**
54
+ * Whether the unsaved change is structural, as the caller defines it (false
55
+ * when it defines nothing): the kind of change a save reports as structural.
56
+ */
57
+ structural: boolean;
58
+ /** Whether saving the unsaved change empties the undo stack, as the caller defines it (false when it defines nothing). */
59
+ clearsHistory: boolean;
60
+ /** Whether the last save can still be taken back: true from a save that landed until the next edit, undo, redo or write. */
61
+ canUndoSave: boolean;
62
+ /** What the save `undoSave` would take back did to the file; absent once that offer is withdrawn. */
63
+ lastSave?: LastSave;
64
+ /** Write the file as it stood before the last save, then put the editor back to just before Save: edits unsaved, history intact. */
65
+ undoSave: () => Promise<SaveOutcome>;
66
+ }
67
+
68
+ export type SaveOutcome = { ok: true } | { ok: false; reason: string };
69
+
70
+ /** The save an undo would take back. */
71
+ export interface LastSave {
72
+ /** The file before the save. */
73
+ before: string;
74
+ /** The file the save wrote. */
75
+ after: string;
76
+ structural: boolean;
77
+ clearsHistory: boolean;
78
+ }
79
+
80
+ /** Why a write happens, and the document the written file holds. */
81
+ export interface SaveContext<T> {
82
+ purpose: "save" | "undo";
83
+ document: T;
84
+ }
85
+
86
+ /** Persists the patched file; rejecting leaves the editor as it was. */
87
+ export type SaveHandler<T> = (
88
+ source: string,
89
+ context: SaveContext<T>,
90
+ ) => Promise<void> | void;
91
+
92
+ interface History<T> {
93
+ /** Every document state, oldest first. */
94
+ stack: T[];
95
+ /** Which one is current. Undo and redo move this rather than mutating. */
96
+ index: number;
97
+ }
98
+
99
+ /**
100
+ * How many documents of history to keep.
101
+ *
102
+ * A whole document per entry is affordable because the document is small and
103
+ * immutable — it is the parsed structure, not the file. Keeping
104
+ * whole states is also what makes undo trivially correct: there is no inverse
105
+ * operation to get wrong for each kind of edit.
106
+ */
107
+ const HISTORY_LIMIT = 100;
108
+
109
+ export interface DocumentEditorOptions<T> {
110
+ /** The file as read from storage. */
111
+ source: string;
112
+ /** The document that file produced. */
113
+ document: T;
114
+ /** Persist the patched file. Rejecting leaves the editor dirty. */
115
+ onSave?: SaveHandler<T>;
116
+ /** Patch the document into the file, or say why that was refused. */
117
+ splice: (source: string, document: T) => Promise<SpliceResult>;
118
+ /** The open is itself unsaved: the document is a conversion of the file, so Save is on before any edit and Undo save returns to it. */
119
+ opensDirty?: boolean;
120
+ /** The file the first save overwrites when it is not `source` (a draft opened over the package's file): what Undo save writes back. */
121
+ replaces?: string;
122
+ /** Whether `document` differs structurally from `saved`; omit for never. */
123
+ structural?: (saved: T, document: T) => boolean;
124
+ /** Whether saving `document` over `saved` makes the history unsafe to step back into; the stack is then emptied on save. */
125
+ clearsHistory?: (saved: T, document: T) => boolean;
126
+ }
127
+
128
+ export function useDocumentEditor<T>(
129
+ options: DocumentEditorOptions<T>,
130
+ ): DocumentEditor<T> {
131
+ const {
132
+ splice,
133
+ onSave,
134
+ structural: isStructural,
135
+ clearsHistory: isClearing,
136
+ opensDirty = false,
137
+ } = options;
138
+ const [history, setHistory] = useState<History<T>>({
139
+ stack: [options.document],
140
+ index: 0,
141
+ });
142
+ const [source, setSource] = useState(options.source);
143
+ const [error, setError] = useState<string | undefined>(undefined);
144
+
145
+ // What the file currently holds, so `dirty` compares against the saved state
146
+ // rather than against where the session started.
147
+ //
148
+ // State, not a ref, and the difference is visible: `dirty` is derived from
149
+ // it, and a ref write does not re-render, so a successful save left the
150
+ // editor reporting unsaved changes it had just written.
151
+ const [saved, setSaved] = useState(options.document);
152
+ // Cleared by a save, restored by the undo of that save.
153
+ const [pendingOpen, setPendingOpen] = useState(opensDirty);
154
+ // Cleared by a save, restored by the undo of that save.
155
+ const [replacing, setReplacing] = useState(options.replaces);
156
+ // Undoing a save over a draft writes the package's file back, so the draft text the editor shows is in no file until saved.
157
+ const [unwritten, setUnwritten] = useState(false);
158
+
159
+ const document = history.stack[history.index];
160
+ // Read by `save` and `undoSave`, which compare against the latest history rather than the one their render closed over.
161
+ const historyRef = useRef(history);
162
+ historyRef.current = history;
163
+ // A ref as well as state, so a second click before the re-render is refused rather than written twice.
164
+ const busyRef = useRef(false);
165
+ const [busy, setBusy] = useState(false);
166
+ const setWriting = (value: boolean) => {
167
+ busyRef.current = value;
168
+ setBusy(value);
169
+ };
170
+ // The editor as it stood when the last save began, and the history that save left. Any later edit, undo or redo replaces `history`, which withdraws the offer.
171
+ const [undoable, setUndoable] = useState<
172
+ | {
173
+ before: {
174
+ source: string;
175
+ saved: T;
176
+ history: History<T>;
177
+ pendingOpen: boolean;
178
+ replacing: string | undefined;
179
+ unwritten: boolean;
180
+ };
181
+ after: History<T>;
182
+ lastSave: LastSave;
183
+ }
184
+ | undefined
185
+ >(undefined);
186
+
187
+ const update = useCallback((change: (draft: T) => void) => {
188
+ setError(undefined);
189
+ setHistory((previous) => {
190
+ const draft = structuredClone(previous.stack[previous.index]);
191
+ change(draft);
192
+ // A change that changes nothing does not deserve a history entry;
193
+ // otherwise a click that sets a value to what it already was makes
194
+ // undo appear broken.
195
+ if (
196
+ JSON.stringify(draft) ===
197
+ JSON.stringify(previous.stack[previous.index])
198
+ )
199
+ return previous;
200
+ // Editing after undo discards the redo tail, which is what every
201
+ // editor does and what a reader expects.
202
+ const kept = previous.stack.slice(0, previous.index + 1);
203
+ const stack = [...kept, draft].slice(-HISTORY_LIMIT);
204
+ return { stack, index: stack.length - 1 };
205
+ });
206
+ }, []);
207
+
208
+ const undo = useCallback(() => {
209
+ setError(undefined);
210
+ setHistory((previous) =>
211
+ previous.index > 0
212
+ ? { ...previous, index: previous.index - 1 }
213
+ : previous,
214
+ );
215
+ }, []);
216
+
217
+ const redo = useCallback(() => {
218
+ setError(undefined);
219
+ setHistory((previous) =>
220
+ previous.index < previous.stack.length - 1
221
+ ? { ...previous, index: previous.index + 1 }
222
+ : previous,
223
+ );
224
+ }, []);
225
+
226
+ // A splice that rejects is the writer failing, not the reader's mistake; it must surface as a refusal rather than an unhandled rejection.
227
+ const safeSplice = useCallback(
228
+ async (from: string, doc: T): Promise<SpliceResult> => {
229
+ try {
230
+ return await splice(from, doc);
231
+ } catch (failure) {
232
+ return {
233
+ ok: false,
234
+ reason: `Could not build the file: ${failure instanceof Error ? failure.message : String(failure)}`,
235
+ };
236
+ }
237
+ },
238
+ [splice],
239
+ );
240
+
241
+ const save = useCallback(async (): Promise<SaveOutcome> => {
242
+ if (busyRef.current)
243
+ return { ok: false, reason: "A save is still being written." };
244
+ const before = {
245
+ source,
246
+ saved,
247
+ history: historyRef.current,
248
+ pendingOpen,
249
+ replacing,
250
+ unwritten,
251
+ };
252
+ const clearing = isClearing?.(saved, document) ?? false;
253
+ const lastStructural = isStructural?.(saved, document) ?? false;
254
+ setWriting(true);
255
+ try {
256
+ const result = await safeSplice(source, document);
257
+ if (spliceFailed(result)) {
258
+ setError(result.reason);
259
+ return { ok: false, reason: result.reason };
260
+ }
261
+ try {
262
+ await onSave?.(result.source, { purpose: "save", document });
263
+ } catch (failure) {
264
+ // The file may or may not have been written; what is certain is that
265
+ // the editor must not pretend it was. Staying dirty is the safe read.
266
+ const reason = `Could not save: ${failure instanceof Error ? failure.message : String(failure)}`;
267
+ setError(reason);
268
+ return { ok: false, reason };
269
+ }
270
+ // The patched file is the new baseline, so a second save patches what is
271
+ // now on disk rather than re-deriving from the text this session opened.
272
+ setSource(result.source);
273
+ setSaved(document);
274
+ setPendingOpen(false);
275
+ setReplacing(undefined);
276
+ setUnwritten(false);
277
+ const after = clearing
278
+ ? { stack: [document], index: 0 }
279
+ : before.history;
280
+ // Whatever is current when the write resolves survives, including edits typed during it.
281
+ setHistory((p) =>
282
+ p === before.history
283
+ ? after
284
+ : clearing
285
+ ? { stack: [p.stack[p.index]], index: 0 }
286
+ : p,
287
+ );
288
+ // An edit typed during the write moved `history` off `after`, so it is never offered.
289
+ setUndoable(
290
+ onSave
291
+ ? {
292
+ before,
293
+ after,
294
+ lastSave: {
295
+ before: replacing ?? source,
296
+ after: result.source,
297
+ structural: lastStructural,
298
+ clearsHistory: clearing,
299
+ },
300
+ }
301
+ : undefined,
302
+ );
303
+ setError(undefined);
304
+ return { ok: true };
305
+ } finally {
306
+ setWriting(false);
307
+ }
308
+ }, [
309
+ document,
310
+ safeSplice,
311
+ onSave,
312
+ source,
313
+ saved,
314
+ pendingOpen,
315
+ replacing,
316
+ unwritten,
317
+ isClearing,
318
+ isStructural,
319
+ ]);
320
+
321
+ const undoSave = useCallback(async (): Promise<SaveOutcome> => {
322
+ if (busyRef.current)
323
+ return { ok: false, reason: "A save is still being written." };
324
+ if (
325
+ !onSave ||
326
+ undoable === undefined ||
327
+ undoable.after !== historyRef.current
328
+ )
329
+ return { ok: false, reason: "There is no save to undo." };
330
+ const { before } = undoable;
331
+ setWriting(true);
332
+ try {
333
+ await onSave(before.replacing ?? before.source, {
334
+ purpose: "undo",
335
+ document: before.saved,
336
+ });
337
+ } catch (failure) {
338
+ // The save still stands, so the offer does too.
339
+ const reason = `Could not undo the save: ${failure instanceof Error ? failure.message : String(failure)}`;
340
+ setError(reason);
341
+ return { ok: false, reason };
342
+ } finally {
343
+ setWriting(false);
344
+ }
345
+ // One batch, so no render sees the restored file under the saved history.
346
+ setSource(before.source);
347
+ setSaved(before.saved);
348
+ setPendingOpen(before.pendingOpen);
349
+ setReplacing(before.replacing);
350
+ setUnwritten(before.unwritten || before.replacing !== undefined);
351
+ // An edit typed during the undo's write is kept, as it is during a save's; it is unsaved against the restored file.
352
+ setHistory((p) => (p === undoable.after ? before.history : p));
353
+ setUndoable(undefined);
354
+ setError(undefined);
355
+ return { ok: true };
356
+ }, [onSave, undoable]);
357
+
358
+ const edited = useMemo(
359
+ () => unwritten || JSON.stringify(document) !== JSON.stringify(saved),
360
+ [document, saved, unwritten],
361
+ );
362
+ const structural = useMemo(
363
+ () => isStructural?.(saved, document) ?? false,
364
+ [document, saved, isStructural],
365
+ );
366
+ const clearsHistory = useMemo(
367
+ () => isClearing?.(saved, document) ?? false,
368
+ [document, saved, isClearing],
369
+ );
370
+ const offered = undoable !== undefined && undoable.after === history;
371
+ const preview = useCallback(async () => {
372
+ const result = await safeSplice(source, document);
373
+ return spliceFailed(result)
374
+ ? { ok: false as const, reason: result.reason }
375
+ : { ok: true as const, source: result.source };
376
+ }, [document, safeSplice, source]);
377
+
378
+ return {
379
+ document,
380
+ saved,
381
+ source,
382
+ canUndo: history.index > 0,
383
+ canRedo: history.index < history.stack.length - 1,
384
+ dirty: edited || pendingOpen,
385
+ edited,
386
+ pendingOpen,
387
+ ...(error === undefined ? {} : { error }),
388
+ update,
389
+ undo,
390
+ redo,
391
+ save,
392
+ preview,
393
+ structural,
394
+ clearsHistory,
395
+ canUndoSave: offered && !busy && onSave !== undefined,
396
+ ...(offered ? { lastSave: undoable.lastSave } : {}),
397
+ undoSave,
398
+ };
399
+ }
@@ -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,159 @@
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 { useExitGuard } from "./useExitGuard";
7
+
8
+ interface Props {
9
+ dirty: boolean;
10
+ saving: boolean;
11
+ canSave: boolean;
12
+ }
13
+
14
+ /** A save the test settles by hand: it flips `saving` the way a builder's does. */
15
+ const mount = (initial: Partial<Props> = {}) => {
16
+ const onExit = mock(() => {});
17
+ let current: Props = {
18
+ dirty: false,
19
+ saving: false,
20
+ canSave: true,
21
+ ...initial,
22
+ };
23
+ let resolve = () => {};
24
+ let throwOnSave = false;
25
+ const save = mock(() => {
26
+ if (throwOnSave) throw new Error("no");
27
+ return new Promise<void>((r) => {
28
+ resolve = r;
29
+ });
30
+ });
31
+ const view = renderHook(
32
+ (props: Props) => useExitGuard({ ...props, save, onExit }),
33
+ { initialProps: current },
34
+ );
35
+ const set = (next: Partial<Props>) => {
36
+ current = { ...current, ...next };
37
+ view.rerender(current);
38
+ };
39
+ /** The builder's save settling: `saving` falls, `dirty` is what it left, then the promise resolves. */
40
+ const settle = async (next: Partial<Props>) => {
41
+ await act(async () => {
42
+ set({ saving: false, ...next });
43
+ resolve();
44
+ });
45
+ };
46
+ return {
47
+ view,
48
+ onExit,
49
+ save,
50
+ set,
51
+ settle,
52
+ failing: () => {
53
+ throwOnSave = true;
54
+ },
55
+ };
56
+ };
57
+
58
+ const saveAndExit = async (view: ReturnType<typeof mount>["view"]) => {
59
+ act(() => view.result.current.requestExit());
60
+ await act(async () => view.result.current.dialog.onSaveAndExit());
61
+ };
62
+
63
+ describe("useExitGuard", () => {
64
+ it("exits at once when nothing is unsaved", () => {
65
+ const { view, onExit } = mount();
66
+ act(() => view.result.current.requestExit());
67
+ expect(onExit).toHaveBeenCalledTimes(1);
68
+ expect(view.result.current.dialog.open).toBe(false);
69
+ });
70
+
71
+ it("asks first when dirty, and Keep editing closes without exiting", () => {
72
+ const { view, onExit } = mount({ dirty: true });
73
+ act(() => view.result.current.requestExit());
74
+ expect(view.result.current.dialog.open).toBe(true);
75
+ expect(onExit).not.toHaveBeenCalled();
76
+ act(() => view.result.current.dialog.onKeepEditing());
77
+ expect(view.result.current.dialog.open).toBe(false);
78
+ expect(onExit).not.toHaveBeenCalled();
79
+ });
80
+
81
+ it("Discard exits without saving", () => {
82
+ const { view, onExit, save } = mount({ dirty: true });
83
+ act(() => view.result.current.requestExit());
84
+ act(() => view.result.current.dialog.onDiscard());
85
+ expect(onExit).toHaveBeenCalledTimes(1);
86
+ expect(save).not.toHaveBeenCalled();
87
+ });
88
+
89
+ it("offers no save when the builder cannot save", () => {
90
+ const { view } = mount({ dirty: true, canSave: false });
91
+ act(() => view.result.current.requestExit());
92
+ expect(view.result.current.dialog.canSave).toBe(false);
93
+ });
94
+
95
+ it("Save and exit closes the dialog, saves, and exits once the save lands clean", async () => {
96
+ const { view, onExit, save, set, settle } = mount({ dirty: true });
97
+ await saveAndExit(view);
98
+ expect(view.result.current.dialog.open).toBe(false);
99
+ expect(save).toHaveBeenCalledTimes(1);
100
+ act(() => set({ saving: true }));
101
+ expect(onExit).not.toHaveBeenCalled();
102
+ await settle({ dirty: false });
103
+ expect(onExit).toHaveBeenCalledTimes(1);
104
+ });
105
+
106
+ it("stays when the save settles still dirty, and a later clean state is not that save", async () => {
107
+ const { view, onExit, set, settle } = mount({ dirty: true });
108
+ await saveAndExit(view);
109
+ act(() => set({ saving: true }));
110
+ await settle({});
111
+ expect(onExit).not.toHaveBeenCalled();
112
+ act(() => set({ dirty: false }));
113
+ expect(onExit).not.toHaveBeenCalled();
114
+ });
115
+
116
+ it("waits out a save already in flight, then saves what is still dirty", async () => {
117
+ const { view, onExit, save, set, settle } = mount({
118
+ dirty: true,
119
+ saving: true,
120
+ });
121
+ await saveAndExit(view);
122
+ expect(save).not.toHaveBeenCalled();
123
+ await act(async () => set({ saving: false, dirty: true }));
124
+ expect(save).toHaveBeenCalledTimes(1);
125
+ act(() => set({ saving: true }));
126
+ await settle({ dirty: false });
127
+ expect(onExit).toHaveBeenCalledTimes(1);
128
+ });
129
+
130
+ it("exits when the in-flight save leaves nothing dirty", async () => {
131
+ const { view, onExit, save, set } = mount({ dirty: true, saving: true });
132
+ await saveAndExit(view);
133
+ await act(async () => set({ saving: false, dirty: false }));
134
+ expect(save).not.toHaveBeenCalled();
135
+ expect(onExit).toHaveBeenCalledTimes(1);
136
+ });
137
+
138
+ it("clears the request when the save throws", async () => {
139
+ const { view, onExit, set, failing } = mount({ dirty: true });
140
+ failing();
141
+ await saveAndExit(view);
142
+ act(() => set({ dirty: false }));
143
+ expect(onExit).not.toHaveBeenCalled();
144
+ // The request is over, so a fresh Done asks again rather than being swallowed.
145
+ set({ dirty: true });
146
+ act(() => view.result.current.requestExit());
147
+ expect(view.result.current.dialog.open).toBe(true);
148
+ });
149
+
150
+ it("ignores the dialog's buttons once it is closed", () => {
151
+ const { view, onExit, save } = mount({ dirty: true });
152
+ act(() => view.result.current.requestExit());
153
+ act(() => view.result.current.dialog.onKeepEditing());
154
+ act(() => view.result.current.dialog.onDiscard());
155
+ act(() => view.result.current.dialog.onSaveAndExit());
156
+ expect(onExit).not.toHaveBeenCalled();
157
+ expect(save).not.toHaveBeenCalled();
158
+ });
159
+ });
@@ -0,0 +1,89 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { useCallback, useEffect, useRef, useState } from "react";
5
+
6
+ export interface ExitGuardOptions {
7
+ dirty: boolean;
8
+ saving: boolean;
9
+ canSave: boolean;
10
+ /** Resolves once the save has run. */
11
+ save: () => Promise<void> | void;
12
+ onExit: () => void;
13
+ }
14
+
15
+ type Phase = "idle" | "asking" | "waiting" | "saving";
16
+
17
+ /** "Close" exits at once when clean and asks otherwise; "Save and exit" exits only once its own save settles clean. */
18
+ export function useExitGuard({
19
+ dirty,
20
+ saving,
21
+ canSave,
22
+ save,
23
+ onExit,
24
+ }: ExitGuardOptions) {
25
+ const [phase, setPhase] = useState<Phase>("idle");
26
+ // `save` has returned, so a quiet builder after it means the save is over, not that it has yet to start.
27
+ const [issued, setIssued] = useState(false);
28
+ const saveRef = useRef(save);
29
+ saveRef.current = save;
30
+ const onExitRef = useRef(onExit);
31
+ onExitRef.current = onExit;
32
+ const attempt = useRef(0);
33
+
34
+ const issue = useCallback(async () => {
35
+ const mine = ++attempt.current;
36
+ setIssued(false);
37
+ setPhase("saving");
38
+ try {
39
+ await saveRef.current();
40
+ } catch {
41
+ if (attempt.current === mine) setPhase("idle");
42
+ return;
43
+ }
44
+ if (attempt.current === mine) setIssued(true);
45
+ }, []);
46
+
47
+ useEffect(() => {
48
+ if (phase === "waiting") {
49
+ if (saving) return;
50
+ if (dirty) void issue();
51
+ else {
52
+ setPhase("idle");
53
+ onExitRef.current();
54
+ }
55
+ } else if (phase === "saving") {
56
+ if (!issued || saving) return;
57
+ setPhase("idle");
58
+ if (!dirty) onExitRef.current();
59
+ }
60
+ }, [phase, issued, saving, dirty, issue]);
61
+
62
+ const requestExit = useCallback(() => {
63
+ if (phase !== "idle") return;
64
+ if (dirty) setPhase("asking");
65
+ else onExitRef.current();
66
+ }, [phase, dirty]);
67
+
68
+ return {
69
+ requestExit,
70
+ dialog: {
71
+ open: phase === "asking",
72
+ canSave,
73
+ onKeepEditing: () => {
74
+ if (phase !== "asking") return;
75
+ setPhase("idle");
76
+ },
77
+ onDiscard: () => {
78
+ if (phase !== "asking") return;
79
+ setPhase("idle");
80
+ onExitRef.current();
81
+ },
82
+ onSaveAndExit: () => {
83
+ if (phase !== "asking") return;
84
+ if (saving) setPhase("waiting");
85
+ else void issue();
86
+ },
87
+ },
88
+ };
89
+ }