import { useEffect, useState } from 'react' import type { Readiness } from '../catalog' function prefersReducedMotion() { try { return window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false } catch { return false } } /** Animate a number from 0 to its value once; static when reduced motion is requested. */ export function useCountUp(value: number, duration = 700) { const [display, setDisplay] = useState(() => (prefersReducedMotion() || typeof window.requestAnimationFrame !== 'function' ? value : 0)) useEffect(() => { if (prefersReducedMotion() || typeof window.requestAnimationFrame !== 'function') { setDisplay(value); return } let frame = 0 let synchronous = true const start = performance.now() const tick = () => { // Some environments run animation callbacks synchronously; settle immediately there. if (synchronous) { setDisplay(value); return } const progress = Math.max(0, Math.min(1, (performance.now() - start) / duration)) const eased = 1 - Math.pow(1 - progress, 3) setDisplay(Math.round(value * eased)) if (progress < 1) frame = window.requestAnimationFrame(tick) } frame = window.requestAnimationFrame(tick) synchronous = false return () => window.cancelAnimationFrame(frame) }, [value, duration]) return display } export function CountUp({ value }: { value: number }) { return <>{useCountUp(value)} } /** A compact health gauge: the arc follows the retained 0–100 score, coloured by readiness. */ export function HealthRing({ value, readiness, size = 40, label = true }: { value: number | null; readiness: Readiness; size?: number; label?: boolean }) { const stroke = size >= 64 ? 5 : 3.5 const radius = (size - stroke) / 2 const circumference = 2 * Math.PI * radius const score = value == null ? 0 : Math.max(0, Math.min(100, value)) return ( {label && {value ?? '—'}} ) }