Enhance winner management with localStorage updates
Refactored winner storage logic by introducing `clearWinnerLS` and replacing outdated functions with `getWinnerLS` and `setWinnerLS`. Added a `WinnerBadge` component to display the winner's status and updated game lifecycle handling to ensure proper winner reset and management.
This commit is contained in:
28
frontend/src/components/WinnerBadge.jsx
Normal file
28
frontend/src/components/WinnerBadge.jsx
Normal file
@@ -0,0 +1,28 @@
|
||||
// frontend/src/utils/winnerStorage.js
|
||||
|
||||
function winnerKey(gameId) {
|
||||
return `winner:${gameId}`;
|
||||
}
|
||||
|
||||
export function getWinnerLS(gameId) {
|
||||
if (!gameId) return "";
|
||||
try {
|
||||
return localStorage.getItem(winnerKey(gameId)) || "";
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
export function setWinnerLS(gameId, name) {
|
||||
if (!gameId) return;
|
||||
try {
|
||||
localStorage.setItem(winnerKey(gameId), (name || "").trim());
|
||||
} catch {}
|
||||
}
|
||||
|
||||
export function clearWinnerLS(gameId) {
|
||||
if (!gameId) return;
|
||||
try {
|
||||
localStorage.removeItem(winnerKey(gameId));
|
||||
} catch {}
|
||||
}
|
||||
Reference in New Issue
Block a user