"use client"; import { useEffect } from "react"; import { Pause, Play } from "lucide-react"; import { motion } from "motion/react"; import { Button } from "~/components/ui/button"; import { cn } from "~/lib/utils"; interface TimersProps { whiteTime: number; // in seconds blackTime: number; // in seconds currentTurn: "w" | "b"; running: boolean; onTimeUpdate: (color: "w" | "b", time: number) => void; onTimeExpired: (color: "w" | "b") => void; onTogglePause: () => void; className?: string; } /** * Chess clock component with countdown timers */ const Timers: React.FC = ({ whiteTime, blackTime, currentTurn, running, onTimeUpdate, onTimeExpired, onTogglePause, className, }) => { useEffect(() => { if (!running) return; const interval = setInterval(() => { if (currentTurn === "w") { onTimeUpdate("w", Math.max(0, whiteTime - 1)); if (whiteTime <= 1) { onTimeExpired("w"); } } else { onTimeUpdate("b", Math.max(0, blackTime - 1)); if (blackTime <= 1) { onTimeExpired("b"); } } }, 1000); return () => clearInterval(interval); }, [running, currentTurn, onTimeUpdate, onTimeExpired, whiteTime, blackTime]); const formatTime = (seconds: number): string => { const mins = Math.floor(seconds / 60); const secs = seconds % 60; return `${mins}:${secs.toString().padStart(2, "0")}`; }; const getTimeColor = (time: number, isActive: boolean): string => { if (!isActive) return "text-muted-foreground"; if (time <= 10) return "text-red-500"; if (time <= 30) return "text-orange-500"; return "text-foreground"; }; return (
{/* Black timer */}
Black
{formatTime(blackTime)} {/* Pause/Play button */}
{/* White timer */}
White
{formatTime(whiteTime)}
); }; export { Timers };