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
This commit is contained in:
Hördle Bot
2025-12-01 23:58:11 +01:00
parent 3eb6c7f5cf
commit 9eb07ee8d5

View File

@@ -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