From 57734822eab706e3439aa830a1f63954bcfff4ef Mon Sep 17 00:00:00 2001 From: wukko Date: Sun, 24 Nov 2024 00:23:06 +0600 Subject: [PATCH] web/settings/migrate: refactor, migrate to v4 schema why the fuck was tab 2 spaces here --- web/src/lib/settings/migrate.ts | 66 ++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/web/src/lib/settings/migrate.ts b/web/src/lib/settings/migrate.ts index e452b6f8..4054a0b4 100644 --- a/web/src/lib/settings/migrate.ts +++ b/web/src/lib/settings/migrate.ts @@ -1,38 +1,52 @@ import type { RecursivePartial } from "$lib/types/generic"; import type { - AllPartialSettingsWithSchema, - CobaltSettingsV3, - PartialSettings, + AllPartialSettingsWithSchema, + CobaltSettingsV3, + CobaltSettingsV4, + PartialSettings, } from "$lib/types/settings"; import { getBrowserLanguage } from "$lib/settings/youtube-lang"; -type Migrator = ( - s: AllPartialSettingsWithSchema -) => AllPartialSettingsWithSchema; +type Migrator = (s: AllPartialSettingsWithSchema) => AllPartialSettingsWithSchema; + const migrations: Record = { - [3]: (settings: AllPartialSettingsWithSchema) => { - const out = settings as RecursivePartial; - out.schemaVersion = 3; + [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(); - } + if (settings?.save && "youtubeDubBrowserLang" in settings.save) { + if (settings.save.youtubeDubBrowserLang) { + out.save!.youtubeDubLang = getBrowserLanguage(); + } - delete settings.save.youtubeDubBrowserLang; - } + delete settings.save.youtubeDubBrowserLang; + } - return out as AllPartialSettingsWithSchema; - }, + return out as AllPartialSettingsWithSchema; + }, + + [4]: (settings: AllPartialSettingsWithSchema) => { + const out = settings as RecursivePartial; + out.schemaVersion = 4; + + if (settings?.processing) { + if ("allowDefaultOverride" in settings.processing) { + delete settings.processing.allowDefaultOverride; + } + if ("seenOverrideWarning" in settings.processing) { + delete settings.processing.seenOverrideWarning; + } + } + + return out as AllPartialSettingsWithSchema; + }, }; -export const migrate = ( - settings: AllPartialSettingsWithSchema -): PartialSettings => { - return Object.keys(migrations) - .map(Number) - .filter((version) => version > settings.schemaVersion) - .reduce((settings, migrationVersion) => { - return migrations[migrationVersion](settings); - }, settings as AllPartialSettingsWithSchema) as PartialSettings; +export const migrate = (settings: AllPartialSettingsWithSchema): PartialSettings => { + return Object.keys(migrations) + .map(Number) + .filter((version) => version > settings.schemaVersion) + .reduce((settings, migrationVersion) => { + return migrations[migrationVersion](settings); + }, settings as AllPartialSettingsWithSchema) as PartialSettings; };