@orsetra/shared-ui 1.10.5 → 1.10.6
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.
|
@@ -26,11 +26,30 @@ export interface RelativeTimeRangePickerProps {
|
|
|
26
26
|
triggerLabel?: string
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
const DAY_SECONDS = 24 * 60 * 60
|
|
30
|
+
|
|
31
|
+
// Refreshed periodically (see the interval effect below) so the label doesn't drift far
|
|
32
|
+
// from reality if the picker is left open for a while without the selection changing.
|
|
33
|
+
const REFRESH_INTERVAL_MS = 30_000
|
|
34
|
+
|
|
35
|
+
const timeFormatter = new Intl.DateTimeFormat(undefined, { hour: "2-digit", minute: "2-digit", hour12: false })
|
|
36
|
+
const dayFormatter = new Intl.DateTimeFormat(undefined, { month: "short", day: "numeric" })
|
|
37
|
+
|
|
38
|
+
/** "Since 14:32" for hour/minute-scale options, "Since Aug 18, 14:32" once the option spans a day or more. */
|
|
39
|
+
function formatSince(sinceMs: number, isDayScale: boolean): string {
|
|
40
|
+
const date = new Date(sinceMs)
|
|
41
|
+
const time = timeFormatter.format(date)
|
|
42
|
+
return isDayScale ? `Since ${dayFormatter.format(date)}, ${time}` : `Since ${time}`
|
|
43
|
+
}
|
|
44
|
+
|
|
29
45
|
/**
|
|
30
46
|
* Flyout picker for a small set of relative time ranges (20m, 1h, 3h, ...), styled as a gallery
|
|
31
47
|
* of choices rather than a dropdown list — the current selection is highlighted with both a
|
|
32
48
|
* filled background and a check mark (not color alone), mirroring how Calendar marks the
|
|
33
49
|
* selected day and Combobox marks the selected item in this same component set.
|
|
50
|
+
*
|
|
51
|
+
* The trigger shows the resolved absolute start of the window ("Since ...") rather than the
|
|
52
|
+
* bare option label, so the picker doubles as a readout of what's actually being queried.
|
|
34
53
|
*/
|
|
35
54
|
export function RelativeTimeRangePicker({
|
|
36
55
|
id,
|
|
@@ -45,6 +64,23 @@ export function RelativeTimeRangePicker({
|
|
|
45
64
|
const [open, setOpen] = React.useState(false)
|
|
46
65
|
const selected = options.find(o => o.seconds === value)
|
|
47
66
|
|
|
67
|
+
const [now, setNow] = React.useState(() => Date.now())
|
|
68
|
+
|
|
69
|
+
// Freshen immediately on selection change and whenever the flyout opens.
|
|
70
|
+
React.useEffect(() => {
|
|
71
|
+
setNow(Date.now())
|
|
72
|
+
}, [value, open])
|
|
73
|
+
|
|
74
|
+
// ...and keep it from going stale while left open/mounted without interaction.
|
|
75
|
+
React.useEffect(() => {
|
|
76
|
+
const interval = setInterval(() => setNow(Date.now()), REFRESH_INTERVAL_MS)
|
|
77
|
+
return () => clearInterval(interval)
|
|
78
|
+
}, [])
|
|
79
|
+
|
|
80
|
+
const triggerText = selected
|
|
81
|
+
? formatSince(now - selected.seconds * 1000, selected.seconds >= DAY_SECONDS)
|
|
82
|
+
: "Select…"
|
|
83
|
+
|
|
48
84
|
return (
|
|
49
85
|
<Popover open={open} onOpenChange={setOpen}>
|
|
50
86
|
<PopoverTrigger asChild>
|
|
@@ -59,7 +95,7 @@ export function RelativeTimeRangePicker({
|
|
|
59
95
|
leftIcon={<Clock className="h-3.5 w-3.5" />}
|
|
60
96
|
rightIcon={<ChevronDown className="h-3.5 w-3.5 opacity-60" />}
|
|
61
97
|
>
|
|
62
|
-
{
|
|
98
|
+
{triggerText}
|
|
63
99
|
</Button>
|
|
64
100
|
</PopoverTrigger>
|
|
65
101
|
<PopoverContent align={align} sideOffset={6} className="w-auto rounded-none p-2">
|