import { useEffect, useRef, useState } from 'react'; import { prefersReducedMotion } from '@/components/overview/format'; /** A number that counts up (or down) to its new value, so changes are easy to notice. */ export function useCountUp(target: number, duration = 900): number { const [value, setValue] = useState(() => prefersReducedMotion() ? target : 0, ); const from = useRef(value); useEffect(() => { if (prefersReducedMotion()) { from.current = target; setValue(target); return; } const start = performance.now(); const begin = from.current; let frame = 0; const step = (now: number) => { const progress = Math.min(1, (now - start) / duration); const eased = 1 - Math.pow(1 - progress, 3); const next = begin + (target - begin) * eased; from.current = next; setValue(next); if (progress < 1) { frame = requestAnimationFrame(step); } }; frame = requestAnimationFrame(step); return () => cancelAnimationFrame(frame); }, [target, duration]); return value; }