Fix bug where store was reset at every payload.

Remove additional unused localStorage keys.
This commit is contained in:
Fred Boniface 2025-10-14 21:56:52 +01:00
parent 1ce509ffa5
commit 475ab3b6de
2 changed files with 32 additions and 23 deletions

View File

@ -4,7 +4,7 @@ import { get } from "svelte/store";
export function CheckMigrationStatus() { export function CheckMigrationStatus() {
let currentStore = get(preferences); let currentStore = get(preferences);
if ( currentStore.format == 1 ) { if ( currentStore.format < 2 ) {
const hasOldKeys = const hasOldKeys =
localStorage.getItem('ql') !== null || localStorage.getItem('ql') !== null ||
localStorage.getItem('location') !== null || localStorage.getItem('location') !== null ||
@ -54,6 +54,7 @@ export function CheckMigrationStatus() {
// Migrations // Migrations
function migrateToV1() { function migrateToV1() {
console.log("Migrating to Preferences-v1");
const oldQlRaw = localStorage.getItem('ql'); const oldQlRaw = localStorage.getItem('ql');
const oldLocationRaw = localStorage.getItem('location'); const oldLocationRaw = localStorage.getItem('location');
const oldTelemetryRaw = localStorage.getItem('telemetry'); const oldTelemetryRaw = localStorage.getItem('telemetry');
@ -72,6 +73,8 @@ function migrateToV1() {
localStorage.removeItem('location'); localStorage.removeItem('location');
localStorage.removeItem('telemetry'); localStorage.removeItem('telemetry');
localStorage.removeItem('telemetryRequested'); localStorage.removeItem('telemetryRequested');
localStorage.removeItem('welcome');
localStorage.removeItem('version');
console.info('[Migration] Preferences migrated to version 1'); console.info('[Migration] Preferences migrated to version 1');
} }

View File

@ -22,27 +22,30 @@ const DEFAULT_PREFERENCES: Preferences = {
}; };
function createPreferencesStore() { function createPreferencesStore() {
let stored: Preferences; const { subscribe, set, update } = writable<Preferences>(DEFAULT_PREFERENCES);
try { if (typeof window !== 'undefined') {
const json = localStorage.getItem(STORAGE_KEY); let stored: Preferences;
if (json) { try {
stored = JSON.parse(json); const json = localStorage.getItem(STORAGE_KEY);
// Convert telemetryRequested back to Date if it exists if (json) {
stored.telemetryRequested = stored.telemetryRequested stored = JSON.parse(json);
? new Date(stored.telemetryRequested) // Convert telemetryRequested back to Date if it exists
: null; stored.telemetryRequested = stored.telemetryRequested
} else { ? new Date(stored.telemetryRequested)
: null;
} else {
stored = DEFAULT_PREFERENCES;
}
} catch {
stored = DEFAULT_PREFERENCES; stored = DEFAULT_PREFERENCES;
} }
} catch {
stored = DEFAULT_PREFERENCES;
}
const { subscribe, set, update } = writable<Preferences>(stored); set(stored);
subscribe((value) => { subscribe((value) => {
localStorage.setItem(STORAGE_KEY, JSON.stringify(value)); localStorage.setItem(STORAGE_KEY, JSON.stringify(value));
}); });
};
return { return {
subscribe, subscribe,
@ -53,11 +56,14 @@ function createPreferencesStore() {
} }
export const preferences = createPreferencesStore(); export const preferences = createPreferencesStore();
try {
CheckMigrationStatus(); if (typeof window !== 'undefined') {
} catch (e) { try {
console.error("Preferences migration failed, resetting store", e); CheckMigrationStatus();
preferences.reset(); } catch (e) {
console.error("Preferences migration failed, resetting store", e);
preferences.reset();
}
} }
// Utility Functions // Utility Functions