import type { PointerEvent } from 'react'; import { useEffect, useId, useRef, useState } from 'react'; import { prefersReducedMotion, ringgit, shortRinggit, } from '@/components/overview/format'; import type { OverviewStats } from '@/types'; const LEFT = 56; const RIGHT = 12; const TOP = 14; const BOTTOM = 30; /** A round step for the side labels, so they read RM 0, 200, 400… */ function niceStep(max: number): number { const raw = Math.max(max, 1) / 4; const power = 10 ** Math.floor(Math.log10(raw)); return ( [1, 2, 2.5, 5, 10].map((f) => f * power).find((s) => s >= raw) ?? raw ); } /** * Sales over the chosen range, with the period before as a dashed line. Drawn at the real pixel * width of its box, so the labels stay readable on a phone. Point at it to see each day. */ export default function SalesChart({ stats }: { stats: OverviewStats }) { const box = useRef(null); const line = useRef(null); const gradient = useId(); const [width, setWidth] = useState(640); const [hover, setHover] = useState(null); useEffect(() => { const element = box.current; if (!element) { return; } const observer = new ResizeObserver(([entry]) => setWidth(Math.max(280, Math.round(entry.contentRect.width))), ); observer.observe(element); return () => observer.disconnect(); }, []); const points = stats.series; const count = points.length; const height = width < 500 ? 210 : 260; // The line draws itself when the data changes. useEffect(() => { const path = line.current; if (!path || prefersReducedMotion()) { return; } const length = path.getTotalLength(); path.style.transition = 'none'; path.style.strokeDasharray = `${length}`; path.style.strokeDashoffset = `${length}`; path.getBoundingClientRect(); path.style.transition = 'stroke-dashoffset 1.1s cubic-bezier(.2,.8,.2,1)'; path.style.strokeDashoffset = '0'; }, [stats.range, stats.sales, count]); const top = Math.max( ...points.map((p) => Math.max(p.sales, p.previous)), 0, ); const step = niceStep(top); const yMax = step * 4; const x = (i: number) => LEFT + (count <= 1 ? 0 : (i / (count - 1)) * (width - LEFT - RIGHT)); const y = (value: number) => TOP + (1 - value / yMax) * (height - TOP - BOTTOM); const path = (key: 'sales' | 'previous') => points .map( (p, i) => `${i ? 'L' : 'M'}${x(i).toFixed(1)},${y(p[key]).toFixed(1)}`, ) .join(''); // Side labels must not crowd: keep about 72px between bottom labels. const pxPer = (width - LEFT - RIGHT) / Math.max(1, count - 1); const every = Math.max(1, Math.ceil(72 / pxPer)); const last = count - 1; const onMove = (event: PointerEvent) => { const rect = event.currentTarget.ownerSVGElement!.getBoundingClientRect(); const sx = ((event.clientX - rect.left) / rect.width) * width; setHover( Math.max( 0, Math.min( last, Math.round(((sx - LEFT) / (width - LEFT - RIGHT)) * last), ), ), ); }; const hovered = hover !== null ? points[hover] : null; return (
{[0, 1, 2, 3, 4].map((k) => ( {shortRinggit(step * k)} ))} {points.map((p, i) => (i % every === 0 && (last - i) * pxPer >= 72) || i === last ? ( {p.label} ) : null, )} {hovered && hover !== null && ( <> )} setHover(null)} /> {hovered && hover !== null && (
{hovered.label}
{ringgit(hovered.sales, hovered.sales % 1 !== 0)} {' '} before:{' '} {ringgit(hovered.previous, hovered.previous % 1 !== 0)}
)}
); }