@malloy-publisher/sdk 0.3.0 → 0.3.1

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 (156) hide show
  1. package/README.md +8 -0
  2. package/dist/DocumentStorageProvider-BeBD0uWc.cjs.js +134 -0
  3. package/dist/DocumentStorageProvider-tGvodI8f.es.js +24268 -0
  4. package/dist/RenderedResult-CPi6uQWq.cjs.js +115 -0
  5. package/dist/RenderedResult-mDE-4u8S.es.js +542 -0
  6. package/dist/ServerProvider-CH71GsoL.cjs.js +1 -0
  7. package/dist/{ServerProvider-DSK9rfSY.es.js → ServerProvider-X2Ma0HRs.es.js} +486 -521
  8. package/dist/builder/index.cjs.js +105 -0
  9. package/dist/builder/index.d.ts +2 -0
  10. package/dist/builder/index.es.js +9561 -0
  11. package/dist/builder-entry.d.ts +8 -0
  12. package/dist/client/index.cjs.js +1 -1
  13. package/dist/client/index.es.js +1 -1
  14. package/dist/components/Dashboard/Dashboard.d.ts +19 -14
  15. package/dist/components/Dashboard/DashboardGrid.d.ts +67 -0
  16. package/dist/components/Dashboard/DashboardTile.d.ts +6 -1
  17. package/dist/components/Dashboard/ExploreDialog.d.ts +18 -0
  18. package/dist/components/Dashboard/RowsDialog.d.ts +47 -0
  19. package/dist/components/Dashboard/TileCard.d.ts +44 -0
  20. package/dist/components/Dashboard/index.d.ts +2 -0
  21. package/dist/components/Dashboard/promoteMeasureRow.d.ts +7 -3
  22. package/dist/components/Dashboard/telemetry.d.ts +43 -0
  23. package/dist/components/DashboardBuilder/AddTileDialog.d.ts +35 -0
  24. package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +29 -0
  25. package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +112 -0
  26. package/dist/components/DashboardBuilder/DashboardEditor.d.ts +32 -0
  27. package/dist/components/DashboardBuilder/DiffDialog.d.ts +15 -0
  28. package/dist/components/DashboardBuilder/DrillDialog.d.ts +15 -0
  29. package/dist/components/DashboardBuilder/FilterDialog.d.ts +54 -0
  30. package/dist/components/DashboardBuilder/FilterStrip.d.ts +17 -0
  31. package/dist/components/DashboardBuilder/SettingsPopover.d.ts +20 -0
  32. package/dist/components/DashboardBuilder/TileFrame.d.ts +42 -0
  33. package/dist/components/DashboardBuilder/TileMenu.d.ts +24 -0
  34. package/dist/components/DashboardBuilder/catalog.d.ts +62 -0
  35. package/dist/components/DashboardBuilder/controls.d.ts +157 -0
  36. package/dist/components/DashboardBuilder/diff.d.ts +35 -0
  37. package/dist/components/DashboardBuilder/document.d.ts +218 -0
  38. package/dist/components/DashboardBuilder/layout.d.ts +71 -0
  39. package/dist/components/DashboardBuilder/malloyText.d.ts +45 -0
  40. package/dist/components/DashboardBuilder/preview.d.ts +60 -0
  41. package/dist/components/DashboardBuilder/readDocument.d.ts +106 -0
  42. package/dist/components/DashboardBuilder/sortable.d.ts +51 -0
  43. package/dist/components/DashboardBuilder/spliceDocument.d.ts +44 -0
  44. package/dist/components/DashboardBuilder/testing/fixtures.d.ts +10 -0
  45. package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +22 -0
  46. package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +63 -0
  47. package/dist/components/DashboardBuilder/useDraft.d.ts +13 -0
  48. package/dist/components/DashboardBuilder/useFilterForm.d.ts +65 -0
  49. package/dist/components/DashboardBuilder/useTileReorder.d.ts +22 -0
  50. package/dist/components/DashboardBuilder/useTileResize.d.ts +29 -0
  51. package/dist/components/DocumentStorage/DocumentStorageProvider.d.ts +7 -0
  52. package/dist/components/DocumentStorage/index.d.ts +1 -1
  53. package/dist/components/FloatingIconButton.d.ts +16 -0
  54. package/dist/components/Prose.d.ts +46 -0
  55. package/dist/components/RenderedResult/ResultPanel.d.ts +30 -0
  56. package/dist/components/RenderedResult/index.d.ts +1 -0
  57. package/dist/components/drill/index.d.ts +1 -1
  58. package/dist/components/drill/resolveDrill.d.ts +22 -0
  59. package/dist/components/drill/useDrill.d.ts +11 -2
  60. package/dist/components/index.d.ts +1 -0
  61. package/dist/components/styles.d.ts +0 -4
  62. package/dist/hooks/index.d.ts +2 -0
  63. package/dist/hooks/useDocumentControls.d.ts +70 -0
  64. package/dist/hooks/useQueryResult.d.ts +40 -0
  65. package/dist/index.cjs.js +23 -270
  66. package/dist/index.es.js +16977 -41293
  67. package/dist/utils/malloyLiteral.d.ts +11 -0
  68. package/package.json +24 -9
  69. package/src/builder-entry.ts +55 -0
  70. package/src/components/Dashboard/Dashboard.spec.tsx +7 -10
  71. package/src/components/Dashboard/Dashboard.tsx +165 -175
  72. package/src/components/Dashboard/DashboardGrid.tsx +124 -0
  73. package/src/components/Dashboard/DashboardTile.spec.tsx +30 -6
  74. package/src/components/Dashboard/DashboardTile.tsx +66 -123
  75. package/src/components/Dashboard/ExploreDialog.tsx +84 -0
  76. package/src/components/Dashboard/RowsDialog.spec.ts +42 -0
  77. package/src/components/Dashboard/RowsDialog.tsx +234 -0
  78. package/src/components/Dashboard/TileCard.tsx +120 -0
  79. package/src/components/Dashboard/index.ts +2 -0
  80. package/src/components/Dashboard/promoteMeasureRow.spec.ts +70 -0
  81. package/src/components/Dashboard/promoteMeasureRow.ts +33 -2
  82. package/src/components/Dashboard/telemetry.ts +44 -0
  83. package/src/components/Dashboard/tileGridColumn.spec.ts +1 -1
  84. package/src/components/DashboardBuilder/AddTileDialog.tsx +249 -0
  85. package/src/components/DashboardBuilder/BuilderToolbar.tsx +270 -0
  86. package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +858 -0
  87. package/src/components/DashboardBuilder/DashboardBuilder.tsx +653 -0
  88. package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +211 -0
  89. package/src/components/DashboardBuilder/DashboardEditor.tsx +493 -0
  90. package/src/components/DashboardBuilder/DiffDialog.tsx +129 -0
  91. package/src/components/DashboardBuilder/DrillDialog.tsx +281 -0
  92. package/src/components/DashboardBuilder/FilterDialog.tsx +581 -0
  93. package/src/components/DashboardBuilder/FilterStrip.tsx +125 -0
  94. package/src/components/DashboardBuilder/SettingsPopover.tsx +168 -0
  95. package/src/components/DashboardBuilder/TileFrame.tsx +380 -0
  96. package/src/components/DashboardBuilder/TileMenu.tsx +188 -0
  97. package/src/components/DashboardBuilder/catalog.spec.ts +215 -0
  98. package/src/components/DashboardBuilder/catalog.ts +225 -0
  99. package/src/components/DashboardBuilder/controls.spec.ts +243 -0
  100. package/src/components/DashboardBuilder/controls.ts +398 -0
  101. package/src/components/DashboardBuilder/diff.spec.ts +44 -0
  102. package/src/components/DashboardBuilder/diff.ts +93 -0
  103. package/src/components/DashboardBuilder/document.ts +209 -0
  104. package/src/components/DashboardBuilder/keepRowStructure.spec.ts +84 -0
  105. package/src/components/DashboardBuilder/layout.ts +143 -0
  106. package/src/components/DashboardBuilder/malloyText.spec.ts +84 -0
  107. package/src/components/DashboardBuilder/malloyText.ts +151 -0
  108. package/src/components/DashboardBuilder/moveIntoGap.spec.ts +90 -0
  109. package/src/components/DashboardBuilder/preview.spec.ts +149 -0
  110. package/src/components/DashboardBuilder/preview.ts +172 -0
  111. package/src/components/DashboardBuilder/readDocument.spec.ts +375 -0
  112. package/src/components/DashboardBuilder/readDocument.ts +467 -0
  113. package/src/components/DashboardBuilder/sortable.tsx +151 -0
  114. package/src/components/DashboardBuilder/spliceDocument.spec.ts +1108 -0
  115. package/src/components/DashboardBuilder/spliceDocument.ts +1098 -0
  116. package/src/components/DashboardBuilder/testing/fixtures.ts +48 -0
  117. package/src/components/DashboardBuilder/useBuilderShortcuts.ts +88 -0
  118. package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +255 -0
  119. package/src/components/DashboardBuilder/useDashboardEditor.ts +193 -0
  120. package/src/components/DashboardBuilder/useDraft.ts +43 -0
  121. package/src/components/DashboardBuilder/useFilterForm.spec.tsx +144 -0
  122. package/src/components/DashboardBuilder/useFilterForm.ts +352 -0
  123. package/src/components/DashboardBuilder/useTileReorder.spec.tsx +97 -0
  124. package/src/components/DashboardBuilder/useTileReorder.ts +115 -0
  125. package/src/components/DashboardBuilder/useTileResize.spec.tsx +121 -0
  126. package/src/components/DashboardBuilder/useTileResize.ts +110 -0
  127. package/src/components/DashboardBuilder/withGaps.spec.ts +96 -0
  128. package/src/components/DocumentStorage/DocumentStorageProvider.tsx +12 -0
  129. package/src/components/DocumentStorage/index.ts +1 -0
  130. package/src/components/Environment/About.tsx +4 -36
  131. package/src/components/FloatingIconButton.tsx +41 -0
  132. package/src/components/Model/Model.tsx +6 -23
  133. package/src/components/Model/ModelCell.tsx +33 -72
  134. package/src/components/Notebook/Notebook.tsx +25 -88
  135. package/src/components/Notebook/NotebookCell.tsx +35 -183
  136. package/src/components/Prose.spec.tsx +85 -0
  137. package/src/components/Prose.tsx +179 -0
  138. package/src/components/QueryResult/QueryResult.tsx +15 -43
  139. package/src/components/RenderedResult/ResultContainer.tsx +4 -12
  140. package/src/components/RenderedResult/ResultPanel.spec.tsx +49 -0
  141. package/src/components/RenderedResult/ResultPanel.tsx +73 -0
  142. package/src/components/RenderedResult/index.ts +1 -0
  143. package/src/components/drill/index.ts +1 -0
  144. package/src/components/drill/resolveDrill.ts +22 -1
  145. package/src/components/drill/useDrill.spec.tsx +94 -0
  146. package/src/components/drill/useDrill.tsx +52 -12
  147. package/src/components/given/GivensPanel.tsx +1 -1
  148. package/src/components/index.ts +6 -0
  149. package/src/components/styles.ts +0 -37
  150. package/src/hooks/index.ts +13 -0
  151. package/src/hooks/useDocumentControls.ts +170 -0
  152. package/src/hooks/useQueryResult.ts +82 -0
  153. package/src/utils/malloyLiteral.spec.ts +43 -0
  154. package/src/utils/malloyLiteral.ts +76 -0
  155. package/vite.config.ts +5 -0
  156. package/dist/ServerProvider-CGnLjzag.cjs.js +0 -1
@@ -0,0 +1,124 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { Box } from "@mui/material";
5
+ import type { ReactNode } from "react";
6
+
7
+ /** Grid width when the dashboard declares no `# dashboard { columns=N }`. */
8
+ export const DEFAULT_COLUMNS = 2;
9
+
10
+ /**
11
+ * The gutter between tiles, in px.
12
+ *
13
+ * A number rather than a `gap: 2` spacing unit because the BUILDER has to do
14
+ * arithmetic with it: turning a dragged edge into a column count means solving
15
+ * for the track width, and that needs the gutter in the same units as a
16
+ * `getBoundingClientRect`. Exported so the value the grid paints and the value
17
+ * the drag solves with cannot drift apart.
18
+ */
19
+ export const GRID_GAP_PX = 16;
20
+
21
+ /**
22
+ * The `grid-column` one tile occupies: its `# colspan`, and a `# break` forcing
23
+ * it to start a fresh row.
24
+ *
25
+ * Clamped to the grid width the same way @malloydata/render clamps it, so one
26
+ * view laid out as a composite tile and as a `nest:` under `# dashboard` lands
27
+ * in the same place. A break is `1 / span N` — an explicit start line, which is
28
+ * what pushes the tile down to the next row; the renderer's grid does the same.
29
+ */
30
+ export function tileGridColumn(
31
+ tile: { colspan?: number; break?: boolean },
32
+ columns: number,
33
+ ): string {
34
+ const span = Math.min(tile.colspan ?? 1, columns);
35
+ return tile.break ? `1 / span ${span}` : `span ${span}`;
36
+ }
37
+
38
+ /** The layout a tile carries, whatever else its own shape holds. */
39
+ export interface GridTile {
40
+ colspan?: number;
41
+ break?: boolean;
42
+ }
43
+
44
+ /**
45
+ * The composite dashboard's grid: the column track, and each tile's place on
46
+ * it.
47
+ *
48
+ * Shared by the READER ({@link Dashboard}) and the BUILDER, which is the whole
49
+ * point of it being a component rather than a rule each surface applies. An
50
+ * author arranging a dashboard is arranging the thing a reader will open, so
51
+ * the two cannot be allowed to lay tiles out even slightly differently; a
52
+ * restated track without the grid item's own `display: grid` ends rows at
53
+ * ragged heights on one surface and level on the other.
54
+ *
55
+ * **Why the item is a grid and not a block.** A grid item stretches to its row,
56
+ * but a block CHILD of one does not — it keeps its content height. Results
57
+ * divide on exactly that line (see `resultSizing`): a table, a map or a
58
+ * `# big_value` sizes to its content, while a plotted chart fills whatever box
59
+ * it is handed. So a block item put a short table beside a tall chart and let
60
+ * the row end twice. Making the item a grid passes the row's height down to the
61
+ * tile, and every card in a row ends level.
62
+ *
63
+ * The tile itself is the caller's: the reader renders a `DashboardTile`, the
64
+ * builder wraps one in its selection outline. Anything this component rendered
65
+ * on their behalf would be a third opinion about what a tile looks like.
66
+ */
67
+ export function DashboardGrid<T extends GridTile>({
68
+ tiles,
69
+ columns,
70
+ keyOf,
71
+ renderTile,
72
+ }: {
73
+ tiles: readonly T[];
74
+ /** Track count — `# dashboard { columns=N }`, or {@link DEFAULT_COLUMNS}. */
75
+ columns: number;
76
+ /**
77
+ * This tile's React key. Taken from the caller because the two surfaces
78
+ * identify a tile differently and both are right: the manifest's `tiles=[…]`
79
+ * can repeat one expression (a typo, not a request for two identical
80
+ * panels), so the reader keys on position as well.
81
+ */
82
+ keyOf: (tile: T, index: number) => string;
83
+ renderTile: (tile: T, index: number) => ReactNode;
84
+ }) {
85
+ return (
86
+ <Box
87
+ sx={{
88
+ display: "grid",
89
+ gridTemplateColumns: {
90
+ xs: "1fr",
91
+ md: `repeat(${columns}, minmax(0, 1fr))`,
92
+ },
93
+ gap: `${GRID_GAP_PX}px`,
94
+ }}
95
+ >
96
+ {tiles.map((tile, index) => (
97
+ <Box
98
+ key={keyOf(tile, index)}
99
+ sx={{
100
+ // Load-bearing — see the note above.
101
+ display: "grid",
102
+ // A tile follows its TRACK, whatever it holds. A grid item's
103
+ // minimum width is `auto` — its content's minimum — and a
104
+ // chart's content is an SVG drawn at the width the tile had
105
+ // when it rendered. So without this, narrowing a tile did not
106
+ // narrow it: the item stayed as wide as the old chart, ran
107
+ // under its neighbour, and the renderer's own size observer
108
+ // never saw a change to redraw for. Widening worked, which is
109
+ // what made it look like the chart filled in one direction
110
+ // only. With the minimum at zero the item takes the track's
111
+ // width, the chart's box shrinks with it, and the renderer
112
+ // redraws to fit — the same way it already did on growth.
113
+ minWidth: 0,
114
+ // Only above `md`: the narrow breakpoint is one column, where
115
+ // a span would overflow the grid rather than widen anything.
116
+ gridColumn: { md: tileGridColumn(tile, columns) },
117
+ }}
118
+ >
119
+ {renderTile(tile, index)}
120
+ </Box>
121
+ ))}
122
+ </Box>
123
+ );
124
+ }
@@ -9,7 +9,7 @@
9
9
  * prop alone.
10
10
  */
11
11
  import { beforeEach, expect, it, mock } from "bun:test";
12
- import { render, waitFor } from "@testing-library/react";
12
+ import { fireEvent, render, screen, waitFor } from "@testing-library/react";
13
13
  import {
14
14
  cacheKeys,
15
15
  clearCache,
@@ -54,7 +54,7 @@ it("puts the version in the request body and in the key", async () => {
54
54
 
55
55
  await waitFor(() => expect(executeQueryModel).toHaveBeenCalled());
56
56
  expect(executeQueryModel.mock.calls[0][3].versionId).toBe("v2");
57
- expect(cacheKeys("dashboardTile")[0]).toContain('"v2"');
57
+ expect(cacheKeys("queryResult")[0]).toContain('"v2"');
58
58
  });
59
59
 
60
60
  it("sends and keys nothing extra without one", async () => {
@@ -64,9 +64,9 @@ it("sends and keys nothing extra without one", async () => {
64
64
  expect(executeQueryModel.mock.calls[0][3].versionId).toBeUndefined();
65
65
  // The whole key: an empty slot rather than a literal "undefined", and
66
66
  // nothing else disturbed. The narrow assertion above cannot see either.
67
- expect(cacheKeys("dashboardTile")[0]).toBe(
68
- '["dashboardTile","env","pkg",null,"dashboards/ops.malloy",null,' +
69
- '"sales_by_month","{}","http://localhost/api/v0"]',
67
+ expect(cacheKeys("queryResult")[0]).toBe(
68
+ '["queryResult","env","pkg",null,"dashboards/ops.malloy",null,' +
69
+ '"run: sales_by_month",null,"{}","http://localhost/api/v0"]',
70
70
  );
71
71
  });
72
72
 
@@ -77,5 +77,29 @@ it("keeps two versions of one tile apart", async () => {
77
77
  rerender(tileAt("v2"));
78
78
 
79
79
  await waitFor(() => expect(executeQueryModel).toHaveBeenCalledTimes(2));
80
- expect(new Set(cacheKeys("dashboardTile")).size).toBe(2);
80
+ expect(new Set(cacheKeys("queryResult")).size).toBe(2);
81
+ });
82
+
83
+ it("offers to explore from the tile when the host can take it somewhere", () => {
84
+ const onExplore = mock(() => {});
85
+ render(
86
+ <DashboardTile
87
+ environmentName="env"
88
+ packageName="pkg"
89
+ modelPath="dashboards/ops.malloy"
90
+ tile="overview -> sales_by_month"
91
+ givens={new Map()}
92
+ declaredTypes={new Map()}
93
+ height={400}
94
+ onExplore={onExplore}
95
+ />,
96
+ { wrapper: serverWrapper },
97
+ );
98
+ fireEvent.click(screen.getByLabelText("Explore Sales by month"));
99
+ expect(onExplore).toHaveBeenCalledTimes(1);
100
+ });
101
+
102
+ it("has no explore button when the host offers nowhere to go", () => {
103
+ render(tileAt(), { wrapper: serverWrapper });
104
+ expect(screen.queryByLabelText(/^Explore /)).toBeNull();
81
105
  });
@@ -1,20 +1,16 @@
1
1
  // Copyright (c) Credible Data Inc.
2
2
  // SPDX-License-Identifier: MIT
3
3
 
4
- import { Box, Paper, Typography } from "@mui/material";
5
- import { useMemo } from "react";
6
- import { DASHBOARD_CARD_PADDING_PX } from "../../theme/buildTableCssVars";
4
+ import ExploreOutlinedIcon from "@mui/icons-material/ExploreOutlined";
5
+ import { IconButton, Tooltip } from "@mui/material";
7
6
  import { usePublisherTheme } from "../../theme/ThemeContext";
8
- import { useQueryWithApiError } from "../../hooks/useQueryWithApiError";
7
+ import { useQueryResult } from "../../hooks/useQueryResult";
9
8
  import type { GivenValue } from "../../hooks/givenValue";
10
- import { CHART_RESULT_QUERY_OPTIONS } from "../../utils/queryClient";
11
- import { ApiErrorDisplay } from "../ApiErrorDisplay";
12
9
  import { humanizeSlug, type DrillBinding } from "../drill";
13
10
  import { givensToRequest } from "../given/paramCodec";
14
- import { Loading } from "../Loading";
15
- import ResultContainer from "../RenderedResult/ResultContainer";
16
- import { useServer } from "../ServerProvider";
11
+ import { ResultPanel } from "../RenderedResult/ResultPanel";
17
12
  import { promoteMeasureRowToKpis } from "./promoteMeasureRow";
13
+ import { TileCard, TileHeading } from "./TileCard";
18
14
 
19
15
  export interface DashboardTileProps {
20
16
  environmentName: string;
@@ -51,6 +47,11 @@ export interface DashboardTileProps {
51
47
  maxResultSize?: number;
52
48
  /** Cell clicks and their affordance, for the dashboard's `# drill`. */
53
49
  drill?: DrillBinding;
50
+ /**
51
+ * Open this tile's query somewhere it can be changed. Shown as a button in
52
+ * the heading, on hover; absent, the heading has no button.
53
+ */
54
+ onExplore?: () => void;
54
55
  }
55
56
 
56
57
  /**
@@ -93,130 +94,72 @@ export function DashboardTile({
93
94
  height,
94
95
  maxResultSize,
95
96
  drill,
97
+ onExplore,
96
98
  }: DashboardTileProps) {
97
- const { apiClients } = useServer();
98
99
  const { theme } = usePublisherTheme();
99
- const requestGivens = givensToRequest(givens, declaredTypes, givenNames);
100
-
101
- const { data, isSuccess, isError, error } = useQueryWithApiError({
102
- queryKey: [
103
- "dashboardTile",
104
- environmentName,
105
- packageName,
106
- versionId,
107
- modelPath,
108
- queryName,
109
- tile,
110
- // Re-runs when the applied values change, which is the whole point of
111
- // the control row.
112
- JSON.stringify(requestGivens),
113
- ],
114
- queryFn: () =>
115
- apiClients.models.executeQueryModel(
116
- environmentName,
117
- packageName,
118
- modelPath,
119
- {
120
- queryName,
121
- query: tile !== undefined ? `run: ${tile}` : undefined,
122
- givens: requestGivens,
123
- versionId,
124
- },
125
- ),
126
- ...CHART_RESULT_QUERY_OPTIONS,
100
+ const state = useQueryResult({
101
+ environmentName,
102
+ packageName,
103
+ modelPath,
104
+ versionId,
105
+ queryName,
106
+ query: tile !== undefined ? `run: ${tile}` : undefined,
107
+ // Narrowed to the givens this tile references: see `givenNames`.
108
+ givens: givensToRequest(givens, declaredTypes, givenNames),
127
109
  });
128
110
 
129
- // A composite tile that is one row of measures draws as KPI cards, the way
130
- // Malloyyo splices the same tile into its grid, rather than as a one-row
131
- // table. Composite only: the single-query form is one result the renderer
132
- // lays out from the query's own tags, and its aggregates are already tiles.
133
- // Memoized on the result string so a large result is not re-parsed on every
134
- // render of the tile around it.
135
- const result = useMemo(() => {
136
- const raw = data?.data.result;
137
- if (raw === undefined || tile === undefined) return raw;
138
- return promoteMeasureRowToKpis(raw);
139
- }, [data, tile]);
140
-
141
111
  return (
142
- <Paper
143
- elevation={0}
112
+ <TileCard
113
+ borderless={borderless}
144
114
  sx={{
145
- // The instance theme's border, not MUI's `divider`: the renderer
146
- // card's edge is this same value, and a card that agrees with the
147
- // theme everywhere except its outline still reads as a different
148
- // card. Radius stays on the host's `shape.borderRadius`, which the
149
- // renderer card is now pointed at too.
150
- //
151
- // `# borderless` asks for the result with no card, which the renderer
152
- // honours by dropping background, border, radius and most padding on
153
- // its own `.dashboard-item`. Same here, so the tag reads the same on
154
- // both forms.
155
- //
156
- // The background is `theme.tile`, the same value the renderer card
157
- // paints, and NOT MUI's Paper default. Leaving it unset was the last
158
- // piece of the two cards' geometry that did not agree: measured on
159
- // the `grid`/`tiled` fixture pair, the renderer card came out
160
- // `#f5fafc` from the theme and this one plain white, so on a theme
161
- // whose page is also white the composite tiles lost the tint that
162
- // separates a card from the page while the single-query form kept
163
- // it. Radius, padding, border, shadow and gap already matched.
164
- border: borderless ? "none" : theme.border,
165
- borderRadius: borderless ? 0 : 1,
166
- background: borderless ? "none" : theme.tile,
167
- overflow: "hidden",
168
- minHeight: 120,
169
- p: borderless ? "12px 0" : `${DASHBOARD_CARD_PADDING_PX}px`,
115
+ // The heading's button shows on hover and keyboard focus, the way
116
+ // a tile's chrome does everywhere else; always-on it competes with
117
+ // the title on every card at once.
118
+ "& .publisher-tile-explore": {
119
+ opacity: 0,
120
+ transition: "opacity 120ms",
121
+ },
122
+ "&:hover .publisher-tile-explore, & .publisher-tile-explore:focus-visible":
123
+ { opacity: 1 },
170
124
  }}
171
125
  >
172
126
  {tile !== undefined && (
173
- <Box sx={{ pb: 1.5 }}>
174
- <Typography
175
- variant="subtitle2"
176
- sx={{
177
- fontWeight: 500,
178
- color: theme.tileTitle,
179
- fontFamily: theme.font.family,
180
- }}
181
- // The expression is what actually ran, so it stays reachable
182
- // as a tooltip rather than as the heading.
183
- title={tile}
184
- >
185
- {label ?? tileTitle(tile)}
186
- </Typography>
187
- {subtitle !== undefined && (
188
- <Typography
189
- variant="caption"
190
- sx={{
191
- display: "block",
192
- color: theme.tileTitle,
193
- fontFamily: theme.font.family,
194
- opacity: 0.8,
195
- }}
196
- >
197
- {subtitle}
198
- </Typography>
199
- )}
200
- </Box>
201
- )}
202
- {!isSuccess && !isError && <Loading text="Running…" />}
203
- {isSuccess && (
204
- <ResultContainer
205
- result={result}
206
- maxHeight={height}
207
- maxResultSize={maxResultSize}
208
- renderLogs={data.data.renderLogs}
209
- drill={drill}
127
+ <TileHeading
128
+ title={label ?? tileTitle(tile)}
129
+ subtitle={subtitle}
130
+ // The expression is what actually ran, so it stays reachable as
131
+ // a tooltip rather than as the heading.
132
+ tooltip={tile}
133
+ action={
134
+ onExplore && (
135
+ <Tooltip title="Explore from here">
136
+ <IconButton
137
+ className="publisher-tile-explore"
138
+ size="small"
139
+ aria-label={`Explore ${label ?? tileTitle(tile)}`}
140
+ onClick={onExplore}
141
+ sx={{ mt: -0.5, mr: -0.5, color: theme.tileTitle }}
142
+ >
143
+ <ExploreOutlinedIcon fontSize="small" />
144
+ </IconButton>
145
+ </Tooltip>
146
+ )
147
+ }
210
148
  />
211
149
  )}
212
- {isError && (
213
- <Box sx={{ p: 2 }}>
214
- <ApiErrorDisplay
215
- context={tile ?? queryName ?? modelPath}
216
- error={error}
217
- />
218
- </Box>
219
- )}
220
- </Paper>
150
+ <ResultPanel
151
+ state={state}
152
+ context={tile ?? queryName ?? modelPath}
153
+ maxHeight={height}
154
+ maxResultSize={maxResultSize}
155
+ drill={drill}
156
+ // A composite tile that is one row of measures draws as KPI cards,
157
+ // the way Malloyyo splices the same tile into its grid, rather than
158
+ // as a one-row table. Composite only: the single-query form is one
159
+ // result the renderer lays out from the query's own tags, and its
160
+ // aggregates are already tiles.
161
+ transform={tile !== undefined ? promoteMeasureRowToKpis : undefined}
162
+ />
163
+ </TileCard>
221
164
  );
222
165
  }
@@ -0,0 +1,84 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import type * as Malloy from "@malloydata/malloy-interfaces";
5
+ import { useMemo } from "react";
6
+ import { encodeResourceUri } from "../../utils/formatting";
7
+ import { ModelExplorerDialog } from "../Model/ModelExplorerDialog";
8
+ import { useModelData } from "../Model/useModelData";
9
+ import { sourceOf, stepsOf } from "./RowsDialog";
10
+
11
+ /**
12
+ * "Explore from here": the tile's query, open in the model explorer on the
13
+ * tile's source, so the reader can change it rather than only look at it.
14
+ *
15
+ * The dashboard file is itself a model, so the explorer opens on that file:
16
+ * the tile's source is an extension declared there, and its query resolves
17
+ * as written. The model is fetched here rather than inside the explorer so
18
+ * the tile's source can be selected before it renders.
19
+ */
20
+ export function ExploreDialog({
21
+ tile,
22
+ environmentName,
23
+ packageName,
24
+ versionId,
25
+ modelPath,
26
+ onClose,
27
+ }: {
28
+ /** The tile expression to open, or undefined when closed. */
29
+ tile: string | undefined;
30
+ environmentName: string;
31
+ packageName: string;
32
+ versionId?: string;
33
+ modelPath: string;
34
+ onClose: () => void;
35
+ }) {
36
+ const resourceUri = encodeResourceUri({
37
+ environmentName,
38
+ packageName,
39
+ modelPath,
40
+ ...(versionId === undefined ? {} : { versionId }),
41
+ });
42
+ const { data } = useModelData(resourceUri, tile !== undefined);
43
+ const sourceIndex = useMemo(() => {
44
+ const source = sourceOf(tile);
45
+ if (!data || source === undefined) return 0;
46
+ const index = (data.sourceInfos ?? []).findIndex((info) => {
47
+ try {
48
+ return JSON.parse(info)?.name === source;
49
+ } catch {
50
+ return false;
51
+ }
52
+ });
53
+ return index < 0 ? 0 : index;
54
+ }, [data, tile]);
55
+ // As a query AST when the expression is `source -> view`, so the explorer's
56
+ // builder shows the view and its Run button works from the first render; a
57
+ // string otherwise, which the explorer runs as written but cannot show.
58
+ const existingQuery = useMemo(() => {
59
+ if (tile === undefined) return undefined;
60
+ const steps = stepsOf(tile);
61
+ const malloyQuery: Malloy.Query | string = steps
62
+ ? {
63
+ definition: {
64
+ kind: "arrow",
65
+ source: { kind: "source_reference", name: steps.source },
66
+ view: { kind: "view_reference", name: steps.view },
67
+ },
68
+ }
69
+ : `run: ${tile}`;
70
+ return { query: `run: ${tile}`, malloyQuery, malloyResult: undefined };
71
+ }, [tile]);
72
+ if (tile === undefined || !data) return null;
73
+ return (
74
+ <ModelExplorerDialog
75
+ open
76
+ onClose={onClose}
77
+ resourceUri={resourceUri}
78
+ data={data}
79
+ title={`Explore: ${tile}`}
80
+ existingQuery={existingQuery}
81
+ initialSelectedSourceIndex={sourceIndex}
82
+ />
83
+ );
84
+ }
@@ -0,0 +1,42 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import { rowsQuery, sourceOf, stepsOf } from "./RowsDialog";
6
+
7
+ describe("the rows query", () => {
8
+ it("drills through the tile's view to the value's rows", () => {
9
+ expect(
10
+ rowsQuery({
11
+ source: "overview",
12
+ view: "best_sellers",
13
+ field: "name",
14
+ rawValue: "Loft Insulated Jacket",
15
+ label: "Loft Insulated Jacket",
16
+ }),
17
+ ).toBe(
18
+ "run: overview -> { drill: best_sellers.name = 'Loft Insulated Jacket'; select: *; limit: 200 }",
19
+ );
20
+ expect(
21
+ rowsQuery({
22
+ source: "a",
23
+ view: "v",
24
+ field: "f",
25
+ rawValue: {},
26
+ label: "",
27
+ }),
28
+ ).toBeUndefined();
29
+ });
30
+
31
+ it("reads the source and view off a tile expression, and nothing off any other shape", () => {
32
+ expect(stepsOf("overview -> revenue_trend")).toEqual({
33
+ source: "overview",
34
+ view: "revenue_trend",
35
+ });
36
+ expect(sourceOf("overview->kpis")).toBe("overview");
37
+ expect(stepsOf("{ group_by: x } -> y")).toBeUndefined();
38
+ expect(stepsOf("a -> b -> c")).toBeUndefined();
39
+ expect(stepsOf("a -> b + { limit: 2 }")).toBeUndefined();
40
+ expect(stepsOf(undefined)).toBeUndefined();
41
+ });
42
+ });