@orsetra/shared-ui 1.10.4 → 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.
package/components/ui/index.ts
CHANGED
|
@@ -47,6 +47,7 @@ export { Pagination, PaginationContent, PaginationEllipsis, PaginationItem, Pagi
|
|
|
47
47
|
export { Popover, PopoverTrigger, PopoverContent } from './popover'
|
|
48
48
|
export { Progress } from './progress'
|
|
49
49
|
export { RadioGroup, RadioGroupItem } from './radio-group'
|
|
50
|
+
export { RelativeTimeRangePicker, type RelativeTimeRangeOption, type RelativeTimeRangePickerProps } from './relative-time-range-picker'
|
|
50
51
|
export { ResizablePanelGroup, ResizablePanel, ResizableHandle } from './resizable'
|
|
51
52
|
export { ScrollArea, ScrollBar } from './scroll-area'
|
|
52
53
|
export { Select, SelectGroup, SelectValue, SelectTrigger, SelectContent, SelectLabel, SelectItem, SelectSeparator } from './select'
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import { Check, ChevronDown, Clock } from "lucide-react"
|
|
5
|
+
import { cn } from "../../lib/utils"
|
|
6
|
+
import { Button } from "./button"
|
|
7
|
+
import { Popover, PopoverContent, PopoverTrigger } from "./popover"
|
|
8
|
+
|
|
9
|
+
export interface RelativeTimeRangeOption {
|
|
10
|
+
label: string
|
|
11
|
+
/** Length of the window, in seconds (e.g. 3600 for "1h"). Used as the option's identity. */
|
|
12
|
+
seconds: number
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface RelativeTimeRangePickerProps {
|
|
16
|
+
id?: string
|
|
17
|
+
value: number
|
|
18
|
+
onChange: (seconds: number) => void
|
|
19
|
+
options: RelativeTimeRangeOption[]
|
|
20
|
+
disabled?: boolean
|
|
21
|
+
className?: string
|
|
22
|
+
/** Popover alignment relative to the trigger. Defaults to "end" (picker anchored right — the
|
|
23
|
+
* common placement next to a panel's close button). */
|
|
24
|
+
align?: "start" | "center" | "end"
|
|
25
|
+
/** Accessible name and popover heading. Defaults to "Time range". */
|
|
26
|
+
triggerLabel?: string
|
|
27
|
+
}
|
|
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
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Flyout picker for a small set of relative time ranges (20m, 1h, 3h, ...), styled as a gallery
|
|
47
|
+
* of choices rather than a dropdown list — the current selection is highlighted with both a
|
|
48
|
+
* filled background and a check mark (not color alone), mirroring how Calendar marks the
|
|
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.
|
|
53
|
+
*/
|
|
54
|
+
export function RelativeTimeRangePicker({
|
|
55
|
+
id,
|
|
56
|
+
value,
|
|
57
|
+
onChange,
|
|
58
|
+
options,
|
|
59
|
+
disabled = false,
|
|
60
|
+
className,
|
|
61
|
+
align = "end",
|
|
62
|
+
triggerLabel = "Time range",
|
|
63
|
+
}: RelativeTimeRangePickerProps) {
|
|
64
|
+
const [open, setOpen] = React.useState(false)
|
|
65
|
+
const selected = options.find(o => o.seconds === value)
|
|
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
|
+
|
|
84
|
+
return (
|
|
85
|
+
<Popover open={open} onOpenChange={setOpen}>
|
|
86
|
+
<PopoverTrigger asChild>
|
|
87
|
+
<Button
|
|
88
|
+
id={id}
|
|
89
|
+
type="button"
|
|
90
|
+
variant="secondary"
|
|
91
|
+
size="sm"
|
|
92
|
+
disabled={disabled}
|
|
93
|
+
aria-label={triggerLabel}
|
|
94
|
+
className={cn("rounded-none", className)}
|
|
95
|
+
leftIcon={<Clock className="h-3.5 w-3.5" />}
|
|
96
|
+
rightIcon={<ChevronDown className="h-3.5 w-3.5 opacity-60" />}
|
|
97
|
+
>
|
|
98
|
+
{triggerText}
|
|
99
|
+
</Button>
|
|
100
|
+
</PopoverTrigger>
|
|
101
|
+
<PopoverContent align={align} sideOffset={6} className="w-auto rounded-none p-2">
|
|
102
|
+
<p className="px-1 pb-2 text-xs font-medium text-ibm-gray-50">{triggerLabel}</p>
|
|
103
|
+
<div role="listbox" aria-label={triggerLabel} className="grid grid-cols-3 gap-1.5">
|
|
104
|
+
{options.map(opt => {
|
|
105
|
+
const isSelected = opt.seconds === value
|
|
106
|
+
return (
|
|
107
|
+
<button
|
|
108
|
+
key={opt.seconds}
|
|
109
|
+
type="button"
|
|
110
|
+
role="option"
|
|
111
|
+
aria-selected={isSelected}
|
|
112
|
+
onClick={() => { onChange(opt.seconds); setOpen(false) }}
|
|
113
|
+
className={cn(
|
|
114
|
+
"flex items-center justify-center gap-1 h-9 px-2 text-sm font-medium border transition-colors whitespace-nowrap",
|
|
115
|
+
isSelected
|
|
116
|
+
? "bg-ibm-blue-60 text-white border-ibm-blue-60"
|
|
117
|
+
: "bg-white text-ibm-gray-100 border-ibm-gray-20 hover:bg-ibm-blue-10 hover:border-ibm-blue-40"
|
|
118
|
+
)}
|
|
119
|
+
>
|
|
120
|
+
{isSelected && <Check className="h-3 w-3 flex-shrink-0" />}
|
|
121
|
+
{opt.label}
|
|
122
|
+
</button>
|
|
123
|
+
)
|
|
124
|
+
})}
|
|
125
|
+
</div>
|
|
126
|
+
</PopoverContent>
|
|
127
|
+
</Popover>
|
|
128
|
+
)
|
|
129
|
+
}
|