From 9eb07ee8d5a20b2496a928290cc156637ad6fd6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=B6rdle=20Bot?= Date: Mon, 1 Dec 2025 23:58:11 +0100 Subject: [PATCH] refactor: Ensure genreKey is always recomputed before use - Recompute genreKey inside useEffect and save functions to ensure current values - Prevents potential closure issues with stale genreKey values - Improves code quality and prevents future bugs --- lib/gameState.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/gameState.ts b/lib/gameState.ts index 1a0cd42..7bd650e 100644 --- a/lib/gameState.ts +++ b/lib/gameState.ts @@ -69,10 +69,13 @@ export function useGameState( useEffect(() => { const today = getTodayISOString(); + // Always recompute genreKey to ensure it's current + const currentGenreKey = getGenreKey(isSpecial ? null : genre, isSpecial, isSpecial ? genre || undefined : undefined); + // Try to load from backend first const loadFromBackend = async () => { try { - const backendState = await loadPlayerState(genreKey); + const backendState = await loadPlayerState(currentGenreKey); if (backendState) { const { gameState: loadedState, statistics: loadedStats } = backendState; @@ -88,7 +91,7 @@ export function useGameState( setGameState(newState); setStatistics(loadedStats); // Keep statistics across days // Save new state to backend - await savePlayerState(genreKey, newState, loadedStats); + await savePlayerState(currentGenreKey, newState, loadedStats); return; } } else { @@ -99,7 +102,7 @@ export function useGameState( const newStats = createNewStatistics(); setStatistics(newStats); // Save to backend for cross-domain sync - await savePlayerState(genreKey, newState, newStats); + await savePlayerState(currentGenreKey, newState, newStats); return; } } catch (error) { @@ -113,7 +116,7 @@ export function useGameState( setStatistics(newStats); // Try to save to backend (may fail, but we try) try { - await savePlayerState(genreKey, newState, newStats); + await savePlayerState(currentGenreKey, newState, newStats); } catch (saveError) { console.error('[gameState] Failed to save new state to backend:', saveError); } @@ -121,7 +124,7 @@ export function useGameState( }; loadFromBackend(); - }, [genre, isSpecial, genreKey]); // Re-run when genre or isSpecial changes + }, [genre, isSpecial]); // Re-run when genre or isSpecial changes const saveState = async (newState: GameState) => { setGameState(newState); @@ -129,7 +132,9 @@ export function useGameState( // Save to backend only if (statistics) { try { - await savePlayerState(genreKey, newState, statistics); + // Always use the current genreKey (recompute it in case genre/isSpecial changed) + const currentGenreKey = getGenreKey(isSpecial ? null : genre, isSpecial, isSpecial ? genre || undefined : undefined); + await savePlayerState(currentGenreKey, newState, statistics); } catch (error) { console.error('[gameState] Failed to save to backend:', error); // No fallback - backend is required for cross-domain sync @@ -163,7 +168,9 @@ export function useGameState( // Save to backend only if (gameState) { try { - await savePlayerState(genreKey, gameState, newStats); + // Always use the current genreKey (recompute it in case genre/isSpecial changed) + const currentGenreKey = getGenreKey(isSpecial ? null : genre, isSpecial, isSpecial ? genre || undefined : undefined); + await savePlayerState(currentGenreKey, gameState, newStats); } catch (error) { console.error('[gameState] Failed to save statistics to backend:', error); // No fallback - backend is required for cross-domain sync