34 lines
755 B
TypeScript
34 lines
755 B
TypeScript
import { useEffect, useRef } from "react";
|
|
import type { TimerManager } from "./types";
|
|
import type { GameStatus } from "../useGameStatus/types";
|
|
|
|
type UseScoreTimeParams = {
|
|
gameStatus: GameStatus;
|
|
timer: TimerManager;
|
|
};
|
|
|
|
export function useScoreTime({ gameStatus, timer }: UseScoreTimeParams) {
|
|
const prevStatusRef = useRef<GameStatus | null>(null);
|
|
const { startTimer, stopTimer, resetTimer } = timer;
|
|
|
|
useEffect(() => {
|
|
prevStatusRef.current = gameStatus;
|
|
|
|
switch (gameStatus) {
|
|
case "started":
|
|
resetTimer();
|
|
startTimer();
|
|
break;
|
|
|
|
case "has-key":
|
|
break;
|
|
|
|
case "game-over":
|
|
case "game-won":
|
|
case "waiting":
|
|
stopTimer();
|
|
break;
|
|
}
|
|
}, [gameStatus]);
|
|
}
|