Add migration
All checks were successful
Build & Test / build-run (push) Successful in 42s

This commit is contained in:
CenTdemeern1 2025-02-11 22:11:56 +01:00
parent f0617522e3
commit 5e3817c1a7
2 changed files with 47 additions and 5 deletions

37
static/data_migration.mts Normal file
View file

@ -0,0 +1,37 @@
import { LocalStorage } from "./storage_manager.mjs"
type InstanceV0 = {
name: string,
origin: string,
software: string,
iconURL?: string,
preferredFor?: string[],
}
type LocalStorageV0 = {
version: undefined,
instances: InstanceV0[],
}
type LocalStorageV1 = LocalStorage;
function migrate0to1(s: LocalStorageV0): LocalStorageV1 {
return {
version: 1,
instances: s.instances.map(i => ({
preferredFor: i.preferredFor ?? [],
...i
}))
};
}
type AnyLocalStorage = LocalStorageV0 | LocalStorageV1;
export default function migrate(storage: AnyLocalStorage): LocalStorage {
switch (storage.version) {
case undefined:
storage = migrate0to1(storage);
case 1:
default:
return storage;
}
}

View file

@ -1,3 +1,5 @@
import migrate from "./data_migration.mjs";
export type Instance = { export type Instance = {
/** /**
* The instance's (nick)name * The instance's (nick)name
@ -21,18 +23,19 @@ export type Instance = {
software: string, software: string,
/** /**
* The instance's icon URL * The instance's icon URL
* * @example undefined
* Make sure to sanitize this! Could lead to XSS * @example "https://void.lgbt/favicon.png"
*/ */
iconURL?: string, iconURL?: string,
/** /**
* The list of software names and groups the user prefers to autoredirect to this instance * The list of software names and groups the user prefers to autoredirect to this instance
* @example ["sharkey", "misskey-compliant"] * @example ["sharkey", "misskey-compliant"]
*/ */
preferredFor?: string[], preferredFor: string[],
} }
type LocalStorage = { export type LocalStorage = {
version: number,
instances: Instance[], instances: Instance[],
} }
@ -46,12 +49,14 @@ export default new class StorageManager {
default(): LocalStorage { default(): LocalStorage {
return { return {
version: 1,
instances: [] instances: []
} }
} }
load() { load() {
this.storage = JSON.parse(window.localStorage.getItem("storage") ?? "null") ?? this.default(); const data = JSON.parse(window.localStorage.getItem("storage") ?? "null") ?? this.default();
this.storage = migrate(data);
} }
save() { save() {