import { useId, useState } from "react" import { Area, AreaChart, CartesianGrid, ResponsiveContainer, Tooltip, XAxis, YAxis, } from "recharts" import { STATUS_COLORS, formatTokens, type TimeBucket, } from "@/lib/usage-data" import { cn } from "@/lib/utils" import { ChartCard, ChartTooltip } from "./chart-primitives" type Metric = "requests" | "tokens" const METRICS: { key: Metric; label: string }[] = [ { key: "requests", label: "Requests" }, { key: "tokens", label: "Tokens" }, ] export function VolumeAreaChart({ buckets }: { buckets: TimeBucket[] }) { const [metric, setMetric] = useState("requests") const rawId = useId() const suffix = rawId.replace(/[^a-zA-Z0-9]/g, "") const data = buckets.map((bucket) => ({ label: bucket.label, success: metric === "requests" ? bucket.success : bucket.successTokens, error: metric === "requests" ? bucket.error : bucket.errorTokens, cancelled: metric === "requests" ? bucket.cancelled : bucket.cancelledTokens, })) const totals = data.reduce( (sum, row) => ({ success: sum.success + row.success, error: sum.error + row.error, cancelled: sum.cancelled + row.cancelled, }), { success: 0, error: 0, cancelled: 0 }, ) const formatter = (value: number, _name: string) => metric === "tokens" ? formatTokens(value) : String(value) return ( {METRICS.map((option) => ( ))} } >
{(["success", "error", "cancelled"] as const).map((status) => ( ))} } />
{( [ ["success", "Success"], ["error", "Error"], ["cancelled", "Cancelled"], ] as const ).map(([status, label]) => ( {label} {metric === "tokens" ? formatTokens(totals[status]) : totals[status]} ))}
) }