"use client"; import { Area, AreaChart, ResponsiveContainer, type TooltipContentProps, Tooltip, YAxis, } from "recharts"; import { useId } from "react"; import { cn } from "@/lib/utils"; type MetricSparklineProps = { values: number[]; bucket?: string; labels?: string[]; className?: string; }; const buildSparkData = (values: number[]) => values.map((value, index) => ({ index, value, })); const formatSparkValue = (value: number) => { if (!Number.isFinite(value)) { return "--"; } const rounded = Math.round(value * 10) / 10; return Number.isInteger(rounded) ? rounded.toString() : rounded.toFixed(1); }; const SparklineTooltip = ({ active, payload, bucket, labels, }: TooltipContentProps & { bucket?: string; labels?: string[]; }) => { if (!active || !payload?.length) { return null; } const entry = payload[0]; const rawValue = entry?.value; if (typeof rawValue !== "number") { return null; } const dayIndex = typeof entry.payload?.index === "number" ? entry.payload.index + 1 : null; const labelIndex = typeof entry.payload?.index === "number" ? entry.payload.index : null; const resolvedLabel = labelIndex !== null ? labels?.[labelIndex] : undefined; const label = bucket === "week" ? "Week" : bucket === "month" ? "Month" : bucket === "year" ? "Year" : "Day"; const prefix = resolvedLabel ?? (dayIndex ? `${label} ${dayIndex}` : ""); return (
{prefix ? `${prefix}: ` : ""} {formatSparkValue(rawValue)}
); }; export default function MetricSparkline({ values, bucket, labels, className, }: MetricSparklineProps) { const gradientId = useId(); if (!values.length) { return null; } const data = buildSparkData(values); const strokeColor = "#60a5fa"; const fillColor = "#bfdbfe"; return (
cursor={false} content={(props) => ( )} />
); }