@malloy-publisher/sdk 0.4.1 → 0.5.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 (59) hide show
  1. package/dist/{DocumentStorage-BiFqO15i.es.js → DocumentStorage-DkwqA8SA.es.js} +2284 -2319
  2. package/dist/{DocumentStorage-CMyLifsz.cjs.js → DocumentStorage-mMn5SZ1c.cjs.js} +29 -29
  3. package/dist/{RenderedResult-CvrmYyr5.es.js → RenderedResult-CTD2Ej2S.es.js} +100 -80
  4. package/dist/{RenderedResult-CZzE8wv-.cjs.js → RenderedResult-GukOadCi.cjs.js} +27 -7
  5. package/dist/ServerProvider-CC9ein5-.cjs.js +1 -0
  6. package/dist/{ServerProvider-BWOEKg0O.es.js → ServerProvider-IzXlqSVe.es.js} +572 -553
  7. package/dist/builder/index.cjs.js +54 -49
  8. package/dist/builder/index.es.js +4124 -3521
  9. package/dist/client/api.d.ts +54 -2
  10. package/dist/client/index.cjs.js +1 -1
  11. package/dist/client/index.es.js +1 -1
  12. package/dist/components/Dashboard/TileCard.d.ts +7 -3
  13. package/dist/components/DashboardBuilder/DashboardEditor.d.ts +15 -4
  14. package/dist/components/DashboardBuilder/controls.d.ts +8 -6
  15. package/dist/components/DashboardBuilder/document.d.ts +19 -5
  16. package/dist/components/DashboardBuilder/malloyText.d.ts +0 -30
  17. package/dist/components/DashboardBuilder/malloyTree.d.ts +232 -0
  18. package/dist/components/DashboardBuilder/preview.d.ts +17 -0
  19. package/dist/components/DashboardBuilder/readDocument.d.ts +23 -28
  20. package/dist/components/DashboardBuilder/spliceDocument.d.ts +12 -0
  21. package/dist/index.cjs.js +1 -1
  22. package/dist/index.es.js +5 -5
  23. package/dist/theme/resolveTheme.d.ts +1 -1
  24. package/dist/theme/types.d.ts +12 -2
  25. package/package.json +1 -1
  26. package/src/components/Dashboard/TileCard.tsx +8 -4
  27. package/src/components/DashboardBuilder/BuilderToolbar.tsx +4 -5
  28. package/src/components/DashboardBuilder/DashboardEditor.resourceUri.spec.tsx +356 -0
  29. package/src/components/DashboardBuilder/DashboardEditor.tsx +116 -25
  30. package/src/components/DashboardBuilder/FilterDialog.tsx +19 -6
  31. package/src/components/DashboardBuilder/TileFrame.tsx +11 -2
  32. package/src/components/DashboardBuilder/TileMenu.tsx +6 -0
  33. package/src/components/DashboardBuilder/__test__/inventory.ts +367 -0
  34. package/src/components/DashboardBuilder/controls.spec.ts +53 -3
  35. package/src/components/DashboardBuilder/controls.ts +9 -7
  36. package/src/components/DashboardBuilder/document.ts +17 -5
  37. package/src/components/DashboardBuilder/invariants.spec.ts +297 -0
  38. package/src/components/DashboardBuilder/malloyText.spec.ts +27 -66
  39. package/src/components/DashboardBuilder/malloyText.ts +4 -109
  40. package/src/components/DashboardBuilder/malloyTree.spec.ts +362 -0
  41. package/src/components/DashboardBuilder/malloyTree.ts +954 -0
  42. package/src/components/DashboardBuilder/preview.spec.ts +18 -0
  43. package/src/components/DashboardBuilder/preview.ts +24 -6
  44. package/src/components/DashboardBuilder/readDocument.spec.ts +312 -21
  45. package/src/components/DashboardBuilder/readDocument.ts +144 -165
  46. package/src/components/DashboardBuilder/scannerShapes.spec.ts +550 -0
  47. package/src/components/DashboardBuilder/spliceDocument.spec.ts +955 -21
  48. package/src/components/DashboardBuilder/spliceDocument.ts +509 -119
  49. package/src/components/DashboardBuilder/statementExtent.spec.ts +606 -0
  50. package/src/components/RenderedResult/RenderedResult.tsx +21 -1
  51. package/src/components/given/GivensPanel.tsx +15 -3
  52. package/src/theme/buildTableCssVars.spec.ts +3 -0
  53. package/src/theme/buildTableCssVars.ts +6 -0
  54. package/src/theme/defaults.ts +16 -12
  55. package/src/theme/resolveTheme.spec.ts +14 -7
  56. package/src/theme/resolveTheme.ts +14 -8
  57. package/src/theme/types.ts +12 -2
  58. package/tsconfig.spec.json +5 -1
  59. package/dist/ServerProvider-Cc489A3f.cjs.js +0 -1
@@ -14,6 +14,7 @@ import {
14
14
  type DashboardEventHandler,
15
15
  } from "../Dashboard/telemetry";
16
16
  import { useDocumentControls } from "../../hooks/useDocumentControls";
17
+ import { parseResourceUri } from "../../utils/formatting";
17
18
  import {
18
19
  isDocumentNotFound,
19
20
  useOptionalDocumentStorage,
@@ -51,11 +52,25 @@ import { sha256Hex } from "../../utils/sha256";
51
52
  * editor (its value is written into each tile's query) but reaches the package
52
53
  * only when the file is saved into it.
53
54
  */
54
- export interface DashboardEditorProps {
55
- environmentName: string;
56
- packageName: string;
57
- /** The dashboard's slug: `overview`, not `dashboards/overview.malloy`. */
58
- dashboardName: string;
55
+ export type DashboardEditorProps = (
56
+ | {
57
+ /** `publisher://environments/{env}/packages/{pkg}`, optionally `?versionId=`. */
58
+ resourceUri: string;
59
+ /** The dashboard's slug, as listed by the dashboards endpoint. */
60
+ dashboard: string;
61
+ }
62
+ | {
63
+ /** @deprecated Pass `resourceUri` and `dashboard` instead. */
64
+ environmentName: string;
65
+ /** @deprecated Pass `resourceUri` and `dashboard` instead. */
66
+ packageName: string;
67
+ /**
68
+ * @deprecated Pass `resourceUri` and `dashboard` instead. The
69
+ * dashboard's slug: `overview`, not `dashboards/overview.malloy`.
70
+ */
71
+ dashboardName: string;
72
+ }
73
+ ) & {
59
74
  /** Leave the editor: the host's "Done editing". Absent, no such button. */
60
75
  onExit?: () => void;
61
76
  /**
@@ -72,7 +87,7 @@ export interface DashboardEditorProps {
72
87
  * without this a host cannot tell whether leaving costs anything.
73
88
  */
74
89
  onDirtyChange?: (dirty: boolean) => void;
75
- }
90
+ };
76
91
 
77
92
  /** The Console's key for a dashboard's copy; see the storage seam's locator rule. */
78
93
  const dashboardLocator = (
@@ -86,14 +101,40 @@ const dashboardLocator = (
86
101
  path: `${environmentName}/${packageName}/${modelPath}`,
87
102
  });
88
103
 
89
- export function DashboardEditor({
90
- environmentName,
91
- packageName,
92
- dashboardName,
93
- onExit,
94
- onEvent,
95
- onDirtyChange,
96
- }: DashboardEditorProps) {
104
+ export function DashboardEditor(props: DashboardEditorProps) {
105
+ const { onExit, onEvent, onDirtyChange } = props;
106
+ // Resolved once, so the rest of the body reads these locals and never the
107
+ // union directly. Degraded, not thrown, on a bad URI: see `Dashboard`'s
108
+ // note on the same call — a throw in a render body takes the host's whole
109
+ // tree down, which is a white screen instead of a message. The deprecated
110
+ // form cannot be malformed the same way, so it always names both.
111
+ // `parseResourceUri` THROWS on a string that is not a `publisher://` URI at
112
+ // all, which a render body must not do; only its missing-name case returns.
113
+ const parsed = (() => {
114
+ if (!("resourceUri" in props)) return undefined;
115
+ try {
116
+ return parseResourceUri(props.resourceUri);
117
+ } catch {
118
+ return undefined;
119
+ }
120
+ })();
121
+ const environmentName =
122
+ "resourceUri" in props
123
+ ? (parsed?.environmentName ?? "")
124
+ : props.environmentName;
125
+ const packageName =
126
+ "resourceUri" in props ? (parsed?.packageName ?? "") : props.packageName;
127
+ const dashboardName =
128
+ "resourceUri" in props ? props.dashboard : props.dashboardName;
129
+ const versionId = parsed?.versionId;
130
+ // Keyed on the prop form, not on `parsed`: a URI that failed to parse at all
131
+ // also leaves `parsed` undefined, and reading that as "no URI to check" would
132
+ // skip the error display and run the editor against empty names.
133
+ const uriNamesBoth =
134
+ "resourceUri" in props
135
+ ? !!parsed?.environmentName && !!parsed?.packageName
136
+ : true;
137
+
97
138
  const { apiClients, mutable } = useServer();
98
139
  const queryClient = useQueryClient();
99
140
  // When the editor was asked for — or the reader chose what to open — so
@@ -114,9 +155,16 @@ export function DashboardEditor({
114
155
  environmentName,
115
156
  packageName,
116
157
  modelPath,
158
+ versionId,
117
159
  ],
118
160
  queryFn: () =>
119
- apiClients.models.getModel(environmentName, packageName, modelPath),
161
+ apiClients.models.getModel(
162
+ environmentName,
163
+ packageName,
164
+ modelPath,
165
+ versionId,
166
+ ),
167
+ enabled: uriNamesBoth,
120
168
  });
121
169
  const packageText = (
122
170
  modelQuery.data?.data as { sourceText?: string } | undefined
@@ -380,6 +428,7 @@ export function DashboardEditor({
380
428
  environmentName,
381
429
  packageName,
382
430
  modelPath,
431
+ versionId,
383
432
  ],
384
433
  });
385
434
  throw new Error(apiErrorMessage(error));
@@ -415,6 +464,7 @@ export function DashboardEditor({
415
464
  environmentName,
416
465
  packageName,
417
466
  modelPath,
467
+ versionId,
418
468
  ],
419
469
  });
420
470
  for (const key of [
@@ -429,6 +479,7 @@ export function DashboardEditor({
429
479
  environmentName,
430
480
  packageName,
431
481
  modelPath,
482
+ versionId,
432
483
  storage,
433
484
  locator,
434
485
  queryClient,
@@ -440,15 +491,25 @@ export function DashboardEditor({
440
491
  // record instead of the record.
441
492
  const savesTo = authoritative ? "host" : mutable ? "package" : "browser";
442
493
  const canWriteWorkspace = workspace?.writeable === true;
494
+ // A version is an immutable checkpoint, and `updateModelSource` cannot be
495
+ // told to write against one (the server answers 501). Without this guard,
496
+ // `expectedHash` would be the hash of the pinned text, the server would
497
+ // refuse every save against the current file, and the catch would refetch
498
+ // the same pinned text, so the "changed since you opened it" banner would
499
+ // never fire either: a dead end with no way out. Storage-backed saves are
500
+ // unaffected, since they never touch the package's compare-and-swap.
501
+ const pinnedPackageSave = versionId !== undefined && savesTo === "package";
443
502
  const writer = authoritative
444
503
  ? storage && locator && canWriteWorkspace
445
504
  ? saveToStorage
446
505
  : undefined
447
- : mutable
448
- ? saveToPackage
449
- : storage && locator && canWriteWorkspace
450
- ? saveToStorage
451
- : undefined;
506
+ : pinnedPackageSave
507
+ ? undefined
508
+ : mutable
509
+ ? saveToPackage
510
+ : storage && locator && canWriteWorkspace
511
+ ? saveToStorage
512
+ : undefined;
452
513
  // A copy that could not be read is not a copy that is not there. Saving on
453
514
  // that belief is what rewinds the record, so Save is off until a reader can
454
515
  // be told what actually happened.
@@ -486,6 +547,15 @@ export function DashboardEditor({
486
547
  setAccepted(held);
487
548
  };
488
549
 
550
+ // After every hook, so the hook order does not depend on the URI. Same
551
+ // reasoning as `Dashboard`'s own check.
552
+ if (!uriNamesBoth && "resourceUri" in props)
553
+ return (
554
+ <Alert severity="error" sx={{ m: 2 }}>
555
+ A dashboard resource URI must name an environment and a package.
556
+ Received: {props.resourceUri}
557
+ </Alert>
558
+ );
489
559
  if (modelQuery.isError)
490
560
  return (
491
561
  <ApiErrorDisplay
@@ -570,6 +640,7 @@ export function DashboardEditor({
570
640
  packageName={packageName}
571
641
  modelPath={modelPath}
572
642
  slug={dashboardName}
643
+ {...(versionId !== undefined ? { versionId } : {})}
573
644
  opened={opened}
574
645
  onSave={save}
575
646
  onDirtyChange={reportDirty}
@@ -587,8 +658,10 @@ export function DashboardEditor({
587
658
  note={caption({
588
659
  authoritative,
589
660
  mutable,
661
+ pinnedPackageSave,
590
662
  ...(workspace ? { workspace } : {}),
591
663
  ...(readFailure !== undefined ? { readFailure } : {}),
664
+ ...(versionId !== undefined ? { versionId } : {}),
592
665
  })}
593
666
  />
594
667
  )}
@@ -605,13 +678,17 @@ export function DashboardEditor({
605
678
  function caption({
606
679
  authoritative,
607
680
  mutable,
681
+ pinnedPackageSave,
608
682
  workspace,
609
683
  readFailure,
684
+ versionId,
610
685
  }: {
611
686
  authoritative: boolean;
612
687
  mutable: boolean;
688
+ pinnedPackageSave: boolean;
613
689
  workspace?: Workspace;
614
690
  readFailure?: string;
691
+ versionId?: string;
615
692
  }): string {
616
693
  if (readFailure !== undefined)
617
694
  return `The saved copy could not be read, so Save is off: ${readFailure}`;
@@ -619,6 +696,8 @@ function caption({
619
696
  return workspace.writeable
620
697
  ? workspace.description
621
698
  : `${workspace.description}: you cannot save into it.`;
699
+ if (pinnedPackageSave)
700
+ return `Reading version ${versionId}: a version is a fixed point in history, so Save is off.`;
622
701
  if (mutable) return "Save writes the file into the package.";
623
702
  if (workspace)
624
703
  return `${workspace.description}: this server does not take writes.`;
@@ -641,6 +720,7 @@ function Surface({
641
720
  packageName,
642
721
  modelPath,
643
722
  slug,
723
+ versionId,
644
724
  opened,
645
725
  onSave,
646
726
  onDirtyChange,
@@ -653,6 +733,7 @@ function Surface({
653
733
  packageName: string;
654
734
  modelPath: string;
655
735
  slug: string;
736
+ versionId?: string;
656
737
  opened: { source: string; document: DashboardDocument; generation: number };
657
738
  onSave?: (source: string) => Promise<void>;
658
739
  onDirtyChange: (dirty: boolean) => void;
@@ -669,25 +750,31 @@ function Surface({
669
750
  environmentName,
670
751
  packageName,
671
752
  slug,
753
+ versionId,
672
754
  ],
673
755
  queryFn: () =>
674
756
  apiClients.dashboards.getDashboard(
675
757
  environmentName,
676
758
  packageName,
677
759
  slug,
678
- undefined,
760
+ versionId,
679
761
  ),
680
762
  });
681
763
  const manifest = data?.data;
682
764
 
683
765
  // The package's other dashboards, by slug: where a clicked cell can go.
684
766
  const { data: dashboardList } = useQueryWithApiError({
685
- queryKey: ["dashboard-editor-dashboards", environmentName, packageName],
767
+ queryKey: [
768
+ "dashboard-editor-dashboards",
769
+ environmentName,
770
+ packageName,
771
+ versionId,
772
+ ],
686
773
  queryFn: () =>
687
774
  apiClients.dashboards.listDashboards(
688
775
  environmentName,
689
776
  packageName,
690
- undefined,
777
+ versionId,
691
778
  ),
692
779
  });
693
780
  const otherDashboards = useMemo(
@@ -715,12 +802,13 @@ function Surface({
715
802
  environmentName,
716
803
  packageName,
717
804
  ...importPaths,
805
+ versionId,
718
806
  ],
719
807
  queryFn: async () => {
720
808
  const models = await Promise.all(
721
809
  importPaths.map((path) =>
722
810
  apiClients.models
723
- .getModel(environmentName, packageName, path)
811
+ .getModel(environmentName, packageName, path, versionId)
724
812
  .then((response) => response.data),
725
813
  ),
726
814
  );
@@ -749,11 +837,12 @@ function Surface({
749
837
  specs,
750
838
  loaded: isSuccess,
751
839
  startingValues: manifest?.startingGivens,
752
- documentKey: `${environmentName}/${packageName}/${slug}/edit`,
840
+ documentKey: `${environmentName}/${packageName}/${versionId ?? ""}/${slug}/edit`,
753
841
  autorun: manifest?.autorun !== false,
754
842
  environmentName,
755
843
  packageName,
756
844
  modelPath: manifest?.path,
845
+ versionId,
757
846
  documentName: slug,
758
847
  });
759
848
 
@@ -765,6 +854,7 @@ function Surface({
765
854
  <DashboardTile
766
855
  environmentName={environmentName}
767
856
  packageName={packageName}
857
+ versionId={versionId}
768
858
  modelPath={modelPath}
769
859
  tile={query.expression}
770
860
  label={
@@ -784,6 +874,7 @@ function Surface({
784
874
  runnable,
785
875
  environmentName,
786
876
  packageName,
877
+ versionId,
787
878
  modelPath,
788
879
  applied,
789
880
  declaredTypes,
@@ -164,6 +164,23 @@ function FieldPicker({
164
164
  );
165
165
  }
166
166
 
167
+ /**
168
+ * Why a tile cannot take a binding. A tile declared on its source and one
169
+ * declared HERE in a body with no single block for a `where:` are different
170
+ * situations, and saying "from the model" about the second one is untrue.
171
+ */
172
+ const unbindable = (tile: DashboardTile | undefined) =>
173
+ tile?.declaration.kind === "opaque"
174
+ ? {
175
+ label: "Not bindable",
176
+ reason: `Its body is ${tile.declaration.why}, so a filter has no single place to go.`,
177
+ }
178
+ : {
179
+ label: "From the model",
180
+ reason:
181
+ "Declared on its source, which this dashboard does not write.",
182
+ };
183
+
167
184
  export function FilterDialog({
168
185
  open,
169
186
  document,
@@ -502,18 +519,14 @@ export function FilterDialog({
502
519
  {!bindable[index] ? (
503
520
  <Tooltip
504
521
  title={
505
- tile.declaration.kind === "inherited"
506
- ? "Declared on its source, which this dashboard does not write."
507
- : "Its query is written out here rather than named, so a filter cannot be added to it."
522
+ unbindable(document.tiles[index]).reason
508
523
  }
509
524
  >
510
525
  <Typography
511
526
  variant="caption"
512
527
  sx={{ color: theme.tileTitle }}
513
528
  >
514
- {tile.declaration.kind === "inherited"
515
- ? "From the model"
516
- : "Inline query"}
529
+ {unbindable(document.tiles[index]).label}
517
530
  </Typography>
518
531
  </Tooltip>
519
532
  ) : (
@@ -132,7 +132,9 @@ export function TileFrame({
132
132
  // will respond to the pointer, told apart
133
133
  // from the ones that will not.
134
134
  "&:hover": {
135
- outlineColor: selected ? theme.drillLink : theme.border,
135
+ outlineColor: selected
136
+ ? theme.drillLink
137
+ : theme.cardBorder,
136
138
  boxShadow: "0 2px 10px rgba(0, 0, 0, 0.10)",
137
139
  },
138
140
  }}
@@ -333,7 +335,14 @@ export function GapTarget({ after }: { after: string }) {
333
335
  minHeight: 48,
334
336
  borderRadius: 1,
335
337
  border: `1px dashed ${theme.drillLink}`,
336
- bgcolor: isDropTarget ? theme.tile : "transparent",
338
+ // Tinted with the same hue as the dashed edge, so the fill
339
+ // and the border read as one affordance lighting up. It was
340
+ // `theme.tile`, the CARD colour, which is the one value
341
+ // guaranteed to match whatever this sits on: a near-no-op
342
+ // before the card went white, and an exact one after.
343
+ bgcolor: isDropTarget
344
+ ? `color-mix(in srgb, ${theme.drillLink} 12%, transparent)`
345
+ : "transparent",
337
346
  opacity: isDropTarget ? 0.95 : 0.4,
338
347
  transition: "opacity 120ms, background-color 120ms",
339
348
  }}
@@ -155,6 +155,12 @@ export function TileMenu({
155
155
  the model. It can still be moved.
156
156
  </Typography>
157
157
  )}
158
+ {draft?.declaration.kind === "opaque" && (
159
+ <Typography variant="body2" sx={{ color: theme.tileTitle }}>
160
+ Its body is {draft.declaration.why}, so a filter has no
161
+ single place to go. Everything else here is editable.
162
+ </Typography>
163
+ )}
158
164
  <Divider />
159
165
  <Stack direction="row" sx={{ justifyContent: "space-between" }}>
160
166
  {editable ? (