@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
@@ -0,0 +1,172 @@
1
+ import { fireEvent, render, screen } from "@testing-library/react";
2
+ import { describe, expect, it } from "bun:test";
3
+ import { useState } from "react";
4
+ import type { Given } from "../../client";
5
+ import { useGivensState } from "../../hooks/useGivensState";
6
+ import { decodeFilterList } from "./filterValue";
7
+ import { GivensPanel } from "./GivensPanel";
8
+
9
+ const TYPES = new Map<string, string | undefined>([
10
+ ["BRAND", "filter<string>"],
11
+ ]);
12
+
13
+ const MULTISELECT: Given = {
14
+ name: "BRAND",
15
+ type: "filter<string>",
16
+ control: "multiselect",
17
+ };
18
+
19
+ let lastDraft: string | undefined;
20
+
21
+ /**
22
+ * The panel wired to the state hook through a host that keeps applied values in
23
+ * its "URL" and feeds them straight back in, which is what `NotebookPage` does
24
+ * through react-router. The loop is the point: a control's next value is
25
+ * computed from the value it is currently rendering, so anything that delays
26
+ * that value getting back to it is where a lost click would come from.
27
+ */
28
+ function Harness({ options }: { options?: Map<string, string[]> }) {
29
+ const [params, setParams] = useState<Record<string, string>>({});
30
+ const { draft, setGiven } = useGivensState({
31
+ declaredTypes: TYPES,
32
+ params,
33
+ onParamsChange: setParams,
34
+ autorun: true,
35
+ });
36
+ lastDraft = draft.get("BRAND") as string | undefined;
37
+ return (
38
+ <GivensPanel
39
+ givens={[MULTISELECT]}
40
+ values={draft}
41
+ onChange={setGiven}
42
+ onReset={() => {}}
43
+ options={options ?? new Map([["BRAND", ["Loft", "Aurora", "Nike"]]])}
44
+ />
45
+ );
46
+ }
47
+
48
+ // The combobox by role, not by label: once a value is picked the control also
49
+ // renders a chip carrying the same accessible name, and getByLabelText then
50
+ // matches two elements.
51
+ const combobox = () => screen.getByRole("combobox");
52
+
53
+ function pick(label: string) {
54
+ fireEvent.keyDown(combobox(), { key: "ArrowDown" });
55
+ fireEvent.click(screen.getByRole("option", { name: label }));
56
+ }
57
+
58
+ describe("GivensPanel: multiselect", () => {
59
+ it("joins a second pick onto the first rather than replacing it", () => {
60
+ // A Playwright spec on the originating branch documented the opposite,
61
+ // "a second click sent immediately lands on the pre-first-pick selection
62
+ // and replaces Loft instead of joining it", and worked around it by
63
+ // awaiting a URL change between the clicks. It does not reproduce here,
64
+ // with the host's round trip in place and no await between the clicks, so
65
+ // this pins the behaviour that is actually observable at this level
66
+ // rather than encoding a race nobody can currently demonstrate.
67
+ render(<Harness />);
68
+
69
+ pick("Loft");
70
+ expect(decodeFilterList(lastDraft ?? "")).toEqual(["Loft"]);
71
+
72
+ pick("Aurora");
73
+ expect(decodeFilterList(lastDraft ?? "")).toEqual(["Loft", "Aurora"]);
74
+ });
75
+
76
+ it("encodes the selection as a filter Malloy reads as 'any of these'", () => {
77
+ render(<Harness options={new Map([["BRAND", ["Ben & Jerry, Inc"]]])} />);
78
+
79
+ pick("Ben & Jerry, Inc");
80
+
81
+ // Escaped, so the comma stays inside one value instead of splitting it
82
+ // into two brands.
83
+ expect(lastDraft).toBe("Ben\\ &\\ Jerry\\,\\ Inc");
84
+ expect(decodeFilterList(lastDraft ?? "")).toEqual(["Ben & Jerry, Inc"]);
85
+ });
86
+ });
87
+
88
+ describe("GivensPanel: a suggest query that failed", () => {
89
+ it("says so, instead of showing an empty dropdown", () => {
90
+ // An empty option list and a failed query render identically, and mean
91
+ // opposite things. Without this the reader sees a picker with nothing in
92
+ // it and concludes the data has no values.
93
+ render(
94
+ <GivensPanel
95
+ givens={[MULTISELECT]}
96
+ values={new Map()}
97
+ onChange={() => {}}
98
+ onReset={() => {}}
99
+ options={new Map()}
100
+ optionsFailed={new Set(["BRAND"])}
101
+ />,
102
+ );
103
+
104
+ expect(
105
+ screen.getByText(/Could not load the options for this control/),
106
+ ).toBeDefined();
107
+ });
108
+
109
+ it("says nothing when the dimension simply has no values", () => {
110
+ render(
111
+ <GivensPanel
112
+ givens={[MULTISELECT]}
113
+ values={new Map()}
114
+ onChange={() => {}}
115
+ onReset={() => {}}
116
+ options={new Map([["BRAND", []]])}
117
+ />,
118
+ );
119
+
120
+ expect(screen.queryAllByText(/Could not load the options/)).toHaveLength(
121
+ 0,
122
+ );
123
+ });
124
+ });
125
+
126
+ describe("layout and title", () => {
127
+ // Both props exist for the dashboard surface, which does not land in this
128
+ // slice, so nothing in the repo passes either one. Exercised here so the
129
+ // first render of the `bar` branch is not an external consumer's.
130
+ const props = {
131
+ givens: [MULTISELECT],
132
+ values: new Map(),
133
+ onChange: () => {},
134
+ onReset: () => {},
135
+ options: new Map([["BRAND", ["Nike"]]]),
136
+ };
137
+
138
+ it('titles the panel "Parameters" by default', () => {
139
+ render(<GivensPanel {...props} />);
140
+ expect(screen.getByText("Parameters")).toBeDefined();
141
+ });
142
+
143
+ it("uses a given title in place of the default", () => {
144
+ render(<GivensPanel {...props} title="Filters" />);
145
+ expect(screen.getByText("Filters")).toBeDefined();
146
+ expect(screen.queryAllByText("Parameters")).toHaveLength(0);
147
+ });
148
+
149
+ it("renders the bar layout with its controls and no heading", () => {
150
+ render(<GivensPanel {...props} layout="bar" />);
151
+ // The bar is meant to sit inline above a dashboard, so it carries the
152
+ // controls but drops the heading the panel layout shows.
153
+ expect(screen.getByRole("combobox")).toBeDefined();
154
+ expect(screen.queryAllByText("Parameters")).toHaveLength(0);
155
+ });
156
+
157
+ it("still offers Reset from the bar layout", () => {
158
+ let reset = 0;
159
+ render(
160
+ <GivensPanel
161
+ {...props}
162
+ layout="bar"
163
+ values={new Map([["BRAND", "Nike"]])}
164
+ onReset={() => {
165
+ reset += 1;
166
+ }}
167
+ />,
168
+ );
169
+ fireEvent.click(screen.getByRole("button", { name: "Reset" }));
170
+ expect(reset).toBe(1);
171
+ });
172
+ });
@@ -1,32 +1,149 @@
1
1
  import { Box, Button, Paper, Stack, Typography } from "@mui/material";
2
2
  import { Given } from "../../client";
3
- import { GivenValue } from "../../hooks/useGivensForm";
3
+ import { GivenValue } from "../../hooks/givenValue";
4
4
  import { GivenInput } from "./GivenInput";
5
5
 
6
+ /**
7
+ * How the control row is laid out.
8
+ *
9
+ * - `panel`: the vertical "Parameters" block a notebook shows above its cells.
10
+ * - `bar`: a horizontal filter bar above a dashboard grid.
11
+ *
12
+ * Two presentations of one control implementation, rather than two
13
+ * implementations: a control gains a behavior (a suggest dropdown, an Apply
14
+ * button) on both surfaces at once.
15
+ */
16
+ export type GivensLayout = "panel" | "bar";
17
+
6
18
  export interface GivensPanelProps {
7
19
  givens: Given[];
8
20
  values: Map<string, GivenValue>;
9
21
  onChange: (name: string, value: GivenValue) => void;
10
- onClearAll: () => void;
22
+ onReset: () => void;
23
+ layout?: GivensLayout;
24
+ /** Resolved `suggest` options per given name. */
25
+ options?: Map<string, string[]>;
26
+ optionsLoading?: boolean;
27
+ /** Givens whose `suggest` query failed, from `useSuggestOptions.failed`. */
28
+ optionsFailed?: ReadonlySet<string>;
29
+ /**
30
+ * Set to render an Apply button, for `autorun=false`. Absent means controls
31
+ * take effect as they change, which is the default everywhere.
32
+ */
33
+ apply?: {
34
+ onApply: () => void;
35
+ /** Whether anything has changed since the last apply. */
36
+ pending: boolean;
37
+ };
38
+ /** Overrides the `panel` layout's "Parameters" heading. */
39
+ title?: string;
11
40
  }
12
41
 
13
42
  /**
14
- * Renders the "Parameters" panel — one input per declared `given:`.
43
+ * Renders the control row: one input per declared `given:`.
15
44
  * Returns null when the model declares no givens, so callers can drop
16
45
  * `<GivensPanel ... />` unconditionally without a length guard.
17
46
  *
18
- * A "Reset" button appears in the panel header when at least one value
19
- * is set; it fires `onClearAll` which the parent should wire to
20
- * `useGivensForm.clearAll`.
47
+ * A "Reset" button appears in the header when at least one value is set; it
48
+ * fires `onReset`, which the parent should wire to `useGivensState.reset`. That
49
+ * puts the document's starting values back rather than emptying the row, so the
50
+ * button and the state agree on what "Reset" means.
21
51
  */
22
52
  export function GivensPanel({
23
53
  givens,
24
54
  values,
25
55
  onChange,
26
- onClearAll,
56
+ onReset,
57
+ layout = "panel",
58
+ options,
59
+ optionsLoading,
60
+ optionsFailed,
61
+ apply,
62
+ title,
27
63
  }: GivensPanelProps) {
28
64
  if (givens.length === 0) return null;
29
- const hasValues = values.size > 0;
65
+ // Some value actually SET, not merely some entry present. `paramsToGivens`
66
+ // records `?X=` as a null entry, so counting entries offered Reset for a
67
+ // control that is unset, and pressing it changed nothing and left the empty
68
+ // parameter in the URL.
69
+ const hasValues = Array.from(values.values()).some((v) => v !== null);
70
+
71
+ const inputs = givens.map((given) => (
72
+ <Box
73
+ key={given.name}
74
+ sx={
75
+ layout === "bar"
76
+ ? { minWidth: 200, flex: "1 1 200px", maxWidth: 320 }
77
+ : undefined
78
+ }
79
+ >
80
+ <GivenInput
81
+ given={given}
82
+ value={given.name ? values.get(given.name) : undefined}
83
+ onChange={(next) => given.name && onChange(given.name, next)}
84
+ options={given.name ? options?.get(given.name) : undefined}
85
+ optionsLoading={optionsLoading}
86
+ optionsFailed={
87
+ given.name ? optionsFailed?.has(given.name) : undefined
88
+ }
89
+ />
90
+ </Box>
91
+ ));
92
+
93
+ const actions = (
94
+ <Stack direction="row" alignItems="center" spacing={1}>
95
+ {apply && (
96
+ <Button
97
+ variant="contained"
98
+ size="small"
99
+ disableElevation
100
+ onClick={apply.onApply}
101
+ disabled={!apply.pending}
102
+ sx={{ textTransform: "none" }}
103
+ >
104
+ Apply
105
+ </Button>
106
+ )}
107
+ {hasValues && (
108
+ <Button
109
+ variant="text"
110
+ size="small"
111
+ onClick={onReset}
112
+ sx={{ textTransform: "none" }}
113
+ >
114
+ Reset
115
+ </Button>
116
+ )}
117
+ </Stack>
118
+ );
119
+
120
+ if (layout === "bar") {
121
+ return (
122
+ <Paper
123
+ elevation={0}
124
+ sx={{
125
+ p: 2,
126
+ border: 1,
127
+ borderColor: "divider",
128
+ borderRadius: 1,
129
+ backgroundColor: "background.paper",
130
+ }}
131
+ >
132
+ <Stack
133
+ direction="row"
134
+ alignItems="flex-start"
135
+ spacing={2}
136
+ useFlexGap
137
+ sx={{ flexWrap: "wrap" }}
138
+ >
139
+ {inputs}
140
+ <Box sx={{ flex: "0 0 auto", ml: "auto", pt: 0.5 }}>
141
+ {actions}
142
+ </Box>
143
+ </Stack>
144
+ </Paper>
145
+ );
146
+ }
30
147
 
31
148
  return (
32
149
  <Paper
@@ -48,37 +165,27 @@ export function GivensPanel({
48
165
  variant="subtitle2"
49
166
  sx={{ fontWeight: 600, color: "#333" }}
50
167
  >
51
- Parameters
168
+ {title ?? "Parameters"}
52
169
  </Typography>
53
- {hasValues && (
54
- <Button
55
- variant="text"
56
- size="small"
57
- onClick={onClearAll}
58
- sx={{ textTransform: "none" }}
59
- >
60
- Reset
61
- </Button>
62
- )}
170
+ {actions}
63
171
  </Stack>
64
172
  <Box
65
173
  sx={{
66
174
  display: "grid",
67
- gridTemplateColumns: "repeat(auto-fill, minmax(250px, 1fr))",
68
- gap: 3,
175
+ // Equal columns, so controls line up in a grid rather than
176
+ // packing by their own widths. 220px is a deliberate floor: at
177
+ // 250 a four-given notebook came out three-across with a lone
178
+ // control stranded on a second row at the common page width.
179
+ gridTemplateColumns: "repeat(auto-fill, minmax(220px, 1fr))",
180
+ // Each control is label + field + a helper line or two, so rows
181
+ // are top-aligned; a taller helper must not push its neighbours'
182
+ // fields down.
183
+ alignItems: "start",
184
+ columnGap: 3,
185
+ rowGap: 2.5,
69
186
  }}
70
187
  >
71
- {givens.map((given) => (
72
- <Box key={given.name}>
73
- <GivenInput
74
- given={given}
75
- value={given.name ? values.get(given.name) : undefined}
76
- onChange={(next) =>
77
- given.name && onChange(given.name, next)
78
- }
79
- />
80
- </Box>
81
- ))}
188
+ {inputs}
82
189
  </Box>
83
190
  </Paper>
84
191
  );