FeDirect/static/storage_manager.mts

65 lines
No EOL
1.4 KiB
TypeScript

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
*
* Make sure to sanitize this! Could lead to XSS
*/
iconURL?: string,
/**
* The list of software names and groups the user prefers to autoredirect to this instance
* @example ["sharkey", "misskey-compliant"]
*/
preferredFor?: string[],
}
type LocalStorage = {
instances: Instance[],
}
export default new class StorageManager {
storage: LocalStorage;
saveCallbacks: (() => void)[] = [];
constructor() {
this.load();
}
default(): LocalStorage {
return {
instances: []
}
}
load() {
this.storage = JSON.parse(window.localStorage.getItem("storage") ?? "null") ?? this.default();
}
save() {
window.localStorage.setItem("storage", JSON.stringify(this.storage));
this.saveCallbacks.forEach(c => c());
}
addSaveCallback(callback: () => void) {
this.saveCallbacks.push(callback);
}
}();