"use client"; import { useMemo, useRef, useState } from "react"; // --------------------------------------------------------------------------- // A run's spend, step by step, CUMULATIVE (canvas "Transparent run", the // `.spark`): one series, so no legend -- the heading names it. A step line in // Meter (measurement only), 2 px; an 8 px marker per step; the lit step's // marker in Signal (the interaction colour: it is the step the list has lit); // the endpoint labelled with the total. Recessive grid: three hairlines with // their dollar labels. // // The hover layer: a crosshair snaps to the nearest step and a tooltip gives // the total (strong) and the step (secondary). Its hit target is the step's // whole column, not the dot. The same values are in the step list on the // left (its right column), so nothing here is reachable by hover only. A // click on a column lights that step (onPick). // --------------------------------------------------------------------------- export type SpendPoint = { id: string; t: string; label: string; usd: number }; const W = 640; const H = 130; const L = 44; const R = 64; const T = 10; const B = 24; const money = (v: number) => `$${v.toFixed(2)}`; export default function SpendChart({ points, lit, onPick }: { points: SpendPoint[]; lit: string | null; onPick?: (id: string) => void }) { const [hover, setHover] = useState(null); const ref = useRef(null); const max = useMemo(() => { const m = Math.max(0.01, ...points.map((p) => p.usd)); // A round top: the next 1, 2 or 5 step above the total. const mag = 10 ** Math.floor(Math.log10(m)); const step = [1, 2, 2.5, 5, 10].map((k) => k * mag).find((s) => s >= m) ?? m; return step; }, [points]); if (!points.length) return

No spend recorded yet.

; const n = points.length; const x = (i: number) => L + (n === 1 ? (W - L - R) / 2 : (i * (W - L - R)) / (n - 1)); const y = (v: number) => T + (1 - v / max) * (H - T - B); let d = `M${x(0)},${y(points[0]!.usd)}`; points.forEach((p, i) => { if (i) d += ` H${x(i)} V${y(p.usd)}`; }); const ticks = [0, max / 2, max]; // Thin the x labels so they never collide (about 34 px each). const every = Math.max(1, Math.ceil(n / Math.floor((W - L - R) / 34))); const litIndex = points.findIndex((p) => p.id === lit); const last = points[n - 1]!; const colW = n === 1 ? W - L - R : (W - L - R) / (n - 1); const shown = hover ?? null; const tip = shown !== null ? points[shown]! : null; return (
setHover(null)} > {ticks.map((v) => ( {money(v)} ))} {points.map((p, i) => i % every === 0 || i === n - 1 ? ( {p.t} ) : null, )} {tip && } {points.map((p, i) => ( ))} {money(last.usd)} {/* The hit layer: each step's whole column. */} {points.map((p, i) => ( setHover(i)} onPointerMove={() => setHover(i)} onClick={() => onPick?.(p.id)} /> ))} {tip && (
{money(tip.usd)} so far ยท {tip.t} {tip.label}
)}
); }