FeDirect/static/storage_manager.mts
CenTdemeern1 5e3817c1a7
All checks were successful
Build & Test / build-run (push) Successful in 42s
Add migration
2025-02-11 22:11:56 +01:00

74 lines
No EOL
1.6 KiB
TypeScript

import migrate from "./data_migration.mjs";
export type Instance = {
/**
* The instance's (nick)name
* @example "eepy.moe"
* @example "KitsuClub"
* @example "My Sharkey Instance"
*/
name: string,
/**
* The instance's origin
* @example "https://eepy.moe"
* @example "https://kitsunes.club"
*/
origin: string,
/**
* The kind of software the instance is running
* @example "misskey"
* @example "sharkey"
* @example "akkoma"
*/
software: string,
/**
* The instance's icon URL
* @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[],
}
export type LocalStorage = {
version: number,
instances: Instance[],
}
export default new class StorageManager {
storage: LocalStorage;
saveCallbacks: (() => void)[] = [];
constructor() {
this.load();
}
default(): LocalStorage {
return {
version: 1,
instances: []
}
}
load() {
const data = JSON.parse(window.localStorage.getItem("storage") ?? "null") ?? this.default();
this.storage = migrate(data);
}
save() {
window.localStorage.setItem("storage", JSON.stringify(this.storage));
this.saveCallbacks.forEach(c => c());
}
addSaveCallback(callback: () => void) {
this.saveCallbacks.push(callback);
}
reset() {
this.storage = this.default();
}
}();