|
| 1 | +import { |
| 2 | + Bar, |
| 3 | + BarChart, |
| 4 | + ReferenceLine, |
| 5 | + ResponsiveContainer, |
| 6 | + Tooltip, |
| 7 | + YAxis, |
| 8 | + type TooltipProps, |
| 9 | +} from "recharts"; |
| 10 | +import { cn } from "~/utils/cn"; |
| 11 | +import { formatDateTime } from "./DateTime"; |
| 12 | +import { Header3 } from "./Headers"; |
| 13 | +import TooltipPortal from "./TooltipPortal"; |
| 14 | + |
| 15 | +type UsageDatum = { date: Date; count: number }; |
| 16 | + |
| 17 | +type UnitLabel = { singular: string; plural: string }; |
| 18 | + |
| 19 | +export type UsageSparklineProps = { |
| 20 | + /** Equal-width time buckets, oldest first. */ |
| 21 | + data?: number[]; |
| 22 | + /** Epoch ms of the first bucket's start. When omitted, the last bucket is anchored to now. */ |
| 23 | + bucketStartMs?: number; |
| 24 | + /** Width of each bucket in ms. Defaults to one hour. */ |
| 25 | + bucketIntervalMs?: number; |
| 26 | + /** Bar colour. Defaults to blue. */ |
| 27 | + color?: string; |
| 28 | + /** Unit shown in the tooltip (e.g. calls, tokens). */ |
| 29 | + unitLabel?: UnitLabel; |
| 30 | + /** Format the trailing total. Defaults to `toLocaleString`. */ |
| 31 | + formatTotal?: (total: number) => string; |
| 32 | + /** Class for the trailing total label. */ |
| 33 | + totalClassName?: string; |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Inline 24h sparkline for list rows. Renders a small bar chart plus a trailing |
| 38 | + * total, or an em-dash when there's no data. Shared by the prompts and models |
| 39 | + * lists — keep it presentational (the caller supplies the zero-filled buckets). |
| 40 | + */ |
| 41 | +export function UsageSparkline({ |
| 42 | + data, |
| 43 | + bucketStartMs, |
| 44 | + bucketIntervalMs, |
| 45 | + color = "#3B82F6", |
| 46 | + unitLabel = { singular: "call", plural: "calls" }, |
| 47 | + formatTotal, |
| 48 | + totalClassName = "text-blue-400", |
| 49 | +}: UsageSparklineProps) { |
| 50 | + if (!data || data.every((v) => v === 0)) { |
| 51 | + return <span className="text-text-dimmed">–</span>; |
| 52 | + } |
| 53 | + |
| 54 | + const total = data.reduce((a, b) => a + b, 0); |
| 55 | + const max = Math.max(...data); |
| 56 | + |
| 57 | + // Map each bucket to a dated point so the tooltip can show the window it |
| 58 | + // represents. Buckets are `intervalMs` wide; if the caller didn't pass the |
| 59 | + // first bucket's start, anchor the last bucket to now (hourly default). |
| 60 | + const intervalMs = bucketIntervalMs ?? 3600_000; |
| 61 | + const startMs = bucketStartMs ?? Date.now() - (data.length - 1) * intervalMs; |
| 62 | + const chartData: UsageDatum[] = data.map((count, i) => ({ |
| 63 | + date: new Date(startMs + i * intervalMs), |
| 64 | + count, |
| 65 | + })); |
| 66 | + |
| 67 | + return ( |
| 68 | + <div className="flex items-start gap-2"> |
| 69 | + <div className="h-6 w-[7rem] rounded-sm"> |
| 70 | + <ResponsiveContainer width="100%" height="100%"> |
| 71 | + <BarChart data={chartData} margin={{ top: 0, right: 0, left: 0, bottom: 0 }}> |
| 72 | + <YAxis domain={[0, max || 1]} hide /> |
| 73 | + <Tooltip |
| 74 | + cursor={{ fill: "rgba(255, 255, 255, 0.06)" }} |
| 75 | + content={<UsageSparklineTooltip unitLabel={unitLabel} />} |
| 76 | + allowEscapeViewBox={{ x: true, y: true }} |
| 77 | + wrapperStyle={{ zIndex: 1000 }} |
| 78 | + animationDuration={0} |
| 79 | + /> |
| 80 | + <Bar |
| 81 | + dataKey="count" |
| 82 | + fill={color} |
| 83 | + strokeWidth={0} |
| 84 | + isAnimationActive={false} |
| 85 | + minPointSize={1} |
| 86 | + /> |
| 87 | + <ReferenceLine y={0} stroke="#2C3034" strokeWidth={1} /> |
| 88 | + {max > 0 && ( |
| 89 | + <ReferenceLine y={max} stroke="#4D525B" strokeDasharray="4 4" strokeWidth={1} /> |
| 90 | + )} |
| 91 | + </BarChart> |
| 92 | + </ResponsiveContainer> |
| 93 | + </div> |
| 94 | + <span className={cn("-mt-1 text-xs tabular-nums", totalClassName)}> |
| 95 | + {formatTotal ? formatTotal(total) : total.toLocaleString()} |
| 96 | + </span> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +function UsageSparklineTooltip({ |
| 102 | + active, |
| 103 | + payload, |
| 104 | + unitLabel, |
| 105 | +}: TooltipProps<number, string> & { unitLabel: UnitLabel }) { |
| 106 | + if (!active || !payload || payload.length === 0) return null; |
| 107 | + const entry = payload[0].payload as UsageDatum; |
| 108 | + const date = entry.date instanceof Date ? entry.date : new Date(entry.date); |
| 109 | + const formattedDate = formatDateTime(date, "UTC", [], false, true); |
| 110 | + return ( |
| 111 | + <TooltipPortal active={active}> |
| 112 | + <div className="rounded-sm border border-grid-bright bg-background-dimmed px-3 py-2"> |
| 113 | + <Header3 className="border-b border-b-charcoal-650 pb-2">{formattedDate}</Header3> |
| 114 | + <div className="mt-2 text-xs text-text-bright"> |
| 115 | + <span className="tabular-nums">{entry.count.toLocaleString()}</span>{" "} |
| 116 | + <span className="text-text-dimmed"> |
| 117 | + {entry.count === 1 ? unitLabel.singular : unitLabel.plural} |
| 118 | + </span> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + </TooltipPortal> |
| 122 | + ); |
| 123 | +} |
0 commit comments