@malloy-publisher/sdk 0.8.1 → 0.8.3

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 (39) hide show
  1. package/dist/{DocumentStorage-BCpTBa7U.es.js → DocumentStorage-CigwNjz4.es.js} +4882 -4729
  2. package/dist/DocumentStorage-DvUNSOiN.cjs.js +134 -0
  3. package/dist/{RenderedResult-CDA9mN_6.es.js → RenderedResult-BfayOSk1.es.js} +2 -2
  4. package/dist/{RenderedResult-ChkvK7b2.cjs.js → RenderedResult-DpHdrFtp.cjs.js} +1 -1
  5. package/dist/{ServerProvider-CvMqBhuj.es.js → ServerProvider-CfUFleYe.es.js} +4 -4
  6. package/dist/builder/index.cjs.js +1 -1
  7. package/dist/builder/index.es.js +2 -2
  8. package/dist/client/api.d.ts +26 -8
  9. package/dist/client/index.es.js +1 -1
  10. package/dist/components/Dashboard/ExploreDialog.d.ts +3 -1
  11. package/dist/components/Model/Model.d.ts +10 -3
  12. package/dist/components/Model/ModelExplorer.d.ts +21 -1
  13. package/dist/components/Model/ModelExplorerDialog.d.ts +4 -1
  14. package/dist/components/Model/SourcesExplorer.d.ts +8 -1
  15. package/dist/components/Model/index.d.ts +1 -0
  16. package/dist/components/Model/runGate.d.ts +19 -0
  17. package/dist/components/Notebook/NotebookCell.d.ts +10 -1
  18. package/dist/components/given/filterValue.d.ts +11 -0
  19. package/dist/index.cjs.js +13 -13
  20. package/dist/index.es.js +3754 -3732
  21. package/package.json +1 -1
  22. package/src/components/Dashboard/Dashboard.spec.tsx +58 -2
  23. package/src/components/Dashboard/Dashboard.tsx +8 -1
  24. package/src/components/Dashboard/ExploreDialog.tsx +5 -0
  25. package/src/components/Model/Model.tsx +28 -1
  26. package/src/components/Model/ModelExplorer.spec.tsx +404 -0
  27. package/src/components/Model/ModelExplorer.tsx +65 -2
  28. package/src/components/Model/ModelExplorerDialog.tsx +12 -0
  29. package/src/components/Model/SourcesExplorer.tsx +70 -15
  30. package/src/components/Model/index.ts +1 -0
  31. package/src/components/Model/runGate.spec.ts +102 -0
  32. package/src/components/Model/runGate.ts +67 -0
  33. package/src/components/Notebook/Notebook.spec.tsx +67 -3
  34. package/src/components/Notebook/Notebook.tsx +12 -2
  35. package/src/components/Notebook/NotebookCell.tsx +26 -7
  36. package/src/components/given/GivenInput.spec.tsx +66 -0
  37. package/src/components/given/GivenInput.tsx +99 -0
  38. package/src/components/given/filterValue.ts +21 -0
  39. package/dist/DocumentStorage-D7yF4_T3.cjs.js +0 -134
@@ -20,6 +20,7 @@ import {
20
20
  Typography,
21
21
  } from "@mui/material";
22
22
  import React, { useEffect, useState } from "react";
23
+ import type { Given } from "../../client";
23
24
  import { usePublisherTheme } from "../../theme/ThemeContext";
24
25
  import { parseResourceUri } from "../../utils/formatting";
25
26
  import { highlight } from "../highlighter";
@@ -82,6 +83,14 @@ interface NotebookCellProps {
82
83
  * drill inert rather than dead-ended, and removes the affordance with it.
83
84
  */
84
85
  onDrillNavigate?: (target: DrillNavigation, event?: MouseEvent) => void;
86
+ /**
87
+ * The notebook's current control values, to seed the "Data Sources"
88
+ * dialog's own so exploring from a cell starts from what the reader is
89
+ * looking at rather than the model's bare defaults.
90
+ */
91
+ givens?: Record<string, string>;
92
+ /** The notebook's declared givens, so that dialog renders their controls. */
93
+ givenSpecs?: Given[];
85
94
  }
86
95
 
87
96
  export function NotebookCell({
@@ -97,6 +106,8 @@ export function NotebookCell({
97
106
  onDrillSelf,
98
107
  canDrillSelf,
99
108
  onDrillNavigate,
109
+ givens,
110
+ givenSpecs,
100
111
  }: NotebookCellProps) {
101
112
  const [codeDialogOpen, setCodeDialogOpen] = React.useState<boolean>(false);
102
113
  const [embeddingDialogOpen, setEmbeddingDialogOpen] =
@@ -209,13 +220,20 @@ export function NotebookCell({
209
220
  return 0; // Default to the first source
210
221
  };
211
222
 
212
- const modelDataFromNewSources =
213
- cell.newSources && cell.newSources.length > 0
214
- ? {
215
- sourceInfos: cell.newSources,
216
- resource: resourceUri,
217
- }
218
- : undefined;
223
+ // Memoized: the dialog's given controls key off this object's identity.
224
+ const modelDataFromNewSources = React.useMemo(
225
+ () =>
226
+ cell.newSources && cell.newSources.length > 0
227
+ ? {
228
+ sourceInfos: cell.newSources,
229
+ resource: resourceUri,
230
+ // A cell's sources carry no givens of their own; without the
231
+ // notebook's, the dialog has no controls and a gated source 403s.
232
+ givens: givenSpecs,
233
+ }
234
+ : undefined,
235
+ [cell.newSources, resourceUri, givenSpecs],
236
+ );
219
237
 
220
238
  const queryResultCodeSnippet = createEmbeddedQueryResult({
221
239
  query: cell.text,
@@ -349,6 +367,7 @@ export function NotebookCell({
349
367
  resourceUri={resourceUri}
350
368
  data={modelDataFromNewSources}
351
369
  initialSelectedSourceIndex={getInitialSourceIndex()}
370
+ startingGivens={givens}
352
371
  />
353
372
 
354
373
  {/* Code Dialog */}
@@ -383,6 +383,7 @@ describe("GivenInput: the whole type x control matrix", () => {
383
383
  // through every branch and renders nothing at all.
384
384
  const widget = () => {
385
385
  if (screen.queryAllByTestId("time-range").length) return "timerange";
386
+ if (screen.queryAllByTestId("boolean-filter").length) return "boolfilter";
386
387
  if (screen.queryAllByRole("combobox").length) return "picker";
387
388
  if (screen.queryAllByRole("slider").length) return "slider";
388
389
  if (screen.queryAllByRole("checkbox").length) return "checkbox";
@@ -432,6 +433,7 @@ describe("GivenInput: the whole type x control matrix", () => {
432
433
  "timestamptz",
433
434
  "filter<number>",
434
435
  "filter<date>",
436
+ "filter<boolean>",
435
437
  ]) {
436
438
  expect(show(type, "select")).not.toBe("picker");
437
439
  }
@@ -503,6 +505,7 @@ describe("GivenInput: the whole type x control matrix", () => {
503
505
  "filter<number>",
504
506
  "filter<date>",
505
507
  "filter<timestamp>",
508
+ "filter<boolean>",
506
509
  "array<string>",
507
510
  "record",
508
511
  undefined,
@@ -763,6 +766,69 @@ describe("GivenInput: a date renders its UTC day, whatever zone the runner is in
763
766
  );
764
767
  });
765
768
 
769
+ describe("GivenInput: the true/false dropdown for a boolean filter", () => {
770
+ const given: Given = { name: "FLAG", type: "filter<boolean>" };
771
+ const dropdown = () =>
772
+ screen
773
+ .getAllByRole("combobox")
774
+ .find((el) => el.tagName !== "INPUT") as HTMLElement;
775
+ const widget = (value: GivenValue | undefined) => {
776
+ cleanup();
777
+ render(<GivenInput given={given} value={value} onChange={() => {}} />);
778
+ return screen.queryAllByTestId("boolean-filter").length
779
+ ? "dropdown"
780
+ : "text";
781
+ };
782
+
783
+ it("offers true and false, and commits the one picked", () => {
784
+ const onChange = mock((_next: GivenValue) => {});
785
+ render(
786
+ <GivenInput given={given} value={undefined} onChange={onChange} />,
787
+ );
788
+ fireEvent.mouseDown(dropdown());
789
+ expect(
790
+ screen.getAllByRole("option").map((option) => option.textContent),
791
+ ).toEqual(["true", "false"]);
792
+ fireEvent.click(screen.getByRole("option", { name: "false" }));
793
+ expect(onChange).toHaveBeenCalledWith("false");
794
+ });
795
+
796
+ it("shows an unset given as its default, with no revert", () => {
797
+ render(
798
+ <GivenInput
799
+ given={{ ...given, default: "f'true'" }}
800
+ value={undefined}
801
+ onChange={() => {}}
802
+ />,
803
+ );
804
+ expect(dropdown().textContent).toBe("true");
805
+ expect(clearButtons()).toHaveLength(0);
806
+ });
807
+
808
+ it("shows a picked value, however it is spelled, and reverts it", () => {
809
+ const onChange = mock((_next: GivenValue) => {});
810
+ render(<GivenInput given={given} value=" TRUE " onChange={onChange} />);
811
+ expect(dropdown().textContent).toBe("true");
812
+ fireEvent.click(clearButtons()[0]);
813
+ expect(onChange).toHaveBeenCalledWith(null);
814
+ });
815
+
816
+ it("shows the empty filter as Any, an override it can revert", () => {
817
+ render(<GivenInput given={given} value="" onChange={() => {}} />);
818
+ expect(dropdown().textContent).toBe("Any");
819
+ expect(clearButtons()).toHaveLength(1);
820
+ });
821
+
822
+ it("leaves any other filter in the text box, as written", () => {
823
+ // `=false` leaves null rows out and `false` keeps them, so showing it as
824
+ // "false" would claim a filter that is not running.
825
+ for (const value of ["=false", "not true", "null", "asdf"]) {
826
+ expect(widget(value)).toBe("text");
827
+ }
828
+ expect(widget("false")).toBe("dropdown");
829
+ });
830
+ });
831
+
766
832
  describe("GivenInput: the time-range control for a date filter", () => {
767
833
  const given: Given = { name: "PERIOD", type: "filter<timestamp>" };
768
834
  const dropdown = () =>
@@ -30,6 +30,7 @@ import { GivenValue } from "../../hooks/givenValue";
30
30
  import {
31
31
  decodeAtLeast,
32
32
  decodeBetween,
33
+ decodeBooleanFilter,
33
34
  decodeDayRange,
34
35
  decodeFilterList,
35
36
  decodeTimePreset,
@@ -676,6 +677,31 @@ export function GivenInput({
676
677
  );
677
678
  }
678
679
 
680
+ // A `filter<boolean>` gets a dropdown of its two values. Unset and the empty
681
+ // filter both mean no filtering; any other spelling (`=false`, `not true`,
682
+ // `null`) falls through to the text box, where it stays as written.
683
+ if (filterInnerType(type) === "boolean") {
684
+ const picked =
685
+ typeof value === "string" ? decodeBooleanFilter(value) : undefined;
686
+ if (
687
+ value === undefined ||
688
+ value === null ||
689
+ value === "" ||
690
+ picked !== undefined
691
+ ) {
692
+ return (
693
+ <BooleanFilterControl
694
+ label={label}
695
+ picked={picked}
696
+ emptyOverride={value === ""}
697
+ onChange={onChange}
698
+ helperNode={helperNode}
699
+ defaultDisplay={defaultDisplay}
700
+ />
701
+ );
702
+ }
703
+ }
704
+
679
705
  if (type === "boolean") {
680
706
  // Three states for a boolean. When unset, reflect the model DEFAULT so the
681
707
  // box shows what the query will actually run with (not a misleading
@@ -878,6 +904,79 @@ function ClearAdornment({ onClear }: { onClear: () => void }) {
878
904
  );
879
905
  }
880
906
 
907
+ const ANY_BOOLEAN = "__any__";
908
+
909
+ /**
910
+ * A dropdown of `true` and `false` for a `filter<boolean>` given.
911
+ *
912
+ * Unset shows the model's own default, the way every other control does, and
913
+ * picks nothing. An explicit `""` (the empty filter, which a URL can carry) is
914
+ * shown as "Any" and offered only while it is in force: the gesture for "no
915
+ * filter" is the ×, which reverts to the model default, as in the time-range
916
+ * control.
917
+ */
918
+ function BooleanFilterControl({
919
+ label,
920
+ picked,
921
+ emptyOverride,
922
+ onChange,
923
+ helperNode,
924
+ defaultDisplay,
925
+ }: {
926
+ label: string;
927
+ picked: "true" | "false" | undefined;
928
+ emptyOverride: boolean;
929
+ onChange: (next: GivenValue) => void;
930
+ helperNode: ReactNode;
931
+ defaultDisplay: string | undefined;
932
+ }) {
933
+ const selected = picked ?? (emptyOverride ? ANY_BOOLEAN : "");
934
+ const isOverridden = selected !== "";
935
+ return (
936
+ <Box data-testid="boolean-filter">
937
+ <TextField
938
+ select
939
+ label={label}
940
+ value={selected}
941
+ size="small"
942
+ fullWidth
943
+ helperText={helperNode}
944
+ slotProps={{
945
+ select: {
946
+ displayEmpty: true,
947
+ renderValue: (v) =>
948
+ v === "true" || v === "false"
949
+ ? v
950
+ : v === ANY_BOOLEAN
951
+ ? "Any"
952
+ : defaultDisplay || "Any",
953
+ },
954
+ input: {
955
+ endAdornment: isOverridden ? (
956
+ <IconButton
957
+ size="small"
958
+ aria-label="clear value"
959
+ onClick={() => onChange(null)}
960
+ sx={{ mr: 2 }}
961
+ >
962
+ <ClearIcon fontSize="small" />
963
+ </IconButton>
964
+ ) : undefined,
965
+ },
966
+ }}
967
+ onChange={(e) => {
968
+ // "Any" is only listed while it is already in force.
969
+ if (e.target.value !== ANY_BOOLEAN) onChange(e.target.value);
970
+ }}
971
+ >
972
+ {emptyOverride && <MenuItem value={ANY_BOOLEAN}>Any</MenuItem>}
973
+ <MenuItem value="true">true</MenuItem>
974
+ <MenuItem value="false">false</MenuItem>
975
+ </TextField>
976
+ </Box>
977
+ );
978
+ }
979
+
881
980
  /** What a time-range control is showing; undefined means it cannot show it. */
882
981
  type TimeRangeState =
883
982
  | { kind: "unset" }
@@ -23,6 +23,7 @@
23
23
  */
24
24
 
25
25
  import {
26
+ BooleanFilterExpression,
26
27
  isStringCondition,
27
28
  NumberFilterExpression,
28
29
  StringFilterExpression,
@@ -42,6 +43,26 @@ export function isFilterType(type: string | undefined): boolean {
42
43
  return (type ?? "").startsWith("filter<");
43
44
  }
44
45
 
46
+ /**
47
+ * The value a `filter<boolean>` dropdown shows for `value`: `"true"` or
48
+ * `"false"`, however it is spelled (`TRUE`, ` true `). Undefined for anything
49
+ * else the grammar can say (`=false`, `not true`, `null`, and text that does
50
+ * not parse), which the dropdown cannot show and must not rewrite.
51
+ *
52
+ * The plain forms are the ones offered because they are what a reader means:
53
+ * `true` matches true rows, and `false` matches false or null. `=false`, which
54
+ * leaves null out, is a different filter and stays in the text box as written.
55
+ */
56
+ export function decodeBooleanFilter(
57
+ value: string,
58
+ ): "true" | "false" | undefined {
59
+ const { parsed } = BooleanFilterExpression.parse(value);
60
+ if (!parsed || parsed.not) return undefined;
61
+ return parsed.operator === "true" || parsed.operator === "false"
62
+ ? parsed.operator
63
+ : undefined;
64
+ }
65
+
45
66
  /** The `T` of a `filter<T>`, or undefined for any other type. */
46
67
  export function filterInnerType(type: string | undefined): string | undefined {
47
68
  const match = /^filter<(.+)>$/.exec(type ?? "");