From 4ac87e478d29c0c8763066ca69e3876704ad9f12 Mon Sep 17 00:00:00 2001 From: Manoah Tervoort <149895ja@gmail.com> Date: Mon, 28 Oct 2024 14:43:14 +0100 Subject: [PATCH] fix: safely parse migration values --- docs/utils/general.ts | 10 +++++ src/stores/app-settings.ts | 81 ++++++++++++++++++++++++-------------- 2 files changed, 62 insertions(+), 29 deletions(-) diff --git a/docs/utils/general.ts b/docs/utils/general.ts index a0ca0b7d54..82143d964f 100644 --- a/docs/utils/general.ts +++ b/docs/utils/general.ts @@ -1,2 +1,12 @@ export const camelToKebabCase = (str: string) => str.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`); + +export const parseJsonSafe = (json: null | string | T, fallback: T): T => { + if (!json) return fallback; + try { + return typeof json === 'string' ? (JSON.parse(json) as T) : json; + } catch (e) { + console.error(e); + return fallback; + } +}; diff --git a/src/stores/app-settings.ts b/src/stores/app-settings.ts index 3e02ee033d..40bdd24811 100644 --- a/src/stores/app-settings.ts +++ b/src/stores/app-settings.ts @@ -1,7 +1,8 @@ import type { OldAppConfig, ScreenPreferences } from 'src/types'; +import { parseJsonSafe } from 'app/docs/utils/general'; import { defineStore, storeToRefs } from 'pinia'; -import { LocalStorage, uid } from 'quasar'; +import { LocalStorage as QuasarStorage, uid } from 'quasar'; import { electronApi } from 'src/helpers/electron-api'; import { errorCatcher } from 'src/helpers/error-catcher'; import { @@ -43,37 +44,59 @@ export const useAppSettingsStore = defineStore('app-settings', { } } } else if (type === 'localStorageToPiniaPersist') { - congregations.value = LocalStorage.getItem('congregations') || {}; - LocalStorage.removeItem('congregations'); - jwStore.additionalMediaMaps.value = - LocalStorage.getItem('additionalMediaMaps') || {}; - LocalStorage.removeItem('additionalMediaMaps'); - jwStore.customDurations.value = - LocalStorage.getItem('customDurations') || {}; - LocalStorage.removeItem('customDurations'); - jwStore.jwLanguages.value = LocalStorage.getItem('jwLanguages') || { - list: [], - updated: new Date(1900, 0, 1), - }; - LocalStorage.removeItem('jwLanguages'); - jwStore.jwSongs.value = LocalStorage.getItem('jwSongs') || {}; - LocalStorage.removeItem('jwSongs'); - jwStore.lookupPeriod.value = - LocalStorage.getItem('lookupPeriod') || {}; - LocalStorage.removeItem('lookupPeriod'); - jwStore.mediaSort.value = LocalStorage.getItem('mediaSort') || {}; - LocalStorage.removeItem('mediaSort'); - jwStore.yeartexts.value = LocalStorage.getItem('yeartexts') || {}; - LocalStorage.removeItem('yeartexts'); + congregations.value = parseJsonSafe( + QuasarStorage.getItem('congregations'), + {}, + ); + QuasarStorage.removeItem('congregations'); + jwStore.additionalMediaMaps.value = parseJsonSafe( + QuasarStorage.getItem('additionalMediaMaps'), + {}, + ); + QuasarStorage.removeItem('additionalMediaMaps'); + jwStore.customDurations.value = parseJsonSafe( + QuasarStorage.getItem('customDurations'), + {}, + ); + QuasarStorage.removeItem('customDurations'); + jwStore.jwLanguages.value = parseJsonSafe( + QuasarStorage.getItem('jwLanguages'), + { + list: [], + updated: new Date(1900, 0, 1), + }, + ); + QuasarStorage.removeItem('jwLanguages'); + jwStore.jwSongs.value = parseJsonSafe( + QuasarStorage.getItem('jwSongs'), + {}, + ); + QuasarStorage.removeItem('jwSongs'); + jwStore.lookupPeriod.value = parseJsonSafe( + QuasarStorage.getItem('lookupPeriod'), + {}, + ); + QuasarStorage.removeItem('lookupPeriod'); + jwStore.mediaSort.value = parseJsonSafe( + QuasarStorage.getItem('mediaSort'), + {}, + ); + QuasarStorage.removeItem('mediaSort'); + jwStore.yeartexts.value = parseJsonSafe( + QuasarStorage.getItem('yeartexts'), + {}, + ); + QuasarStorage.removeItem('yeartexts'); this.migrations = this.migrations.concat( - LocalStorage.getItem('migrations') || [], + parseJsonSafe(QuasarStorage.getItem('migrations'), []), ); - LocalStorage.removeItem('migrations'); + QuasarStorage.removeItem('migrations'); // this.migrations.push('firstRun'); - this.screenPreferences = LocalStorage.getItem( - 'screenPreferences', - ) || { preferredScreenNumber: 0, preferWindowed: false }; - LocalStorage.removeItem('screenPreferences'); + this.screenPreferences = parseJsonSafe( + QuasarStorage.getItem('screenPreferences'), + { preferredScreenNumber: 0, preferWindowed: false }, + ); + QuasarStorage.removeItem('screenPreferences'); } else { // other migrations will go here }