From 7e4d1684291971719865681749896bc3420322b1 Mon Sep 17 00:00:00 2001 From: Kitteh Date: Fri, 28 May 2021 10:08:21 +0100 Subject: [PATCH] Add incognito mode. --- src/globalPageState.ts | 2 +- src/pageUtils.ts | 2 +- src/pages/Secrets/SecretTitleElement.tsx | 2 +- src/pages/Secrets/TOTP/TOTPView.tsx | 2 +- src/playground.tsx | 2 +- src/{ => state}/PageState.ts | 63 ++++++++++++++---------- src/state/storage/StorageType.ts | 5 ++ src/types/Page.ts | 2 +- 8 files changed, 47 insertions(+), 33 deletions(-) rename src/{ => state}/PageState.ts (58%) create mode 100644 src/state/storage/StorageType.ts diff --git a/src/globalPageState.ts b/src/globalPageState.ts index 63925e1..b2448ec 100644 --- a/src/globalPageState.ts +++ b/src/globalPageState.ts @@ -1,2 +1,2 @@ -import { PageState } from "./PageState"; +import { PageState } from "./state/PageState"; export const pageState = new PageState(); diff --git a/src/pageUtils.ts b/src/pageUtils.ts index 0663cba..b4af84f 100644 --- a/src/pageUtils.ts +++ b/src/pageUtils.ts @@ -1,5 +1,5 @@ import { PageRouter } from "z-pagerouter"; -import { PageState } from "./PageState"; +import { PageState } from "./state/PageState"; import { getSealStatus } from "./api/sys/getSealStatus"; import { lookupSelf } from "./api/sys/lookupSelf"; import ClipboardJS from "clipboard"; diff --git a/src/pages/Secrets/SecretTitleElement.tsx b/src/pages/Secrets/SecretTitleElement.tsx index 7abbbed..79838e7 100644 --- a/src/pages/Secrets/SecretTitleElement.tsx +++ b/src/pages/Secrets/SecretTitleElement.tsx @@ -1,6 +1,6 @@ import { JSX } from "preact/jsx-runtime"; import { PageRouter } from "z-pagerouter"; -import { PageState } from "../../PageState"; +import { PageState } from "../../state/PageState"; function currentTitleSecretText(state: PageState): string { let secretItemText = state.secretItem; diff --git a/src/pages/Secrets/TOTP/TOTPView.tsx b/src/pages/Secrets/TOTP/TOTPView.tsx index 0613dba..5845e1f 100644 --- a/src/pages/Secrets/TOTP/TOTPView.tsx +++ b/src/pages/Secrets/TOTP/TOTPView.tsx @@ -5,7 +5,7 @@ import { Grid, GridSizes } from "../../../elements/Grid"; import { MarginInline } from "../../../elements/MarginInline"; import { Page } from "../../../types/Page"; import { PageRouter } from "z-pagerouter"; -import { PageState } from "../../../PageState"; +import { PageState } from "../../../state/PageState"; import { SecretTitleElement } from "../SecretTitleElement"; import { getTOTPCode } from "../../../api/totp/getTOTPCode"; import { getTOTPKeys } from "../../../api/totp/getTOTPKeys"; diff --git a/src/playground.tsx b/src/playground.tsx index f5dfa18..d084d02 100644 --- a/src/playground.tsx +++ b/src/playground.tsx @@ -1,5 +1,5 @@ import { PageRouter } from "z-pagerouter"; -import { PageState } from "./PageState"; +import { PageState } from "./state/PageState"; import i18next from "i18next"; // Playground is a way to debug and test things. diff --git a/src/PageState.ts b/src/state/PageState.ts similarity index 58% rename from src/PageState.ts rename to src/state/PageState.ts index 8828418..d94b256 100644 --- a/src/PageState.ts +++ b/src/state/PageState.ts @@ -1,9 +1,18 @@ +import { StorageType } from "./storage/StorageType"; + export class PageState { constructor() { const params = new URLSearchParams(window.location.search); - if (params.has("reset")) localStorage.clear(); + if (params.has("reset")) this.storage.clear(); + if (params.has("incognito")) { + this.storage = sessionStorage; + } else { + this.storage = localStorage; + } } + private storage: StorageType; + // 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 @@ -14,40 +23,40 @@ export class PageState { // the clunkyness of this approach, but for now, this works. get apiURL(): string | null { - const apiurl = localStorage.getItem("apiURL") || ""; + const apiurl = this.storage.getItem("apiURL") || ""; return apiurl.length > 0 ? apiurl : null; } set apiURL(value: string) { - localStorage.setItem("apiURL", value); + this.storage.setItem("apiURL", value); } get token(): string | null { - const tok = localStorage.getItem("token") || ""; + const tok = this.storage.getItem("token") || ""; return tok.length > 0 ? tok : null; } set token(value: string) { - localStorage.setItem("token", value); + this.storage.setItem("token", value); } get pageDirection(): string { - return localStorage.getItem("pageDirection") || "ltr"; + return this.storage.getItem("pageDirection") || "ltr"; } set pageDirection(value: string) { - localStorage.setItem("pageDirection", value); + this.storage.setItem("pageDirection", value); } get language(): string { - return localStorage.getItem("language") || ""; + return this.storage.getItem("language") || ""; } set language(value: string) { - localStorage.setItem("language", value); + this.storage.setItem("language", value); } get baseMount(): string { - return localStorage.getItem("baseMount") || ""; + return this.storage.getItem("baseMount") || ""; } set baseMount(value: string) { - localStorage.setItem("baseMount", value); + this.storage.setItem("baseMount", value); } // Since this is a array we can't act directly on it so we need @@ -65,60 +74,60 @@ export class PageState { } get secretPath(): string[] { - return JSON.parse(localStorage.getItem("secretPath") || "[]") as string[]; + return JSON.parse(this.storage.getItem("secretPath") || "[]") as string[]; } set secretPath(value: string[]) { - localStorage.setItem("secretPath", JSON.stringify(value)); + this.storage.setItem("secretPath", JSON.stringify(value)); } get secretVersion(): string | null { - const result = localStorage.getItem("secretVersion"); + const result = this.storage.getItem("secretVersion"); return result != "null" ? result || null : null; } set secretVersion(value: string) { - localStorage.setItem("secretVersion", String(value)); + this.storage.setItem("secretVersion", String(value)); } get secretItem(): string { - return localStorage.getItem("secretItem") || ""; + return this.storage.getItem("secretItem") || ""; } set secretItem(value: string) { - localStorage.setItem("secretItem", value); + this.storage.setItem("secretItem", value); } get secretMountType(): string { - return localStorage.getItem("secretMountType") || ""; + return this.storage.getItem("secretMountType") || ""; } set secretMountType(value: string) { - localStorage.setItem("secretMountType", value); + this.storage.setItem("secretMountType", value); } get policyItem(): string { - return localStorage.getItem("policyItem") || ""; + return this.storage.getItem("policyItem") || ""; } set policyItem(value: string) { - localStorage.setItem("policyItem", value); + this.storage.setItem("policyItem", value); } get authPath(): string { - return localStorage.getItem("authPath") || ""; + return this.storage.getItem("authPath") || ""; } set authPath(value: string) { - localStorage.setItem("authPath", value); + this.storage.setItem("authPath", value); } get userPassUser(): string { - return localStorage.getItem("userPassUser") || ""; + return this.storage.getItem("userPassUser") || ""; } set userPassUser(value: string) { - localStorage.setItem("userPassUser", value); + this.storage.setItem("userPassUser", value); } get currentPage(): string { - const curPage = localStorage.getItem("currentPage") || "HOME"; + const curPage = this.storage.getItem("currentPage") || "HOME"; return curPage; } set currentPage(value: string) { - localStorage.setItem("currentPage", value); + this.storage.setItem("currentPage", value); } } diff --git a/src/state/storage/StorageType.ts b/src/state/storage/StorageType.ts new file mode 100644 index 0000000..5f839ec --- /dev/null +++ b/src/state/storage/StorageType.ts @@ -0,0 +1,5 @@ +export interface StorageType { + clear(): void; + getItem(key: string): string; + setItem(key: string, value: string); +} \ No newline at end of file diff --git a/src/types/Page.ts b/src/types/Page.ts index 1c9a02a..082dc4d 100644 --- a/src/types/Page.ts +++ b/src/types/Page.ts @@ -1,5 +1,5 @@ import { PageRouter } from "z-pagerouter"; -import { PageState } from "../PageState"; +import { PageState } from "../state/PageState"; export class Page { constructor() {