feat: global time range context with custom date picker

Lift time range state into a shared React context so the selected
range persists across page navigation and browser refreshes
(localStorage). Add a "Custom" option with a popover date picker
that lets users specify arbitrary start/end dates. All preset end
times now use endOf("day") (23:59:59) instead of the current moment.
This commit is contained in:
2026-04-07 14:49:58 +08:00
parent 004fd37622
commit 9bb36432ba
10 changed files with 280 additions and 55 deletions

View File

@@ -4,7 +4,8 @@ import { useEffect, useState, useCallback } from "react";
import { motion } from "motion/react";
import { Users, Calendar, Hash, Zap, ArrowUpDown, ArrowDown, ArrowUp } from "lucide-react";
import { TimeRangeSelector } from "@/components/TimeRangeSelector";
import { type TimeRange, getTimeRange, buildQuery, formatNumber, formatTokens } from "@/lib/utils";
import { buildQuery, formatNumber, formatTokens } from "@/lib/utils";
import { useTimeRange } from "@/lib/time-range-context";
import { useI18n } from "@/lib/i18n";
interface AggItem {
@@ -16,7 +17,7 @@ type SortKey = "total_tokens" | "calls" | "prompt_tokens" | "completion_tokens";
export default function AggregationPage() {
const { t } = useI18n();
const [range, setRange] = useState<TimeRange>("30d");
const { getEffectiveRange } = useTimeRange();
const [data, setData] = useState<AggItem[]>([]);
const [loading, setLoading] = useState(true);
const [sortKey, setSortKey] = useState<SortKey>("total_tokens");
@@ -24,11 +25,11 @@ export default function AggregationPage() {
const fetchData = useCallback(async () => {
setLoading(true);
const { start, end } = getTimeRange(range);
const { start, end } = getEffectiveRange();
const res = await fetch(buildQuery("/api/aggregation", { start, end }));
setData(await res.json());
setLoading(false);
}, [range]);
}, [getEffectiveRange]);
useEffect(() => { fetchData(); }, [fetchData]);
@@ -68,7 +69,7 @@ export default function AggregationPage() {
<Users className="h-5 w-5" style={{ color: "var(--accent)", opacity: 0.6 }} />
<h1 className="text-2xl font-bold gradient-text">{t("agg.title")}</h1>
</motion.div>
<TimeRangeSelector value={range} onChange={setRange} />
<TimeRangeSelector />
</div>
{!loading && data.length > 0 && (