1
0
Fork 0
VaultUI/src/state/PageState.ts
2022-01-06 23:02:34 +00:00

172 lines
4.9 KiB
TypeScript

import { StorageType } from "./storage/StorageType";
export class PageState {
constructor() {
const params = new URLSearchParams(window.location.search);
if (params.has("reset")) this.storage.clear();
if (params.has("state")) {
console.log("state owo");
this.loadState(JSON.parse(params.get("state")));
}
if (params.has("incognito")) {
this.storage = sessionStorage;
} else {
this.storage = localStorage;
}
}
private storage: StorageType;
dumpState(): { [key: string]: unknown } {
return {
baseMount: this.baseMount,
secretPath: this.secretPath,
secretVersion: this.secretVersion,
secretItem: this.secretItem,
secretMountType: this.secretMountType,
policyItem: this.policyItem,
authPath: this.authPath,
userPassUser: this.userPassUser,
currentPage: this.currentPage,
};
}
loadState(newState: { [key: string]: unknown }) {
console.log(newState);
for (const prop in newState) {
console.log("WANTS", prop);
if (prop in this) {
console.log("HAS", prop.toString());
// @ts-ignore
this[prop] = newState[prop];
}
}
}
// NOTE: When a item in the page state isn't a string (e.g it is a array or object),
// you need to add helper methods to mutate it or else it wont save.
// example: secretPath is a array so when you try to .push() to it
// it will modify the object that was getted from this class
// then when you try to access it again, there will be a different object.
// I guess you could make another class that emulates a Array or Map
// by using a bunch of functions and modifying localStorage in order to remove some of
// the clunkyness of this approach, but for now, this works.
// these are all ones that persist across browser sessions
// usually in localStorage unless in incognito.
get apiURL(): string | null {
const apiurl = this.storage.getItem("apiURL") || "";
return apiurl.length > 0 ? apiurl : null;
}
set apiURL(value: string) {
this.storage.setItem("apiURL", value);
}
get token(): string | null {
const tok = this.storage.getItem("token") || "";
return tok.length > 0 ? tok : null;
}
set token(value: string) {
this.storage.setItem("token", value);
}
get pageDirection(): string {
return this.storage.getItem("pageDirection") || "ltr";
}
set pageDirection(value: string) {
this.storage.setItem("pageDirection", value);
}
get language(): string {
return this.storage.getItem("language") || "";
}
set language(value: string) {
this.storage.setItem("language", value);
}
// all of these are sessionStorage and not persisted
// so can have multiple tabs open
// TODO: move to just local variables maybe
get baseMount(): string {
return sessionStorage.getItem("baseMount") || "";
}
set baseMount(value: string) {
sessionStorage.setItem("baseMount", value);
}
// Since this is a array we can't act directly on it so we need
// functions to do the same modifications.
// See the note at the start o
popSecretPath(): void {
const secPath = this.secretPath;
secPath.pop();
this.secretPath = secPath;
}
pushSecretPath(...args: string[]): void {
const secPath = this.secretPath;
secPath.push(...args);
this.secretPath = secPath;
}
get secretPath(): string[] {
return JSON.parse(sessionStorage.getItem("secretPath") || "[]") as string[];
}
set secretPath(value: string[]) {
sessionStorage.setItem("secretPath", JSON.stringify(value));
}
get secretVersion(): string | null {
const result = sessionStorage.getItem("secretVersion");
return result != "null" ? result || null : null;
}
set secretVersion(value: string) {
sessionStorage.setItem("secretVersion", String(value));
}
get secretItem(): string {
return sessionStorage.getItem("secretItem") || "";
}
set secretItem(value: string) {
sessionStorage.setItem("secretItem", value);
}
get secretMountType(): string {
return sessionStorage.getItem("secretMountType") || "";
}
set secretMountType(value: string) {
sessionStorage.setItem("secretMountType", value);
}
get policyItem(): string {
return sessionStorage.getItem("policyItem") || "";
}
set policyItem(value: string) {
sessionStorage.setItem("policyItem", value);
}
get authPath(): string {
return sessionStorage.getItem("authPath") || "";
}
set authPath(value: string) {
sessionStorage.setItem("authPath", value);
}
get userPassUser(): string {
return sessionStorage.getItem("userPassUser") || "";
}
set userPassUser(value: string) {
sessionStorage.setItem("userPassUser", value);
}
get currentPage(): string {
const curPage = sessionStorage.getItem("currentPage") || "HOME";
return curPage;
}
set currentPage(value: string) {
sessionStorage.setItem("currentPage", value);
}
}