"use client"; import { useEffect, useState, useCallback } from "react"; import { motion } from "motion/react"; import { Zap, Hash, Users, Cpu, TrendingUp, BarChart3 } from "lucide-react"; import { StatsCard } from "@/components/StatsCard"; import { TimeRangeSelector } from "@/components/TimeRangeSelector"; import { TrendChart } from "@/components/charts/TrendChart"; import { RankingBar } from "@/components/charts/RankingBar"; import { buildQuery } from "@/lib/utils"; import { useTimeRange } from "@/lib/time-range-context"; import { useI18n } from "@/lib/i18n"; export default function DashboardPage() { const { t } = useI18n(); const { getEffectiveRange } = useTimeRange(); const [granularity, setGranularity] = useState<"day" | "week" | "month">("day"); const [trendMetric, setTrendMetric] = useState<"total_tokens" | "calls">("total_tokens"); const [overview, setOverview] = useState(null); const [trends, setTrends] = useState([]); const [userRank, setUserRank] = useState([]); const [modelRank, setModelRank] = useState([]); const [loading, setLoading] = useState(true); const fetchData = useCallback(async () => { setLoading(true); const { start, end } = getEffectiveRange(); const tp = { start, end }; const [ov, tr, ur, mr] = await Promise.all([ fetch(buildQuery("/api/overview", tp)).then(r => r.json()), fetch(buildQuery("/api/trends", { ...tp, granularity })).then(r => r.json()), fetch(buildQuery("/api/rankings", { ...tp, type: "user", limit: 10 })).then(r => r.json()), fetch(buildQuery("/api/rankings", { ...tp, type: "model", limit: 10 })).then(r => r.json()), ]); setOverview(ov); setTrends(tr); setUserRank(ur); setModelRank(mr); setLoading(false); }, [granularity, getEffectiveRange]); useEffect(() => { fetchData(); }, [fetchData]); const grans = [ { key: "day" as const, label: t("gran.day") }, { key: "week" as const, label: t("gran.week") }, { key: "month" as const, label: t("gran.month") }, ]; return (
{t("dash.title")}
{overview && (
)}

{t("dash.trend")}

{grans.map(g => ( ))}
{([["total_tokens", t("metric.token")], ["calls", t("metric.calls")]] as const).map(([k, l]) => ( ))}
{loading ? (
) : ( )}
); }