@malloy-publisher/sdk 0.12.0 → 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 (214) hide show
  1. package/README.md +79 -6
  2. package/dist/{RenderedResult-SgDlxzs9.cjs.js → RenderedResult-Cgw4iqev.cjs.js} +1 -1
  3. package/dist/{RenderedResult-D8bCgThO.es.js → RenderedResult-DEjFM1DL.es.js} +2 -2
  4. package/dist/ServerProvider-BRQl_cBo.cjs.js +1 -0
  5. package/dist/{ServerProvider-DBw-thY0.es.js → ServerProvider-RnliA2Ao.es.js} +1084 -723
  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 +754 -41
  9. package/dist/client/index.cjs.js +1 -1
  10. package/dist/client/index.es.js +120 -110
  11. package/dist/components/Materializations/MaterializationDetailDialog.d.ts +16 -1
  12. package/dist/components/Model/ExplorerPage.d.ts +9 -0
  13. package/dist/components/Model/Model.d.ts +27 -11
  14. package/dist/components/ModelViewer/AccessChips.d.ts +34 -0
  15. package/dist/components/ModelViewer/EmptyRow.d.ts +23 -0
  16. package/dist/components/ModelViewer/Facts.d.ts +15 -0
  17. package/dist/components/ModelViewer/FieldGroup.d.ts +31 -0
  18. package/dist/components/ModelViewer/FieldRow.d.ts +69 -0
  19. package/dist/components/ModelViewer/GivenFacts.d.ts +21 -0
  20. package/dist/components/ModelViewer/HeaderBar.d.ts +75 -0
  21. package/dist/components/ModelViewer/InlineDoc.d.ts +13 -0
  22. package/dist/components/ModelViewer/ModelGivensSection.d.ts +32 -0
  23. package/dist/components/ModelViewer/ModelSection.d.ts +21 -0
  24. package/dist/components/ModelViewer/ModelViewer.d.ts +91 -0
  25. package/dist/components/ModelViewer/QueriesGroup.d.ts +15 -0
  26. package/dist/components/ModelViewer/RowGlyph.d.ts +27 -0
  27. package/dist/components/ModelViewer/RunResult.d.ts +46 -0
  28. package/dist/components/ModelViewer/SampleValues.d.ts +20 -0
  29. package/dist/components/ModelViewer/SchemaJoin.d.ts +51 -0
  30. package/dist/components/ModelViewer/SchemaView.d.ts +52 -0
  31. package/dist/components/ModelViewer/SegmentIcons.d.ts +70 -0
  32. package/dist/components/ModelViewer/SourcesDropdown.d.ts +53 -0
  33. package/dist/components/ModelViewer/SourcesNav.d.ts +25 -0
  34. package/dist/components/ModelViewer/StatusPill.d.ts +56 -0
  35. package/dist/components/ModelViewer/ValueChip.d.ts +17 -0
  36. package/dist/components/ModelViewer/ViewRow.d.ts +36 -0
  37. package/dist/components/ModelViewer/clickableBox.d.ts +29 -0
  38. package/dist/components/ModelViewer/codeDisplay.d.ts +44 -0
  39. package/dist/components/ModelViewer/docAnnotations.d.ts +90 -0
  40. package/dist/components/ModelViewer/explorerModel.d.ts +12 -0
  41. package/dist/components/ModelViewer/extensions.d.ts +94 -0
  42. package/dist/components/ModelViewer/fieldCode.d.ts +74 -0
  43. package/dist/components/ModelViewer/fieldEntities.d.ts +45 -0
  44. package/dist/components/ModelViewer/fieldExpansion.d.ts +41 -0
  45. package/dist/components/ModelViewer/givenUsage.d.ts +71 -0
  46. package/dist/components/ModelViewer/headerCard.d.ts +31 -0
  47. package/dist/components/ModelViewer/httpStatus.d.ts +4 -0
  48. package/dist/components/ModelViewer/icons.d.ts +31 -0
  49. package/dist/components/ModelViewer/index.d.ts +9 -0
  50. package/dist/components/ModelViewer/joinAccess.d.ts +52 -0
  51. package/dist/components/ModelViewer/malloyRunText.d.ts +22 -0
  52. package/dist/components/ModelViewer/malloyText.d.ts +21 -0
  53. package/dist/components/ModelViewer/modelGivens.d.ts +61 -0
  54. package/dist/components/ModelViewer/modelGivensContext.d.ts +9 -0
  55. package/dist/components/ModelViewer/modelHeaderPresentation.d.ts +28 -0
  56. package/dist/components/ModelViewer/modelTextDeclarations.d.ts +32 -0
  57. package/dist/components/ModelViewer/modelTextParse.d.ts +27 -0
  58. package/dist/components/ModelViewer/namedQueryCode.d.ts +12 -0
  59. package/dist/components/ModelViewer/offSurface.d.ts +54 -0
  60. package/dist/components/ModelViewer/rowLayers.d.ts +30 -0
  61. package/dist/components/ModelViewer/runGate.d.ts +49 -0
  62. package/dist/components/ModelViewer/schemaModel.d.ts +109 -0
  63. package/dist/components/ModelViewer/sectionOpenCommand.d.ts +45 -0
  64. package/dist/components/ModelViewer/sourceAccess.d.ts +67 -0
  65. package/dist/components/ModelViewer/sourceLineage.d.ts +14 -0
  66. package/dist/components/ModelViewer/sourcePicker.d.ts +66 -0
  67. package/dist/components/ModelViewer/surface.d.ts +47 -0
  68. package/dist/components/ModelViewer/text.d.ts +6 -0
  69. package/dist/components/ModelViewer/theme.d.ts +63 -0
  70. package/dist/components/ModelViewer/tokens.d.ts +52 -0
  71. package/dist/components/ModelViewer/ui.d.ts +48 -0
  72. package/dist/components/ModelViewer/useDimensionSamples.d.ts +76 -0
  73. package/dist/components/ModelViewer/useModelSources.d.ts +92 -0
  74. package/dist/components/ModelViewer/useRunQuery.d.ts +115 -0
  75. package/dist/components/ReportBuilder/loadMalloy.d.ts +4 -0
  76. package/dist/components/ServerProvider.d.ts +17 -2
  77. package/dist/components/filter/utils.d.ts +1 -1
  78. package/dist/components/given/GivenInput.d.ts +7 -1
  79. package/dist/components/given/GivensPanel.d.ts +3 -1
  80. package/dist/components/given/filterSummary.d.ts +6 -0
  81. package/dist/components/given/useGivenUrlParams.d.ts +11 -0
  82. package/dist/components/index.d.ts +2 -1
  83. package/dist/components/modelPage.d.ts +36 -0
  84. package/dist/hooks/useDimensionalFilterRangeData.d.ts +9 -0
  85. package/dist/hooks/useSuggestOptions.d.ts +26 -2
  86. package/dist/index.cjs.js +47 -19
  87. package/dist/index.es.js +14137 -9697
  88. package/dist/useCompiledDocument-D88_2cGS.cjs.js +143 -0
  89. package/dist/{useCompiledDocument-CXMPtJkJ.es.js → useCompiledDocument-Dmm6xkom.es.js} +6975 -6893
  90. package/dist/utils/filterCondition.d.ts +14 -0
  91. package/dist/utils/malloyLiteral.d.ts +20 -1
  92. package/package.json +2 -2
  93. package/src/components/Materializations/MaterializationDetailDialog.tsx +33 -7
  94. package/src/components/Materializations/Materializations.tsx +47 -4
  95. package/src/components/Materializations/Materializations.versions.spec.tsx +140 -1
  96. package/src/components/Model/ExplorerPage.tsx +269 -0
  97. package/src/components/Model/Model.tsx +66 -250
  98. package/src/components/Model/ModelExplorer.spec.tsx +64 -1
  99. package/src/components/ModelViewer/AccessChips.tsx +149 -0
  100. package/src/components/ModelViewer/EmptyRow.tsx +71 -0
  101. package/src/components/ModelViewer/Facts.tsx +46 -0
  102. package/src/components/ModelViewer/FieldGroup.tsx +137 -0
  103. package/src/components/ModelViewer/FieldRow.tsx +566 -0
  104. package/src/components/ModelViewer/GivenFacts.tsx +89 -0
  105. package/src/components/ModelViewer/HeaderBar.tsx +362 -0
  106. package/src/components/ModelViewer/InlineDoc.tsx +50 -0
  107. package/src/components/ModelViewer/ModelGivensSection.tsx +142 -0
  108. package/src/components/ModelViewer/ModelSection.tsx +98 -0
  109. package/src/components/ModelViewer/ModelViewer.spec.tsx +937 -0
  110. package/src/components/ModelViewer/ModelViewer.tsx +777 -0
  111. package/src/components/ModelViewer/QueriesGroup.tsx +107 -0
  112. package/src/components/ModelViewer/RowGlyph.tsx +138 -0
  113. package/src/components/ModelViewer/RunResult.tsx +298 -0
  114. package/src/components/ModelViewer/SampleValues.tsx +97 -0
  115. package/src/components/ModelViewer/SchemaJoin.tsx +258 -0
  116. package/src/components/ModelViewer/SchemaView.tsx +222 -0
  117. package/src/components/ModelViewer/SegmentIcons.tsx +129 -0
  118. package/src/components/ModelViewer/SourcesDropdown.tsx +265 -0
  119. package/src/components/ModelViewer/SourcesNav.tsx +75 -0
  120. package/src/components/ModelViewer/StatusPill.tsx +145 -0
  121. package/src/components/ModelViewer/ValueChip.tsx +39 -0
  122. package/src/components/ModelViewer/ViewRow.tsx +274 -0
  123. package/src/components/ModelViewer/clickableBox.ts +59 -0
  124. package/src/components/ModelViewer/codeDisplay.spec.ts +176 -0
  125. package/src/components/ModelViewer/codeDisplay.ts +118 -0
  126. package/src/components/ModelViewer/docAnnotations.spec.ts +181 -0
  127. package/src/components/ModelViewer/docAnnotations.ts +244 -0
  128. package/src/components/ModelViewer/docAnnotations.typeDetail.spec.ts +114 -0
  129. package/src/components/ModelViewer/explorerModel.spec.ts +43 -0
  130. package/src/components/ModelViewer/explorerModel.ts +31 -0
  131. package/src/components/ModelViewer/extensions.tsx +112 -0
  132. package/src/components/ModelViewer/fieldCode.spec.ts +168 -0
  133. package/src/components/ModelViewer/fieldCode.ts +118 -0
  134. package/src/components/ModelViewer/fieldEntities.ts +63 -0
  135. package/src/components/ModelViewer/fieldExpansion.spec.ts +116 -0
  136. package/src/components/ModelViewer/fieldExpansion.ts +95 -0
  137. package/src/components/ModelViewer/givenUsage.spec.ts +304 -0
  138. package/src/components/ModelViewer/givenUsage.ts +325 -0
  139. package/src/components/ModelViewer/headerCard.tsx +102 -0
  140. package/src/components/ModelViewer/httpStatus.ts +15 -0
  141. package/src/components/ModelViewer/icons.tsx +67 -0
  142. package/src/components/ModelViewer/index.ts +26 -0
  143. package/src/components/ModelViewer/joinAccess.spec.ts +95 -0
  144. package/src/components/ModelViewer/joinAccess.ts +54 -0
  145. package/src/components/ModelViewer/malloyRunText.spec.ts +48 -0
  146. package/src/components/ModelViewer/malloyRunText.ts +51 -0
  147. package/src/components/ModelViewer/malloyText.spec.ts +47 -0
  148. package/src/components/ModelViewer/malloyText.ts +77 -0
  149. package/src/components/ModelViewer/modelGivens.ts +228 -0
  150. package/src/components/ModelViewer/modelGivensContext.ts +13 -0
  151. package/src/components/ModelViewer/modelHeaderPresentation.spec.ts +31 -0
  152. package/src/components/ModelViewer/modelHeaderPresentation.ts +41 -0
  153. package/src/components/ModelViewer/modelTextDeclarations.spec.ts +336 -0
  154. package/src/components/ModelViewer/modelTextDeclarations.ts +228 -0
  155. package/src/components/ModelViewer/modelTextParse.ts +112 -0
  156. package/src/components/ModelViewer/namedQueryCode.spec.ts +58 -0
  157. package/src/components/ModelViewer/namedQueryCode.ts +26 -0
  158. package/src/components/ModelViewer/offSurface.tsx +124 -0
  159. package/src/components/ModelViewer/rowLayers.tsx +104 -0
  160. package/src/components/ModelViewer/runGate.spec.ts +219 -0
  161. package/src/components/ModelViewer/runGate.ts +145 -0
  162. package/src/components/ModelViewer/schemaModel.defaultSource.spec.ts +126 -0
  163. package/src/components/ModelViewer/schemaModel.queries.spec.ts +37 -0
  164. package/src/components/ModelViewer/schemaModel.ts +300 -0
  165. package/src/components/ModelViewer/sectionOpenCommand.tsx +125 -0
  166. package/src/components/ModelViewer/sourceAccess.spec.ts +130 -0
  167. package/src/components/ModelViewer/sourceAccess.ts +194 -0
  168. package/src/components/ModelViewer/sourceLineage.spec.ts +73 -0
  169. package/src/components/ModelViewer/sourceLineage.ts +38 -0
  170. package/src/components/ModelViewer/sourcePicker.spec.ts +294 -0
  171. package/src/components/ModelViewer/sourcePicker.ts +162 -0
  172. package/src/components/ModelViewer/surface.ts +51 -0
  173. package/src/components/ModelViewer/text.tsx +33 -0
  174. package/src/components/ModelViewer/theme.spec.ts +38 -0
  175. package/src/components/ModelViewer/theme.ts +58 -0
  176. package/src/components/ModelViewer/tokens.ts +87 -0
  177. package/src/components/ModelViewer/ui.tsx +201 -0
  178. package/src/components/ModelViewer/useDimensionSamples.spec.tsx +139 -0
  179. package/src/components/ModelViewer/useDimensionSamples.ts +220 -0
  180. package/src/components/ModelViewer/useModelSources.spec.ts +110 -0
  181. package/src/components/ModelViewer/useModelSources.ts +238 -0
  182. package/src/components/ModelViewer/useRunQuery.spec.ts +50 -0
  183. package/src/components/ModelViewer/useRunQuery.ts +223 -0
  184. package/src/components/Package/Package.agents.spec.tsx +5 -4
  185. package/src/components/Package/Package.tsx +8 -1
  186. package/src/components/ReportBuilder/emptyStart.spec.ts +8 -0
  187. package/src/components/ReportBuilder/loadMalloy.ts +8 -1
  188. package/src/components/ServerProvider.tsx +29 -3
  189. package/src/components/filter/utils.ts +4 -60
  190. package/src/components/given/FilterButton.tsx +4 -1
  191. package/src/components/given/GivenInput.tsx +8 -0
  192. package/src/components/given/GivensPanel.tsx +6 -1
  193. package/src/components/given/filterSummary.spec.ts +31 -1
  194. package/src/components/given/filterSummary.ts +41 -19
  195. package/src/components/given/useGivenUrlParams.spec.tsx +32 -0
  196. package/src/components/given/useGivenUrlParams.ts +49 -14
  197. package/src/components/index.ts +6 -1
  198. package/src/components/legacy/malloynb/Notebook.spec.tsx +38 -4
  199. package/src/components/legacy/malloynb/NotebookCell.tsx +8 -2
  200. package/src/components/modelPage.spec.ts +79 -0
  201. package/src/components/modelPage.ts +48 -0
  202. package/src/hooks/useDimensionFilters.ts +3 -58
  203. package/src/hooks/useDimensionFiltersQuery.ts +3 -58
  204. package/src/hooks/useDimensionalFilterRangeData.spec.ts +57 -0
  205. package/src/hooks/useDimensionalFilterRangeData.ts +8 -63
  206. package/src/hooks/useSuggestOptions.dom.spec.ts +54 -1
  207. package/src/hooks/useSuggestOptions.spec.ts +71 -0
  208. package/src/hooks/useSuggestOptions.ts +102 -12
  209. package/src/utils/filterCondition.spec.ts +87 -0
  210. package/src/utils/filterCondition.ts +66 -0
  211. package/src/utils/malloyLiteral.spec.ts +53 -1
  212. package/src/utils/malloyLiteral.ts +47 -12
  213. package/dist/ServerProvider-BHfg1Ed6.cjs.js +0 -1
  214. package/dist/useCompiledDocument-D7X_K6YU.cjs.js +0 -142
@@ -0,0 +1,107 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { useMemo } from "react";
5
+ import { scanGivensOfNamedQuery } from "./givenUsage";
6
+ import { namedQueryRunQuery } from "./malloyRunText";
7
+ import { ModelSection } from "./ModelSection";
8
+ import {
9
+ SectionOpenCommandContext,
10
+ useSectionExpand,
11
+ } from "./sectionOpenCommand";
12
+ import { namedQueryCode } from "./namedQueryCode";
13
+ import { useParsedModelText } from "./modelTextParse";
14
+ import type { ExplorerField, ExplorerQuery } from "./schemaModel";
15
+ import type { RunContext } from "./useRunQuery";
16
+ import { ViewRow } from "./ViewRow";
17
+
18
+ /**
19
+ * A model's named queries as a section of rows, drawn and run exactly as a
20
+ * source's views are (ViewRow): opening one runs it, under Result | Code. Its
21
+ * code is the `query:` declaration read from the
22
+ * model file; where the file isn't served, Code stays in place, disabled, saying
23
+ * why.
24
+ */
25
+ export function QueriesGroup({
26
+ queries,
27
+ sourceText,
28
+ modelPath,
29
+ run,
30
+ }: {
31
+ queries: ReadonlyArray<ExplorerQuery>;
32
+ sourceText?: string;
33
+ modelPath: string;
34
+ run?: RunContext;
35
+ }) {
36
+ const { parsed, pending } = useParsedModelText(
37
+ sourceText,
38
+ queries.length > 0,
39
+ );
40
+ const cards = useMemo(
41
+ () =>
42
+ queries.map((query) => {
43
+ const code = namedQueryCode(parsed, query.name);
44
+ // With no source to gate on, the givens its own text reads and
45
+ // those of the sources it names, not every given the model
46
+ // declares; and which of them it can't tie to the query. Not while
47
+ // the file is still being read.
48
+ const scan =
49
+ query.sourceName || !run?.givens || pending
50
+ ? undefined
51
+ : scanGivensOfNamedQuery(
52
+ code,
53
+ run.givens,
54
+ run.givensBySource,
55
+ run.unknownGivensBySource,
56
+ );
57
+ return {
58
+ query,
59
+ field: {
60
+ name: query.name,
61
+ kind: "view",
62
+ doc: query.doc,
63
+ tags: query.tags,
64
+ } satisfies ExplorerField,
65
+ code: { code, loading: pending },
66
+ runGivens: scan?.givens,
67
+ runGivensUnknown: scan?.unknown,
68
+ };
69
+ }),
70
+ [
71
+ queries,
72
+ parsed,
73
+ pending,
74
+ run?.givens,
75
+ run?.givensBySource,
76
+ run?.unknownGivensBySource,
77
+ ],
78
+ );
79
+ // A query opens on its result, so Expand runs every query in the section.
80
+ const expand = useSectionExpand({ noun: ["query", "queries"] });
81
+ return (
82
+ <ModelSection
83
+ label="Queries"
84
+ count={queries.length}
85
+ testId="model-viewer-queries"
86
+ action={expand.action}
87
+ >
88
+ <SectionOpenCommandContext.Provider value={expand.command}>
89
+ {cards.map(
90
+ ({ query, field, code, runGivens, runGivensUnknown }) => (
91
+ <ViewRow
92
+ key={query.name}
93
+ field={field}
94
+ modelPath={modelPath}
95
+ sourceName={query.sourceName ?? ""}
96
+ fieldCode={code}
97
+ run={run}
98
+ runQuery={namedQueryRunQuery(query.name)}
99
+ runGivens={runGivens}
100
+ runGivensUnknown={runGivensUnknown}
101
+ />
102
+ ),
103
+ )}
104
+ </SectionOpenCommandContext.Provider>
105
+ </ModelSection>
106
+ );
107
+ }
@@ -0,0 +1,138 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { Box } from "@mui/material";
5
+ import { createElement } from "react";
6
+ import {
7
+ IconBoolean,
8
+ IconCalculation,
9
+ IconColumn,
10
+ IconDate,
11
+ IconJoin,
12
+ IconList,
13
+ IconNumber,
14
+ IconRecord,
15
+ IconResult,
16
+ IconText,
17
+ IconTimestamp,
18
+ type Icon,
19
+ } from "./icons";
20
+ import { ACCENT, kindAccent } from "./tokens";
21
+
22
+ /**
23
+ * Every kind of row the model page draws, one glyph each: a model's dimensions,
24
+ * measures, calculations, views (and named queries) and joins, and a table's
25
+ * columns, which take a dimension's marks.
26
+ */
27
+ export type RowGlyphKind =
28
+ | "dimension"
29
+ | "measure"
30
+ | "calculate"
31
+ | "column"
32
+ | "view"
33
+ | "join";
34
+
35
+ /**
36
+ * A field kind's glyph when its type says nothing more: a view takes the Result
37
+ * glyph (it opens on its Result); a join, two lines merging; a field with no
38
+ * known type, a column.
39
+ */
40
+ const MODEL_GLYPH: Record<
41
+ "dimension" | "measure" | "calculate" | "view" | "join",
42
+ Icon
43
+ > = {
44
+ dimension: IconColumn,
45
+ measure: IconNumber,
46
+ calculate: IconCalculation,
47
+ view: IconResult,
48
+ join: IconJoin,
49
+ };
50
+
51
+ /** The hue each kind holds: its field accent (`kindAccent`), a join brown. */
52
+ function glyphColor(kind: RowGlyphKind): string {
53
+ switch (kind) {
54
+ case "dimension":
55
+ case "measure":
56
+ case "calculate":
57
+ case "view":
58
+ return kindAccent(kind).main;
59
+ // A table column is what a dimension is drawn from: its hue and marks.
60
+ case "column":
61
+ return kindAccent("dimension").main;
62
+ case "join":
63
+ return ACCENT.brown.main;
64
+ }
65
+ }
66
+
67
+ /**
68
+ * A field's glyph by its DATA TYPE (text, number, date, timestamp, boolean,
69
+ * list, record), the way a BI tool marks its fields: the glyph says what kind
70
+ * of value it is, and the hue says whether it groups or aggregates. `dataType`
71
+ * is the row's own type detail ("number · integer", "date · month",
72
+ * "array<string>"); an unknown or missing type keeps the role's glyph.
73
+ */
74
+ export function fieldTypeGlyph(dataType: string | undefined): Icon | undefined {
75
+ const t = dataType?.trim().toLowerCase() ?? "";
76
+ if (!t) return undefined;
77
+ if (t.startsWith("array")) return IconList;
78
+ if (t.startsWith("record") || /\bfields?\b/.test(t)) return IconRecord;
79
+ const base = t.split(/[\s·<]/)[0];
80
+ switch (base) {
81
+ case "string":
82
+ return IconText;
83
+ case "number":
84
+ return IconNumber;
85
+ case "date":
86
+ return IconDate;
87
+ case "timestamp":
88
+ case "timestamptz":
89
+ return IconTimestamp;
90
+ case "boolean":
91
+ return IconBoolean;
92
+ default:
93
+ return undefined;
94
+ }
95
+ }
96
+
97
+ /** One step over the row's 13px name. */
98
+ export const ROW_GLYPH_SIZE = 16;
99
+
100
+ /**
101
+ * The bare glyph that leads a model-page row: 16px, in its kind's hue (a
102
+ * field's glyph by its data type, `fieldTypeGlyph`). Decorative: the section a
103
+ * row sits in already names its kind, and the row's button names the row, so it
104
+ * is hidden from assistive tech.
105
+ */
106
+ export function RowGlyph({
107
+ kind,
108
+ dataType,
109
+ }: {
110
+ kind: RowGlyphKind;
111
+ dataType?: string;
112
+ }) {
113
+ const typed =
114
+ kind === "dimension" ||
115
+ kind === "measure" ||
116
+ kind === "calculate" ||
117
+ kind === "column"
118
+ ? fieldTypeGlyph(dataType)
119
+ : undefined;
120
+ return (
121
+ <Box
122
+ component="span"
123
+ aria-hidden
124
+ data-row-glyph={kind}
125
+ sx={{
126
+ display: "inline-flex",
127
+ alignSelf: "center",
128
+ flexShrink: 0,
129
+ color: glyphColor(kind),
130
+ }}
131
+ >
132
+ {createElement(
133
+ typed ?? MODEL_GLYPH[kind === "column" ? "dimension" : kind],
134
+ { size: ROW_GLYPH_SIZE },
135
+ )}
136
+ </Box>
137
+ );
138
+ }
@@ -0,0 +1,298 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { Box } from "@mui/material";
5
+ import { type ReactNode, useEffect, useMemo, useState } from "react";
6
+ import { Loading, LOADING_COPY } from "../Loading";
7
+ import { warmMalloyRenderer } from "../RenderedResult/loadRenderer";
8
+ import ResultContainer from "../RenderedResult/ResultContainer";
9
+ import { EmptyRow } from "./EmptyRow";
10
+ import { givensKeyFor, type GivenInfo } from "./modelGivens";
11
+ import { type RunGate, runGate } from "./runGate";
12
+ import { accessOf, filteredEmptyCaption, refusalMessage } from "./sourceAccess";
13
+ import { PANE_CARD_SX, PANE_INSET } from "./surface";
14
+ import { BodyText, CaptionText } from "./text";
15
+ import { PrimaryButton, QueryFailure } from "./ui";
16
+ import { givensOfRun, type RunContext, useRunQuery } from "./useRunQuery";
17
+
18
+ /** How tall a run's result may grow on the page before it scrolls inside its card. */
19
+ const RESULT_MAX_HEIGHT = 480;
20
+
21
+ /** A run's result as shown, with the given values it ran under. */
22
+ export interface ShownRun {
23
+ result: string;
24
+ /** The values it ran with, as `givensKeyFor` writes them: under the "values
25
+ * changed" banner these are not the panel's. */
26
+ givensKey: string;
27
+ }
28
+
29
+ /** What the body shows: the run's result (Session), or the Malloy definition
30
+ * (Base). The two layer segments a view card switches between. */
31
+ export type RunResultMode = "result" | "code";
32
+
33
+ /**
34
+ * The body of an open view's card (ViewRow): the result of running it, or the Malloy
35
+ * that declares it, whichever the card's Result | Code switch picks. Body only —
36
+ * the card above it carries the name and the switch.
37
+ *
38
+ * The run happens the first time the Result mode is shown, never in Code mode:
39
+ * reading a definition must not bill a query. It renders through the SDK's own
40
+ * result container, so a result here and in a report are one thing.
41
+ */
42
+ export function RunResult({
43
+ run,
44
+ modelPath,
45
+ query,
46
+ sourceName,
47
+ givens,
48
+ givensUnknown,
49
+ mode = "result",
50
+ renderCode,
51
+ onShownChange,
52
+ }: {
53
+ run: RunContext;
54
+ modelPath: string;
55
+ query: string;
56
+ /** The source a view belongs to, for its required-filter check; a named
57
+ * query has none. */
58
+ sourceName?: string;
59
+ /** The givens the run reads, where its source can't say (a named query the
60
+ * model gives no source); else its source's. */
61
+ givens?: ReadonlyArray<GivenInfo>;
62
+ /** With `givens`: those of them the page can't tie to the run either way
63
+ * (scanGivensOfNamedQuery). Without, its source's (`unknownGivensBySource`). */
64
+ givensUnknown?: ReadonlySet<string>;
65
+ /** Which body to show. The switching is the card's job; this renders
66
+ * whichever mode it is told. */
67
+ mode?: RunResultMode;
68
+ /** Renders the Malloy body for the `definition` mode. A function, called
69
+ * only when that body is shown, so a result-mode stage does none of the
70
+ * code-stripping work. */
71
+ renderCode?: () => ReactNode;
72
+ /** Told the result this stage is showing, including one held under the "values
73
+ * changed" banner, so a host's download exports exactly what is on screen. */
74
+ onShownChange?: (shown: ShownRun | undefined) => void;
75
+ }) {
76
+ // A run is armed for one set of given values — the first time Result is
77
+ // shown, at the values then in force — and stays armed for those, so
78
+ // switching to Code and back shows the cached result rather than
79
+ // running again. Committing a different value in the panel does NOT re-run
80
+ // every card: a warehouse query is billed, so the card keeps its last
81
+ // result, says the values changed, and re-runs on the reader's click.
82
+ const givensKey = givensKeyFor(run.givenValues);
83
+ const [armedKey, setArmedKey] = useState<string | null>(
84
+ mode === "result" ? givensKey : null,
85
+ );
86
+ if (mode === "result" && armedKey === null) setArmedKey(givensKey);
87
+ const valuesChanged = armedKey !== null && armedKey !== givensKey;
88
+
89
+ // Can this run be honest about its inputs? A blocked run never fires.
90
+ const requiredFilters = sourceName
91
+ ? run.requiredFiltersBySource?.get(sourceName)
92
+ : undefined;
93
+ // "Run anyway", at the values it was chosen for: a change of value asks again.
94
+ const [anywayAt, setAnywayAt] = useState<string | null>(null);
95
+ const runAnyway = anywayAt === givensKey;
96
+ const gate: RunGate = useMemo(
97
+ () =>
98
+ runGate(
99
+ givens ?? givensOfRun(run, sourceName),
100
+ requiredFilters,
101
+ run.givenValues,
102
+ givens
103
+ ? givensUnknown
104
+ : sourceName
105
+ ? run.unknownGivensBySource?.get(sourceName)
106
+ : undefined,
107
+ runAnyway,
108
+ givens ? "query" : "source",
109
+ ),
110
+ [run, sourceName, givens, givensUnknown, requiredFilters, runAnyway],
111
+ );
112
+ const { result, isSuccess, isError, error, refetch } = useRunQuery({
113
+ run,
114
+ modelPath,
115
+ query,
116
+ enabled: armedKey === givensKey && gate.kind !== "blocked",
117
+ });
118
+ // `result` is always the one for the values now in force: the cache answers
119
+ // for them even while the card is armed at others. So when the values come
120
+ // back to ones this card already ran, it is armed there again, and nothing
121
+ // on it is stale.
122
+ if (armedKey !== null && armedKey !== givensKey && result !== undefined) {
123
+ setArmedKey(givensKey);
124
+ }
125
+ // A refusal says, in words, that the reader can't access the source — naming
126
+ // a restricted source's rule.
127
+ const sourceAccess = accessOf(run.accessBySource, sourceName);
128
+ const forbiddenMessage = refusalMessage(sourceName, sourceAccess);
129
+ const runAtCurrentValues = () =>
130
+ valuesChanged ? setArmedKey(givensKey) : void refetch();
131
+ // The last result this card showed, kept while the panel's values move on:
132
+ // the query is keyed by the new values (and disabled until the reader
133
+ // clicks), so its own `result` is empty then — but the run already paid for
134
+ // stays on screen under the banner until the banner's Run swaps it.
135
+ // Labelled by the values it ran at, which are `result`'s own.
136
+ const [held, setHeld] = useState<{
137
+ result: string;
138
+ givensKey: string;
139
+ } | null>(null);
140
+ if (
141
+ result !== undefined &&
142
+ (held?.givensKey !== givensKey || held.result !== result)
143
+ ) {
144
+ setHeld({ result, givensKey });
145
+ }
146
+ // `result` is the query keyed by the panel's values, so it ran with them. A blocked
147
+ // card shows only why, so it reports no result: a download offers what is on screen.
148
+ const shown: ShownRun | undefined =
149
+ gate.kind === "blocked"
150
+ ? undefined
151
+ : result !== undefined
152
+ ? { result, givensKey }
153
+ : valuesChanged && held
154
+ ? { result: held.result, givensKey: held.givensKey }
155
+ : undefined;
156
+ const shownResult = shown?.result;
157
+ // Parses the result only on a row-filtered source, and once per result.
158
+ const filteredCaption = useMemo(
159
+ () => filteredEmptyCaption(sourceAccess, shownResult),
160
+ [sourceAccess, shownResult],
161
+ );
162
+ // Reported by value, so the card re-renders only when what is shown changes.
163
+ const shownGivensKey = shown?.givensKey ?? "";
164
+ useEffect(() => {
165
+ if (!onShownChange) return;
166
+ if (shownResult === undefined) return onShownChange(undefined);
167
+ onShownChange({ result: shownResult, givensKey: shownGivensKey });
168
+ }, [onShownChange, shownResult, shownGivensKey]);
169
+ const willShow = mode === "result" && gate.kind !== "blocked";
170
+ useEffect(() => {
171
+ if (willShow) warmMalloyRenderer();
172
+ }, [willShow]);
173
+ return (
174
+ <Box data-testid="model-viewer-run-result" data-mode={mode}>
175
+ {/* A run that finished at values the panel has since changed offers its
176
+ re-run inline, in the stale banner below. */}
177
+ {mode === "code" ? (
178
+ renderCode?.()
179
+ ) : gate.kind === "blocked" ? (
180
+ // Refused, not failed: a bare 400 would say nothing about the
181
+ // value the model wanted.
182
+ <Box
183
+ sx={{
184
+ display: "flex",
185
+ alignItems: "center",
186
+ gap: 1.5,
187
+ flexWrap: "wrap",
188
+ }}
189
+ >
190
+ <BodyText data-testid="model-viewer-run-blocked">
191
+ {gate.reason}
192
+ </BodyText>
193
+ {gate.canRunAnyway && (
194
+ // Held only for givens the source may not read: the reader's
195
+ // call, since a source that does read one fails without it.
196
+ <PrimaryButton
197
+ size="small"
198
+ onClick={() => {
199
+ setAnywayAt(givensKey);
200
+ setArmedKey(givensKey);
201
+ }}
202
+ >
203
+ Run anyway
204
+ </PrimaryButton>
205
+ )}
206
+ </Box>
207
+ ) : isError && !valuesChanged ? (
208
+ <QueryFailure
209
+ query={query}
210
+ error={error}
211
+ onRetry={() => void refetch()}
212
+ forbiddenMessage={forbiddenMessage}
213
+ />
214
+ ) : isSuccess && !result && !valuesChanged ? (
215
+ // The run finished and the publisher sent no result body: say so,
216
+ // rather than holding a skeleton that promises one.
217
+ <EmptyRow message="The query returned no result." />
218
+ ) : (
219
+ // Skeleton and render share one box: the skeleton holds the
220
+ // height, the render fades in over it, and the box is released
221
+ // to the content's own height only once there is content. In the
222
+ // same card as a code block (PANE_CARD_SX), so an opened view frames
223
+ // its chart or table as a measure frames its code: a contained result,
224
+ // like a dashboard's tile, not a table loose on the page. A table wider
225
+ // than the card scrolls inside it.
226
+ <Box
227
+ data-testid="model-viewer-result-card"
228
+ sx={{ ...PANE_CARD_SX, p: PANE_INSET, overflowX: "auto" }}
229
+ >
230
+ {valuesChanged && (
231
+ // The panel's values moved on since this ran. Re-running is
232
+ // the reader's call, not a side effect of a blur — and the
233
+ // result below is the one already paid for, kept until that click.
234
+ <Box
235
+ data-testid="model-viewer-run-stale"
236
+ sx={{
237
+ display: "flex",
238
+ alignItems: "center",
239
+ gap: 1.5,
240
+ flexWrap: "wrap",
241
+ mb: 1,
242
+ }}
243
+ >
244
+ <BodyText>
245
+ The given values changed since this ran.
246
+ </BodyText>
247
+ {/* One word on the face: the sentence beside it carries the
248
+ qualifier, which stays in the accessible name. */}
249
+ <PrimaryButton
250
+ size="small"
251
+ onClick={runAtCurrentValues}
252
+ aria-label="Run with these values"
253
+ >
254
+ Run
255
+ </PrimaryButton>
256
+ </Box>
257
+ )}
258
+ {gate.kind === "noted" && shownResult && !valuesChanged && (
259
+ // What produced this result: a report shows its givens above
260
+ // the cells; the same view here must name the defaults it
261
+ // ran at, or it reads as a different answer.
262
+ <CaptionText
263
+ data-testid="model-viewer-run-defaults"
264
+ component="div"
265
+ sx={{ mb: 1 }}
266
+ >
267
+ {gate.note}
268
+ </CaptionText>
269
+ )}
270
+ {/* With values changed and nothing held (the run failed or hadn't
271
+ landed), nothing is running, so the banner stands alone rather
272
+ than over a spinner. */}
273
+ {!(valuesChanged && !shownResult) &&
274
+ (shownResult === undefined ? (
275
+ <Loading text={LOADING_COPY.running} />
276
+ ) : (
277
+ <ResultContainer
278
+ result={shownResult}
279
+ maxHeight={RESULT_MAX_HEIGHT}
280
+ maxResultSize={run.maxResultSize}
281
+ />
282
+ ))}
283
+ {filteredCaption && (
284
+ // An access filter that admits none of this reader's rows answers
285
+ // with an empty result, which would otherwise read as "no data".
286
+ <CaptionText
287
+ data-testid="model-viewer-run-filtered"
288
+ component="div"
289
+ sx={{ mt: 1 }}
290
+ >
291
+ {filteredCaption}
292
+ </CaptionText>
293
+ )}
294
+ </Box>
295
+ )}
296
+ </Box>
297
+ );
298
+ }
@@ -0,0 +1,97 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { Box, CircularProgress } from "@mui/material";
5
+ import type { ExplorerField } from "./schemaModel";
6
+ import { fontSizes, TEXT_TERTIARY } from "./theme";
7
+ import {
8
+ useDimensionSamples,
9
+ type SampleValuesFetcher,
10
+ } from "./useDimensionSamples";
11
+ import type { RunContext } from "./useRunQuery";
12
+ import { ValueChip } from "./ValueChip";
13
+
14
+ /**
15
+ * A dimension's sample values: its most common values, most common first,
16
+ * fetched lazily (see useDimensionSamples). What the dimension MEANS, so its
17
+ * own card on the open row.
18
+ *
19
+ * <p>Mounts only inside an open row, so the fetch is paid for on a reader's
20
+ * click rather than while browsing.
21
+ */
22
+ export function SampleValues({
23
+ run,
24
+ fetchSamples,
25
+ modelPath,
26
+ sourceName,
27
+ field,
28
+ generation,
29
+ }: {
30
+ run: RunContext;
31
+ fetchSamples?: SampleValuesFetcher;
32
+ modelPath: string;
33
+ sourceName: string;
34
+ field: ExplorerField;
35
+ /** The host build the values come from (see useDimensionSamples). */
36
+ generation?: string;
37
+ }) {
38
+ const { samples, total, unavailable, loading, failed } = useDimensionSamples(
39
+ {
40
+ run,
41
+ fetchSamples,
42
+ modelPath,
43
+ sourceName,
44
+ field,
45
+ generation,
46
+ },
47
+ );
48
+ return (
49
+ <Box data-testid="model-viewer-values" sx={{ fontSize: fontSizes.small }}>
50
+ <Box
51
+ component="div"
52
+ sx={{
53
+ color: TEXT_TERTIARY,
54
+ fontSize: fontSizes.caption,
55
+ mb: "6px",
56
+ }}
57
+ >
58
+ {total != null
59
+ ? `The most common of ${total.toLocaleString()} ${total === 1 ? "value" : "values"}`
60
+ : "The most common values"}
61
+ </Box>
62
+ {loading ? (
63
+ <CircularProgress size={12} sx={{ color: "text.secondary" }} />
64
+ ) : unavailable ? (
65
+ // Why there are none: a given a run needs first, or a host's reason.
66
+ <Box
67
+ component="span"
68
+ data-testid="model-viewer-values-unavailable"
69
+ sx={{ color: "text.secondary" }}
70
+ >
71
+ {unavailable}
72
+ </Box>
73
+ ) : failed ? (
74
+ // A failed read is an unknown, not an empty list.
75
+ <Box
76
+ component="span"
77
+ sx={{ fontStyle: "italic", color: "text.secondary" }}
78
+ >
79
+ Sample values couldn&apos;t be loaded.
80
+ </Box>
81
+ ) : samples.length === 0 ? (
82
+ <Box
83
+ component="span"
84
+ sx={{ fontStyle: "italic", color: "text.secondary" }}
85
+ >
86
+ No values found.
87
+ </Box>
88
+ ) : (
89
+ <Box sx={{ display: "flex", flexWrap: "wrap", gap: "6px" }}>
90
+ {samples.map((value) => (
91
+ <ValueChip key={value}>{value}</ValueChip>
92
+ ))}
93
+ </Box>
94
+ )}
95
+ </Box>
96
+ );
97
+ }