From cafe05d5fb923c2d32ca3a262f0092de293e8bcc Mon Sep 17 00:00:00 2001 From: jj Date: Wed, 30 Oct 2024 12:12:51 +0000 Subject: [PATCH] web/settings: add version 3 of setting schema --- web/src/lib/settings/defaults.ts | 2 +- web/src/lib/state/settings.ts | 23 +++++++++++++++++++---- web/src/lib/types/settings.ts | 6 ++++-- web/src/lib/types/settings/v3.ts | 9 +++++++++ 4 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 web/src/lib/types/settings/v3.ts diff --git a/web/src/lib/settings/defaults.ts b/web/src/lib/settings/defaults.ts index 49f44ac0..e629a306 100644 --- a/web/src/lib/settings/defaults.ts +++ b/web/src/lib/settings/defaults.ts @@ -2,7 +2,7 @@ import { defaultLocale } from "$lib/i18n/translations"; import type { CobaltSettings } from "$lib/types/settings"; const defaultSettings: CobaltSettings = { - schemaVersion: 2, + schemaVersion: 3, advanced: { debug: false, }, diff --git a/web/src/lib/state/settings.ts b/web/src/lib/state/settings.ts index 0b69b86e..2bb6702e 100644 --- a/web/src/lib/state/settings.ts +++ b/web/src/lib/state/settings.ts @@ -4,11 +4,13 @@ import { merge } from 'ts-deepmerge'; import type { PartialSettings, AllPartialSettingsWithSchema, - CobaltSettings + CobaltSettings, + CobaltSettingsV3 } from '../types/settings'; - +import { getBrowserLanguage } from '$lib/settings/youtube-lang'; import { migrateOldSettings } from '../settings/migrate'; import defaultSettings from '../settings/defaults'; +import type { RecursivePartial } from '$lib/types/generic'; const updatePlausiblePreference = (settings: PartialSettings) => { if (settings.privacy?.disableAnalytics) { @@ -29,7 +31,20 @@ const writeToStorage = (settings: PartialSettings) => { type Migrator = (s: AllPartialSettingsWithSchema) => AllPartialSettingsWithSchema; const migrations: Record = { + [3]: (settings: AllPartialSettingsWithSchema) => { + const out = settings as RecursivePartial; + out.schemaVersion = 3; + if (settings?.save && 'youtubeDubBrowserLang' in settings.save) { + if (settings.save.youtubeDubBrowserLang) { + out.save!.youtubeDubLang = getBrowserLanguage(); + } + + delete settings.save.youtubeDubBrowserLang; + } + + return out as AllPartialSettingsWithSchema; + } } const migrate = (settings: AllPartialSettingsWithSchema): PartialSettings => { @@ -65,7 +80,7 @@ export const loadFromString = (settings: string): PartialSettings => { return migrate(parsed); } - return parsed; + return parsed as PartialSettings; } let update: (_: Updater) => void; @@ -106,4 +121,4 @@ export function resetSettings() { export default derived( storedSettings, $settings => mergeWithDefaults($settings) -); \ No newline at end of file +); diff --git a/web/src/lib/types/settings.ts b/web/src/lib/types/settings.ts index 1a553c8a..fd9f31a5 100644 --- a/web/src/lib/types/settings.ts +++ b/web/src/lib/types/settings.ts @@ -1,11 +1,13 @@ import type { RecursivePartial } from "$lib/types/generic"; import type { CobaltSettingsV2 } from "./settings/v2"; +import type { CobaltSettingsV3 } from "./settings/v3"; export * from "./settings/v2"; +export * from "./settings/v3"; -export type CobaltSettings = CobaltSettingsV2; +export type CobaltSettings = CobaltSettingsV3; -export type AnyCobaltSettings = CobaltSettings; +export type AnyCobaltSettings = CobaltSettingsV2 | CobaltSettings; export type PartialSettings = RecursivePartial; diff --git a/web/src/lib/types/settings/v3.ts b/web/src/lib/types/settings/v3.ts new file mode 100644 index 00000000..fb376d8e --- /dev/null +++ b/web/src/lib/types/settings/v3.ts @@ -0,0 +1,9 @@ +import type { YoutubeLang } from "$lib/settings/youtube-lang"; +import { type CobaltSettingsV2 } from "./v2"; + +export type CobaltSettingsV3 = Omit & { + schemaVersion: 3, + save: Omit & { + youtubeDubLang: YoutubeLang; + }; +};