Procházet zdrojové kódy

fix: separate localStorage per language to prevent cross-language game leaks

Storage keys are now wordle-state-en and wordle-state-ru,
so switching languages doesn't carry over guesses or game
state from the other language.
Oleg Panashchenko před 1 týdnem
rodič
revize
f2db4f967b
1 změnil soubory, kde provedl 6 přidání a 3 odebrání
  1. 6 3
      client/src/hooks/usePersistence.ts

+ 6 - 3
client/src/hooks/usePersistence.ts

@@ -1,12 +1,15 @@
 import { useState, useCallback, useEffect } from 'react';
 import type { PersistedState } from '@wordle/shared';
 import { defaultPersistedState } from '@wordle/shared';
+import { detectLang } from '../messages/index.js';
 
-const STORAGE_KEY = 'wordle-state';
+function storageKey(): string {
+  return `wordle-state-${detectLang()}`;
+}
 
 function loadState(): PersistedState {
   try {
-    const raw = localStorage.getItem(STORAGE_KEY);
+    const raw = localStorage.getItem(storageKey());
     if (raw) {
       return JSON.parse(raw) as PersistedState;
     }
@@ -17,7 +20,7 @@ function loadState(): PersistedState {
 }
 
 function saveState(state: PersistedState): void {
-  localStorage.setItem(STORAGE_KEY, JSON.stringify(state));
+  localStorage.setItem(storageKey(), JSON.stringify(state));
 }
 
 export function usePersistence() {