web/settings: add version 3 of setting schema

This commit is contained in:
jj 2024-10-30 12:12:51 +00:00
parent ec10019bfa
commit cafe05d5fb
No known key found for this signature in database
4 changed files with 33 additions and 7 deletions

View file

@ -2,7 +2,7 @@ import { defaultLocale } from "$lib/i18n/translations";
import type { CobaltSettings } from "$lib/types/settings"; import type { CobaltSettings } from "$lib/types/settings";
const defaultSettings: CobaltSettings = { const defaultSettings: CobaltSettings = {
schemaVersion: 2, schemaVersion: 3,
advanced: { advanced: {
debug: false, debug: false,
}, },

View file

@ -4,11 +4,13 @@ import { merge } from 'ts-deepmerge';
import type { import type {
PartialSettings, PartialSettings,
AllPartialSettingsWithSchema, AllPartialSettingsWithSchema,
CobaltSettings CobaltSettings,
CobaltSettingsV3
} from '../types/settings'; } from '../types/settings';
import { getBrowserLanguage } from '$lib/settings/youtube-lang';
import { migrateOldSettings } from '../settings/migrate'; import { migrateOldSettings } from '../settings/migrate';
import defaultSettings from '../settings/defaults'; import defaultSettings from '../settings/defaults';
import type { RecursivePartial } from '$lib/types/generic';
const updatePlausiblePreference = (settings: PartialSettings) => { const updatePlausiblePreference = (settings: PartialSettings) => {
if (settings.privacy?.disableAnalytics) { if (settings.privacy?.disableAnalytics) {
@ -29,7 +31,20 @@ const writeToStorage = (settings: PartialSettings) => {
type Migrator = (s: AllPartialSettingsWithSchema) => AllPartialSettingsWithSchema; type Migrator = (s: AllPartialSettingsWithSchema) => AllPartialSettingsWithSchema;
const migrations: Record<number, Migrator> = { const migrations: Record<number, Migrator> = {
[3]: (settings: AllPartialSettingsWithSchema) => {
const out = settings as RecursivePartial<CobaltSettingsV3>;
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 => { const migrate = (settings: AllPartialSettingsWithSchema): PartialSettings => {
@ -65,7 +80,7 @@ export const loadFromString = (settings: string): PartialSettings => {
return migrate(parsed); return migrate(parsed);
} }
return parsed; return parsed as PartialSettings;
} }
let update: (_: Updater<PartialSettings>) => void; let update: (_: Updater<PartialSettings>) => void;
@ -106,4 +121,4 @@ export function resetSettings() {
export default derived( export default derived(
storedSettings, storedSettings,
$settings => mergeWithDefaults($settings) $settings => mergeWithDefaults($settings)
); );

View file

@ -1,11 +1,13 @@
import type { RecursivePartial } from "$lib/types/generic"; import type { RecursivePartial } from "$lib/types/generic";
import type { CobaltSettingsV2 } from "./settings/v2"; import type { CobaltSettingsV2 } from "./settings/v2";
import type { CobaltSettingsV3 } from "./settings/v3";
export * from "./settings/v2"; 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<CobaltSettings>; export type PartialSettings = RecursivePartial<CobaltSettings>;

View file

@ -0,0 +1,9 @@
import type { YoutubeLang } from "$lib/settings/youtube-lang";
import { type CobaltSettingsV2 } from "./v2";
export type CobaltSettingsV3 = Omit<CobaltSettingsV2, 'schemaVersion' | 'save'> & {
schemaVersion: 3,
save: Omit<CobaltSettingsV2['save'], 'youtubeDubBrowserLang'> & {
youtubeDubLang: YoutubeLang;
};
};