'use client'; import { useState, useEffect, useCallback } from 'react'; import { Play, Pause, RotateCcw, Check } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { cn } from '@/lib/utils'; interface StudyTimerProps { durationMinutes: number; isRunning: boolean; onComplete: () => void; onPause?: () => void; onResume?: () => void; className?: string; } export function StudyTimer({ durationMinutes, isRunning: initiallyRunning, onComplete, onPause, onResume, className, }: StudyTimerProps) { const totalSeconds = durationMinutes * 60; const [secondsRemaining, setSecondsRemaining] = useState(totalSeconds); const [isPaused, setIsPaused] = useState(!initiallyRunning); const progress = ((totalSeconds - secondsRemaining) / totalSeconds) * 100; const formatTime = (seconds: number) => { const mins = Math.floor(seconds / 60); const secs = seconds % 60; return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`; }; const handlePauseResume = useCallback(() => { if (isPaused) { setIsPaused(false); onResume?.(); } else { setIsPaused(true); onPause?.(); } }, [isPaused, onPause, onResume]); const handleReset = useCallback(() => { setSecondsRemaining(totalSeconds); setIsPaused(true); }, [totalSeconds]); useEffect(() => { if (isPaused || secondsRemaining <= 0) return; const interval = setInterval(() => { setSecondsRemaining((prev) => { if (prev <= 1) { clearInterval(interval); onComplete(); return 0; } return prev - 1; }); }, 1000); return () => clearInterval(interval); }, [isPaused, secondsRemaining, onComplete]); // Auto-start when initially running useEffect(() => { if (initiallyRunning) { setIsPaused(false); } }, [initiallyRunning]); const circumference = 2 * Math.PI * 45; // radius = 45 const strokeDashoffset = circumference * (1 - progress / 100); return (
{isPaused ? '⏸️ Paused' : '📚 Studying...'}