@malloy-publisher/sdk 0.9.10 → 0.9.11

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 (257) hide show
  1. package/dist/RenderedResult-BlNoIiSi.cjs.js +153 -0
  2. package/dist/{RenderedResult-Dp1Qdajz.es.js → RenderedResult-Cmx8sX8d.es.js} +102 -102
  3. package/dist/ServerProvider-BOgiW8MX.cjs.js +1 -0
  4. package/dist/{ServerProvider-CQy4jVYK.es.js → ServerProvider-azXLkk1q.es.js} +619 -586
  5. package/dist/builder/index.cjs.js +78 -130
  6. package/dist/builder/index.es.js +7493 -6769
  7. package/dist/builder-entry.d.ts +3 -0
  8. package/dist/client/api.d.ts +26 -22
  9. package/dist/client/index.cjs.js +1 -1
  10. package/dist/client/index.es.js +1 -1
  11. package/dist/components/AppDialog.d.ts +9 -1
  12. package/dist/components/Dashboard/DashboardGrid.d.ts +14 -15
  13. package/dist/components/Dashboard/DashboardTile.d.ts +8 -1
  14. package/dist/components/Dashboard/DashboardView.d.ts +22 -34
  15. package/dist/components/Dashboard/TileCard.d.ts +10 -3
  16. package/dist/components/Dashboard/useStuck.d.ts +15 -7
  17. package/dist/components/DocumentCreate/useDocumentChoices.d.ts +6 -0
  18. package/dist/components/Package/PackageProblems.d.ts +14 -0
  19. package/dist/components/RemoveBadge.d.ts +40 -0
  20. package/dist/components/Report/Report.d.ts +13 -4
  21. package/dist/components/Report/ReportPage.d.ts +60 -0
  22. package/dist/components/Report/layoutRules.d.ts +32 -0
  23. package/dist/components/Report/reportQuery.d.ts +30 -0
  24. package/dist/components/Report/telemetry.d.ts +0 -2
  25. package/dist/components/ReportBuilder/AddTileDialog.d.ts +22 -3
  26. package/dist/components/ReportBuilder/BuilderBar.d.ts +17 -0
  27. package/dist/components/ReportBuilder/BuilderDialogs.d.ts +7 -6
  28. package/dist/components/ReportBuilder/BuilderGrid.d.ts +10 -4
  29. package/dist/components/ReportBuilder/BuilderHeader.d.ts +4 -7
  30. package/dist/components/ReportBuilder/BuilderToolbar.d.ts +88 -0
  31. package/dist/components/ReportBuilder/DrillDialog.d.ts +3 -1
  32. package/dist/components/ReportBuilder/EditorSurface.d.ts +7 -9
  33. package/dist/components/ReportBuilder/FilterDialog.d.ts +3 -1
  34. package/dist/components/ReportBuilder/FilterStrip.d.ts +30 -8
  35. package/dist/components/ReportBuilder/InlineMarkdown.d.ts +4 -4
  36. package/dist/components/ReportBuilder/ReportBuilder.d.ts +49 -12
  37. package/dist/components/ReportBuilder/ReportEditor.d.ts +53 -3
  38. package/dist/components/ReportBuilder/TextTileBody.d.ts +2 -5
  39. package/dist/components/ReportBuilder/TileFrame.d.ts +10 -4
  40. package/dist/components/ReportBuilder/TileMenu.d.ts +12 -9
  41. package/dist/components/ReportBuilder/ViewPicker.d.ts +53 -0
  42. package/dist/components/ReportBuilder/addTileToDocument.d.ts +24 -3
  43. package/dist/components/ReportBuilder/builderControls.d.ts +60 -0
  44. package/dist/components/ReportBuilder/catalog.d.ts +2 -4
  45. package/dist/components/ReportBuilder/inlineEdit.d.ts +9 -10
  46. package/dist/components/ReportBuilder/localGiven.d.ts +12 -0
  47. package/dist/components/ReportBuilder/preview.d.ts +17 -2
  48. package/dist/components/ReportBuilder/readDocument.d.ts +0 -2
  49. package/dist/components/ReportBuilder/readForEditor.d.ts +2 -7
  50. package/dist/components/ReportBuilder/reportTag.d.ts +11 -1
  51. package/dist/components/ReportBuilder/sortable.d.ts +4 -3
  52. package/dist/components/ReportBuilder/testing/inline.d.ts +5 -1
  53. package/dist/components/ReportBuilder/useBuilderSelection.d.ts +0 -1
  54. package/dist/components/ReportBuilder/useBuilderSession.d.ts +21 -12
  55. package/dist/components/ReportBuilder/useDocumentEditor.d.ts +2 -6
  56. package/dist/components/ReportBuilder/useExitGuard.d.ts +3 -0
  57. package/dist/components/ReportBuilder/useFilterForm.d.ts +3 -1
  58. package/dist/components/ReportBuilder/useOpenedDocument.d.ts +0 -4
  59. package/dist/components/ReportBuilder/useReportDraft.d.ts +66 -0
  60. package/dist/components/ReportBuilder/useReportEditor.d.ts +0 -9
  61. package/dist/components/ReportBuilder/useTileEditing.d.ts +6 -3
  62. package/dist/components/ReportBuilder/viewAnnotations.d.ts +9 -0
  63. package/dist/components/ReportBuilder/widthLabel.d.ts +6 -0
  64. package/dist/components/ServerProvider.d.ts +9 -0
  65. package/dist/components/controlTokens.d.ts +45 -0
  66. package/dist/components/drill/markDrillableCells.d.ts +1 -1
  67. package/dist/components/given/FilterButton.d.ts +35 -0
  68. package/dist/components/given/GivensPanel.d.ts +2 -1
  69. package/dist/components/given/filterEditing.d.ts +54 -0
  70. package/dist/components/given/filterSummary.d.ts +9 -0
  71. package/dist/components/given/paramCodec.d.ts +11 -0
  72. package/dist/components/given/required.d.ts +25 -0
  73. package/dist/components/index.d.ts +3 -0
  74. package/dist/components/legacy/index.d.ts +1 -1
  75. package/dist/components/{Report/CellReport.d.ts → legacy/malloynb/CellRunnerView.d.ts} +9 -17
  76. package/dist/components/legacy/{MalloynbCells.d.ts → malloynb/MalloynbCells.d.ts} +3 -3
  77. package/dist/components/legacy/{Notebook.d.ts → malloynb/Notebook.d.ts} +1 -1
  78. package/dist/components/{Notebook → legacy/malloynb}/NotebookCell.d.ts +3 -3
  79. package/dist/components/legacy/malloynb/cellKind.d.ts +5 -0
  80. package/dist/components/{Notebook → legacy/malloynb}/types.d.ts +1 -1
  81. package/dist/components/styles.d.ts +0 -2
  82. package/dist/hooks/useQueryResult.d.ts +7 -0
  83. package/dist/index.cjs.js +16 -19
  84. package/dist/index.es.js +8324 -7769
  85. package/dist/theme/ThemeContext.d.ts +8 -1
  86. package/dist/theme/classes.d.ts +49 -0
  87. package/dist/theme/index.d.ts +2 -0
  88. package/dist/theme/types.d.ts +7 -0
  89. package/dist/theme/ui.d.ts +75 -0
  90. package/dist/{useCompiledDocument-BMPDvdFx.es.js → useCompiledDocument-7j5cBjQd.es.js} +8669 -7851
  91. package/dist/useCompiledDocument-B21sVbb4.cjs.js +141 -0
  92. package/package.json +2 -2
  93. package/src/builder-entry.ts +9 -0
  94. package/src/components/AppDialog.tsx +23 -0
  95. package/src/components/Dashboard/DashboardGrid.tsx +39 -17
  96. package/src/components/Dashboard/DashboardTile.tsx +19 -2
  97. package/src/components/Dashboard/DashboardView.spec.tsx +216 -15
  98. package/src/components/Dashboard/DashboardView.tsx +137 -155
  99. package/src/components/Dashboard/RowsDialog.tsx +1 -2
  100. package/src/components/Dashboard/TileCard.tsx +28 -10
  101. package/src/components/Dashboard/defaultTileSpan.spec.ts +24 -0
  102. package/src/components/Dashboard/useStuck.ts +28 -17
  103. package/src/components/DocumentCreate/useDocumentChoices.ts +19 -2
  104. package/src/components/Package/NewDocumentDialog.spec.tsx +76 -59
  105. package/src/components/Package/NewDocumentDialog.tsx +383 -119
  106. package/src/components/Package/Package.artifacts.spec.tsx +1 -1
  107. package/src/components/Package/Package.create.spec.tsx +7 -6
  108. package/src/components/Package/Package.tsx +10 -7
  109. package/src/components/Package/PackageProblems.spec.tsx +41 -0
  110. package/src/components/Package/PackageProblems.tsx +95 -0
  111. package/src/components/Prose.tsx +12 -7
  112. package/src/components/RemoveBadge.tsx +94 -0
  113. package/src/components/RenderedResult/RenderedResult.tsx +5 -9
  114. package/src/components/RenderedResult/ResultPanel.tsx +59 -3
  115. package/src/components/Report/Report.spec.tsx +50 -32
  116. package/src/components/Report/Report.telemetry.spec.tsx +0 -45
  117. package/src/components/Report/Report.tsx +45 -77
  118. package/src/components/Report/ReportPage.tsx +168 -0
  119. package/src/components/Report/layoutRules.spec.ts +35 -0
  120. package/src/components/Report/layoutRules.ts +55 -0
  121. package/src/components/Report/reportQuery.ts +53 -0
  122. package/src/components/Report/telemetry.ts +0 -2
  123. package/src/components/ReportBuilder/AddTileDialog.spec.tsx +76 -41
  124. package/src/components/ReportBuilder/AddTileDialog.tsx +368 -133
  125. package/src/components/ReportBuilder/BuilderBar.tsx +599 -0
  126. package/src/components/ReportBuilder/BuilderDialogs.tsx +28 -39
  127. package/src/components/ReportBuilder/BuilderGrid.tsx +31 -29
  128. package/src/components/ReportBuilder/BuilderHeader.tsx +63 -75
  129. package/src/components/ReportBuilder/BuilderToolbar.tsx +95 -9
  130. package/src/components/ReportBuilder/DrillDialog.tsx +240 -128
  131. package/src/components/ReportBuilder/EditorSurface.tsx +345 -69
  132. package/src/components/ReportBuilder/FilterDialog.tsx +516 -166
  133. package/src/components/ReportBuilder/FilterStrip.tsx +130 -151
  134. package/src/components/ReportBuilder/InlineMarkdown.tsx +71 -45
  135. package/src/components/ReportBuilder/ReportBuilder.charts.spec.tsx +35 -40
  136. package/src/components/ReportBuilder/ReportBuilder.controls.spec.tsx +491 -0
  137. package/src/components/ReportBuilder/ReportBuilder.exit.spec.tsx +2 -4
  138. package/src/components/ReportBuilder/ReportBuilder.inline.spec.tsx +13 -15
  139. package/src/components/ReportBuilder/ReportBuilder.notebook.spec.tsx +27 -118
  140. package/src/components/ReportBuilder/ReportBuilder.openDraft.spec.tsx +12 -5
  141. package/src/components/ReportBuilder/ReportBuilder.sources.spec.tsx +2 -10
  142. package/src/components/ReportBuilder/ReportBuilder.spec.tsx +37 -31
  143. package/src/components/ReportBuilder/ReportBuilder.textTiles.spec.tsx +22 -15
  144. package/src/components/ReportBuilder/ReportBuilder.tsx +292 -117
  145. package/src/components/ReportBuilder/ReportEditor.emptyStart.spec.tsx +2 -1
  146. package/src/components/ReportBuilder/ReportEditor.modelFilters.spec.tsx +312 -0
  147. package/src/components/ReportBuilder/ReportEditor.notebook.spec.tsx +26 -75
  148. package/src/components/ReportBuilder/ReportEditor.package.spec.tsx +6 -2
  149. package/src/components/ReportBuilder/ReportEditor.resourceUri.spec.tsx +3 -2
  150. package/src/components/ReportBuilder/ReportEditor.spec.tsx +2 -4
  151. package/src/components/ReportBuilder/ReportEditor.textSource.spec.tsx +93 -13
  152. package/src/components/ReportBuilder/ReportEditor.tsx +104 -14
  153. package/src/components/ReportBuilder/TextTileBody.tsx +6 -7
  154. package/src/components/ReportBuilder/TileFrame.tsx +212 -75
  155. package/src/components/ReportBuilder/TileMenu.tsx +241 -140
  156. package/src/components/ReportBuilder/ViewPicker.tsx +349 -0
  157. package/src/components/ReportBuilder/addTile.compile.spec.ts +1 -1
  158. package/src/components/ReportBuilder/addTileToDocument.spec.ts +102 -3
  159. package/src/components/ReportBuilder/addTileToDocument.ts +122 -25
  160. package/src/components/ReportBuilder/builderControls.ts +164 -0
  161. package/src/components/ReportBuilder/builderReport.ts +1 -2
  162. package/src/components/ReportBuilder/catalog.ts +10 -26
  163. package/src/components/ReportBuilder/inlineEdit.tsx +22 -54
  164. package/src/components/ReportBuilder/localGiven.ts +56 -0
  165. package/src/components/ReportBuilder/preview.spec.ts +125 -5
  166. package/src/components/ReportBuilder/preview.ts +60 -44
  167. package/src/components/ReportBuilder/readDocument.spec.ts +16 -22
  168. package/src/components/ReportBuilder/readDocument.ts +14 -11
  169. package/src/components/ReportBuilder/readForEditor.spec.ts +12 -41
  170. package/src/components/ReportBuilder/readForEditor.ts +4 -26
  171. package/src/components/ReportBuilder/reportTag.ts +28 -7
  172. package/src/components/ReportBuilder/sortable.tsx +15 -7
  173. package/src/components/ReportBuilder/spliceDocument.imports.spec.ts +1 -1
  174. package/src/components/ReportBuilder/tagParse.spec.ts +0 -40
  175. package/src/components/ReportBuilder/testing/inline.ts +25 -5
  176. package/src/components/ReportBuilder/testing/reportLayoutParity.json +13 -8
  177. package/src/components/ReportBuilder/textTiles.spec.ts +7 -6
  178. package/src/components/ReportBuilder/useBuilderSelection.ts +35 -0
  179. package/src/components/ReportBuilder/useBuilderSession.spec.ts +2 -2
  180. package/src/components/ReportBuilder/useBuilderSession.ts +21 -13
  181. package/src/components/ReportBuilder/useDocumentEditor.spec.ts +0 -58
  182. package/src/components/ReportBuilder/useDocumentEditor.ts +3 -16
  183. package/src/components/ReportBuilder/useEditorCatalog.spec.tsx +26 -0
  184. package/src/components/ReportBuilder/useEditorCatalog.ts +7 -2
  185. package/src/components/ReportBuilder/useExitGuard.ts +16 -0
  186. package/src/components/ReportBuilder/useFilterForm.spec.tsx +17 -0
  187. package/src/components/ReportBuilder/useFilterForm.ts +5 -1
  188. package/src/components/ReportBuilder/useOpenedDocument.ts +0 -2
  189. package/src/components/ReportBuilder/useReportDraft.spec.ts +149 -0
  190. package/src/components/ReportBuilder/useReportDraft.ts +292 -0
  191. package/src/components/ReportBuilder/useReportEditor.ts +3 -12
  192. package/src/components/ReportBuilder/useSaveChannel.ts +0 -1
  193. package/src/components/ReportBuilder/useTileEditing.ts +43 -12
  194. package/src/components/ReportBuilder/viewAnnotations.ts +29 -0
  195. package/src/components/ReportBuilder/widthLabel.spec.ts +25 -0
  196. package/src/components/ReportBuilder/widthLabel.ts +26 -0
  197. package/src/components/ServerProvider.tsx +11 -0
  198. package/src/components/controlTokens.ts +47 -0
  199. package/src/components/drill/markDrillableCells.ts +2 -1
  200. package/src/components/given/FilterButton.spec.tsx +143 -0
  201. package/src/components/given/FilterButton.tsx +478 -0
  202. package/src/components/given/GivenInput.spec.tsx +47 -0
  203. package/src/components/given/GivenInput.tsx +20 -2
  204. package/src/components/given/GivensPanel.spec.tsx +11 -3
  205. package/src/components/given/GivensPanel.tsx +45 -44
  206. package/src/components/given/filterEditing.ts +60 -0
  207. package/src/components/given/filterSummary.spec.ts +108 -0
  208. package/src/components/given/filterSummary.ts +109 -0
  209. package/src/components/given/paramCodec.ts +1 -1
  210. package/src/components/given/required.spec.ts +60 -0
  211. package/src/components/given/required.ts +78 -0
  212. package/src/components/index.ts +9 -0
  213. package/src/components/legacy/Dashboard.spec.tsx +3 -1
  214. package/src/components/legacy/index.ts +1 -1
  215. package/src/components/{Report/CellReport.spec.tsx → legacy/malloynb/CellRunnerView.spec.tsx} +53 -57
  216. package/src/components/{Report/CellReport.tsx → legacy/malloynb/CellRunnerView.tsx} +122 -136
  217. package/src/components/legacy/{MalloynbCells.tsx → malloynb/MalloynbCells.tsx} +6 -6
  218. package/src/components/legacy/{Notebook.layout.spec.tsx → malloynb/Notebook.layout.spec.tsx} +16 -7
  219. package/src/components/legacy/malloynb/Notebook.served.spec.tsx +84 -0
  220. package/src/components/legacy/{Notebook.spec.tsx → malloynb/Notebook.spec.tsx} +5 -3
  221. package/src/components/legacy/{Notebook.tsx → malloynb/Notebook.tsx} +9 -7
  222. package/src/components/{Notebook → legacy/malloynb}/NotebookCell.tsx +70 -150
  223. package/src/components/legacy/malloynb/cellKind.spec.ts +33 -0
  224. package/src/components/legacy/malloynb/cellKind.ts +24 -0
  225. package/src/components/{Notebook → legacy/malloynb}/types.ts +1 -1
  226. package/src/components/styles.ts +0 -18
  227. package/src/hooks/useGivensState.ts +7 -3
  228. package/src/hooks/useQueryResult.placeholder.spec.ts +44 -0
  229. package/src/hooks/useQueryResult.ts +26 -1
  230. package/src/theme/ThemeContext.tsx +21 -1
  231. package/src/theme/classes.ts +52 -0
  232. package/src/theme/index.ts +8 -0
  233. package/src/theme/resolveTheme.ts +3 -0
  234. package/src/theme/types.ts +7 -0
  235. package/src/theme/ui.spec.tsx +91 -0
  236. package/src/theme/ui.ts +115 -0
  237. package/dist/RenderedResult-BcgYjkZ0.cjs.js +0 -153
  238. package/dist/ServerProvider-B0dUY8be.cjs.js +0 -1
  239. package/dist/components/Notebook/cellKind.d.ts +0 -20
  240. package/dist/components/Report/LinearReport.d.ts +0 -18
  241. package/dist/components/ReportBuilder/SourceViewPicker.d.ts +0 -9
  242. package/dist/components/ReportBuilder/legacyNotebook.d.ts +0 -73
  243. package/dist/components/ReportBuilder/useConversionConfirm.d.ts +0 -20
  244. package/dist/useCompiledDocument-UY28ITET.cjs.js +0 -141
  245. package/src/components/Notebook/cellKind.spec.ts +0 -199
  246. package/src/components/Notebook/cellKind.ts +0 -74
  247. package/src/components/Report/LinearReport.tsx +0 -71
  248. package/src/components/ReportBuilder/SourceViewPicker.tsx +0 -122
  249. package/src/components/ReportBuilder/legacyNotebook.chart.spec.ts +0 -77
  250. package/src/components/ReportBuilder/legacyNotebook.convert.spec.ts +0 -626
  251. package/src/components/ReportBuilder/legacyNotebook.read.spec.ts +0 -338
  252. package/src/components/ReportBuilder/legacyNotebook.ts +0 -1122
  253. package/src/components/ReportBuilder/useConversionConfirm.ts +0 -55
  254. package/src/components/legacy/Notebook.served.spec.tsx +0 -183
  255. /package/dist/components/{Notebook → legacy/malloynb}/runPlan.d.ts +0 -0
  256. /package/src/components/{Notebook → legacy/malloynb}/runPlan.spec.ts +0 -0
  257. /package/src/components/{Notebook → legacy/malloynb}/runPlan.ts +0 -0
@@ -1,23 +1,31 @@
1
1
  // Copyright (c) Credible Data Inc.
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
+ import { segmentedSx } from "./BuilderToolbar";
5
+ import { reportClasses } from "../../theme/classes";
4
6
  import {
7
+ Box,
5
8
  Button,
6
- Stack,
7
9
  TextField,
8
10
  ToggleButton,
9
11
  ToggleButtonGroup,
10
12
  Typography,
11
13
  } from "@mui/material";
12
- import { useEffect, useMemo, useState } from "react";
14
+ import { useEffect, useMemo, useRef, useState, type ReactNode } from "react";
13
15
  import { usePublisherTheme } from "../../theme/ThemeContext";
14
- import type { PackageCatalog } from "./catalog";
15
- import type { ChartPick } from "./chartLine";
16
16
  import { isBareName } from "../../utils/malloyText";
17
- import type { DashboardDocument } from "./document";
18
- import { ChartPicker } from "./ChartPicker";
19
17
  import { AppDialog } from "../AppDialog";
20
- import { SourceViewPicker } from "./SourceViewPicker";
18
+ import { edgeColor } from "../controlTokens";
19
+ import type { CatalogSource, CatalogView, PackageCatalog } from "./catalog";
20
+ import { chartChoices } from "./ChartPicker";
21
+ import {
22
+ ChartGlyph,
23
+ readableName,
24
+ ViewPicker,
25
+ type PickerGroup,
26
+ } from "./ViewPicker";
27
+ import type { ChartPick } from "./chartLine";
28
+ import type { DashboardDocument } from "./document";
21
29
 
22
30
  /**
23
31
  * What a new tile is: a view, picked from the package.
@@ -50,6 +58,21 @@ export interface NewTile {
50
58
  colspan: number;
51
59
  }
52
60
 
61
+ /** A view the picker shows a preview of, as the tile it would add. */
62
+ export interface ViewPreview {
63
+ base: string;
64
+ /** The model that exports the source: what the preview runs against. */
65
+ modelPath: string;
66
+ view: string;
67
+ /** The title the tile would have. */
68
+ label: string;
69
+ /** A chart for the preview; absent keeps the view's own. */
70
+ chart?: ChartPick | "none";
71
+ chartCarried?: string[];
72
+ /** The givens the source is scoped by, the ones the preview sends. */
73
+ givens: string[];
74
+ }
75
+
53
76
  export interface AddTileDialogProps {
54
77
  open: boolean;
55
78
  document: DashboardDocument;
@@ -57,10 +80,36 @@ export interface AddTileDialogProps {
57
80
  columns: number;
58
81
  onClose: () => void;
59
82
  onAdd: (tile: NewTile) => void;
60
- /** Add an empty text tile, whose markdown is written on the tile itself. */
83
+ /** Add an empty text tile instead, whose markdown is written on the tile itself. */
61
84
  onAddText: () => void;
85
+ /** The picked view run as a tile, with the page's filters. Absent, a card says what will run. */
86
+ renderPreview?: (view: ViewPreview) => ReactNode;
87
+ }
88
+
89
+ /** `by_category` -> `By category`: a view's name, read as words, where it has no doc line. */
90
+ export function viewTitle(view: CatalogView): string {
91
+ return view.description ?? readableName(view.name);
92
+ }
93
+
94
+ /** What a view's own chart tag is, in a sentence. */
95
+ const DECLARED: Record<string, string> = {
96
+ bar_chart: "a bar chart",
97
+ line_chart: "a line chart",
98
+ scatter_chart: "a scatter chart",
99
+ shape_map: "a shape map",
100
+ segment_map: "a segment map",
101
+ big_value: "a big value",
102
+ sparkline: "a sparkline",
103
+ };
104
+
105
+ interface Entry {
106
+ source: CatalogSource;
107
+ view: CatalogView;
108
+ key: string;
62
109
  }
63
110
 
111
+ const entryKey = (source: string, view: string) => `${source}\u0000${view}`;
112
+
64
113
  export function AddTileDialog({
65
114
  open,
66
115
  document,
@@ -69,170 +118,356 @@ export function AddTileDialog({
69
118
  onClose,
70
119
  onAdd,
71
120
  onAddText,
121
+ renderPreview,
72
122
  }: AddTileDialogProps) {
73
123
  const { theme } = usePublisherTheme();
124
+ const ui = theme.ui;
125
+ const edge = edgeColor(theme.cardBorder);
74
126
  const sources = useMemo(() => catalog?.sources ?? [], [catalog]);
75
- const [mode, setMode] = useState<"query" | "text">("query");
76
- const [base, setBase] = useState<string>("");
77
- const [view, setView] = useState<string>("");
127
+ const [query, setQuery] = useState("");
128
+ const [pickedKey, setPickedKey] = useState<string | undefined>();
78
129
  const [label, setLabel] = useState("");
79
130
  const [chart, setChart] = useState<ChartPick | "none" | "default">(
80
131
  "default",
81
132
  );
82
- const [colspan, setColspan] = useState<number>(Math.ceil(columns / 2));
133
+ const searchRef = useRef<HTMLInputElement>(null);
134
+
135
+ // The source the tiles already read comes first, so the common case is one
136
+ // click on a view near the top.
137
+ const ordered = useMemo(() => {
138
+ const read = new Set(document.sources.map((s) => s.base));
139
+ return [...sources].sort(
140
+ (a, b) => Number(read.has(b.name)) - Number(read.has(a.name)),
141
+ );
142
+ }, [sources, document]);
143
+ const withViews = ordered.filter((s) => s.views.length > 0);
144
+ const withoutViews = ordered.filter((s) => s.views.length === 0);
145
+
146
+ // Every view on offer, by key: the picker's entries and what a key means.
147
+ const entries = useMemo(
148
+ () =>
149
+ new Map(
150
+ withViews.flatMap((source) =>
151
+ source.views.map(
152
+ (view) =>
153
+ [
154
+ entryKey(source.name, view.name),
155
+ { source, view, key: entryKey(source.name, view.name) },
156
+ ] as const,
157
+ ),
158
+ ),
159
+ ),
160
+ [withViews],
161
+ );
162
+ const groups = useMemo<PickerGroup[]>(
163
+ () =>
164
+ withViews.map((source) => ({
165
+ key: source.name,
166
+ label: source.name,
167
+ ...(source.description ? { detail: source.description } : {}),
168
+ entries: source.views.map((view) => ({
169
+ key: entryKey(source.name, view.name),
170
+ name: view.name,
171
+ title: viewTitle(view),
172
+ ...(view.chart ? { chart: view.chart } : {}),
173
+ })),
174
+ })),
175
+ [withViews],
176
+ );
177
+ // Kept while a search hides it, so typing does not drop the choice.
178
+ const picked = pickedKey === undefined ? undefined : entries.get(pickedKey);
83
179
 
84
180
  useEffect(() => {
85
181
  if (!open) return;
86
- setMode("query");
87
- // Open on the source the tiles already read, so the common case is one
88
- // click on a view.
89
- const withViews = (name: string) =>
90
- sources.some((s) => s.name === name && s.views.length > 0);
91
- setBase(
92
- document.sources.map((s) => s.base).find(withViews) ??
93
- sources.find((s) => s.views.length > 0)?.name ??
94
- "",
95
- );
96
- setView("");
182
+ setQuery("");
97
183
  setLabel("");
98
184
  setChart("default");
99
- setColspan(Math.ceil(columns / 2));
100
- }, [open, document, sources, columns]);
185
+ const first = withViews[0]?.views[0];
186
+ setPickedKey(
187
+ first && withViews[0]
188
+ ? entryKey(withViews[0].name, first.name)
189
+ : undefined,
190
+ );
191
+ // Only on opening: the catalog arriving later must not move the pick.
192
+ // eslint-disable-next-line react-hooks/exhaustive-deps
193
+ }, [open]);
101
194
 
102
- const canAdd = base !== "" && view !== "";
103
- const unwritable = [base, view]
104
- .filter((name) => name !== "" && !isBareName(name))
105
- .map(
106
- (name) =>
107
- `"${name}" is not a plain Malloy name, so a tile cannot be written for it.`,
108
- )[0];
109
- const pickedSource = sources.find((source) => source.name === base);
110
- const picked = pickedSource?.views.find(
111
- (candidate) => candidate.name === view,
112
- );
195
+ const unwritable = picked
196
+ ? [picked.source.name, picked.view.name]
197
+ .filter((name) => !isBareName(name))
198
+ .map(
199
+ (name) =>
200
+ `"${name}" is not a plain Malloy name, so a tile cannot be written for it.`,
201
+ )[0]
202
+ : undefined;
203
+ const canAdd = picked !== undefined && unwritable === undefined;
204
+ const title = picked ? viewTitle(picked.view) : "";
205
+ const chartPart =
206
+ chart !== "default" && picked
207
+ ? {
208
+ chart,
209
+ chartCarried: picked.view.chart ? [picked.view.chart] : [],
210
+ }
211
+ : {};
212
+ const add = (entry: Entry | undefined = picked) => {
213
+ if (!entry) return;
214
+ if (![entry.source.name, entry.view.name].every(isBareName)) return;
215
+ const same = entry.key === picked?.key;
216
+ onAdd({
217
+ base: entry.source.name,
218
+ modelPath: entry.source.modelPath,
219
+ ...(entry.source.exporters
220
+ ? { exporters: entry.source.exporters }
221
+ : {}),
222
+ view: entry.view.name,
223
+ ...(same && label.trim() ? { label: label.trim() } : {}),
224
+ ...(same ? chartPart : {}),
225
+ colspan: Math.ceil(columns / 2),
226
+ });
227
+ };
228
+ // Only the charts this view can take: a choice that cannot apply is not a
229
+ // choice here, where the view is still being picked.
230
+ const charts = picked
231
+ ? chartChoices(picked.view, chart).filter(
232
+ (choice) => choice.value !== "custom" && choice.disabled !== true,
233
+ )
234
+ : [];
113
235
 
114
236
  return (
115
237
  <AppDialog
238
+ className={`${reportClasses.dialog} ${reportClasses.addTileDialog}`}
116
239
  open={open}
117
240
  onClose={onClose}
241
+ maxWidth="md"
242
+ initialFocus={searchRef}
118
243
  title="Add a tile"
119
- description="A tile shows one view of a source in this package."
244
+ description="Pick a view from this package. You can change its title and chart now or later."
120
245
  actions={
121
246
  <>
247
+ <Button onClick={onAddText} sx={{ mr: "auto" }}>
248
+ Add text instead
249
+ </Button>
122
250
  <Button onClick={onClose}>Cancel</Button>
123
251
  <Button
124
252
  variant="contained"
125
- disabled={
126
- mode === "query" && (!canAdd || unwritable !== undefined)
127
- }
128
- onClick={() =>
129
- mode === "text"
130
- ? onAddText()
131
- : onAdd({
132
- base,
133
- modelPath: pickedSource?.modelPath ?? "",
134
- ...(pickedSource?.exporters
135
- ? { exporters: pickedSource.exporters }
136
- : {}),
137
- view,
138
- ...(label.trim() ? { label: label.trim() } : {}),
139
- ...(chart !== "default"
140
- ? {
141
- chart,
142
- ...(picked
143
- ? {
144
- chartCarried: picked.chart
145
- ? [picked.chart]
146
- : [],
147
- }
148
- : {}),
149
- }
150
- : {}),
151
- colspan,
152
- })
153
- }
253
+ disabled={!canAdd}
254
+ onClick={() => add()}
154
255
  >
155
- {mode === "text" ? "Add text" : "Add tile"}
256
+ Add tile
156
257
  </Button>
157
258
  </>
158
259
  }
159
260
  >
160
- <Stack sx={{ gap: 2, pt: 1 }}>
161
- <ToggleButtonGroup
162
- size="small"
163
- exclusive
164
- value={mode}
165
- aria-label="Tile type"
166
- onChange={(_, next: "query" | "text" | null) => {
167
- if (next !== null) setMode(next);
261
+ <Box
262
+ sx={{
263
+ // Edge to edge inside the window: two panes, not a form.
264
+ mx: -3,
265
+ mb: -1,
266
+ borderTop: `1px solid ${edge}`,
267
+ borderBottom: `1px solid ${edge}`,
268
+ display: "grid",
269
+ gridTemplateColumns: { xs: "1fr", md: "320px minmax(0, 1fr)" },
270
+ height: { md: 460 },
271
+ }}
272
+ >
273
+ <Box
274
+ sx={{
275
+ display: "flex",
276
+ flexDirection: "column",
277
+ minHeight: 0,
278
+ borderRight: { md: `1px solid ${edge}` },
279
+ maxHeight: { xs: 280, md: "none" },
168
280
  }}
169
281
  >
170
- <ToggleButton value="query" sx={{ px: 1.5 }}>
171
- Query
172
- </ToggleButton>
173
- <ToggleButton value="text" sx={{ px: 1.5 }}>
174
- Text
175
- </ToggleButton>
176
- </ToggleButtonGroup>
177
- {mode === "text" ? (
178
- <Typography variant="body2" sx={{ color: theme.tileTitle }}>
179
- A text tile holds markdown: a heading, a paragraph, a list. It
180
- is added empty, and written on the tile itself.
181
- </Typography>
182
- ) : sources.length === 0 ? (
183
- <Typography variant="body2" sx={{ color: theme.tileTitle }}>
184
- {catalog
185
- ? "This package publishes no source with a view to put on a tile."
186
- : "The package's sources are still loading."}
187
- </Typography>
188
- ) : (
189
- <>
190
- <SourceViewPicker
191
- sources={sources}
192
- source={base}
193
- view={view}
194
- onSource={(name) => {
195
- setBase(name);
196
- setView("");
197
- }}
198
- onView={(name) => {
199
- setView(name);
200
- setChart("default");
201
- }}
202
- />
203
- {unwritable && (
204
- <Typography variant="body2" color="error">
205
- {unwritable}
282
+ <ViewPicker
283
+ groups={groups}
284
+ query={query}
285
+ onQuery={setQuery}
286
+ picked={picked?.key}
287
+ onPick={(key) => {
288
+ setPickedKey(key);
289
+ setChart("default");
290
+ }}
291
+ onTake={(key) => add(entries.get(key))}
292
+ searchRef={searchRef}
293
+ empty={
294
+ <Typography
295
+ variant="body2"
296
+ sx={{ color: theme.tileTitle, p: 2 }}
297
+ >
298
+ {catalog
299
+ ? "This package publishes no source with a view to put on a tile."
300
+ : "The package's sources are still loading."}
206
301
  </Typography>
207
- )}
208
- <ChartPicker
209
- state={chart}
210
- view={picked}
211
- cellLabel="new tile"
212
- variant="outlined"
213
- {...(picked
214
- ? {}
215
- : {
216
- disabledReason: "Pick a view to choose its chart.",
217
- })}
218
- onChange={(next) =>
219
- setChart(next as ChartPick | "none" | "default")
220
- }
221
- />
222
- <Stack direction="row" sx={{ gap: 1.5 }}>
302
+ }
303
+ footer={
304
+ withoutViews.length > 0 && (
305
+ <Typography
306
+ variant="caption"
307
+ component="p"
308
+ sx={{ color: "text.secondary", px: 2, pt: 1.5 }}
309
+ >
310
+ Also in this package, with no views to show:{" "}
311
+ {withoutViews.map((s) => s.name).join(", ")}.
312
+ </Typography>
313
+ )
314
+ }
315
+ />
316
+ </Box>
317
+
318
+ <Box
319
+ sx={(mui) => ({
320
+ p: 2.5,
321
+ display: "flex",
322
+ flexDirection: "column",
323
+ gap: 2,
324
+ minHeight: 0,
325
+ overflowY: "auto",
326
+ // A shade off the paper, in the host's own colours.
327
+ bgcolor: `color-mix(in srgb, ${mui.palette.text.primary} 3%, ${mui.palette.background.paper})`,
328
+ })}
329
+ >
330
+ {picked ? (
331
+ <>
332
+ <Box
333
+ aria-label="Preview"
334
+ role="region"
335
+ sx={(mui) => ({
336
+ height: 240,
337
+ flex: "none",
338
+ display: "flex",
339
+ position: "relative",
340
+ // A preview, not the tile: what does not fit is cut
341
+ // off, under a fade that says there is more. The
342
+ // card is drawn here, round the cut, and the tile
343
+ // inside it has none of its own.
344
+ overflow: "hidden",
345
+ borderRadius: ui.radius.tile,
346
+ border: `1px solid ${edge}`,
347
+ bgcolor: mui.palette.background.paper,
348
+ "&::after": {
349
+ content: '""',
350
+ position: "absolute",
351
+ left: 0,
352
+ right: 0,
353
+ bottom: 0,
354
+ height: 40,
355
+ pointerEvents: "none",
356
+ background: `linear-gradient(to bottom, transparent, ${mui.palette.background.paper})`,
357
+ },
358
+ })}
359
+ >
360
+ {renderPreview ? (
361
+ <Box sx={{ flex: 1, minWidth: 0, px: 2, pt: 1.5 }}>
362
+ {renderPreview({
363
+ base: picked.source.name,
364
+ modelPath: picked.source.modelPath,
365
+ view: picked.view.name,
366
+ label: label.trim() || title,
367
+ ...(chart !== "default" ? chartPart : {}),
368
+ givens: picked.source.givens,
369
+ })}
370
+ </Box>
371
+ ) : (
372
+ <Box
373
+ sx={(mui) => ({
374
+ flex: 1,
375
+ border: `1px solid ${edge}`,
376
+ borderRadius: ui.radius.tile,
377
+ bgcolor: mui.palette.background.paper,
378
+ display: "flex",
379
+ flexDirection: "column",
380
+ alignItems: "center",
381
+ justifyContent: "center",
382
+ gap: 1,
383
+ color: "text.secondary",
384
+ })}
385
+ >
386
+ <ChartGlyph chart={picked.view.chart} />
387
+ <Typography variant="body2">
388
+ {picked.source.name} → {picked.view.name}
389
+ </Typography>
390
+ </Box>
391
+ )}
392
+ </Box>
223
393
  <TextField
224
394
  size="small"
225
395
  label="Title"
226
- placeholder="Uses the view's name"
396
+ placeholder={title}
227
397
  value={label}
228
398
  onChange={(event) => setLabel(event.target.value)}
399
+ InputLabelProps={{ shrink: true }}
229
400
  inputProps={{ "aria-label": "Tile title" }}
230
- sx={{ flex: 1 }}
231
401
  />
232
- </Stack>
233
- </>
234
- )}
235
- </Stack>
402
+ <Box>
403
+ <Typography
404
+ component="div"
405
+ sx={{
406
+ ...ui.type.label,
407
+ color: "text.secondary",
408
+ mb: 0.75,
409
+ }}
410
+ >
411
+ Chart
412
+ </Typography>
413
+ <ToggleButtonGroup
414
+ size="small"
415
+ exclusive
416
+ value={chart}
417
+ aria-label="Chart"
418
+ onChange={(_event, next) => {
419
+ if (next !== null)
420
+ setChart(
421
+ next as ChartPick | "none" | "default",
422
+ );
423
+ }}
424
+ // The builder's own segmented control, as the bar's
425
+ // layout switch is, so it takes the host's tokens.
426
+ sx={{
427
+ ...segmentedSx(edge, ui),
428
+ "& .MuiToggleButton-root": {
429
+ ...segmentedSx(edge, ui)[
430
+ "& .MuiToggleButton-root"
431
+ ],
432
+ textTransform: "none",
433
+ },
434
+ }}
435
+ >
436
+ {charts.map((choice) => (
437
+ <ToggleButton
438
+ key={choice.value}
439
+ value={choice.value}
440
+ >
441
+ {choice.label}
442
+ </ToggleButton>
443
+ ))}
444
+ </ToggleButtonGroup>
445
+ <Typography
446
+ variant="caption"
447
+ component="p"
448
+ sx={{ color: "text.secondary", mt: 0.75 }}
449
+ >
450
+ {picked.view.chart
451
+ ? `The view declares ${DECLARED[picked.view.chart] ?? "a chart"}.`
452
+ : "The view declares no chart, so it shows as a table."}
453
+ </Typography>
454
+ </Box>
455
+ {unwritable && (
456
+ <Typography variant="body2" color="error">
457
+ {unwritable}
458
+ </Typography>
459
+ )}
460
+ </>
461
+ ) : (
462
+ <Typography
463
+ variant="body2"
464
+ sx={{ color: "text.secondary", m: "auto" }}
465
+ >
466
+ Pick a view to see it here.
467
+ </Typography>
468
+ )}
469
+ </Box>
470
+ </Box>
236
471
  </AppDialog>
237
472
  );
238
473
  }