import { useState, useEffect, useRef } from 'react'; import type { TimerManager } from './types'; export function useTimer(): TimerManager { const [timeValue, setTimeValue] = useState(0); const intervalRef = useRef(null); const startTimeRef = useRef(0); const startTimer = () => { if (intervalRef.current) return; startTimeRef.current = Date.now() - timeValue; intervalRef.current = setInterval(() => { setTimeValue(Date.now() - startTimeRef.current); }, 100); }; const stopTimer = () => { if (intervalRef.current) { clearInterval(intervalRef.current); intervalRef.current = null; } }; const resetTimer = () => { stopTimer(); setTimeValue(0); startTimeRef.current = 0; }; useEffect(() => { return () => { if (intervalRef.current) { clearInterval(intervalRef.current); } }; }, []); return { timeValue, startTimer, stopTimer, resetTimer }; }