feat: update daily character management and local storage handling
This commit is contained in:
@@ -22,20 +22,36 @@
|
||||
|
||||
// Load from localStorage on mount
|
||||
onMount(() => {
|
||||
const stored = localStorage.getItem('dailyCharacterHistory');
|
||||
if (stored) {
|
||||
try {
|
||||
const storedIds = JSON.parse(stored);
|
||||
// Reconstruct character objects from IDs
|
||||
if (Array.isArray(storedIds)) {
|
||||
selectedCharacters = storedIds
|
||||
.map((id: string) => data.characters.find((c: any) => c.id === id))
|
||||
.filter((c: any) => c !== undefined);
|
||||
const storedDailyCharacterId = localStorage.getItem('currentDailyCharacterId');
|
||||
const currentDailyCharacterId = dailyCharacter?.id;
|
||||
|
||||
// If the daily character has changed, clear the history
|
||||
if (storedDailyCharacterId && storedDailyCharacterId !== currentDailyCharacterId) {
|
||||
localStorage.removeItem('dailyCharacterHistory');
|
||||
selectedCharacters = [];
|
||||
} else {
|
||||
// Load existing history if the character hasn't changed
|
||||
const stored = localStorage.getItem('dailyCharacterHistory');
|
||||
if (stored) {
|
||||
try {
|
||||
const storedIds = JSON.parse(stored);
|
||||
// Reconstruct character objects from IDs
|
||||
if (Array.isArray(storedIds)) {
|
||||
selectedCharacters = storedIds
|
||||
.map((id: string) => data.characters.find((c: any) => c.id === id))
|
||||
.filter((c: any) => c !== undefined);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to parse stored history', e);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to parse stored history', e);
|
||||
}
|
||||
}
|
||||
|
||||
// Store the current daily character ID
|
||||
if (currentDailyCharacterId) {
|
||||
localStorage.setItem('currentDailyCharacterId', currentDailyCharacterId);
|
||||
}
|
||||
|
||||
isLoaded = true;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user