Repository navigation
Expand file tree
/
Copy pathRangePicker.tsx
More file actions
136 lines (132 loc) · 4.52 KB
/
Copy pathRangePicker.tsx
File metadata and controls
136 lines (132 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { useEffect, useRef, useState } from 'react';
import { formatDateTime } from '../lib/format';
import { MAX_HISTORY_MS, PRESETS, fromLocalInput, toLocalInput } from '../lib/ranges';
import { selectRange, useDashboard } from '../state/store';
import { Icon } from './Icon';
/** Preset segmented control + custom from/to popover + ←/→ window shifting. */
export function RangePicker() {
const preset = useDashboard((s) => s.preset);
const custom = useDashboard((s) => s.custom);
const timeZone = useDashboard((s) => s.timeZone);
const setPreset = useDashboard((s) => s.setPreset);
const setCustom = useDashboard((s) => s.setCustom);
const shift = useDashboard((s) => s.shift);
const open = useDashboard((s) => s.rangeOpen);
const setOpen = useDashboard((s) => s.setRangeOpen);
const range = selectRange({ preset, custom, timeZone });
const [from, setFrom] = useState(toLocalInput(range.from));
const [to, setTo] = useState(toLocalInput(range.to));
const popRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!open) return;
const r = selectRange(useDashboard.getState());
setFrom(toLocalInput(r.from));
setTo(toLocalInput(r.to));
const onDown = (e: MouseEvent) => {
if (popRef.current && !popRef.current.contains(e.target as Node)) setOpen(false);
};
document.addEventListener('mousedown', onDown);
return () => document.removeEventListener('mousedown', onDown);
}, [open, setOpen]);
const now = Date.now();
const f = fromLocalInput(from);
const t = Math.min(fromLocalInput(to), now);
const invalid = !Number.isFinite(f) || !Number.isFinite(t) || f >= t;
return (
<div className="range" ref={popRef}>
<button
type="button"
className="icon-btn"
onClick={() => shift(-1)}
aria-label="Previous period (←)"
title="Previous period (←)"
>
<Icon name="left" />
</button>
<div className="segmented" role="radiogroup" aria-label="Date range">
{PRESETS.map((p) => (
<button
key={p.id}
type="button"
role="radio"
aria-checked={preset === p.id}
className={preset === p.id ? 'is-active' : undefined}
onClick={() => setPreset(p.id)}
title={`${p.label} (${p.key})`}
>
{p.short}
</button>
))}
<button
type="button"
role="radio"
aria-checked={preset === 'custom'}
aria-expanded={open}
className={preset === 'custom' ? 'is-active' : undefined}
onClick={() => setOpen(!open)}
title="Custom range (C)"
>
<Icon name="calendar" size={14} />
<span className="hide-sm">
{preset === 'custom' && custom
? `${formatDateTime(custom.from)} – ${formatDateTime(custom.to)}`
: 'Custom'}
</span>
</button>
</div>
<button
type="button"
className="icon-btn"
onClick={() => shift(1)}
aria-label="Next period (→)"
title="Next period (→)"
>
<Icon name="right" />
</button>
{open && (
<form
className="popover"
aria-label="Custom date range"
onSubmit={(e) => {
e.preventDefault();
if (invalid) return;
setCustom({ from: Math.max(f, now - MAX_HISTORY_MS), to: t });
setOpen(false);
}}
>
<label>
From
<input
type="datetime-local"
value={from}
min={toLocalInput(now - MAX_HISTORY_MS)}
max={toLocalInput(now)}
onChange={(e) => setFrom(e.target.value)}
/>
</label>
<label>
To
<input
type="datetime-local"
value={to}
min={toLocalInput(now - MAX_HISTORY_MS)}
max={toLocalInput(now)}
onChange={(e) => setTo(e.target.value)}
/>
</label>
<p className="muted small">
Up to 30 days of history. Charts pick minute, hour or day buckets automatically.
</p>
<div className="popover__actions">
<button type="button" className="btn btn--ghost btn--sm" onClick={() => setOpen(false)}>
Cancel
</button>
<button type="submit" className="btn btn--sm" disabled={invalid}>
Apply
</button>
</div>
</form>
)}
</div>
);
}