'use client'; import { useState, useEffect } from 'react'; export interface GameState { date: string; guesses: string[]; // Array of song titles or IDs guessed isSolved: boolean; isFailed: boolean; lastPlayed: number; // Timestamp } const STORAGE_KEY = 'hoerdle_game_state'; export function useGameState() { const [gameState, setGameState] = useState(null); useEffect(() => { const stored = localStorage.getItem(STORAGE_KEY); const today = new Date().toISOString().split('T')[0]; if (stored) { const parsed: GameState = JSON.parse(stored); if (parsed.date === today) { setGameState(parsed); return; } } // New day or no state const newState: GameState = { date: today, guesses: [], isSolved: false, isFailed: false, lastPlayed: Date.now(), }; setGameState(newState); localStorage.setItem(STORAGE_KEY, JSON.stringify(newState)); }, []); const saveState = (newState: GameState) => { setGameState(newState); localStorage.setItem(STORAGE_KEY, JSON.stringify(newState)); }; const addGuess = (guess: string, correct: boolean) => { if (!gameState) return; const newGuesses = [...gameState.guesses, guess]; const isSolved = correct; const isFailed = !correct && newGuesses.length >= 6; const newState = { ...gameState, guesses: newGuesses, isSolved, isFailed, lastPlayed: Date.now(), }; saveState(newState); }; return { gameState, addGuess }; }