fix: refactor time range to single source of truth with correct dates

- Default range changed from 30d to 7d
- Presets (today/7d/30d) now directly set customStart/customEnd dates,
  eliminating duplicate getTimeRange() calculation
- "All" preset fetches actual data boundaries from /api/date-range
  and backfills the custom date picker
- Clicking "custom" opens popover without triggering data refresh;
  only confirm applies changes
- SQL trend dates cast to ::text to avoid pg driver Date timezone offset
- Fix created_at filter from < to <= for end timestamp
This commit is contained in:
2026-04-07 16:22:18 +08:00
parent 35b8fec96c
commit 13805a47be
5 changed files with 108 additions and 62 deletions

View File

@@ -8,18 +8,14 @@ import { useTimeRange } from "@/lib/time-range-context";
import { useI18n } from "@/lib/i18n";
export function TimeRangeSelector() {
const { t, locale } = useI18n();
const { t } = useI18n();
const { range, setRange, customStart, customEnd, setCustomStart, setCustomEnd } = useTimeRange();
const [showPopover, setShowPopover] = useState(false);
const [localStart, setLocalStart] = useState(customStart);
const [localEnd, setLocalEnd] = useState(customEnd);
const [localStart, setLocalStart] = useState("");
const [localEnd, setLocalEnd] = useState("");
const popoverRef = useRef<HTMLDivElement>(null);
const containerRef = useRef<HTMLDivElement>(null);
// Sync local state when context changes
useEffect(() => { setLocalStart(customStart); }, [customStart]);
useEffect(() => { setLocalEnd(customEnd); }, [customEnd]);
// Close popover on click outside
useEffect(() => {
if (!showPopover) return;
@@ -48,7 +44,9 @@ export function TimeRangeSelector() {
if (range === "custom" && showPopover) {
setShowPopover(false);
} else {
setRange("custom");
// customStart/customEnd already reflects the current preset's dates
setLocalStart(customStart);
setLocalEnd(customEnd);
setShowPopover(true);
}
}
@@ -56,6 +54,7 @@ export function TimeRangeSelector() {
function handleConfirm() {
setCustomStart(localStart);
setCustomEnd(localEnd);
setRange("custom");
setShowPopover(false);
}
@@ -126,7 +125,6 @@ export function TimeRangeSelector() {
</label>
<input
type="date"
lang={locale === "zh" ? "zh-CN" : "en-US"}
value={localStart}
onChange={(e) => setLocalStart(e.target.value)}
className="input-glass w-full rounded-md px-3 py-1.5 text-xs font-[family-name:var(--font-geist-mono)]"
@@ -138,7 +136,6 @@ export function TimeRangeSelector() {
</label>
<input
type="date"
lang={locale === "zh" ? "zh-CN" : "en-US"}
value={localEnd}
onChange={(e) => setLocalEnd(e.target.value)}
className="input-glass w-full rounded-md px-3 py-1.5 text-xs font-[family-name:var(--font-geist-mono)]"