@malloy-publisher/sdk 0.2.7 → 0.3.0
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.
- package/README.md +172 -204
- package/dist/ServerProvider-CGnLjzag.cjs.js +1 -0
- package/dist/{ServerProvider-ksC5Su_x.es.js → ServerProvider-DSK9rfSY.es.js} +245 -245
- package/dist/client/api.d.ts +13 -1
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +1 -1
- package/dist/components/ApiErrorDisplay.d.ts +1 -2
- package/dist/components/Dashboard/Dashboard.d.ts +4 -2
- package/dist/components/Dashboard/promoteMeasureRow.d.ts +36 -0
- package/dist/components/DocumentStorage/BrowserDocumentStorage.d.ts +14 -0
- package/dist/components/DocumentStorage/DocumentStorage.d.ts +58 -0
- package/dist/components/DocumentStorage/DocumentStorageProvider.d.ts +18 -0
- package/dist/components/DocumentStorage/index.d.ts +3 -0
- package/dist/components/Model/index.d.ts +0 -1
- package/dist/components/RenderedResult/RenderedResult.d.ts +12 -0
- package/dist/components/RenderedResult/ResultContainer.d.ts +7 -1
- package/dist/components/RenderedResult/index.d.ts +1 -0
- package/dist/components/RenderedResult/resultSizing.d.ts +183 -0
- package/dist/components/drill/index.d.ts +1 -0
- package/dist/components/drill/useDrillSelf.d.ts +46 -0
- package/dist/components/given/filterValue.d.ts +63 -0
- package/dist/components/given/index.d.ts +1 -0
- package/dist/components/given/useGivenUrlParams.d.ts +39 -0
- package/dist/components/index.d.ts +2 -3
- package/dist/components/styles.d.ts +3 -3
- package/dist/core-8NVBCLZh.cjs.js +148 -0
- package/dist/{core-BY7vB5gN.es.js → core-B8oMW4sm.es.js} +3540 -2335
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/useSuggestOptions.d.ts +28 -12
- package/dist/index.cjs.js +281 -1
- package/dist/index.es.js +45803 -79
- package/dist/sdk.css +1 -1
- package/dist/styles.css +0 -1
- package/package.json +2 -6
- package/src/components/ApiErrorDisplay.tsx +1 -1
- package/src/components/Connections/EditConnectionDialog.tsx +96 -86
- package/src/components/Dashboard/Dashboard.tsx +60 -95
- package/src/components/Dashboard/DashboardTile.tsx +25 -2
- package/src/components/Dashboard/promoteMeasureRow.spec.ts +137 -0
- package/src/components/Dashboard/promoteMeasureRow.ts +129 -0
- package/src/components/DocumentStorage/BrowserDocumentStorage.spec.ts +81 -0
- package/src/components/DocumentStorage/BrowserDocumentStorage.ts +105 -0
- package/src/components/DocumentStorage/DocumentStorage.ts +73 -0
- package/src/components/DocumentStorage/DocumentStorageProvider.tsx +46 -0
- package/src/components/DocumentStorage/index.ts +15 -0
- package/src/components/Model/ModelCell.tsx +2 -1
- package/src/components/Model/index.ts +0 -1
- package/src/components/Notebook/Notebook.tsx +10 -76
- package/src/components/Notebook/NotebookCell.tsx +2 -8
- package/src/components/RenderedResult/RenderedResult.tsx +86 -76
- package/src/components/RenderedResult/ResultContainer.tsx +21 -24
- package/src/components/RenderedResult/index.ts +15 -0
- package/src/components/RenderedResult/resultSizing.spec.ts +249 -0
- package/src/components/RenderedResult/resultSizing.ts +308 -0
- package/src/components/ResultsDialog.tsx +2 -1
- package/src/components/drill/index.ts +5 -0
- package/src/components/drill/useDrillSelf.spec.tsx +64 -0
- package/src/components/drill/useDrillSelf.ts +110 -0
- package/src/components/given/GivenInput.spec.tsx +196 -2
- package/src/components/given/GivenInput.tsx +300 -25
- package/src/components/given/filterValue.spec.ts +112 -0
- package/src/components/given/filterValue.ts +188 -0
- package/src/components/given/index.ts +4 -0
- package/src/components/given/useGivenUrlParams.spec.tsx +74 -0
- package/src/components/given/useGivenUrlParams.ts +87 -0
- package/src/components/index.ts +5 -3
- package/src/hooks/index.ts +0 -1
- package/src/hooks/useGivensState.spec.ts +158 -0
- package/src/hooks/useGivensState.ts +114 -23
- package/src/hooks/useSuggestOptions.dom.spec.ts +60 -1
- package/src/hooks/useSuggestOptions.ts +94 -51
- package/vite.config.ts +0 -1
- package/dist/ServerProvider-xXW1HJOJ.cjs.js +0 -1
- package/dist/components/AnalyzePackageButton.d.ts +0 -6
- package/dist/components/Workbook/BrowserWorkbookStorage.d.ts +0 -9
- package/dist/components/Workbook/EditableMalloyCell.d.ts +0 -11
- package/dist/components/Workbook/ModelPicker.d.ts +0 -11
- package/dist/components/Workbook/MutableCell.d.ts +0 -17
- package/dist/components/Workbook/Workbook.d.ts +0 -7
- package/dist/components/Workbook/WorkbookList.d.ts +0 -7
- package/dist/components/Workbook/WorkbookManager.d.ts +0 -84
- package/dist/components/Workbook/WorkbookStorage.d.ts +0 -17
- package/dist/components/Workbook/WorkbookStorageProvider.d.ts +0 -12
- package/dist/components/Workbook/index.d.ts +0 -7
- package/dist/core-CWKmWEg2.cjs.js +0 -148
- package/dist/index-B0WyZAUA.es.js +0 -66412
- package/dist/index-kxVD1RjY.cjs.js +0 -356
- package/dist/markdown-editor.css +0 -341
- package/src/components/AnalyzePackageButton.tsx +0 -319
- package/src/components/Workbook/BrowserWorkbookStorage.ts +0 -64
- package/src/components/Workbook/EditableMalloyCell.tsx +0 -49
- package/src/components/Workbook/ModelPicker.tsx +0 -134
- package/src/components/Workbook/MutableCell.tsx +0 -458
- package/src/components/Workbook/Workbook.tsx +0 -501
- package/src/components/Workbook/WorkbookList.tsx +0 -110
- package/src/components/Workbook/WorkbookManager.ts +0 -214
- package/src/components/Workbook/WorkbookStorage.ts +0 -35
- package/src/components/Workbook/WorkbookStorageProvider.tsx +0 -40
- 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.
|
|
387
|
-
//
|
|
388
|
-
//
|
|
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
|
-
//
|
|
393
|
-
// else (a range, an upper
|
|
394
|
-
//
|
|
395
|
-
//
|
|
396
|
-
//
|
|
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
|
-
|
|
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
|
-
?
|
|
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
|
-
?
|
|
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
|
-
|
|
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
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
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
|
-
|
|
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
|
+
}
|
|
@@ -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
|
+
});
|