FeDirect/static/storage_manager.mts

74 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-02-11 22:11:56 +01:00
import migrate from "./data_migration.mjs";
export type Instance = {
2025-01-12 07:34:39 +01:00
/**
* 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
2025-02-11 22:11:56 +01:00
* @example undefined
* @example "https://void.lgbt/favicon.png"
2025-01-12 07:34:39 +01:00
*/
2025-01-12 10:14:17 +01:00
iconURL?: string,
2025-01-15 06:44:47 +01:00
/**
* The list of software names and groups the user prefers to autoredirect to this instance
* @example ["sharkey", "misskey-compliant"]
*/
2025-02-11 22:11:56 +01:00
preferredFor: string[],
2025-01-12 07:34:39 +01:00
}
2025-02-11 22:11:56 +01:00
export type LocalStorage = {
version: number,
2025-01-12 07:34:39 +01:00
instances: Instance[],
}
export default new class StorageManager {
storage: LocalStorage;
2025-01-14 17:41:14 +01:00
saveCallbacks: (() => void)[] = [];
2025-01-12 07:34:39 +01:00
constructor() {
this.load();
}
default(): LocalStorage {
return {
2025-02-11 22:11:56 +01:00
version: 1,
2025-01-12 07:34:39 +01:00
instances: []
}
}
load() {
2025-02-11 22:11:56 +01:00
const data = JSON.parse(window.localStorage.getItem("storage") ?? "null") ?? this.default();
this.storage = migrate(data);
2025-01-12 07:34:39 +01:00
}
save() {
window.localStorage.setItem("storage", JSON.stringify(this.storage));
2025-01-14 17:41:14 +01:00
this.saveCallbacks.forEach(c => c());
}
addSaveCallback(callback: () => void) {
this.saveCallbacks.push(callback);
2025-01-12 07:34:39 +01:00
}
reset() {
this.storage = this.default();
}
2025-01-12 07:34:39 +01:00
}();