import React, { useEffect, useState, useRef } from 'react'; import { motion, useInView, useSpring, useTransform } from 'framer-motion'; import { useUserStore } from '@/store/userStore'; interface AnimatedCounterProps { value: number; label: string; prefix?: string; suffix?: string; delay?: number; } export const AnimatedCounter: React.FC = ({ value, label, prefix = '', suffix = '', delay = 0, }) => { const ref = useRef(null); const isInView = useInView(ref, { once: true, margin: "-50px" }); const { isDark } = useUserStore(); const [hasAnimated, setHasAnimated] = useState(false); const spring = useSpring(0, { stiffness: 50, damping: 20, mass: 1, }); const display = useTransform(spring, (current) => { return Math.round(current).toLocaleString(); }); useEffect(() => { if (isInView && !hasAnimated) { setTimeout(() => { spring.set(value); setHasAnimated(true); }, delay * 1000); } }, [isInView, spring, value, delay, hasAnimated]); const textColor = isDark ? 'text-white' : 'text-gray-900'; const labelColor = isDark ? 'text-gray-400' : 'text-gray-500'; return (
{prefix} {display} {suffix}
{label}
); };