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

View File

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