2024-07-19 18:26:04 +02:00
|
|
|
// this file is responsible for reading and writing persistent data; all storage shall be handled by this file.
|
|
|
|
|
|
|
|
|
|
import { parse, stringify } from "../utility";
|
|
|
|
|
|
2024-07-19 19:23:48 +02:00
|
|
|
export default class StorageModel {
|
|
|
|
|
storageEntryTree: StorageEntry = {}; // basic
|
|
|
|
|
store = (directoryNames: string[], key: string, value: string): void => {
|
|
|
|
|
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
|
|
|
|
|
localStorage.setItem(unifiedPath, value);
|
|
|
|
|
this.updateTree(...directoryNames, key);
|
|
|
|
|
};
|
2024-07-19 18:39:13 +02:00
|
|
|
|
2024-07-19 19:23:48 +02:00
|
|
|
restore = (directoryNames: string[], key: string): string | null => {
|
|
|
|
|
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
|
|
|
|
|
return localStorage.getItem(unifiedPath);
|
|
|
|
|
};
|
2024-07-19 18:26:04 +02:00
|
|
|
|
2024-07-19 19:23:48 +02:00
|
|
|
remove = (directoryNames: string[], key: string): void => {
|
|
|
|
|
const unifiedPath = this.directoryNamesToKey(...directoryNames, key);
|
|
|
|
|
localStorage.removeItem(unifiedPath);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
list = (...directoryNames: string[]): string[] => {
|
|
|
|
|
let currentParent = this.storageEntryTree;
|
|
|
|
|
for (const directoryName of directoryNames) {
|
|
|
|
|
currentParent = currentParent[directoryName];
|
|
|
|
|
}
|
2024-07-19 18:26:04 +02:00
|
|
|
|
2024-07-19 19:23:48 +02:00
|
|
|
return [...Object.keys(currentParent)];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// array
|
|
|
|
|
storeArray = (directoryNames: string[], key: string, value: any[]): void => {
|
2024-07-19 18:26:04 +02:00
|
|
|
const valueString = stringify(value);
|
2024-07-19 19:23:48 +02:00
|
|
|
this.store(directoryNames, key, valueString);
|
|
|
|
|
};
|
2024-07-19 18:26:04 +02:00
|
|
|
|
2024-07-19 19:23:48 +02:00
|
|
|
restoreArray = (directoryNames: string[], key: string): any[] | null => {
|
|
|
|
|
const valueString = this.restore(directoryNames, key);
|
2024-07-19 18:26:04 +02:00
|
|
|
if (!valueString) return null;
|
2024-07-19 19:23:48 +02:00
|
|
|
|
2024-07-19 18:26:04 +02:00
|
|
|
const parsed = parse(valueString);
|
|
|
|
|
if (!Array.isArray(parsed)) return null;
|
|
|
|
|
|
|
|
|
|
return parsed;
|
2024-07-19 19:23:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// init
|
|
|
|
|
constructor() {
|
|
|
|
|
for (const key of Object.keys(localStorage)) {
|
|
|
|
|
const directoryNames = this.keyToDirectoryNames(key);
|
|
|
|
|
this.updateTree(...directoryNames);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// utility
|
|
|
|
|
updateTree = (...pathParts: string[]) => {
|
|
|
|
|
let currentParent = this.storageEntryTree;
|
|
|
|
|
for (const pathPart of pathParts) {
|
|
|
|
|
if (!currentParent[pathPart]) {
|
|
|
|
|
currentParent[pathPart] = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentParent = currentParent[pathPart];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
directoryNamesToKey = (...directoryNames: string[]): string => {
|
|
|
|
|
return directoryNames.join("/");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
keyToDirectoryNames = (key: string): string[] => {
|
|
|
|
|
return key.split("/");
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// types
|
|
|
|
|
export type StorageEntry = { [key: string]: StorageEntry };
|
|
|
|
|
|
|
|
|
|
// keys
|
|
|
|
|
export enum storageKeys {
|
|
|
|
|
// current
|
|
|
|
|
socketAddress = "v2-socket-address",
|
|
|
|
|
|
|
|
|
|
// settings
|
|
|
|
|
userName = "v2-user-name",
|
|
|
|
|
firstDayOfWeek = "v2-first-day-of-week",
|
|
|
|
|
|
|
|
|
|
// history
|
|
|
|
|
previousAddresses = "v2-previous-addresses",
|
|
|
|
|
previousObjectCategories = "v2-object-categories",
|
|
|
|
|
previousObjectStatuses = "v2-object-statuses",
|
|
|
|
|
previousObjectFilters = "v2-object-filters",
|
|
|
|
|
}
|