@malloy-publisher/sdk 0.0.248 → 0.0.249

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 (98) hide show
  1. package/README.md +32 -17
  2. package/dist/ServerProvider-CcfeBpt3.cjs.js +1 -0
  3. package/dist/{ServerProvider-BxEh3nFI.es.js → ServerProvider-EqBlMMqx.es.js} +1056 -905
  4. package/dist/client/api.d.ts +297 -6
  5. package/dist/client/index.cjs.js +1 -1
  6. package/dist/client/index.es.js +74 -70
  7. package/dist/components/Dashboard/Dashboard.d.ts +49 -0
  8. package/dist/components/Dashboard/DashboardTile.d.ts +48 -0
  9. package/dist/components/Dashboard/index.d.ts +2 -0
  10. package/dist/components/Notebook/Notebook.d.ts +46 -8
  11. package/dist/components/Notebook/NotebookCell.d.ts +37 -3
  12. package/dist/components/Notebook/runPlan.d.ts +29 -0
  13. package/dist/components/Notebook/types.d.ts +10 -0
  14. package/dist/components/Package/ContentTypeIcon.d.ts +17 -1
  15. package/dist/components/Package/Package.d.ts +1 -4
  16. package/dist/components/RenderedResult/RenderedResult.d.ts +7 -1
  17. package/dist/components/RenderedResult/ResultContainer.d.ts +3 -1
  18. package/dist/components/ResultsDialog.d.ts +4 -1
  19. package/dist/components/ServerProvider.d.ts +2 -1
  20. package/dist/components/click_helper.d.ts +10 -1
  21. package/dist/components/drill/index.d.ts +3 -0
  22. package/dist/components/drill/markDrillableCells.d.ts +55 -0
  23. package/dist/components/drill/resolveDrill.d.ts +159 -0
  24. package/dist/components/drill/useDrill.d.ts +63 -0
  25. package/dist/components/given/GivenInput.d.ts +27 -3
  26. package/dist/components/given/GivensPanel.d.ts +36 -7
  27. package/dist/components/given/filterValue.d.ts +117 -0
  28. package/dist/components/given/index.d.ts +1 -1
  29. package/dist/components/given/paramCodec.d.ts +60 -0
  30. package/dist/components/index.d.ts +4 -1
  31. package/dist/components/styles.d.ts +23 -0
  32. package/dist/constants/docLinks.d.ts +6 -0
  33. package/dist/{core-BuvlzSIX.es.js → core-DFBwNhvb.es.js} +1 -1
  34. package/dist/{core-C86Rb7tJ.cjs.js → core-DZk7z7ko.cjs.js} +1 -1
  35. package/dist/hooks/givenValue.d.ts +10 -0
  36. package/dist/hooks/index.d.ts +3 -1
  37. package/dist/hooks/useGivensState.d.ts +64 -0
  38. package/dist/hooks/useSuggestOptions.d.ts +78 -0
  39. package/dist/{index-BdtAZ5k6.es.js → index-CLcRrkxd.es.js} +41221 -39894
  40. package/dist/index-CWQXhe0D.cjs.js +338 -0
  41. package/dist/index.cjs.js +1 -1
  42. package/dist/index.es.js +77 -62
  43. package/dist/theme/types.d.ts +9 -0
  44. package/package.json +3 -1
  45. package/src/components/Connections/AddConnectionDialog.tsx +10 -1
  46. package/src/components/Dashboard/Dashboard.tsx +395 -0
  47. package/src/components/Dashboard/DashboardTile.tsx +150 -0
  48. package/src/components/Dashboard/index.ts +2 -0
  49. package/src/components/Home/Home.tsx +114 -23
  50. package/src/components/Notebook/Notebook.tsx +444 -390
  51. package/src/components/Notebook/NotebookCell.tsx +117 -3
  52. package/src/components/Notebook/runPlan.spec.ts +152 -0
  53. package/src/components/Notebook/runPlan.ts +64 -0
  54. package/src/components/Notebook/types.ts +11 -0
  55. package/src/components/Package/ContentTypeIcon.spec.ts +88 -0
  56. package/src/components/Package/ContentTypeIcon.tsx +77 -6
  57. package/src/components/Package/Package.tsx +148 -38
  58. package/src/components/RenderedResult/RenderedResult.tsx +309 -19
  59. package/src/components/RenderedResult/ResultContainer.tsx +28 -1
  60. package/src/components/ResultsDialog.tsx +5 -0
  61. package/src/components/ServerProvider.tsx +2 -0
  62. package/src/components/click_helper.ts +14 -1
  63. package/src/components/drill/index.ts +25 -0
  64. package/src/components/drill/markDrillableCells.dom.spec.ts +310 -0
  65. package/src/components/drill/markDrillableCells.spec.ts +106 -0
  66. package/src/components/drill/markDrillableCells.ts +311 -0
  67. package/src/components/drill/resolveDrill.spec.ts +446 -0
  68. package/src/components/drill/resolveDrill.ts +372 -0
  69. package/src/components/drill/useDrill.tsx +200 -0
  70. package/src/components/given/GivenInput.spec.tsx +495 -15
  71. package/src/components/given/GivenInput.tsx +562 -30
  72. package/src/components/given/GivensPanel.spec.tsx +172 -0
  73. package/src/components/given/GivensPanel.tsx +139 -32
  74. package/src/components/given/filterValue.spec.ts +361 -0
  75. package/src/components/given/filterValue.ts +204 -0
  76. package/src/components/given/index.ts +5 -1
  77. package/src/components/given/paramCodec.spec.ts +854 -0
  78. package/src/components/given/paramCodec.ts +480 -0
  79. package/src/components/index.ts +4 -1
  80. package/src/components/styles.ts +27 -3
  81. package/src/constants/docLinks.spec.ts +51 -0
  82. package/src/constants/docLinks.ts +26 -0
  83. package/src/hooks/givenValue.ts +10 -0
  84. package/src/hooks/index.ts +6 -4
  85. package/src/hooks/useGivensState.spec.ts +446 -0
  86. package/src/hooks/useGivensState.ts +337 -0
  87. package/src/hooks/useSuggestOptions.spec.ts +109 -0
  88. package/src/hooks/useSuggestOptions.ts +260 -0
  89. package/src/theme/buildTableCssVars.ts +4 -0
  90. package/src/theme/resolveTheme.spec.ts +13 -0
  91. package/src/theme/resolveTheme.ts +3 -0
  92. package/src/theme/types.ts +9 -0
  93. package/test/README.md +16 -10
  94. package/dist/ServerProvider-Dg5S1qBU.cjs.js +0 -1
  95. package/dist/hooks/useGivensForm.d.ts +0 -35
  96. package/dist/index-MES4MGpQ.cjs.js +0 -309
  97. package/src/hooks/useGivensForm.spec.ts +0 -99
  98. package/src/hooks/useGivensForm.ts +0 -107
@@ -10,6 +10,7 @@ import {
10
10
  DialogContent,
11
11
  DialogTitle,
12
12
  IconButton,
13
+ LinearProgress,
13
14
  Snackbar,
14
15
  Stack,
15
16
  Tooltip,
@@ -21,12 +22,21 @@ import { usePublisherTheme } from "../../theme/ThemeContext";
21
22
  import { parseResourceUri } from "../../utils/formatting";
22
23
  import { highlight } from "../highlighter";
23
24
  import { ModelExplorerDialog } from "../Model/ModelExplorerDialog";
25
+ import type { NavigationClick } from "../click_helper";
26
+ import { useDrill, type DrillNavigation } from "../drill";
24
27
  import { createEmbeddedQueryResult } from "../QueryResult/QueryResult";
25
28
  import ResultContainer from "../RenderedResult/ResultContainer";
26
29
  import ResultsDialog from "../ResultsDialog";
27
30
  import { CleanMetricCard, CleanNotebookCell } from "../styles";
28
31
  import { EnhancedNotebookCell } from "./types";
29
32
 
33
+ /**
34
+ * Cap for a cell result that lays itself out: a table, mostly. Tall enough for
35
+ * a screenful of rows, after which the cell scrolls rather than the page
36
+ * turning into one long table.
37
+ */
38
+ const CELL_MAX_HEIGHT = 700;
39
+
30
40
  interface NotebookCellProps {
31
41
  cell: EnhancedNotebookCell;
32
42
  expandCodeCell?: boolean;
@@ -37,7 +47,43 @@ interface NotebookCellProps {
37
47
  index: number;
38
48
  maxResultSize?: number;
39
49
  isExecuting?: boolean;
40
- onNavigate?: (to: string, event?: React.MouseEvent) => void;
50
+ /**
51
+ * A re-run is coming but has not started, i.e. the given-settle window.
52
+ * Separate from `isExecuting` because the spinner below is gated on
53
+ * `!cell.result`, so on a re-run (where a result IS present) it renders
54
+ * nothing at all, which is exactly the case the settle window needs to
55
+ * signal: the values on screen are already stale.
56
+ */
57
+ pendingRerun?: boolean;
58
+ // Takes the modifier subset rather than a synthetic event, so a drill click
59
+ // (which the Malloy renderer reports as a DOM event) can navigate without
60
+ // being converted into a React one first.
61
+ onNavigate?: (to: string, event?: NavigationClick) => void;
62
+ /**
63
+ * Applies a `# drill { to=self }` in place, by setting the named parameter
64
+ * on the notebook that owns this cell. Omitted when the notebook declares no
65
+ * givens, which makes a self-drill inert rather than an error.
66
+ *
67
+ * Takes the clicked value unencoded: only the notebook knows the declared
68
+ * type of the given being set, and the encoding depends on it.
69
+ */
70
+ onDrillSelf?: (given: string, rawValue: unknown) => void;
71
+ /**
72
+ * Whether the owning notebook declares a given, so a `to=self` drill onto it
73
+ * can be honoured. Without it every self-drill reads as clickable and the
74
+ * refusal only shows up in the console.
75
+ */
76
+ canDrillSelf?: (given: string) => boolean;
77
+ /**
78
+ * Opens a `# drill { to=<dashboard> }` destination, seeded with the clicked
79
+ * value. Distinct from {@link onNavigate}, which routes a markdown link and
80
+ * takes a URL: this one takes a destination and the givens to seed, and only
81
+ * the host knows how to turn those into a route.
82
+ *
83
+ * Omitted on a host with no dashboard route, which makes a cross-dashboard
84
+ * drill inert rather than dead-ended, and removes the affordance with it.
85
+ */
86
+ onDrillNavigate?: (target: DrillNavigation, event?: MouseEvent) => void;
41
87
  }
42
88
 
43
89
  interface NotebookMarkdownLinkProps {
@@ -47,7 +93,7 @@ interface NotebookMarkdownLinkProps {
47
93
  envName: string;
48
94
  pkgName: string;
49
95
  sourceDir: string;
50
- onNavigate?: (to: string, event?: React.MouseEvent) => void;
96
+ onNavigate?: (to: string, event?: NavigationClick) => void;
51
97
  }
52
98
 
53
99
  // Links inside a rendered notebook/README are authored relative to the source
@@ -124,7 +170,11 @@ export function NotebookCell({
124
170
  index,
125
171
  maxResultSize,
126
172
  isExecuting,
173
+ pendingRerun,
127
174
  onNavigate,
175
+ onDrillSelf,
176
+ canDrillSelf,
177
+ onDrillNavigate,
128
178
  }: NotebookCellProps) {
129
179
  const [codeDialogOpen, setCodeDialogOpen] = React.useState<boolean>(false);
130
180
  const [embeddingDialogOpen, setEmbeddingDialogOpen] =
@@ -142,6 +192,36 @@ export function NotebookCell({
142
192
 
143
193
  const { environmentName, packageName, modelPath } =
144
194
  parseResourceUri(resourceUri);
195
+
196
+ // `# drill` is declared on a model dimension, so a notebook cell that groups
197
+ // by that dimension is clickable for free: the same resolution, and the
198
+ // same hook, the dashboard viewer uses.
199
+ const { drill, drillMenu } = useDrill({
200
+ // The dashboards route now exists, so a `# drill { to=<dashboard> }` from
201
+ // a notebook cell is honoured: the host turns the destination and its
202
+ // seeded givens into `/{env}/{pkg}/dashboards/<slug>?GIVEN=value`. Passed
203
+ // through rather than built here, because only the host knows its routing.
204
+ //
205
+ // Still optional, and the reason matters: a host without a dashboard
206
+ // route omits it, which makes a named destination non-honorable and
207
+ // removes the AFFORDANCE along with the navigation. That is
208
+ // `markDrillableCells`'s principle, that a destination the surface cannot
209
+ // reach never becomes a link, and it is why this is a prop rather than
210
+ // something the cell assumes.
211
+ //
212
+ // Wiring it also makes `drillMenu` reachable for the first time: a menu
213
+ // opens only for two or more honorable destinations, so until there was a
214
+ // second one every drillable cell applied its filter on the first click.
215
+ onNavigate: onDrillNavigate,
216
+ onSelf: onDrillSelf,
217
+ // Only the notebook knows which givens it declares, and a self-drill onto
218
+ // one it does not is refused. Asking here keeps the affordance honest
219
+ // instead of painting a link whose click only logs a warning.
220
+ canSelf: canDrillSelf,
221
+ // The reader is in a notebook, so `to=self` says so: the drill tag is on
222
+ // a shared model dimension and cannot know which document it fired in.
223
+ selfLabel: "Filter this notebook",
224
+ });
145
225
  // Directory of the source file within the package (empty for a package-root
146
226
  // README), used to resolve relative links the author wrote against it.
147
227
  const sourceDir =
@@ -523,11 +603,16 @@ export function NotebookCell({
523
603
  onClose={() => setResultsDialogOpen(false)}
524
604
  result={cell.result || ""}
525
605
  title="Results"
606
+ drill={drill}
526
607
  />
527
608
 
528
609
  {/* Loading state for executing code cells (not import cells) */}
529
610
  {isExecuting &&
530
611
  !cell.result &&
612
+ /* A failed cell keeps `isExecuting` (it is notebook-wide, not
613
+ per cell), so without this the error card below renders under
614
+ a spinner and the cell reads as still loading. */
615
+ !cell.error &&
531
616
  !hasValidImport &&
532
617
  !(cell.newSources && cell.newSources.length > 0) && (
533
618
  <CleanMetricCard
@@ -542,6 +627,33 @@ export function NotebookCell({
542
627
  </CleanMetricCard>
543
628
  )}
544
629
 
630
+ {!cell.result && cell.error && (
631
+ <CleanMetricCard sx={{ p: 2 }}>
632
+ <Typography variant="body2" sx={{ color: "error.main" }}>
633
+ This cell could not be run.
634
+ </Typography>
635
+ <Typography
636
+ variant="body2"
637
+ component="pre"
638
+ sx={{
639
+ color: "text.secondary",
640
+ whiteSpace: "pre-wrap",
641
+ m: 0,
642
+ mt: 1,
643
+ }}
644
+ >
645
+ {cell.error}
646
+ </Typography>
647
+ </CleanMetricCard>
648
+ )}
649
+
650
+ {cell.result && pendingRerun && (
651
+ <LinearProgress
652
+ aria-label="a re-run is pending"
653
+ sx={{ mb: 1, height: 2, borderRadius: 1 }}
654
+ />
655
+ )}
656
+
545
657
  {cell.result && (
546
658
  <CleanMetricCard
547
659
  sx={{
@@ -555,10 +667,12 @@ export function NotebookCell({
555
667
  >
556
668
  <ResultContainer
557
669
  result={cell.result}
558
- maxHeight={700}
670
+ maxHeight={CELL_MAX_HEIGHT}
559
671
  maxResultSize={maxResultSize}
672
+ drill={drill}
560
673
  />
561
674
  </Box>
675
+ {drillMenu}
562
676
 
563
677
  {/* Top right corner controls.
564
678
  `top: -12px` lifts the buttons above the
@@ -0,0 +1,152 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import { planRun } from "./runPlan";
3
+
4
+ const DOC = "uri|1";
5
+ const key = (givens: string) => `${DOC}|${givens}`;
6
+
7
+ describe("planRun", () => {
8
+ it("disarms a pending run when the notebook is not loaded", () => {
9
+ // Handled here rather than by an early return in the effect, because an
10
+ // early return skipped the cancellation: navigating away inside the
11
+ // settle window left a timer holding the previous notebook's closure.
12
+ expect(
13
+ planRun({
14
+ ready: false,
15
+ lastRunKey: key(""),
16
+ pendingKey: key("REGION=West"),
17
+ runKey: key("REGION=West"),
18
+ documentKey: DOC,
19
+ }),
20
+ ).toEqual({ cancelPending: true, dispatch: "none" });
21
+ });
22
+
23
+ it("does nothing at all when there is nothing pending and nothing loaded", () => {
24
+ expect(
25
+ planRun({
26
+ ready: false,
27
+ lastRunKey: null,
28
+ pendingKey: undefined,
29
+ runKey: key(""),
30
+ documentKey: DOC,
31
+ }),
32
+ ).toEqual({ cancelPending: false, dispatch: "none" });
33
+ });
34
+
35
+ it("runs the first pass immediately", () => {
36
+ expect(
37
+ planRun({
38
+ ready: true,
39
+ lastRunKey: null,
40
+ pendingKey: undefined,
41
+ runKey: key(""),
42
+ documentKey: DOC,
43
+ }),
44
+ ).toEqual({ cancelPending: false, dispatch: "now" });
45
+ });
46
+
47
+ it("makes a changed parameter wait for the value to settle", () => {
48
+ expect(
49
+ planRun({
50
+ ready: true,
51
+ lastRunKey: key(""),
52
+ pendingKey: undefined,
53
+ runKey: key("REGION=West"),
54
+ documentKey: DOC,
55
+ }),
56
+ ).toEqual({ cancelPending: false, dispatch: "settle" });
57
+ });
58
+
59
+ it("does not make a different notebook wait", () => {
60
+ // A document change is not a burst, so delaying it is only a blank screen.
61
+ expect(
62
+ planRun({
63
+ ready: true,
64
+ lastRunKey: "uri|1|",
65
+ pendingKey: undefined,
66
+ runKey: "other|1|",
67
+ documentKey: "other|1",
68
+ }).dispatch,
69
+ ).toBe("now");
70
+ });
71
+
72
+ it("does not make a package reload wait", () => {
73
+ // A reload bumps the generation, which changes the document key.
74
+ expect(
75
+ planRun({
76
+ ready: true,
77
+ lastRunKey: "uri|1|REGION=West",
78
+ pendingKey: undefined,
79
+ runKey: "uri|2|REGION=West",
80
+ documentKey: "uri|2",
81
+ }).dispatch,
82
+ ).toBe("now");
83
+ });
84
+
85
+ it("lets a pending run for the same key finish its clock", () => {
86
+ // Restarting it on every unrelated re-render could hold it off forever.
87
+ expect(
88
+ planRun({
89
+ ready: true,
90
+ lastRunKey: key(""),
91
+ pendingKey: key("REGION=West"),
92
+ runKey: key("REGION=West"),
93
+ documentKey: DOC,
94
+ }),
95
+ ).toEqual({ cancelPending: false, dispatch: "none" });
96
+ });
97
+
98
+ it("supersedes a pending run when the value changes again", () => {
99
+ expect(
100
+ planRun({
101
+ ready: true,
102
+ lastRunKey: key(""),
103
+ pendingKey: key("REGION=We"),
104
+ runKey: key("REGION=West"),
105
+ documentKey: DOC,
106
+ }),
107
+ ).toEqual({ cancelPending: true, dispatch: "settle" });
108
+ });
109
+
110
+ it("cancels a pending run when the user reverts to what already ran", () => {
111
+ // The defect this function was extracted for. Typing a value and deleting
112
+ // it again inside the settle window lands on an already-run key. Cancelling
113
+ // has to happen even though there is nothing to dispatch, or the abandoned
114
+ // value's results arrive and nothing is left to correct them.
115
+ expect(
116
+ planRun({
117
+ ready: true,
118
+ lastRunKey: key(""),
119
+ pendingKey: key("REGION=West"),
120
+ runKey: key(""),
121
+ documentKey: DOC,
122
+ }),
123
+ ).toEqual({ cancelPending: true, dispatch: "none" });
124
+ });
125
+
126
+ it("cancels a pending run when the DOCUMENT changes under it", () => {
127
+ // The reachable case the first table missed: a parameter edit arms a
128
+ // timer, then the reader navigates to another notebook inside the window.
129
+ // The pending run belongs to the notebook they just left.
130
+ expect(
131
+ planRun({
132
+ ready: true,
133
+ lastRunKey: "uri|1|",
134
+ pendingKey: "uri|1|REGION=West",
135
+ runKey: "other|1|",
136
+ documentKey: "other|1",
137
+ }),
138
+ ).toEqual({ cancelPending: true, dispatch: "now" });
139
+ });
140
+
141
+ it("does nothing when the state is unchanged", () => {
142
+ expect(
143
+ planRun({
144
+ ready: true,
145
+ lastRunKey: key("REGION=West"),
146
+ pendingKey: undefined,
147
+ runKey: key("REGION=West"),
148
+ documentKey: DOC,
149
+ }),
150
+ ).toEqual({ cancelPending: false, dispatch: "none" });
151
+ });
152
+ });
@@ -0,0 +1,64 @@
1
+ /**
2
+ * What the notebook's run effect should do about the state it just observed.
3
+ *
4
+ * Pulled out as a pure function because the inline version produced two defects
5
+ * that no test could reach: it first lost the bound on concurrent runs
6
+ * entirely, then, once a settle delay put the bound back, it let an abandoned
7
+ * run fire. Both were reasoning errors about
8
+ * three interacting pieces of state, which is exactly what a table of cases
9
+ * pins and a component test does not.
10
+ */
11
+ export interface RunPlanInput {
12
+ /** Whether the notebook is loaded and runnable at all. */
13
+ ready: boolean;
14
+ /** Key of the run that has already been dispatched, or null before the first. */
15
+ lastRunKey: string | null;
16
+ /** Key of a run currently waiting out the settle delay, if any. */
17
+ pendingKey: string | undefined;
18
+ /** Key for the state the notebook is in now. */
19
+ runKey: string;
20
+ /** The `resourceUri|generation` prefix of {@link runKey}, without the parameters. */
21
+ documentKey: string;
22
+ }
23
+
24
+ export interface RunPlan {
25
+ /** Clear the pending timer, because what it would run is no longer current. */
26
+ cancelPending: boolean;
27
+ /** `now` skips the settle delay; `settle` waits it out; `none` does nothing. */
28
+ dispatch: "now" | "settle" | "none";
29
+ }
30
+
31
+ export function planRun({
32
+ ready,
33
+ lastRunKey,
34
+ pendingKey,
35
+ runKey,
36
+ documentKey,
37
+ }: RunPlanInput): RunPlan {
38
+ // A notebook that is not loaded still has to disarm. This is the reason the
39
+ // readiness check belongs here rather than as an early return in the effect:
40
+ // as an early return it skipped the cancellation below, so navigating away
41
+ // inside the settle window left a timer holding the PREVIOUS notebook's
42
+ // closure, which then ran a full wave of cells against the notebook the
43
+ // reader had just left and wrote them into the one now on screen.
44
+ if (!ready) {
45
+ return { cancelPending: pendingKey !== undefined, dispatch: "none" };
46
+ }
47
+
48
+ // A pending run for any other key is stale the moment the notebook moves on,
49
+ // INCLUDING when it moves back to a key that has already run. That last case
50
+ // is the one that bit: typing a value and deleting it again inside the
51
+ // window returns to an already-run key, and leaving the timer armed meant the
52
+ // abandoned value's results landed on screen with nothing to correct them.
53
+ const cancelPending = pendingKey !== undefined && pendingKey !== runKey;
54
+
55
+ if (lastRunKey === runKey) return { cancelPending, dispatch: "none" };
56
+ if (pendingKey === runKey) return { cancelPending, dispatch: "none" };
57
+
58
+ // Only a changed PARAMETER waits. A first run, a different notebook, and a
59
+ // package reload all change the document rather than a value, and none of
60
+ // them is a burst worth coalescing: delaying them is just a blank screen.
61
+ const changedDocument =
62
+ lastRunKey === null || !lastRunKey.startsWith(`${documentKey}|`);
63
+ return { cancelPending, dispatch: changedDocument ? "now" : "settle" };
64
+ }
@@ -21,4 +21,15 @@ export interface EnhancedNotebookCell extends ClientNotebookCell {
21
21
  * that become available in this cell (e.g., from imports or source definitions)
22
22
  */
23
23
  newSources?: string[];
24
+
25
+ /**
26
+ * Why this cell has no result, when the server refused to run it.
27
+ *
28
+ * A cell that throws used to be logged to the console and nothing else, so
29
+ * the reader got an empty space with no indication anything had happened.
30
+ * That was survivable while the only way to reach it was a server fault; it
31
+ * stopped being survivable once a model with a required `#(filter)` began
32
+ * failing every cell by design.
33
+ */
34
+ error?: string;
24
35
  }
@@ -0,0 +1,88 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import { MALLOY_ACCENT } from "../styles";
3
+ import { CONTENT_TINT, type ContentType } from "./ContentTypeIcon";
4
+
5
+ /** Relative luminance, WCAG 2.x definition. */
6
+ function luminance(hex: string): number {
7
+ const channels = [1, 3, 5].map(
8
+ (i) => parseInt(hex.slice(i, i + 2), 16) / 255,
9
+ );
10
+ const [r, g, b] = channels.map((c) =>
11
+ c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4,
12
+ );
13
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
14
+ }
15
+
16
+ function contrastWithWhite(hex: string): number {
17
+ return (1 + 0.05) / (luminance(hex) + 0.05);
18
+ }
19
+
20
+ describe("CONTENT_TINT", () => {
21
+ /**
22
+ * The whole point of the map. Before the package row derived its color from
23
+ * its type, four of the six rows were handed the same teal by four separate
24
+ * call sites, so color distinguished two kinds out of six. A repeat here is
25
+ * that regression arriving again, and nothing renders differently enough for
26
+ * a person to notice it in review.
27
+ */
28
+ it("gives every content type its own color", () => {
29
+ const values = Object.values(CONTENT_TINT);
30
+ expect(new Set(values).size).toBe(values.length);
31
+ });
32
+ });
33
+
34
+ /**
35
+ * Content types whose colour predates this bar. `report` is backed by
36
+ * `MALLOY_BRAND.teal` at 2.5:1, which is below it; that is inherited from the
37
+ * logo and is a brand decision rather than a component one.
38
+ *
39
+ * Keyed by content TYPE rather than by colour, which is the whole point. A new
40
+ * type handed teal is not grandfathered and fails, and fixing teal one day
41
+ * leaves this entry merely unused rather than turning a correct fix into a red
42
+ * suite.
43
+ */
44
+ const GRANDFATHERED: ContentType[] = ["report"];
45
+
46
+ describe("CONTENT_TINT contrast", () => {
47
+ /**
48
+ * Every tint fills a 32px backplate behind an 18px white glyph, so it has to
49
+ * clear 3:1
50
+ * against white, WCAG's minimum for a graphical object. Asserted rather than
51
+ * written down, because the failure is invisible: a lighter colour looks fine
52
+ * in a screenshot and the icon simply stops being readable.
53
+ *
54
+ * Over `CONTENT_TINT` rather than over `MALLOY_ACCENT`, because the tints are
55
+ * what actually get painted. Asserting the accent palette alone left three of
56
+ * the six painted values unchecked.
57
+ */
58
+ it.each(
59
+ Object.entries(CONTENT_TINT).filter(
60
+ ([type]) => !GRANDFATHERED.includes(type as ContentType),
61
+ ),
62
+ )("%s clears 3:1 against white", (_type, hex) => {
63
+ expect(contrastWithWhite(hex)).toBeGreaterThanOrEqual(3);
64
+ });
65
+
66
+ // Guards the exception list against drift: a grandfathered name that no
67
+ // longer matches a `ContentType` fails here rather than silently exempting
68
+ // nothing. It does NOT catch the list being emptied, which passes
69
+ // vacuously; that case is caught instead by the contrast assertion for the
70
+ // type that stops being skipped, and an empty list is the desired end state
71
+ // anyway.
72
+ it("grandfathers only types that exist", () => {
73
+ for (const type of GRANDFATHERED) {
74
+ expect(CONTENT_TINT).toHaveProperty(type);
75
+ }
76
+ });
77
+ });
78
+
79
+ describe("MALLOY_ACCENT", () => {
80
+ // The accents were chosen against this bar, so they hold it even before any
81
+ // of them is assigned to a content type.
82
+ it.each(Object.entries(MALLOY_ACCENT))(
83
+ "%s clears 3:1 against white",
84
+ (_name, hex) => {
85
+ expect(contrastWithWhite(hex)).toBeGreaterThanOrEqual(3);
86
+ },
87
+ );
88
+ });
@@ -1,6 +1,37 @@
1
1
  import SvgIcon, { SvgIconProps } from "@mui/material/SvgIcon";
2
+ import { MALLOY_ACCENT, MALLOY_BRAND } from "../styles";
2
3
 
3
- type ContentType = "report" | "model" | "data" | "materialization" | "dataApp";
4
+ export type ContentType =
5
+ | "report"
6
+ | "model"
7
+ | "data"
8
+ | "materialization"
9
+ | "dataApp"
10
+ | "dashboard";
11
+
12
+ /**
13
+ * The backplate color behind each icon. One per content type and no repeats: on
14
+ * a page that lists all six, a shared color reads as a shared kind, and the
15
+ * glyphs are small enough that color does most of the telling apart.
16
+ *
17
+ * The logo's three go to the three artifacts a reader opens to look at data,
18
+ * dashboards and notebooks and the models under them, and the accents to the
19
+ * rest, so the brand leads on the things the page is mostly for. Not a
20
+ * seniority ranking: dashboards are the newest thing a package can hold, and
21
+ * Package Data is among the oldest and takes an accent.
22
+ *
23
+ * Exhaustive by type rather than by a default, so adding a `ContentType` is a
24
+ * compile error here instead of a row that silently paints itself the same as
25
+ * its neighbour.
26
+ */
27
+ export const CONTENT_TINT: Record<ContentType, string> = {
28
+ dashboard: MALLOY_BRAND.orange,
29
+ report: MALLOY_BRAND.teal,
30
+ model: MALLOY_BRAND.darkBlue,
31
+ dataApp: MALLOY_ACCENT.violet,
32
+ data: MALLOY_ACCENT.moss,
33
+ materialization: MALLOY_ACCENT.magenta,
34
+ };
4
35
 
5
36
  interface ContentTypeIconProps extends Omit<SvgIconProps, "fontSize"> {
6
37
  type: ContentType;
@@ -35,15 +66,31 @@ export default function ContentTypeIcon({
35
66
  ...sx,
36
67
  }}
37
68
  >
38
- {type === "report" && <FileChartPath />}
39
- {type === "model" && <BoxTopPath />}
40
- {type === "data" && <TablePath />}
41
- {type === "materialization" && <StackPath />}
42
- {type === "dataApp" && <BrowserWindowPath />}
69
+ {CONTENT_GLYPH[type]()}
43
70
  </SvgIcon>
44
71
  );
45
72
  }
46
73
 
74
+ /**
75
+ * The glyph for each content type, a `Record` for the same reason
76
+ * `CONTENT_TINT` is one. This was a chain of `type === "x" && <Path />`, which
77
+ * type-checks with no arm for a new content type and renders an empty `<svg>`
78
+ * on a coloured backplate: the colour half of the signal failed loudly at
79
+ * compile time and the glyph half failed silently at runtime. Two halves of one
80
+ * signal should fail the same way. The failure mode differs at runtime as well
81
+ * as at compile time: an unknown type throws here, where the old chain rendered
82
+ * an empty `<svg>`. Unreachable today, since every call site passes a literal,
83
+ * and a crash is the better of the two if it ever is reachable.
84
+ */
85
+ const CONTENT_GLYPH: Record<ContentType, () => React.ReactElement> = {
86
+ report: FileChartPath,
87
+ model: BoxTopPath,
88
+ data: TablePath,
89
+ materialization: StackPath,
90
+ dataApp: BrowserWindowPath,
91
+ dashboard: DashboardGridPath,
92
+ };
93
+
47
94
  /** File outline with a folded top-right corner and three chart bars inside. */
48
95
  function FileChartPath() {
49
96
  return (
@@ -90,6 +137,30 @@ function StackPath() {
90
137
  );
91
138
  }
92
139
 
140
+ /**
141
+ * Panels of unequal size in one frame: the grid of tiles a dashboard lays out.
142
+ *
143
+ * The neighbour to stay distinct from is the TABLE glyph, not the data app's
144
+ * browser window. Both this and the table are a rounded rect with the same
145
+ * `M3.5 9.5` divider, so before the second divider the whole difference between
146
+ * them was one vertical stroke: `M12 9.5 V20.5` here against the table's
147
+ * `M9.5 3.5 V20.5`: shifted 2.5 across, and starting at the divider rather
148
+ * than at the top of the frame. That read as the
149
+ * same icon at 18px, with the Dashboards and Databases sections on one page.
150
+ * The second divider is what makes this one read as a grid rather than as
151
+ * columns.
152
+ */
153
+ function DashboardGridPath() {
154
+ return (
155
+ <>
156
+ <rect x="3.5" y="3.5" width="17" height="17" rx="2" />
157
+ <path d="M3.5 9.5 H20.5" />
158
+ <path d="M12 9.5 V20.5" />
159
+ <path d="M12 15 H20.5" />
160
+ </>
161
+ );
162
+ }
163
+
93
164
  /** Browser-window outline with an address-bar separator and content lines. */
94
165
  function BrowserWindowPath() {
95
166
  return (