- Archive previous implementation attempt (src -> src_failed) - Create comprehensive IMPLEMENTATION_PLAN.md with phased approach - Create IMPLEMENTATION.md to track implementation progress - Update example app to match vim-maze patterns: - Simplify UI to desktop-only (no mobile support) - Add score metrics display (time, keystrokes, final score) - Update instructions for sudoku-specific keys (hjkl, i for edit) - Remove unnecessary mobile hooks Phase 5 complete. Ready to begin Phase 10 (Setup & Core Types).
45 lines
1013 B
TypeScript
45 lines
1013 B
TypeScript
import { useState, useEffect, useRef } from 'react';
|
|
import type { TimerManager } from './types';
|
|
|
|
export function useTimer(): TimerManager {
|
|
const [timeValue, setTimeValue] = useState<number>(0);
|
|
const intervalRef = useRef<NodeJS.Timeout | null>(null);
|
|
const startTimeRef = useRef<number>(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
|
|
};
|
|
} |