@malloy-publisher/sdk 0.2.7 → 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 (221) hide show
  1. package/README.md +180 -204
  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-xXW1HJOJ.cjs.js → ServerProvider-CH71GsoL.cjs.js} +1 -1
  7. package/dist/{ServerProvider-ksC5Su_x.es.js → ServerProvider-X2Ma0HRs.es.js} +324 -359
  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/api.d.ts +13 -1
  13. package/dist/client/index.cjs.js +1 -1
  14. package/dist/client/index.es.js +1 -1
  15. package/dist/components/ApiErrorDisplay.d.ts +1 -2
  16. package/dist/components/Dashboard/Dashboard.d.ts +23 -16
  17. package/dist/components/Dashboard/DashboardGrid.d.ts +67 -0
  18. package/dist/components/Dashboard/DashboardTile.d.ts +6 -1
  19. package/dist/components/Dashboard/ExploreDialog.d.ts +18 -0
  20. package/dist/components/Dashboard/RowsDialog.d.ts +47 -0
  21. package/dist/components/Dashboard/TileCard.d.ts +44 -0
  22. package/dist/components/Dashboard/index.d.ts +2 -0
  23. package/dist/components/Dashboard/promoteMeasureRow.d.ts +40 -0
  24. package/dist/components/Dashboard/telemetry.d.ts +43 -0
  25. package/dist/components/DashboardBuilder/AddTileDialog.d.ts +35 -0
  26. package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +29 -0
  27. package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +112 -0
  28. package/dist/components/DashboardBuilder/DashboardEditor.d.ts +32 -0
  29. package/dist/components/DashboardBuilder/DiffDialog.d.ts +15 -0
  30. package/dist/components/DashboardBuilder/DrillDialog.d.ts +15 -0
  31. package/dist/components/DashboardBuilder/FilterDialog.d.ts +54 -0
  32. package/dist/components/DashboardBuilder/FilterStrip.d.ts +17 -0
  33. package/dist/components/DashboardBuilder/SettingsPopover.d.ts +20 -0
  34. package/dist/components/DashboardBuilder/TileFrame.d.ts +42 -0
  35. package/dist/components/DashboardBuilder/TileMenu.d.ts +24 -0
  36. package/dist/components/DashboardBuilder/catalog.d.ts +62 -0
  37. package/dist/components/DashboardBuilder/controls.d.ts +157 -0
  38. package/dist/components/DashboardBuilder/diff.d.ts +35 -0
  39. package/dist/components/DashboardBuilder/document.d.ts +218 -0
  40. package/dist/components/DashboardBuilder/layout.d.ts +71 -0
  41. package/dist/components/DashboardBuilder/malloyText.d.ts +45 -0
  42. package/dist/components/DashboardBuilder/preview.d.ts +60 -0
  43. package/dist/components/DashboardBuilder/readDocument.d.ts +106 -0
  44. package/dist/components/DashboardBuilder/sortable.d.ts +51 -0
  45. package/dist/components/DashboardBuilder/spliceDocument.d.ts +44 -0
  46. package/dist/components/DashboardBuilder/testing/fixtures.d.ts +10 -0
  47. package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +22 -0
  48. package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +63 -0
  49. package/dist/components/DashboardBuilder/useDraft.d.ts +13 -0
  50. package/dist/components/DashboardBuilder/useFilterForm.d.ts +65 -0
  51. package/dist/components/DashboardBuilder/useTileReorder.d.ts +22 -0
  52. package/dist/components/DashboardBuilder/useTileResize.d.ts +29 -0
  53. package/dist/components/DocumentStorage/BrowserDocumentStorage.d.ts +14 -0
  54. package/dist/components/DocumentStorage/DocumentStorage.d.ts +58 -0
  55. package/dist/components/DocumentStorage/DocumentStorageProvider.d.ts +25 -0
  56. package/dist/components/DocumentStorage/index.d.ts +3 -0
  57. package/dist/components/FloatingIconButton.d.ts +16 -0
  58. package/dist/components/Model/index.d.ts +0 -1
  59. package/dist/components/Prose.d.ts +46 -0
  60. package/dist/components/RenderedResult/RenderedResult.d.ts +12 -0
  61. package/dist/components/RenderedResult/ResultContainer.d.ts +7 -1
  62. package/dist/components/RenderedResult/ResultPanel.d.ts +30 -0
  63. package/dist/components/RenderedResult/index.d.ts +2 -0
  64. package/dist/components/RenderedResult/resultSizing.d.ts +183 -0
  65. package/dist/components/drill/index.d.ts +2 -1
  66. package/dist/components/drill/resolveDrill.d.ts +22 -0
  67. package/dist/components/drill/useDrill.d.ts +11 -2
  68. package/dist/components/drill/useDrillSelf.d.ts +46 -0
  69. package/dist/components/given/filterValue.d.ts +63 -0
  70. package/dist/components/given/index.d.ts +1 -0
  71. package/dist/components/given/useGivenUrlParams.d.ts +39 -0
  72. package/dist/components/index.d.ts +3 -3
  73. package/dist/components/styles.d.ts +3 -7
  74. package/dist/core-8NVBCLZh.cjs.js +148 -0
  75. package/dist/{core-BY7vB5gN.es.js → core-B8oMW4sm.es.js} +3540 -2335
  76. package/dist/hooks/index.d.ts +3 -1
  77. package/dist/hooks/useDocumentControls.d.ts +70 -0
  78. package/dist/hooks/useQueryResult.d.ts +40 -0
  79. package/dist/hooks/useSuggestOptions.d.ts +28 -12
  80. package/dist/index.cjs.js +34 -1
  81. package/dist/index.es.js +21487 -79
  82. package/dist/sdk.css +1 -1
  83. package/dist/styles.css +0 -1
  84. package/dist/utils/malloyLiteral.d.ts +11 -0
  85. package/package.json +25 -14
  86. package/src/builder-entry.ts +55 -0
  87. package/src/components/ApiErrorDisplay.tsx +1 -1
  88. package/src/components/Connections/EditConnectionDialog.tsx +96 -86
  89. package/src/components/Dashboard/Dashboard.spec.tsx +7 -10
  90. package/src/components/Dashboard/Dashboard.tsx +184 -229
  91. package/src/components/Dashboard/DashboardGrid.tsx +124 -0
  92. package/src/components/Dashboard/DashboardTile.spec.tsx +30 -6
  93. package/src/components/Dashboard/DashboardTile.tsx +67 -101
  94. package/src/components/Dashboard/ExploreDialog.tsx +84 -0
  95. package/src/components/Dashboard/RowsDialog.spec.ts +42 -0
  96. package/src/components/Dashboard/RowsDialog.tsx +234 -0
  97. package/src/components/Dashboard/TileCard.tsx +120 -0
  98. package/src/components/Dashboard/index.ts +2 -0
  99. package/src/components/Dashboard/promoteMeasureRow.spec.ts +207 -0
  100. package/src/components/Dashboard/promoteMeasureRow.ts +160 -0
  101. package/src/components/Dashboard/telemetry.ts +44 -0
  102. package/src/components/Dashboard/tileGridColumn.spec.ts +1 -1
  103. package/src/components/DashboardBuilder/AddTileDialog.tsx +249 -0
  104. package/src/components/DashboardBuilder/BuilderToolbar.tsx +270 -0
  105. package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +858 -0
  106. package/src/components/DashboardBuilder/DashboardBuilder.tsx +653 -0
  107. package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +211 -0
  108. package/src/components/DashboardBuilder/DashboardEditor.tsx +493 -0
  109. package/src/components/DashboardBuilder/DiffDialog.tsx +129 -0
  110. package/src/components/DashboardBuilder/DrillDialog.tsx +281 -0
  111. package/src/components/DashboardBuilder/FilterDialog.tsx +581 -0
  112. package/src/components/DashboardBuilder/FilterStrip.tsx +125 -0
  113. package/src/components/DashboardBuilder/SettingsPopover.tsx +168 -0
  114. package/src/components/DashboardBuilder/TileFrame.tsx +380 -0
  115. package/src/components/DashboardBuilder/TileMenu.tsx +188 -0
  116. package/src/components/DashboardBuilder/catalog.spec.ts +215 -0
  117. package/src/components/DashboardBuilder/catalog.ts +225 -0
  118. package/src/components/DashboardBuilder/controls.spec.ts +243 -0
  119. package/src/components/DashboardBuilder/controls.ts +398 -0
  120. package/src/components/DashboardBuilder/diff.spec.ts +44 -0
  121. package/src/components/DashboardBuilder/diff.ts +93 -0
  122. package/src/components/DashboardBuilder/document.ts +209 -0
  123. package/src/components/DashboardBuilder/keepRowStructure.spec.ts +84 -0
  124. package/src/components/DashboardBuilder/layout.ts +143 -0
  125. package/src/components/DashboardBuilder/malloyText.spec.ts +84 -0
  126. package/src/components/DashboardBuilder/malloyText.ts +151 -0
  127. package/src/components/DashboardBuilder/moveIntoGap.spec.ts +90 -0
  128. package/src/components/DashboardBuilder/preview.spec.ts +149 -0
  129. package/src/components/DashboardBuilder/preview.ts +172 -0
  130. package/src/components/DashboardBuilder/readDocument.spec.ts +375 -0
  131. package/src/components/DashboardBuilder/readDocument.ts +467 -0
  132. package/src/components/DashboardBuilder/sortable.tsx +151 -0
  133. package/src/components/DashboardBuilder/spliceDocument.spec.ts +1108 -0
  134. package/src/components/DashboardBuilder/spliceDocument.ts +1098 -0
  135. package/src/components/DashboardBuilder/testing/fixtures.ts +48 -0
  136. package/src/components/DashboardBuilder/useBuilderShortcuts.ts +88 -0
  137. package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +255 -0
  138. package/src/components/DashboardBuilder/useDashboardEditor.ts +193 -0
  139. package/src/components/DashboardBuilder/useDraft.ts +43 -0
  140. package/src/components/DashboardBuilder/useFilterForm.spec.tsx +144 -0
  141. package/src/components/DashboardBuilder/useFilterForm.ts +352 -0
  142. package/src/components/DashboardBuilder/useTileReorder.spec.tsx +97 -0
  143. package/src/components/DashboardBuilder/useTileReorder.ts +115 -0
  144. package/src/components/DashboardBuilder/useTileResize.spec.tsx +121 -0
  145. package/src/components/DashboardBuilder/useTileResize.ts +110 -0
  146. package/src/components/DashboardBuilder/withGaps.spec.ts +96 -0
  147. package/src/components/DocumentStorage/BrowserDocumentStorage.spec.ts +81 -0
  148. package/src/components/DocumentStorage/BrowserDocumentStorage.ts +105 -0
  149. package/src/components/DocumentStorage/DocumentStorage.ts +73 -0
  150. package/src/components/DocumentStorage/DocumentStorageProvider.tsx +58 -0
  151. package/src/components/DocumentStorage/index.ts +16 -0
  152. package/src/components/Environment/About.tsx +4 -36
  153. package/src/components/FloatingIconButton.tsx +41 -0
  154. package/src/components/Model/Model.tsx +6 -23
  155. package/src/components/Model/ModelCell.tsx +34 -72
  156. package/src/components/Model/index.ts +0 -1
  157. package/src/components/Notebook/Notebook.tsx +27 -156
  158. package/src/components/Notebook/NotebookCell.tsx +37 -191
  159. package/src/components/Prose.spec.tsx +85 -0
  160. package/src/components/Prose.tsx +179 -0
  161. package/src/components/QueryResult/QueryResult.tsx +15 -43
  162. package/src/components/RenderedResult/RenderedResult.tsx +86 -76
  163. package/src/components/RenderedResult/ResultContainer.tsx +25 -36
  164. package/src/components/RenderedResult/ResultPanel.spec.tsx +49 -0
  165. package/src/components/RenderedResult/ResultPanel.tsx +73 -0
  166. package/src/components/RenderedResult/index.ts +16 -0
  167. package/src/components/RenderedResult/resultSizing.spec.ts +249 -0
  168. package/src/components/RenderedResult/resultSizing.ts +308 -0
  169. package/src/components/ResultsDialog.tsx +2 -1
  170. package/src/components/drill/index.ts +6 -0
  171. package/src/components/drill/resolveDrill.ts +22 -1
  172. package/src/components/drill/useDrill.spec.tsx +94 -0
  173. package/src/components/drill/useDrill.tsx +52 -12
  174. package/src/components/drill/useDrillSelf.spec.tsx +64 -0
  175. package/src/components/drill/useDrillSelf.ts +110 -0
  176. package/src/components/given/GivenInput.spec.tsx +196 -2
  177. package/src/components/given/GivenInput.tsx +300 -25
  178. package/src/components/given/GivensPanel.tsx +1 -1
  179. package/src/components/given/filterValue.spec.ts +112 -0
  180. package/src/components/given/filterValue.ts +188 -0
  181. package/src/components/given/index.ts +4 -0
  182. package/src/components/given/useGivenUrlParams.spec.tsx +74 -0
  183. package/src/components/given/useGivenUrlParams.ts +87 -0
  184. package/src/components/index.ts +11 -3
  185. package/src/components/styles.ts +0 -37
  186. package/src/hooks/index.ts +13 -1
  187. package/src/hooks/useDocumentControls.ts +170 -0
  188. package/src/hooks/useGivensState.spec.ts +158 -0
  189. package/src/hooks/useGivensState.ts +114 -23
  190. package/src/hooks/useQueryResult.ts +82 -0
  191. package/src/hooks/useSuggestOptions.dom.spec.ts +60 -1
  192. package/src/hooks/useSuggestOptions.ts +94 -51
  193. package/src/utils/malloyLiteral.spec.ts +43 -0
  194. package/src/utils/malloyLiteral.ts +76 -0
  195. package/vite.config.ts +5 -1
  196. package/dist/components/AnalyzePackageButton.d.ts +0 -6
  197. package/dist/components/Workbook/BrowserWorkbookStorage.d.ts +0 -9
  198. package/dist/components/Workbook/EditableMalloyCell.d.ts +0 -11
  199. package/dist/components/Workbook/ModelPicker.d.ts +0 -11
  200. package/dist/components/Workbook/MutableCell.d.ts +0 -17
  201. package/dist/components/Workbook/Workbook.d.ts +0 -7
  202. package/dist/components/Workbook/WorkbookList.d.ts +0 -7
  203. package/dist/components/Workbook/WorkbookManager.d.ts +0 -84
  204. package/dist/components/Workbook/WorkbookStorage.d.ts +0 -17
  205. package/dist/components/Workbook/WorkbookStorageProvider.d.ts +0 -12
  206. package/dist/components/Workbook/index.d.ts +0 -7
  207. package/dist/core-CWKmWEg2.cjs.js +0 -148
  208. package/dist/index-B0WyZAUA.es.js +0 -66412
  209. package/dist/index-kxVD1RjY.cjs.js +0 -356
  210. package/dist/markdown-editor.css +0 -341
  211. package/src/components/AnalyzePackageButton.tsx +0 -319
  212. package/src/components/Workbook/BrowserWorkbookStorage.ts +0 -64
  213. package/src/components/Workbook/EditableMalloyCell.tsx +0 -49
  214. package/src/components/Workbook/ModelPicker.tsx +0 -134
  215. package/src/components/Workbook/MutableCell.tsx +0 -458
  216. package/src/components/Workbook/Workbook.tsx +0 -501
  217. package/src/components/Workbook/WorkbookList.tsx +0 -110
  218. package/src/components/Workbook/WorkbookManager.ts +0 -214
  219. package/src/components/Workbook/WorkbookStorage.ts +0 -35
  220. package/src/components/Workbook/WorkbookStorageProvider.tsx +0 -40
  221. package/src/components/Workbook/index.ts +0 -10
@@ -12,6 +12,7 @@ import {
12
12
  FormHelperText,
13
13
  IconButton,
14
14
  InputAdornment,
15
+ MenuItem,
15
16
  Slider,
16
17
  Stack,
17
18
  TextField,
@@ -22,17 +23,25 @@ import { DatePicker } from "@mui/x-date-pickers/DatePicker";
22
23
  import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider";
23
24
  import dayjs from "dayjs";
24
25
  import utc from "dayjs/plugin/utc";
26
+ import { type ReactNode, useEffect, useState } from "react";
25
27
  import { Given } from "../../client";
26
28
  import { paramToGiven, pickedDayToUtc } from "./paramCodec";
27
29
  import { GivenValue } from "../../hooks/givenValue";
28
30
  import {
29
31
  decodeAtLeast,
32
+ decodeBetween,
33
+ decodeDayRange,
30
34
  decodeFilterList,
35
+ decodeTimePreset,
31
36
  encodeAtLeast,
37
+ encodeBetween,
38
+ encodeDayRange,
32
39
  encodeFilterList,
40
+ encodeTimePreset,
33
41
  filterInnerType,
34
42
  isFilterType,
35
43
  isPlainFilterList,
44
+ TIME_PRESETS,
36
45
  } from "./filterValue";
37
46
  import { renderGivenDefault } from "./utils";
38
47
 
@@ -383,33 +392,48 @@ export function GivenInput({
383
392
  );
384
393
  }
385
394
 
386
- // A slider, when the declaration bounded the range. Only a lower bound is
387
- // expressible as a filter here (see `encodeAtLeast`), so this reads as
388
- // "at least", which is what a threshold control is for.
395
+ // A slider, when the declaration bounded the range. For a `filter<number>`
396
+ // it has two handles and means "between", `[lo to hi]`, the same control and
397
+ // the same spelling Malloyyo gives the same tags; with the upper handle at
398
+ // the ceiling it collapses to the lower bound alone, `>= lo`, so a threshold
399
+ // is still one drag. A plain `number` given is a single value and keeps one
400
+ // handle.
389
401
  const numericFilter =
390
402
  isFilterType(type) && filterInnerType(type) === "number";
391
403
  const { rangeMin, rangeMax } = given;
392
- // A slider can only represent `>= N`, so a `filter<number>` carrying anything
393
- // else (a range, an upper bound, a negation) falls through to the text box.
394
- // Otherwise `decodeAtLeast` returns undefined, the slider claims "Any" while a
395
- // filter is in force, and the first drag silently replaces the author's filter
396
- // with a threshold. Same principle as the picker and the date guards above.
404
+ // The handles can show a closed range or a lower bound and nothing else, so
405
+ // a `filter<number>` carrying anything else (a half-open range, an upper
406
+ // bound alone, a negation) falls through to the text box. Otherwise the
407
+ // decoders return undefined, the slider claims "Any" while a filter is in
408
+ // force, and the first drag silently replaces the author's filter. Same
409
+ // principle as the picker and the date guards above.
410
+ const atLeast =
411
+ numericFilter && typeof value === "string"
412
+ ? decodeAtLeast(value)
413
+ : undefined;
414
+ const between =
415
+ numericFilter && typeof value === "string"
416
+ ? decodeBetween(value)
417
+ : undefined;
397
418
  const sliderIsRepresentable =
398
419
  !numericFilter ||
399
420
  value === undefined ||
400
421
  value === null ||
401
422
  value === "" ||
402
- (typeof value === "string" && decodeAtLeast(value) !== undefined);
423
+ atLeast !== undefined ||
424
+ between !== undefined;
403
425
  if (
404
426
  (numericFilter || type === "number") &&
405
427
  rangeMin !== undefined &&
406
428
  rangeMax !== undefined &&
407
429
  sliderIsRepresentable
408
430
  ) {
431
+ // The filter's handles: a range as written, or a lower bound with the
432
+ // upper handle resting at the ceiling. Undefined when nothing is set.
433
+ const handles: [number, number] | undefined =
434
+ between ?? (atLeast !== undefined ? [atLeast, rangeMax] : undefined);
409
435
  const current = numericFilter
410
- ? typeof value === "string"
411
- ? decodeAtLeast(value)
412
- : undefined
436
+ ? handles?.[0]
413
437
  : typeof value === "number"
414
438
  ? value
415
439
  : undefined;
@@ -427,6 +451,14 @@ export function GivenInput({
427
451
  }
428
452
  // An unset control rests at the low end, which is the no-op threshold.
429
453
  const position = current ?? rangeMin;
454
+ // And an unset range spans the whole track, which selects everything.
455
+ const thumbs: [number, number] = handles ?? [rangeMin, rangeMax];
456
+ // A range reads as `lo to hi`, and as `≥ lo` once the upper handle is at
457
+ // the ceiling, which is also what the value on the wire says.
458
+ const rangeReadout =
459
+ thumbs[1] >= rangeMax
460
+ ? `≥ ${thumbs[0]}`
461
+ : `${thumbs[0]} to ${thumbs[1]}`;
430
462
  // `""` on a filter given is an explicit "no threshold" OVERRIDE, not an
431
463
  // absence: `paramToGiven` keeps it because the empty string is a value a
432
464
  // filter can mean, and `givensToRequest` sends it. Counting it as unset
@@ -484,7 +516,7 @@ export function GivenInput({
484
516
  : emptyFilterOverride
485
517
  ? "Any"
486
518
  : numericFilter
487
- ? `≥ ${position}`
519
+ ? rangeReadout
488
520
  : String(position)}
489
521
  </Typography>
490
522
  {isOverridden && (
@@ -508,27 +540,51 @@ export function GivenInput({
508
540
  size="small"
509
541
  min={rangeMin}
510
542
  max={rangeMax}
511
- value={position}
543
+ value={numericFilter ? thumbs : position}
544
+ // Two handles must not cross: a swapped pair would encode a
545
+ // range the decoder refuses, and the control would fall
546
+ // through to the text box mid-drag.
547
+ disableSwap={numericFilter}
512
548
  valueLabelDisplay="auto"
513
- aria-label={label}
549
+ // One name per handle, so a test or a screen reader can tell
550
+ // them apart; a single handle keeps the plain label.
551
+ aria-label={numericFilter ? undefined : label}
552
+ getAriaLabel={
553
+ numericFilter
554
+ ? (index) =>
555
+ index === 0 ? `${label} from` : `${label} to`
556
+ : undefined
557
+ }
514
558
  // MUI pads a slider vertically for a touch target, which is
515
559
  // 26px this control cannot spare; the row above already
516
560
  // gives the thumb somewhere to be.
517
561
  sx={{ py: 0, mt: 0.5 }}
518
562
  onChange={(_event, next) => {
519
- const picked = Array.isArray(next) ? next[0] : next;
520
- // Back at the floor is no threshold at all for a FILTER,
521
- // not `>= min`, so dragging left the whole way clears
522
- // rather than leaving a filter that reads as a constraint.
523
- //
563
+ if (numericFilter) {
564
+ const [lo, hi] = Array.isArray(next)
565
+ ? [next[0], next[1]]
566
+ : [next, next];
567
+ // Both handles at the ends is no filter at all, not
568
+ // `[min to max]`, so dragging the whole way out clears
569
+ // rather than leaving a filter that reads as a
570
+ // constraint. The upper handle at the ceiling alone is
571
+ // a threshold, `>= lo`, which is what it was before
572
+ // the range existed and what a URL from then holds.
573
+ if (lo <= rangeMin && hi >= rangeMax) {
574
+ onChange(null);
575
+ return;
576
+ }
577
+ onChange(
578
+ hi >= rangeMax
579
+ ? encodeAtLeast(lo)
580
+ : encodeBetween(lo, hi),
581
+ );
582
+ return;
583
+ }
524
584
  // A plain `number` given is not a threshold, so its floor
525
585
  // is an ordinary value and clearing there made the minimum
526
586
  // the one number on the scale the reader could not pick.
527
- if (numericFilter && picked <= rangeMin) {
528
- onChange(null);
529
- return;
530
- }
531
- onChange(numericFilter ? encodeAtLeast(picked) : picked);
587
+ onChange(Array.isArray(next) ? next[0] : next);
532
588
  }}
533
589
  />
534
590
  </Box>
@@ -566,6 +622,28 @@ export function GivenInput({
566
622
  const bareDate =
567
623
  typeof value === "string" && /^\d{4}-\d{2}-\d{2}$/.test(value);
568
624
  const dateIsPickable = value === undefined || value === null || bareDate;
625
+ // A time-range control for a date-typed filter that is unset, the empty
626
+ // filter, one of the preset windows, or a range of whole days. The presets
627
+ // are Malloyyo's, so a `filter<timestamp>` given draws the same control on
628
+ // either host and a value set on one reads on the other. A bare day keeps
629
+ // the single date picker below (it is what `# drill { to=self }` hands over,
630
+ // and one day is a day, not a range); anything else the grammar can say
631
+ // falls through to the text box, as written and editable.
632
+ if (isDateFilter && !bareDate) {
633
+ const state = readTimeRange(value);
634
+ if (state !== undefined) {
635
+ return (
636
+ <TimeRangeControl
637
+ label={label}
638
+ state={state}
639
+ onChange={onChange}
640
+ helperNode={helperNode}
641
+ defaultDisplay={defaultDisplay}
642
+ />
643
+ );
644
+ }
645
+ }
646
+
569
647
  // An unrepresentable date filter falls through to the text box below, for the
570
648
  // same reason a non-list string filter does: a range like
571
649
  // `2024-01-01 to 2024-02-01` handed whole to `dayjs.utc` parses as its
@@ -799,3 +877,200 @@ function ClearAdornment({ onClear }: { onClear: () => void }) {
799
877
  </InputAdornment>
800
878
  );
801
879
  }
880
+
881
+ /** What a time-range control is showing; undefined means it cannot show it. */
882
+ type TimeRangeState =
883
+ | { kind: "unset" }
884
+ /** An explicit `""`: the empty filter, matching every row, as an override. */
885
+ | { kind: "any" }
886
+ | { kind: "preset"; key: string }
887
+ | { kind: "days"; firstDay: string; lastDay: string };
888
+
889
+ function readTimeRange(
890
+ value: GivenValue | undefined,
891
+ ): TimeRangeState | undefined {
892
+ if (value === undefined || value === null) return { kind: "unset" };
893
+ if (typeof value !== "string") return undefined;
894
+ if (value === "") return { kind: "any" };
895
+ const key = decodeTimePreset(value);
896
+ if (key !== undefined) return { kind: "preset", key };
897
+ const days = decodeDayRange(value);
898
+ if (days !== undefined) return { kind: "days", ...days };
899
+ return undefined;
900
+ }
901
+
902
+ const CUSTOM_RANGE = "__custom__";
903
+ const ANY_TIME = "__any__";
904
+
905
+ /**
906
+ * Preset windows in a dropdown, with "Custom range" opening two day pickers.
907
+ *
908
+ * The pickers are inclusive on both ends, which is how a reader thinks about
909
+ * "January 1st to January 31st"; `encodeDayRange` handles the half-open grammar
910
+ * underneath. A custom range commits only once both days are picked and in
911
+ * order: a half-filled pair sends nothing rather than a range with one
912
+ * invented end, and the value in force meanwhile is whatever it was.
913
+ *
914
+ * Custom mode is local state, because choosing "Custom range" changes what is
915
+ * on screen before it changes the value. It follows the value when the value
916
+ * moves under it (a preset arriving from the URL, a revert), keyed on the
917
+ * value's shape rather than on the object, so it does not re-run every render.
918
+ */
919
+ function TimeRangeControl({
920
+ label,
921
+ state,
922
+ onChange,
923
+ helperNode,
924
+ defaultDisplay,
925
+ }: {
926
+ label: string;
927
+ state: TimeRangeState;
928
+ onChange: (next: GivenValue) => void;
929
+ helperNode: ReactNode;
930
+ defaultDisplay: string | undefined;
931
+ }) {
932
+ const stateKey =
933
+ state.kind === "days"
934
+ ? `days:${state.firstDay}/${state.lastDay}`
935
+ : state.kind === "preset"
936
+ ? `preset:${state.key}`
937
+ : state.kind;
938
+ const [custom, setCustom] = useState(state.kind === "days");
939
+ const [draft, setDraft] = useState<{ from?: string; to?: string }>(
940
+ state.kind === "days" ? { from: state.firstDay, to: state.lastDay } : {},
941
+ );
942
+ useEffect(() => {
943
+ if (state.kind === "days") {
944
+ setCustom(true);
945
+ setDraft({ from: state.firstDay, to: state.lastDay });
946
+ } else if (state.kind !== "unset") {
947
+ // A preset or the empty filter arrived: it is what is showing now.
948
+ setCustom(false);
949
+ }
950
+ // `stateKey` stands in for `state`, whose object identity is per render.
951
+ // eslint-disable-next-line react-hooks/exhaustive-deps
952
+ }, [stateKey]);
953
+
954
+ const isOverridden = state.kind !== "unset";
955
+ const selected = custom
956
+ ? CUSTOM_RANGE
957
+ : state.kind === "preset"
958
+ ? state.key
959
+ : state.kind === "any"
960
+ ? ANY_TIME
961
+ : "";
962
+
963
+ const commitDays = (next: { from?: string; to?: string }) => {
964
+ setDraft(next);
965
+ if (
966
+ next.from &&
967
+ next.to &&
968
+ !dayjs.utc(next.to).isBefore(dayjs.utc(next.from))
969
+ ) {
970
+ onChange(encodeDayRange(next.from, next.to));
971
+ }
972
+ };
973
+
974
+ const day = (text: string | undefined) => {
975
+ const parsed = text ? dayjs.utc(text) : null;
976
+ return parsed?.isValid() ? parsed : null;
977
+ };
978
+
979
+ return (
980
+ <Stack direction="row" spacing={1} data-testid="time-range">
981
+ <TextField
982
+ select
983
+ label={label}
984
+ value={selected}
985
+ size="small"
986
+ fullWidth
987
+ // Unset shows the model's own default as the helper line, the way
988
+ // every other control does; the dropdown itself stays blank so it
989
+ // does not assert a window nobody chose.
990
+ helperText={helperNode}
991
+ slotProps={{
992
+ select: {
993
+ displayEmpty: true,
994
+ renderValue: (v) => {
995
+ if (v === CUSTOM_RANGE) return "Custom range";
996
+ if (v === ANY_TIME) return "Any time";
997
+ const preset = TIME_PRESETS.find((p) => p.key === v);
998
+ return preset
999
+ ? preset.label
1000
+ : (defaultDisplay ?? "Any time");
1001
+ },
1002
+ },
1003
+ input: {
1004
+ endAdornment: isOverridden ? (
1005
+ // Before the dropdown arrow, which MUI positions on its
1006
+ // own; the × reverts, like every × in this file.
1007
+ <IconButton
1008
+ size="small"
1009
+ aria-label="clear value"
1010
+ onClick={() => {
1011
+ setCustom(false);
1012
+ setDraft({});
1013
+ onChange(null);
1014
+ }}
1015
+ sx={{ mr: 2 }}
1016
+ >
1017
+ <ClearIcon fontSize="small" />
1018
+ </IconButton>
1019
+ ) : undefined,
1020
+ },
1021
+ }}
1022
+ onChange={(e) => {
1023
+ const key = e.target.value;
1024
+ if (key === CUSTOM_RANGE) {
1025
+ // Nothing committed yet: the pickers decide.
1026
+ setCustom(true);
1027
+ return;
1028
+ }
1029
+ setCustom(false);
1030
+ const encoded = encodeTimePreset(key);
1031
+ if (encoded !== undefined) onChange(encoded);
1032
+ }}
1033
+ >
1034
+ {/* The empty filter is showable, since a URL can carry it, but not
1035
+ offered: the gesture for "no filter" is the ×, which reverts to
1036
+ the model default rather than overriding it with everything. */}
1037
+ {state.kind === "any" && (
1038
+ <MenuItem value={ANY_TIME}>Any time</MenuItem>
1039
+ )}
1040
+ {TIME_PRESETS.map((preset) => (
1041
+ <MenuItem key={preset.key} value={preset.key}>
1042
+ {preset.label}
1043
+ </MenuItem>
1044
+ ))}
1045
+ <MenuItem value={CUSTOM_RANGE}>Custom range</MenuItem>
1046
+ </TextField>
1047
+ {custom && (
1048
+ <LocalizationProvider dateAdapter={AdapterDayjs}>
1049
+ <DatePicker
1050
+ label="From"
1051
+ value={day(draft.from)}
1052
+ onChange={(next) =>
1053
+ commitDays({
1054
+ ...draft,
1055
+ from: next ? next.format("YYYY-MM-DD") : undefined,
1056
+ })
1057
+ }
1058
+ slotProps={{ textField: { size: "small" } }}
1059
+ />
1060
+ <DatePicker
1061
+ label="To"
1062
+ value={day(draft.to)}
1063
+ minDate={day(draft.from) ?? undefined}
1064
+ onChange={(next) =>
1065
+ commitDays({
1066
+ ...draft,
1067
+ to: next ? next.format("YYYY-MM-DD") : undefined,
1068
+ })
1069
+ }
1070
+ slotProps={{ textField: { size: "small" } }}
1071
+ />
1072
+ </LocalizationProvider>
1073
+ )}
1074
+ </Stack>
1075
+ );
1076
+ }
@@ -166,7 +166,7 @@ export function GivensPanel({
166
166
  >
167
167
  <Typography
168
168
  variant="subtitle2"
169
- sx={{ fontWeight: 600, color: "#333" }}
169
+ sx={{ fontWeight: 600, color: "text.primary" }}
170
170
  >
171
171
  {title ?? "Parameters"}
172
172
  </Typography>
@@ -11,6 +11,13 @@ import {
11
11
  filterInnerType,
12
12
  isFilterType,
13
13
  isPlainFilterList,
14
+ decodeBetween,
15
+ decodeDayRange,
16
+ decodeTimePreset,
17
+ encodeBetween,
18
+ encodeDayRange,
19
+ encodeTimePreset,
20
+ TIME_PRESETS,
14
21
  } from "./filterValue";
15
22
 
16
23
  /**
@@ -362,3 +369,108 @@ describe("isPlainFilterList", () => {
362
369
  expect(isPlainFilterList("-Nike")).toBe(false);
363
370
  });
364
371
  });
372
+
373
+ describe("encodeBetween / decodeBetween, the two-handled slider's filter", () => {
374
+ it("writes Malloy's closed range, the spelling Malloyyo writes too", () => {
375
+ expect(encodeBetween(10, 20)).toBe("[10 to 20]");
376
+ expect(decodeBetween("[10 to 20]")).toEqual([10, 20]);
377
+ });
378
+
379
+ it("reads whitespace and decimals the grammar accepts", () => {
380
+ expect(decodeBetween(" [0.5 to 1e3] ")).toEqual([0.5, 1000]);
381
+ });
382
+
383
+ it("refuses a half-open range, which no thumb can show", () => {
384
+ expect(decodeBetween("(10 to 20]")).toBeUndefined();
385
+ expect(decodeBetween("[10 to 20)")).toBeUndefined();
386
+ });
387
+
388
+ it("refuses a negation, a threshold and the empty filter", () => {
389
+ expect(decodeBetween("not [10 to 20]")).toBeUndefined();
390
+ expect(decodeBetween(">= 5")).toBeUndefined();
391
+ expect(decodeBetween("")).toBeUndefined();
392
+ });
393
+
394
+ it("refuses an inverted range rather than drawing it backwards", () => {
395
+ expect(decodeBetween("[20 to 10]")).toBeUndefined();
396
+ });
397
+ });
398
+
399
+ describe("time presets", () => {
400
+ it("spell each preset in the filter grammar Malloyyo uses", () => {
401
+ expect(TIME_PRESETS.map((p) => encodeTimePreset(p.key))).toEqual([
402
+ "today",
403
+ "7 days",
404
+ "30 days",
405
+ "90 days",
406
+ "12 months",
407
+ ]);
408
+ });
409
+
410
+ it("round-trip every preset", () => {
411
+ for (const preset of TIME_PRESETS) {
412
+ expect(decodeTimePreset(encodeTimePreset(preset.key)!)).toBe(
413
+ preset.key,
414
+ );
415
+ }
416
+ });
417
+
418
+ it("match on the clause, not the spelling", () => {
419
+ expect(decodeTimePreset("7 day")).toBe("7d");
420
+ expect(decodeTimePreset(" 12 months ")).toBe("12m");
421
+ });
422
+
423
+ it("are not fooled by a window of another size, or a negation", () => {
424
+ expect(decodeTimePreset("8 days")).toBeUndefined();
425
+ expect(decodeTimePreset("not today")).toBeUndefined();
426
+ expect(decodeTimePreset("last 7 days")).toBeUndefined();
427
+ expect(decodeTimePreset("")).toBeUndefined();
428
+ });
429
+
430
+ it("have no key that is not encodable", () => {
431
+ expect(encodeTimePreset("nope")).toBeUndefined();
432
+ });
433
+ });
434
+
435
+ describe("encodeDayRange / decodeDayRange, inclusive days over a half-open grammar", () => {
436
+ it("encodes the day after the last picked day, since `to` excludes its end", () => {
437
+ expect(encodeDayRange("2024-01-01", "2024-01-31")).toBe(
438
+ "2024-01-01 to 2024-02-01",
439
+ );
440
+ // A single picked day is that whole day.
441
+ expect(encodeDayRange("2024-02-28", "2024-02-28")).toBe(
442
+ "2024-02-28 to 2024-02-29",
443
+ );
444
+ });
445
+
446
+ it("decodes back to the days the filter actually selects", () => {
447
+ expect(decodeDayRange("2024-01-01 to 2024-02-01")).toEqual({
448
+ firstDay: "2024-01-01",
449
+ lastDay: "2024-01-31",
450
+ });
451
+ });
452
+
453
+ it("round-trips through a leap day and a year end", () => {
454
+ for (const [a, b] of [
455
+ ["2024-02-28", "2024-02-29"],
456
+ ["2023-12-25", "2024-01-05"],
457
+ ]) {
458
+ expect(decodeDayRange(encodeDayRange(a, b))).toEqual({
459
+ firstDay: a,
460
+ lastDay: b,
461
+ });
462
+ }
463
+ });
464
+
465
+ it("refuses an end that is not after the start", () => {
466
+ expect(decodeDayRange("2024-01-10 to 2024-01-10")).toBeUndefined();
467
+ expect(decodeDayRange("2024-01-10 to 2024-01-01")).toBeUndefined();
468
+ });
469
+
470
+ it("refuses ends two day pickers cannot show", () => {
471
+ expect(decodeDayRange("2024-01-01 to now")).toBeUndefined();
472
+ expect(decodeDayRange("2024-01-01 12:00 to 2024-01-02")).toBeUndefined();
473
+ expect(decodeDayRange("not 2024-01-01 to 2024-02-01")).toBeUndefined();
474
+ expect(decodeDayRange("7 days")).toBeUndefined();
475
+ });
476
+ });