All files / repo/src/hooks useGameStateStorage.ts

100% Statements 27/27
42.85% Branches 3/7
100% Functions 3/3
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33  1x   1x   1x 4x   4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 1x 1x   4x 4x 4x 4x 4x 4x 4x  
import type { GameState } from '@/components/GameCanvas/GameCanvas.types';
import { useLocalStorage } from './useLocalStorage';
 
const STORAGE_KEY = 'memory-game-state';
 
export function useGameStateStorage() {
  const { data: gameState, clear } = useLocalStorage<GameState | null>(STORAGE_KEY, null);
 
  function save(state: GameState) {
    gameState.value = {
      dateStart: state.dateStart ?? new Date().toISOString(),
      tiles: state.tiles,
      elapsedSeconds: state.elapsedSeconds,
      moves: state.moves,
      seed: state.seed,
      difficulty: state.difficulty,
      matchedPairs: state?.matchedPairs ?? [],
      matchCount: state?.matchCount ?? 0,
      mismatchCount: state?.mismatchCount ?? 0,
    };
  }
  function load(): GameState | null {
    return gameState.value;
  }
 
  return {
    gameState,
    save,
    load,
    clear,
  };
}