web/settings: fix types, migrate old settings from v2

This commit is contained in:
jj 2024-10-30 12:11:21 +00:00
parent bad59750bf
commit ec10019bfa
No known key found for this signature in database

View file

@ -1,11 +1,10 @@
import { derived, readable, type Updater } from 'svelte/store'; import { derived, readable, type Updater } from 'svelte/store';
import { browser } from '$app/environment'; import { browser } from '$app/environment';
import { merge } from 'ts-deepmerge'; import { merge } from 'ts-deepmerge';
import type { import type {
CobaltSettings,
PartialSettings, PartialSettings,
AllPartialSettingsWithSchema AllPartialSettingsWithSchema,
CobaltSettings
} from '../types/settings'; } from '../types/settings';
import { migrateOldSettings } from '../settings/migrate'; import { migrateOldSettings } from '../settings/migrate';
@ -39,7 +38,7 @@ const migrate = (settings: AllPartialSettingsWithSchema): PartialSettings => {
.filter(version => version > settings.schemaVersion) .filter(version => version > settings.schemaVersion)
.reduce((settings, migrationVersion) => { .reduce((settings, migrationVersion) => {
return migrations[migrationVersion](settings); return migrations[migrationVersion](settings);
}, settings as AllPartialSettingsWithSchema); }, settings as AllPartialSettingsWithSchema) as PartialSettings;
} }
@ -51,7 +50,7 @@ const loadFromStorage = () => {
if (!settings) { if (!settings) {
const migrated = migrateOldSettings(); const migrated = migrateOldSettings();
if (migrated) { if (migrated) {
return writeToStorage(migrated); return writeToStorage(migrate(migrated));
} }
return {}; return {};
@ -60,7 +59,7 @@ const loadFromStorage = () => {
return loadFromString(settings); return loadFromString(settings);
} }
export const loadFromString = (settings: string) => { export const loadFromString = (settings: string): PartialSettings => {
const parsed = JSON.parse(settings) as AllPartialSettingsWithSchema; const parsed = JSON.parse(settings) as AllPartialSettingsWithSchema;
if (parsed.schemaVersion < defaultSettings.schemaVersion) { if (parsed.schemaVersion < defaultSettings.schemaVersion) {
return migrate(parsed); return migrate(parsed);