Compare commits

...

2 Commits

Author SHA1 Message Date
Hördle Bot
8c57e938e8 chore: Bump version to 0.1.4.2 2025-12-01 23:59:22 +01:00
Hördle Bot
9eb07ee8d5 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
2025-12-01 23:58:11 +01:00
2 changed files with 15 additions and 8 deletions

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

View File

@@ -1,6 +1,6 @@
{
"name": "hoerdle",
"version": "0.1.4.1",
"version": "0.1.4.2",
"private": true,
"scripts": {
"dev": "next dev",