import React, { useEffect, useRef, useState } from 'react'; export default function StatsCounter({ value, label, duration = 1200, suffix = '', accent = false, decimals = 0 }) { const [display, setDisplay] = useState(0); const elRef = useRef(null); const startedRef = useRef(false); useEffect(() => { const el = elRef.current; if (!el) return; const onEnter = (entries) => { entries.forEach((entry) => { if (entry.isIntersecting && !startedRef.current) { startedRef.current = true; const start = performance.now(); const target = Number(value); const step = (t) => { const p = Math.min(1, (t - start) / duration); const raw = p * target; if (decimals > 0) { setDisplay(Number(raw.toFixed(decimals))); } else { setDisplay(Math.floor(raw)); } if (p < 1) requestAnimationFrame(step); }; requestAnimationFrame(step); } }); }; const obs = new IntersectionObserver(onEnter, { threshold: 0.4 }); obs.observe(el); return () => obs.disconnect(); }, [value, duration]); const formatNumber = (n) => { if (decimals > 0) { // Keep fixed decimals but still use locale for thousands if ever needed return Number(n).toLocaleString(undefined, { minimumFractionDigits: decimals, maximumFractionDigits: decimals }); } return Number(n).toLocaleString(); }; const counterContent = ( {formatNumber(display)}{suffix} ); return (