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 = {
/**
* The instance's (nick)name
@ -21,18 +23,19 @@ export type Instance = {
software: string,
/**
* The instance's icon URL
*
* Make sure to sanitize this! Could lead to XSS
* @example undefined
* @example "https://void.lgbt/favicon.png"
*/
iconURL?: string,
/**
* The list of software names and groups the user prefers to autoredirect to this instance
* @example ["sharkey", "misskey-compliant"]
*/
preferredFor?: string[],
preferredFor: string[],
}
type LocalStorage = {
export type LocalStorage = {
version: number,
instances: Instance[],
}
@ -46,12 +49,14 @@ export default new class StorageManager {
default(): LocalStorage {
return {
version: 1,
instances: []
}
}
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() {