@malloy-publisher/sdk 0.12.1 → 0.12.2

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 (208) hide show
  1. package/README.md +79 -6
  2. package/dist/{RenderedResult-DrNhQ2_9.cjs.js → RenderedResult-Cgw4iqev.cjs.js} +1 -1
  3. package/dist/{RenderedResult-AyM-RQaA.es.js → RenderedResult-DEjFM1DL.es.js} +2 -2
  4. package/dist/ServerProvider-BRQl_cBo.cjs.js +1 -0
  5. package/dist/{ServerProvider-a9Ki0QXO.es.js → ServerProvider-RnliA2Ao.es.js} +577 -555
  6. package/dist/builder/index.cjs.js +1 -1
  7. package/dist/builder/index.es.js +3 -3
  8. package/dist/client/api.d.ts +10 -1
  9. package/dist/client/index.cjs.js +1 -1
  10. package/dist/client/index.es.js +28 -27
  11. package/dist/components/Model/ExplorerPage.d.ts +9 -0
  12. package/dist/components/Model/Model.d.ts +27 -11
  13. package/dist/components/ModelViewer/AccessChips.d.ts +34 -0
  14. package/dist/components/ModelViewer/EmptyRow.d.ts +23 -0
  15. package/dist/components/ModelViewer/Facts.d.ts +15 -0
  16. package/dist/components/ModelViewer/FieldGroup.d.ts +31 -0
  17. package/dist/components/ModelViewer/FieldRow.d.ts +69 -0
  18. package/dist/components/ModelViewer/GivenFacts.d.ts +21 -0
  19. package/dist/components/ModelViewer/HeaderBar.d.ts +75 -0
  20. package/dist/components/ModelViewer/InlineDoc.d.ts +13 -0
  21. package/dist/components/ModelViewer/ModelGivensSection.d.ts +32 -0
  22. package/dist/components/ModelViewer/ModelSection.d.ts +21 -0
  23. package/dist/components/ModelViewer/ModelViewer.d.ts +91 -0
  24. package/dist/components/ModelViewer/QueriesGroup.d.ts +15 -0
  25. package/dist/components/ModelViewer/RowGlyph.d.ts +27 -0
  26. package/dist/components/ModelViewer/RunResult.d.ts +46 -0
  27. package/dist/components/ModelViewer/SampleValues.d.ts +20 -0
  28. package/dist/components/ModelViewer/SchemaJoin.d.ts +51 -0
  29. package/dist/components/ModelViewer/SchemaView.d.ts +52 -0
  30. package/dist/components/ModelViewer/SegmentIcons.d.ts +70 -0
  31. package/dist/components/ModelViewer/SourcesDropdown.d.ts +53 -0
  32. package/dist/components/ModelViewer/SourcesNav.d.ts +25 -0
  33. package/dist/components/ModelViewer/StatusPill.d.ts +56 -0
  34. package/dist/components/ModelViewer/ValueChip.d.ts +17 -0
  35. package/dist/components/ModelViewer/ViewRow.d.ts +36 -0
  36. package/dist/components/ModelViewer/clickableBox.d.ts +29 -0
  37. package/dist/components/ModelViewer/codeDisplay.d.ts +44 -0
  38. package/dist/components/ModelViewer/docAnnotations.d.ts +90 -0
  39. package/dist/components/ModelViewer/explorerModel.d.ts +12 -0
  40. package/dist/components/ModelViewer/extensions.d.ts +94 -0
  41. package/dist/components/ModelViewer/fieldCode.d.ts +74 -0
  42. package/dist/components/ModelViewer/fieldEntities.d.ts +45 -0
  43. package/dist/components/ModelViewer/fieldExpansion.d.ts +41 -0
  44. package/dist/components/ModelViewer/givenUsage.d.ts +71 -0
  45. package/dist/components/ModelViewer/headerCard.d.ts +31 -0
  46. package/dist/components/ModelViewer/httpStatus.d.ts +4 -0
  47. package/dist/components/ModelViewer/icons.d.ts +31 -0
  48. package/dist/components/ModelViewer/index.d.ts +9 -0
  49. package/dist/components/ModelViewer/joinAccess.d.ts +52 -0
  50. package/dist/components/ModelViewer/malloyRunText.d.ts +22 -0
  51. package/dist/components/ModelViewer/malloyText.d.ts +21 -0
  52. package/dist/components/ModelViewer/modelGivens.d.ts +61 -0
  53. package/dist/components/ModelViewer/modelGivensContext.d.ts +9 -0
  54. package/dist/components/ModelViewer/modelHeaderPresentation.d.ts +28 -0
  55. package/dist/components/ModelViewer/modelTextDeclarations.d.ts +32 -0
  56. package/dist/components/ModelViewer/modelTextParse.d.ts +27 -0
  57. package/dist/components/ModelViewer/namedQueryCode.d.ts +12 -0
  58. package/dist/components/ModelViewer/offSurface.d.ts +54 -0
  59. package/dist/components/ModelViewer/rowLayers.d.ts +30 -0
  60. package/dist/components/ModelViewer/runGate.d.ts +49 -0
  61. package/dist/components/ModelViewer/schemaModel.d.ts +109 -0
  62. package/dist/components/ModelViewer/sectionOpenCommand.d.ts +45 -0
  63. package/dist/components/ModelViewer/sourceAccess.d.ts +67 -0
  64. package/dist/components/ModelViewer/sourceLineage.d.ts +14 -0
  65. package/dist/components/ModelViewer/sourcePicker.d.ts +66 -0
  66. package/dist/components/ModelViewer/surface.d.ts +47 -0
  67. package/dist/components/ModelViewer/text.d.ts +6 -0
  68. package/dist/components/ModelViewer/theme.d.ts +63 -0
  69. package/dist/components/ModelViewer/tokens.d.ts +52 -0
  70. package/dist/components/ModelViewer/ui.d.ts +48 -0
  71. package/dist/components/ModelViewer/useDimensionSamples.d.ts +76 -0
  72. package/dist/components/ModelViewer/useModelSources.d.ts +92 -0
  73. package/dist/components/ModelViewer/useRunQuery.d.ts +115 -0
  74. package/dist/components/ReportBuilder/loadMalloy.d.ts +4 -0
  75. package/dist/components/ServerProvider.d.ts +15 -1
  76. package/dist/components/filter/utils.d.ts +1 -1
  77. package/dist/components/given/GivenInput.d.ts +7 -1
  78. package/dist/components/given/GivensPanel.d.ts +3 -1
  79. package/dist/components/given/filterSummary.d.ts +6 -0
  80. package/dist/components/given/useGivenUrlParams.d.ts +11 -0
  81. package/dist/components/index.d.ts +2 -1
  82. package/dist/components/modelPage.d.ts +36 -0
  83. package/dist/hooks/useDimensionalFilterRangeData.d.ts +9 -0
  84. package/dist/hooks/useSuggestOptions.d.ts +26 -2
  85. package/dist/index.cjs.js +47 -19
  86. package/dist/index.es.js +14281 -9879
  87. package/dist/useCompiledDocument-D88_2cGS.cjs.js +143 -0
  88. package/dist/{useCompiledDocument-Utj2u8CG.es.js → useCompiledDocument-Dmm6xkom.es.js} +6975 -6893
  89. package/dist/utils/filterCondition.d.ts +14 -0
  90. package/dist/utils/malloyLiteral.d.ts +20 -1
  91. package/package.json +2 -2
  92. package/src/components/Model/ExplorerPage.tsx +269 -0
  93. package/src/components/Model/Model.tsx +66 -250
  94. package/src/components/Model/ModelExplorer.spec.tsx +64 -1
  95. package/src/components/ModelViewer/AccessChips.tsx +149 -0
  96. package/src/components/ModelViewer/EmptyRow.tsx +71 -0
  97. package/src/components/ModelViewer/Facts.tsx +46 -0
  98. package/src/components/ModelViewer/FieldGroup.tsx +137 -0
  99. package/src/components/ModelViewer/FieldRow.tsx +566 -0
  100. package/src/components/ModelViewer/GivenFacts.tsx +89 -0
  101. package/src/components/ModelViewer/HeaderBar.tsx +362 -0
  102. package/src/components/ModelViewer/InlineDoc.tsx +50 -0
  103. package/src/components/ModelViewer/ModelGivensSection.tsx +142 -0
  104. package/src/components/ModelViewer/ModelSection.tsx +98 -0
  105. package/src/components/ModelViewer/ModelViewer.spec.tsx +937 -0
  106. package/src/components/ModelViewer/ModelViewer.tsx +777 -0
  107. package/src/components/ModelViewer/QueriesGroup.tsx +107 -0
  108. package/src/components/ModelViewer/RowGlyph.tsx +138 -0
  109. package/src/components/ModelViewer/RunResult.tsx +298 -0
  110. package/src/components/ModelViewer/SampleValues.tsx +97 -0
  111. package/src/components/ModelViewer/SchemaJoin.tsx +258 -0
  112. package/src/components/ModelViewer/SchemaView.tsx +222 -0
  113. package/src/components/ModelViewer/SegmentIcons.tsx +129 -0
  114. package/src/components/ModelViewer/SourcesDropdown.tsx +265 -0
  115. package/src/components/ModelViewer/SourcesNav.tsx +75 -0
  116. package/src/components/ModelViewer/StatusPill.tsx +145 -0
  117. package/src/components/ModelViewer/ValueChip.tsx +39 -0
  118. package/src/components/ModelViewer/ViewRow.tsx +274 -0
  119. package/src/components/ModelViewer/clickableBox.ts +59 -0
  120. package/src/components/ModelViewer/codeDisplay.spec.ts +176 -0
  121. package/src/components/ModelViewer/codeDisplay.ts +118 -0
  122. package/src/components/ModelViewer/docAnnotations.spec.ts +181 -0
  123. package/src/components/ModelViewer/docAnnotations.ts +244 -0
  124. package/src/components/ModelViewer/docAnnotations.typeDetail.spec.ts +114 -0
  125. package/src/components/ModelViewer/explorerModel.spec.ts +43 -0
  126. package/src/components/ModelViewer/explorerModel.ts +31 -0
  127. package/src/components/ModelViewer/extensions.tsx +112 -0
  128. package/src/components/ModelViewer/fieldCode.spec.ts +168 -0
  129. package/src/components/ModelViewer/fieldCode.ts +118 -0
  130. package/src/components/ModelViewer/fieldEntities.ts +63 -0
  131. package/src/components/ModelViewer/fieldExpansion.spec.ts +116 -0
  132. package/src/components/ModelViewer/fieldExpansion.ts +95 -0
  133. package/src/components/ModelViewer/givenUsage.spec.ts +304 -0
  134. package/src/components/ModelViewer/givenUsage.ts +325 -0
  135. package/src/components/ModelViewer/headerCard.tsx +102 -0
  136. package/src/components/ModelViewer/httpStatus.ts +15 -0
  137. package/src/components/ModelViewer/icons.tsx +67 -0
  138. package/src/components/ModelViewer/index.ts +26 -0
  139. package/src/components/ModelViewer/joinAccess.spec.ts +95 -0
  140. package/src/components/ModelViewer/joinAccess.ts +54 -0
  141. package/src/components/ModelViewer/malloyRunText.spec.ts +48 -0
  142. package/src/components/ModelViewer/malloyRunText.ts +51 -0
  143. package/src/components/ModelViewer/malloyText.spec.ts +47 -0
  144. package/src/components/ModelViewer/malloyText.ts +77 -0
  145. package/src/components/ModelViewer/modelGivens.ts +228 -0
  146. package/src/components/ModelViewer/modelGivensContext.ts +13 -0
  147. package/src/components/ModelViewer/modelHeaderPresentation.spec.ts +31 -0
  148. package/src/components/ModelViewer/modelHeaderPresentation.ts +41 -0
  149. package/src/components/ModelViewer/modelTextDeclarations.spec.ts +336 -0
  150. package/src/components/ModelViewer/modelTextDeclarations.ts +228 -0
  151. package/src/components/ModelViewer/modelTextParse.ts +112 -0
  152. package/src/components/ModelViewer/namedQueryCode.spec.ts +58 -0
  153. package/src/components/ModelViewer/namedQueryCode.ts +26 -0
  154. package/src/components/ModelViewer/offSurface.tsx +124 -0
  155. package/src/components/ModelViewer/rowLayers.tsx +104 -0
  156. package/src/components/ModelViewer/runGate.spec.ts +219 -0
  157. package/src/components/ModelViewer/runGate.ts +145 -0
  158. package/src/components/ModelViewer/schemaModel.defaultSource.spec.ts +126 -0
  159. package/src/components/ModelViewer/schemaModel.queries.spec.ts +37 -0
  160. package/src/components/ModelViewer/schemaModel.ts +300 -0
  161. package/src/components/ModelViewer/sectionOpenCommand.tsx +125 -0
  162. package/src/components/ModelViewer/sourceAccess.spec.ts +130 -0
  163. package/src/components/ModelViewer/sourceAccess.ts +194 -0
  164. package/src/components/ModelViewer/sourceLineage.spec.ts +73 -0
  165. package/src/components/ModelViewer/sourceLineage.ts +38 -0
  166. package/src/components/ModelViewer/sourcePicker.spec.ts +294 -0
  167. package/src/components/ModelViewer/sourcePicker.ts +162 -0
  168. package/src/components/ModelViewer/surface.ts +51 -0
  169. package/src/components/ModelViewer/text.tsx +33 -0
  170. package/src/components/ModelViewer/theme.spec.ts +38 -0
  171. package/src/components/ModelViewer/theme.ts +58 -0
  172. package/src/components/ModelViewer/tokens.ts +87 -0
  173. package/src/components/ModelViewer/ui.tsx +201 -0
  174. package/src/components/ModelViewer/useDimensionSamples.spec.tsx +139 -0
  175. package/src/components/ModelViewer/useDimensionSamples.ts +220 -0
  176. package/src/components/ModelViewer/useModelSources.spec.ts +110 -0
  177. package/src/components/ModelViewer/useModelSources.ts +238 -0
  178. package/src/components/ModelViewer/useRunQuery.spec.ts +50 -0
  179. package/src/components/ModelViewer/useRunQuery.ts +223 -0
  180. package/src/components/ReportBuilder/emptyStart.spec.ts +8 -0
  181. package/src/components/ReportBuilder/loadMalloy.ts +8 -1
  182. package/src/components/ServerProvider.tsx +27 -3
  183. package/src/components/filter/utils.ts +4 -60
  184. package/src/components/given/FilterButton.tsx +4 -1
  185. package/src/components/given/GivenInput.tsx +8 -0
  186. package/src/components/given/GivensPanel.tsx +6 -1
  187. package/src/components/given/filterSummary.spec.ts +31 -1
  188. package/src/components/given/filterSummary.ts +41 -19
  189. package/src/components/given/useGivenUrlParams.spec.tsx +32 -0
  190. package/src/components/given/useGivenUrlParams.ts +49 -14
  191. package/src/components/index.ts +6 -1
  192. package/src/components/legacy/malloynb/Notebook.spec.tsx +38 -4
  193. package/src/components/legacy/malloynb/NotebookCell.tsx +8 -2
  194. package/src/components/modelPage.spec.ts +79 -0
  195. package/src/components/modelPage.ts +48 -0
  196. package/src/hooks/useDimensionFilters.ts +3 -58
  197. package/src/hooks/useDimensionFiltersQuery.ts +3 -58
  198. package/src/hooks/useDimensionalFilterRangeData.spec.ts +57 -0
  199. package/src/hooks/useDimensionalFilterRangeData.ts +8 -63
  200. package/src/hooks/useSuggestOptions.dom.spec.ts +54 -1
  201. package/src/hooks/useSuggestOptions.spec.ts +71 -0
  202. package/src/hooks/useSuggestOptions.ts +102 -12
  203. package/src/utils/filterCondition.spec.ts +87 -0
  204. package/src/utils/filterCondition.ts +66 -0
  205. package/src/utils/malloyLiteral.spec.ts +53 -1
  206. package/src/utils/malloyLiteral.ts +47 -12
  207. package/dist/ServerProvider-BvD7PYZ7.cjs.js +0 -1
  208. package/dist/useCompiledDocument-CRhFBKv_.cjs.js +0 -142
@@ -0,0 +1,162 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { maskCommentsAndStrings } from "./malloyText";
5
+ import { fieldCount, type ExplorerSource } from "./schemaModel";
6
+
7
+ /**
8
+ * What the source picker (`SourcesDropdown`) says about each source, and in what order —
9
+ * the pure half, so every rule is a unit test (ux-design-guide §4 "Navigation scale").
10
+ *
11
+ * A reader opens the picker to find the source an analysis needs, and its rows are the only
12
+ * place every source of the model sits side by side. So each row says what the source is (its
13
+ * `#(doc)`, else a host's summary, marked), how much is in it (views, fields, joins), and — for
14
+ * a lookup table no one would start from — which source uses it.
15
+ */
16
+
17
+ /** Plural-aware count: `count(1, "view")` → "1 view". */
18
+ function count(n: number, noun: string): string {
19
+ return `${n} ${n === 1 ? noun : `${noun}s`}`;
20
+ }
21
+
22
+ /**
23
+ * The row's meta line: "16 views · 37 fields · 4 joins". The field count is the one the
24
+ * source's own header prints once it is picked (`fieldCount`), so the picker and the page it
25
+ * opens never disagree; joins are left out when there are none.
26
+ */
27
+ export function sourceMetaLine(source: ExplorerSource): string {
28
+ const fields = fieldCount(source);
29
+ const parts = [count(source.views.length, "view"), count(fields, "field")];
30
+ if (source.joins.length > 0) parts.push(count(source.joins.length, "join"));
31
+ return parts.join(" · ");
32
+ }
33
+
34
+ /** The picker's sources in the order it lists them, and the one its token marks. */
35
+ export interface PickerOrder {
36
+ sources: ExplorerSource[];
37
+ /** True on the package's root `index.malloy`, on the surface: its sources are the curated
38
+ * set, and the pill counts them as public. */
39
+ curated: boolean;
40
+ /** The curated sources are listed as the file's `export { … }` orders them, so the picker's
41
+ * header may say so. False when the file's text, and so its order, is unknown. */
42
+ exportOrdered: boolean;
43
+ /** The source the model opens on, when it really has the most views — the "Most views"
44
+ * token. Never set when the claim would be false. */
45
+ mostViews?: string;
46
+ }
47
+
48
+ /**
49
+ * The order the picker lists a model's sources in.
50
+ *
51
+ * - On the package's root `index.malloy`, when it is on the surface (`publicSurface`), the file
52
+ * IS the author's choice of what to publish, so its sources keep the order it exports them
53
+ * in, when its text says (`inExportOrder`); else the compiled model's, and `exportOrdered`
54
+ * is false so nothing claims an order.
55
+ * - On any other model, declaration order is not a ranking (`pickDefaultSource`), so the
56
+ * source the model opens on comes first and the rest follow by view count, most first;
57
+ * equal counts keep declaration order. A lookup table with no views sinks to the end.
58
+ *
59
+ * `mostViews` names the default only when no other source has more views than it and it has
60
+ * any: `pickDefaultSource` sets aside a source that extends another, so the default can be
61
+ * the base with fewer views than its extension, and the token must not claim otherwise.
62
+ */
63
+ export function orderPickerSources(
64
+ sources: readonly ExplorerSource[],
65
+ defaultSource: ExplorerSource | null,
66
+ /** Set on the root `index.malloy` on the surface: the names its `export { … }` lists, in
67
+ * order (`exportOrder`), empty when its text is unknown. */
68
+ publicSurface?: { exportOrder: readonly string[] },
69
+ ): PickerOrder {
70
+ if (publicSurface) {
71
+ const order = publicSurface.exportOrder;
72
+ const exportOrdered =
73
+ order.length > 0 && sources.some((s) => order.includes(s.name));
74
+ return {
75
+ sources: inExportOrder(sources, (s) => s.name, order),
76
+ curated: true,
77
+ exportOrdered,
78
+ };
79
+ }
80
+ const rest = sources
81
+ .map((source, index) => ({ source, index }))
82
+ .filter(({ source }) => source.name !== defaultSource?.name)
83
+ .sort(
84
+ (a, b) =>
85
+ b.source.views.length - a.source.views.length || a.index - b.index,
86
+ )
87
+ .map(({ source }) => source);
88
+ const ordered = defaultSource ? [defaultSource, ...rest] : rest;
89
+ const top = defaultSource?.views.length ?? 0;
90
+ const mostViews =
91
+ defaultSource && top > 0 && sources.every((s) => s.views.length <= top)
92
+ ? defaultSource.name
93
+ : undefined;
94
+ return { sources: ordered, curated: false, exportOrdered: false, mostViews };
95
+ }
96
+
97
+ /** An identifier as written, its backquotes off: `` `order items` `` → `order items`. */
98
+ function unquoteId(word: string): string {
99
+ return /^`.*`$/.test(word) ? word.slice(1, -1) : word;
100
+ }
101
+
102
+ /**
103
+ * The names a model file's `export { … }` statements list, in the order written. The compiled
104
+ * model lists its exports in declaration order (an imported source where the import brings it
105
+ * in), which is not the order an `index.malloy`'s author chose, so the text is read for it.
106
+ * Each item is a bare identifier (Malloy's `exportItem`), backquoted or not; the keyword is
107
+ * matched without case, as Malloy's are, and comments are skipped. Empty when the text is
108
+ * unknown or has no export statement.
109
+ */
110
+ export function exportOrder(sourceText: string | undefined): string[] {
111
+ if (!sourceText) return [];
112
+ const text = maskCommentsAndStrings(sourceText);
113
+ const out: string[] = [];
114
+ for (const match of text.matchAll(/(?:^|[\s;])export\s*\{([^}]*)\}/gi)) {
115
+ for (const entry of match[1].split(",")) {
116
+ const name = unquoteId(entry.trim());
117
+ if (name && !out.includes(name)) out.push(name);
118
+ }
119
+ }
120
+ return out;
121
+ }
122
+
123
+ /**
124
+ * `items` in the order `order` names them; anything it does not name keeps its place after
125
+ * them, in the order given. Unchanged when `order` is empty.
126
+ */
127
+ export function inExportOrder<T>(
128
+ items: readonly T[],
129
+ nameOf: (item: T) => string,
130
+ order: readonly string[],
131
+ ): T[] {
132
+ if (order.length === 0) return [...items];
133
+ const rank = (item: T) => {
134
+ const i = order.indexOf(nameOf(item));
135
+ return i === -1 ? order.length : i;
136
+ };
137
+ return items
138
+ .map((item, index) => ({ item, index }))
139
+ .sort((a, b) => rank(a.item) - rank(b.item) || a.index - b.index)
140
+ .map(({ item }) => item);
141
+ }
142
+
143
+ /** A row's description: the author's words, else a host's generated one, marked. */
144
+ export interface PickerDescription {
145
+ text: string;
146
+ /** From the host, not the model's author: the row marks it Generated. */
147
+ generated: boolean;
148
+ }
149
+
150
+ /**
151
+ * The row's description: the source's own `#(doc)` when it has one — the author's words win —
152
+ * else a host's summary (`describePickerSource`), marked as generated. Nothing when neither.
153
+ */
154
+ export function pickerDescription(
155
+ doc: string | undefined,
156
+ summary: string | undefined,
157
+ ): PickerDescription | undefined {
158
+ const authored = doc?.trim();
159
+ if (authored) return { text: authored, generated: false };
160
+ const generated = summary?.trim();
161
+ return generated ? { text: generated, generated: true } : undefined;
162
+ }
@@ -0,0 +1,51 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ /** The model viewer's page column and pane geometry. */
5
+
6
+ /** The page's reading column. */
7
+ export const CONTENT_COLUMN_SX = {
8
+ width: "100%",
9
+ maxWidth: "1200px",
10
+ mx: "auto",
11
+ } as const;
12
+
13
+ /** The gap between a page's header and its first line (theme spacing units). */
14
+ export const FILE_VIEW_TOP_GAP = 3;
15
+
16
+ /** A card's inner inset (theme spacing units). */
17
+ export const PANE_INSET = 1.5;
18
+
19
+ /**
20
+ * A read-only card: bordered, rounded, on the paper colour. Text's I-beam
21
+ * cursor advertises an edit these cards don't offer, so the default cursor is
22
+ * set once; anything interactive declares `pointer`.
23
+ */
24
+ export const PANE_CARD_SX = {
25
+ display: "flex",
26
+ flexDirection: "column",
27
+ minWidth: 0,
28
+ minHeight: 0,
29
+ border: "1px solid",
30
+ borderColor: "divider",
31
+ borderRadius: 2,
32
+ bgcolor: "background.paper",
33
+ overflow: "hidden",
34
+ cursor: "default",
35
+ } as const;
36
+
37
+ /** The same pane with no card around it: the page's own surface. */
38
+ export const FLAT_PANE_SX = {
39
+ ...PANE_CARD_SX,
40
+ border: "none",
41
+ borderRadius: 0,
42
+ bgcolor: "transparent",
43
+ overflow: "visible",
44
+ } as const;
45
+
46
+ /** A pane's body, under its header. */
47
+ export const PANE_BODY_SX = {
48
+ flex: 1,
49
+ minHeight: 0,
50
+ py: 1,
51
+ } as const;
@@ -0,0 +1,33 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { Typography, type TypographyProps } from "@mui/material";
5
+ import { forwardRef } from "react";
6
+
7
+ /** Secondary body text: a row's explanation, an empty state's line. */
8
+ export const BodyText = forwardRef<HTMLSpanElement, TypographyProps>(
9
+ function BodyText(props, ref) {
10
+ return (
11
+ <Typography
12
+ ref={ref}
13
+ variant="body2"
14
+ {...props}
15
+ sx={{ color: "text.secondary", ...props.sx }}
16
+ />
17
+ );
18
+ },
19
+ );
20
+
21
+ /** Caption text: a note under a result, a meta line. */
22
+ export const CaptionText = forwardRef<HTMLSpanElement, TypographyProps>(
23
+ function CaptionText(props, ref) {
24
+ return (
25
+ <Typography
26
+ ref={ref}
27
+ variant="caption"
28
+ {...props}
29
+ sx={{ color: "text.secondary", ...props.sx }}
30
+ />
31
+ );
32
+ },
33
+ );
@@ -0,0 +1,38 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import { readdirSync, readFileSync } from "fs";
6
+ import path from "path";
7
+
8
+ /**
9
+ * The viewer is drawn inside whatever theme its host gives it, light or dark,
10
+ * so its colours are MUI's own palette keys, which every theme defines in both
11
+ * modes. A hex value, or a key only some hosts add (`background.muted`,
12
+ * `text.tertiary`, `focus.ring`), drew a near-white pill under light text in
13
+ * the Console's dark mode, and a hover tint that never appeared.
14
+ */
15
+ const SOURCES = readdirSync(import.meta.dir)
16
+ .filter((file) => /\.tsx?$/.test(file) && !/\.spec\.tsx?$/.test(file))
17
+ .map((file) => ({
18
+ file,
19
+ text: readFileSync(path.join(import.meta.dir, file), "utf8"),
20
+ }));
21
+
22
+ describe("the viewer's colours", () => {
23
+ it("are never a hex value", () => {
24
+ const hex = SOURCES.filter(({ text }) =>
25
+ /["'`]#[0-9a-fA-F]{3,8}\b/.test(text),
26
+ ).map(({ file }) => file);
27
+ expect(hex).toEqual([]);
28
+ });
29
+
30
+ it("name only palette keys every MUI theme defines", () => {
31
+ const custom =
32
+ /["'](background\.(muted|strong|subtle)|text\.(tertiary|faint)|focus\.ring)["']/;
33
+ const found = SOURCES.filter(({ text }) => custom.test(text)).map(
34
+ ({ file }) => file,
35
+ );
36
+ expect(found).toEqual([]);
37
+ });
38
+ });
@@ -0,0 +1,58 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { MONO_FONT_FAMILY as SDK_MONO_FONT_FAMILY, PALETTE } from "../styles";
5
+
6
+ /**
7
+ * The model viewer's type steps and colour roles, in one place so the rows,
8
+ * pills and cards are set from the same few values. Built on the SDK palette.
9
+ *
10
+ * Every neutral is one of MUI's own palette keys (`text.*`, `action.*`), which
11
+ * every theme defines in both modes, never a hex or a key only some hosts add:
12
+ * a fixed light grey was a near-white pill under light text in a dark theme.
13
+ * So each role is for an `sx` colour prop, where MUI resolves it.
14
+ */
15
+
16
+ /** The custom property the viewer's root sets from the host's `ui.font.mono`. */
17
+ export const MONO_FONT_VAR = "--model-viewer-mono";
18
+
19
+ /** The viewer's code face: the host theme's mono face, set on the viewer's root. */
20
+ export const MONO_FONT_FAMILY = `var(${MONO_FONT_VAR}, ${SDK_MONO_FONT_FAMILY})`;
21
+ export const SANS_FONT_FAMILY = "inherit";
22
+
23
+ export const fontSizes = {
24
+ /** 11px: the floor, for a pill's text. */
25
+ micro: "0.6875rem",
26
+ /** 12px: secondary row detail. */
27
+ caption: "0.75rem",
28
+ /** 13px: dense rows. */
29
+ small: "0.8125rem",
30
+ /** 14px: body. */
31
+ body: "0.875rem",
32
+ /** 16px: prose meant to be read at length. */
33
+ bodyLarge: "1rem",
34
+ } as const;
35
+
36
+ export const fontWeights = { regular: 400, medium: 500, strong: 500 } as const;
37
+
38
+ const tinted = (main: string) => ({
39
+ tint: `color-mix(in srgb, ${main} 12%, transparent)`,
40
+ onTint: main,
41
+ });
42
+
43
+ export const roles = {
44
+ text: {
45
+ primary: "text.primary",
46
+ secondary: "text.secondary",
47
+ tertiary: "text.disabled",
48
+ },
49
+ /** A row's hover and a quiet pill's fill, then one step up: a selected row, a pill on a hovered row. */
50
+ background: { muted: "action.hover", strong: "action.selected" },
51
+ accent: { sage: tinted(PALETTE.lime) },
52
+ warning: tinted(PALETTE.amber),
53
+ error: tinted(PALETTE.red),
54
+ info: tinted(PALETTE.blue),
55
+ } as const;
56
+
57
+ /** Tertiary text: lighter than MUI's `text.secondary`, for a row's least words. */
58
+ export const TEXT_TERTIARY = roles.text.tertiary;
@@ -0,0 +1,87 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { PALETTE } from "../styles";
5
+ import { MONO_FONT_FAMILY } from "./theme";
6
+
7
+ /**
8
+ * Design tokens for the model viewer: its accents and its type scale, drawn
9
+ * from the SDK's palette so the viewer reads as part of the same Console.
10
+ */
11
+
12
+ export const ink = "text.primary";
13
+
14
+ export type AccentName = "steel" | "sage" | "brown" | "olive" | "amber";
15
+
16
+ /** An accent hue: `main` for a fill or glyph, `tint` behind it, `onTint` on top of `tint`. */
17
+ export interface Accent {
18
+ main: string;
19
+ tint: string;
20
+ onTint: string;
21
+ }
22
+
23
+ const accent = (main: string): Accent => ({
24
+ main,
25
+ tint: `color-mix(in srgb, ${main} 12%, transparent)`,
26
+ onTint: main,
27
+ });
28
+
29
+ export const ACCENT: Record<AccentName, Accent> = {
30
+ steel: accent(PALETTE.cyan),
31
+ sage: accent(PALETTE.lime),
32
+ brown: accent(PALETTE.slate),
33
+ olive: accent(PALETTE.violet),
34
+ amber: accent(PALETTE.amber),
35
+ };
36
+
37
+ /**
38
+ * Accent per field kind: steel = dimensions, sage = measures, amber =
39
+ * calculations, olive = views. Joins render brown (see SchemaJoin).
40
+ */
41
+ export function kindAccent(
42
+ kind: "dimension" | "measure" | "calculate" | "view",
43
+ ): Accent {
44
+ if (kind === "measure") return ACCENT.sage;
45
+ if (kind === "calculate") return ACCENT.amber;
46
+ if (kind === "view") return ACCENT.olive;
47
+ return ACCENT.steel;
48
+ }
49
+
50
+ /** Deterministic per-source accent: cycle the palette by source index. */
51
+ const SOURCE_ACCENT_CYCLE: AccentName[] = ["steel", "brown", "olive", "sage"];
52
+ export function sourceAccentName(index: number): AccentName {
53
+ return SOURCE_ACCENT_CYCLE[index % SOURCE_ACCENT_CYCLE.length];
54
+ }
55
+
56
+ /** The type scale the rows are set in. */
57
+ export const FONT_SIZE = { body: 14, small: 13, caption: 12 } as const;
58
+
59
+ /**
60
+ * The identifier style for a CONTAINER row (a join): a thing you open to find
61
+ * more things. Mono, because it exists verbatim in a .malloy file. One step
62
+ * above the leaf rows it holds ({@link ENTITY_NAME_SX}).
63
+ */
64
+ export const CONTAINER_NAME_SX = {
65
+ fontFamily: MONO_FONT_FAMILY,
66
+ fontSize: FONT_SIZE.body,
67
+ fontWeight: 500,
68
+ color: ink,
69
+ } as const;
70
+
71
+ /** The identifier style for a LEAF row (a field): the thing you came to read. */
72
+ export const ENTITY_NAME_SX = {
73
+ fontFamily: MONO_FONT_FAMILY,
74
+ fontSize: FONT_SIZE.small,
75
+ fontWeight: 500,
76
+ color: ink,
77
+ } as const;
78
+
79
+ /**
80
+ * The secondary voice beside a row's name: a type annotation (`string`,
81
+ * `number`) or a dimmed count. Sans on purpose: it describes the row, it isn't
82
+ * a token you would type.
83
+ */
84
+ export const ROW_META_SX = {
85
+ fontSize: FONT_SIZE.caption,
86
+ color: "text.secondary",
87
+ } as const;
@@ -0,0 +1,201 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { Alert, Box, Button, type ButtonProps } from "@mui/material";
5
+ import { forwardRef, useEffect, useState, type ReactNode } from "react";
6
+ import { usePublisherTheme } from "../../theme/ThemeContext";
7
+ import { highlight } from "../highlighter";
8
+ import { httpStatusOf } from "./httpStatus";
9
+ import { MONO_FONT_FAMILY } from "./theme";
10
+
11
+ /**
12
+ * Malloy, highlighted, in a read-only block: a field's definition, a view's
13
+ * code, the query a failed run sent. Plain text until the highlighter answers,
14
+ * so the code is readable on the first frame.
15
+ */
16
+ export function QueryCode({
17
+ code,
18
+ testId,
19
+ framed = true,
20
+ }: {
21
+ code: string;
22
+ testId?: string;
23
+ /** False: the code as one section of a card someone else frames (an open field row's). */
24
+ framed?: boolean;
25
+ }) {
26
+ const { mode } = usePublisherTheme();
27
+ const [html, setHtml] = useState<string | undefined>(undefined);
28
+ useEffect(() => {
29
+ let live = true;
30
+ highlight(code, "malloy", mode)
31
+ .then((out) => live && setHtml(out))
32
+ .catch(() => live && setHtml(undefined));
33
+ return () => {
34
+ live = false;
35
+ };
36
+ }, [code, mode]);
37
+ return (
38
+ <Box
39
+ data-testid={testId}
40
+ sx={{
41
+ fontFamily: MONO_FONT_FAMILY,
42
+ fontSize: "0.75rem",
43
+ overflowX: "auto",
44
+ "& pre": { m: 0, background: "transparent !important" },
45
+ ...(framed && {
46
+ border: "1px solid",
47
+ borderColor: "divider",
48
+ borderRadius: 1,
49
+ px: 1.5,
50
+ py: 1,
51
+ bgcolor: "background.default",
52
+ }),
53
+ }}
54
+ >
55
+ {html ? (
56
+ <div dangerouslySetInnerHTML={{ __html: html }} />
57
+ ) : (
58
+ <Box component="pre" sx={{ whiteSpace: "pre" }}>
59
+ {code}
60
+ </Box>
61
+ )}
62
+ </Box>
63
+ );
64
+ }
65
+
66
+ /** A read that failed, in place of what it would have shown, with a Retry where one can help. */
67
+ export function LoadErrorAlert({
68
+ message,
69
+ onRetry,
70
+ severity = "error",
71
+ }: {
72
+ message: ReactNode;
73
+ onRetry?: () => void;
74
+ severity?: "error" | "warning";
75
+ }) {
76
+ return (
77
+ <Alert
78
+ severity={severity}
79
+ action={
80
+ onRetry && (
81
+ <Button color="inherit" size="small" onClick={onRetry}>
82
+ Retry
83
+ </Button>
84
+ )
85
+ }
86
+ >
87
+ {message}
88
+ </Alert>
89
+ );
90
+ }
91
+
92
+ /** The server's own reason for a failed request (its Error body's `message`), if it gave one. */
93
+ export function serverMessage(error: unknown): string | undefined {
94
+ const message = (
95
+ error as { response?: { data?: { message?: unknown } } } | null
96
+ )?.response?.data?.message;
97
+ return typeof message === "string" && message ? message : undefined;
98
+ }
99
+
100
+ /** The server's own reason for a failed request, else what was being done. */
101
+ export function describeApiError(context: string, error: unknown): string {
102
+ const message = serverMessage(error);
103
+ return message ? `${context}: ${message}` : `${context}.`;
104
+ }
105
+
106
+ /**
107
+ * Can re-sending this query succeed? Not for a 400 (the query does not compile)
108
+ * or a 422 (a value it refuses), which hold until the query or the value
109
+ * changes, nor for a refusal (401/403/404).
110
+ */
111
+ export function isRetryableQueryFailure(error: unknown): boolean {
112
+ const status = httpStatusOf(error);
113
+ return !(
114
+ status === 400 ||
115
+ status === 401 ||
116
+ status === 403 ||
117
+ status === 404 ||
118
+ status === 422
119
+ );
120
+ }
121
+
122
+ /**
123
+ * A failed query, wherever a result would have been. The query is shown
124
+ * verbatim above the error, so a compile error's `line:column` points into text
125
+ * the reader can see, and Retry is offered only when a second attempt can
126
+ * succeed.
127
+ */
128
+ export function QueryFailure({
129
+ query,
130
+ error,
131
+ onRetry,
132
+ testId,
133
+ forbiddenMessage,
134
+ }: {
135
+ /** The query as it was sent; omitted when there is none to show. */
136
+ query?: string;
137
+ error: unknown;
138
+ onRetry?: () => void;
139
+ testId?: string;
140
+ /** What a refusal (403) says, where the caller knows the rule that refused it. */
141
+ forbiddenMessage?: string;
142
+ }) {
143
+ const refused =
144
+ forbiddenMessage !== undefined && httpStatusOf(error) === 403;
145
+ return (
146
+ <Box
147
+ data-testid={testId}
148
+ sx={{ display: "flex", flexDirection: "column", gap: 1 }}
149
+ >
150
+ {query && <QueryCode code={query} testId="query-failure-code" />}
151
+ <LoadErrorAlert
152
+ message={
153
+ refused
154
+ ? forbiddenMessage
155
+ : describeApiError("Query failed", error)
156
+ }
157
+ onRetry={
158
+ onRetry && isRetryableQueryFailure(error) ? onRetry : undefined
159
+ }
160
+ />
161
+ </Box>
162
+ );
163
+ }
164
+
165
+ /** The one filled action on a card: Run. */
166
+ export const PrimaryButton = forwardRef<HTMLButtonElement, ButtonProps>(
167
+ function PrimaryButton(props, ref) {
168
+ return (
169
+ <Button
170
+ ref={ref}
171
+ variant="contained"
172
+ size="small"
173
+ disableElevation
174
+ {...props}
175
+ sx={{ textTransform: "none", ...props.sx }}
176
+ />
177
+ );
178
+ },
179
+ );
180
+
181
+ /** A rounded, quiet button that opens a menu: the sources picker. */
182
+ export const PillButton = forwardRef<HTMLButtonElement, ButtonProps>(
183
+ function PillButton(props, ref) {
184
+ return (
185
+ <Button
186
+ ref={ref}
187
+ variant="outlined"
188
+ size="small"
189
+ color="inherit"
190
+ {...props}
191
+ sx={{
192
+ textTransform: "none",
193
+ borderRadius: 999,
194
+ borderColor: "divider",
195
+ color: "text.secondary",
196
+ ...props.sx,
197
+ }}
198
+ />
199
+ );
200
+ },
201
+ );