Files
vim-sudoku/src_failed/useScore/useTimer.ts
T
pastilhas 9292b83dd0 feat: prepare vim-sudoku for unified API implementation
- 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).
2025-10-22 18:08:33 +01:00

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
};
}