'use client'; import { useEffect, useState } from 'react'; import { motion } from 'framer-motion'; interface CooldownTimerProps { isActive: boolean; duration: number; // in seconds onComplete: () => void; } export function CooldownTimer({ isActive, duration, onComplete }: CooldownTimerProps) { const [timeLeft, setTimeLeft] = useState(0); useEffect(() => { if (isActive) { setTimeLeft(duration); const interval = setInterval(() => { setTimeLeft((prev) => { if (prev <= 1) { clearInterval(interval); // Use setTimeout to avoid setState during render setTimeout(() => onComplete(), 0); return 0; } return prev - 1; }); }, 1000); return () => clearInterval(interval); } }, [isActive, duration, onComplete]); if (!isActive || timeLeft === 0) return null; const progress = ((duration - timeLeft) / duration) * 100; return (
{timeLeft}s
); }