diff --git a/src/api/apiUtils.ts b/src/api/apiUtils.ts index 257eb85..eb7766c 100644 --- a/src/api/apiUtils.ts +++ b/src/api/apiUtils.ts @@ -1,14 +1,14 @@ import { BaseAPIResponse } from "./types/api"; import { DoesNotExistError } from "../types/internalErrors"; -import { pageState } from "../globalPageState"; +import { settings } from "../globalSettings"; export function getHeaders(): Record { return { - "X-Vault-Token": pageState.token, + "X-Vault-Token": settings.token, }; } -export const appendAPIURL = (url: string): string => pageState.apiURL + url; +export const appendAPIURL = (url: string): string => settings.apiURL + url; export async function checkResponse(resp: Response): Promise { if (resp.ok) return; diff --git a/src/globalPageState.ts b/src/globalPageState.ts deleted file mode 100644 index b2448ec..0000000 --- a/src/globalPageState.ts +++ /dev/null @@ -1,2 +0,0 @@ -import { PageState } from "./state/PageState"; -export const pageState = new PageState(); diff --git a/src/globalSettings.ts b/src/globalSettings.ts new file mode 100644 index 0000000..4e120c2 --- /dev/null +++ b/src/globalSettings.ts @@ -0,0 +1,2 @@ +import { Settings } from "./settings/Settings"; +export const settings = new Settings(); diff --git a/src/main.tsx b/src/main.tsx index 21c3c0e..181b565 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -27,13 +27,13 @@ import { getSealStatus } from "./api/sys/getSealStatus"; //import { pageList } from "./allPages"; import { render } from "preact"; import { NavBar } from "./ui/elements/NavBar"; -import { pageState } from "./globalPageState"; +import { settings } from "./globalSettings"; import { playground } from "./playground"; import i18next from "i18next"; import { Main } from "./pages"; async function onLoad(): Promise { - document.documentElement.dir = pageState.pageDirection; + document.documentElement.dir = settings.pageDirection; render( <> @@ -74,7 +74,7 @@ document.addEventListener( // @ts-expect-error console.log("Build Data:", BUILD_STRING); await i18next.init({ - lng: pageState.language, + lng: settings.language, fallbackLng: "en", debug: true, // @ts-ignore @@ -87,7 +87,7 @@ document.addEventListener( }, format: function (value: unknown, format, _): string { if (format === "until_date" && value instanceof Date) - return formatDistance(new Date(), new Date(value), pageState.language); + return formatDistance(new Date(), new Date(value), settings.language); return value as string; }, }, diff --git a/src/pageUtils.ts b/src/pageUtils.ts index 7d7739f..7c61397 100644 --- a/src/pageUtils.ts +++ b/src/pageUtils.ts @@ -1,4 +1,4 @@ -import { PageState } from "./state/PageState"; +import { Settings } from "./settings/Settings"; import { getSealStatus } from "./api/sys/getSealStatus"; import { lookupSelf } from "./api/sys/lookupSelf"; import { route } from "preact-router"; @@ -6,13 +6,13 @@ import ClipboardJS from "clipboard"; import UIkit from "uikit"; import i18next from "i18next"; -async function prePageChecksReal(state: PageState) { - if (state.language.length == 0) { +async function prePageChecksReal(settings: Settings) { + if (settings.language.length == 0) { route("/set_language", true); throw new Error("Language Not Set"); } - if (!state.apiURL) { + if (!settings.apiURL) { route("/set_vault_url", true); throw new Error("Vault URL Not Set"); } @@ -31,9 +31,9 @@ async function prePageChecksReal(state: PageState) { } } -export async function prePageChecks(state: PageState): Promise { +export async function prePageChecks(settings: Settings): Promise { try { - await prePageChecksReal(state); + await prePageChecksReal(settings); } catch (e) { console.log("OHNO", e); return false; diff --git a/src/pages.tsx b/src/pages.tsx index 3eca4e3..2626333 100644 --- a/src/pages.tsx +++ b/src/pages.tsx @@ -1,5 +1,5 @@ import Router from "preact-router"; -import { pageState } from "./globalPageState"; +import { settings } from "./globalSettings"; import { DeleteSecretsEngine } from "./ui/pages/Secrets/DeleteSecretsEngine"; import { Home } from "./ui/pages/Home"; @@ -44,54 +44,54 @@ import { UserPassUserEdit } from "./ui/pages/Access/Auth/userpass/UserPassUserEd export const Main = () => ( - - - + + + - - - + + + - - + + - - - - - + + + + + - - - + + + - - - - - - + + + + + + - - - - - + + + + + - - - + + + - - - - - + + + + +

PAGE NOT YET IMPLEMENTED

diff --git a/src/state/PageState.ts b/src/settings/Settings.ts similarity index 97% rename from src/state/PageState.ts rename to src/settings/Settings.ts index d5a21b5..8256b39 100644 --- a/src/state/PageState.ts +++ b/src/settings/Settings.ts @@ -1,6 +1,6 @@ import { StorageType } from "./storage/StorageType"; -export class PageState { +export class Settings { constructor() { this.storage = localStorage; } diff --git a/src/state/storage/StorageType.ts b/src/settings/storage/StorageType.ts similarity index 100% rename from src/state/storage/StorageType.ts rename to src/settings/storage/StorageType.ts diff --git a/src/types/DefaultPageProps.ts b/src/types/DefaultPageProps.ts index a2d6342..fd0ce6a 100644 --- a/src/types/DefaultPageProps.ts +++ b/src/types/DefaultPageProps.ts @@ -1,6 +1,6 @@ -import { PageState } from "../state/PageState"; +import { Settings } from "../settings/Settings"; export type DefaultPageProps = { - state: PageState; + settings: Settings; matches?: { [key: string]: string }; }; diff --git a/src/ui/pages/Access/AccessHome.tsx b/src/ui/pages/Access/AccessHome.tsx index 273afbf..7d679ba 100644 --- a/src/ui/pages/Access/AccessHome.tsx +++ b/src/ui/pages/Access/AccessHome.tsx @@ -9,7 +9,7 @@ import { PageTitle } from "../../elements/PageTitle"; export class AccessHomePage extends Component { async componentDidMount() { - if (!(await prePageChecks(this.props.state))) return; + if (!(await prePageChecks(this.props.settings))) return; } render() { diff --git a/src/ui/pages/Home.tsx b/src/ui/pages/Home.tsx index a250a41..567dd74 100644 --- a/src/ui/pages/Home.tsx +++ b/src/ui/pages/Home.tsx @@ -1,7 +1,6 @@ import { Component, JSX, render } from "preact"; import { Grid, GridSizes } from "../elements/Grid"; import { Margin } from "../elements/Margin"; -import { PageState } from "../../state/PageState"; import { PageTitle } from "../elements/PageTitle"; import { Tile } from "../elements/Tile"; import { TokenInfo } from "../../api/types/token"; @@ -10,10 +9,7 @@ import { lookupSelf } from "../../api/sys/lookupSelf"; import { prePageChecks, setErrorText } from "../../pageUtils"; import { route } from "preact-router"; import i18next from "i18next"; - -type HomeProps = { - state: PageState; -}; +import { DefaultPageProps } from "../../types/DefaultPageProps"; type HomeState = { selfTokenInfo: TokenInfo; @@ -21,9 +17,9 @@ type HomeState = { policiesCaps: string[]; }; -export class Home extends Component { +export class Home extends Component { async componentDidMount() { - if (!(await prePageChecks(this.props.state))) return; + if (!(await prePageChecks(this.props.settings))) return; let selfTokenInfo: TokenInfo; try { @@ -32,7 +28,7 @@ export class Home extends Component { const error = e as Error; setErrorText(error.message); if (error.message == "permission denied") { - this.props.state.token = ""; + this.props.settings.token = ""; route("/login", true); return; } @@ -53,11 +49,11 @@ export class Home extends Component { return ( this.state.selfTokenInfo && ( <> - +
  • - {i18next.t("home_vaulturl_text", { text: this.props.state.apiURL })} + {i18next.t("home_vaulturl_text", { text: this.props.settings.apiURL })}
  • {i18next.t("home_password_generator_btn")} @@ -105,8 +101,4 @@ export class Home extends Component { ) ); } - - get name(): string { - return i18next.t("home_page_title"); - } } diff --git a/src/ui/pages/Login.tsx b/src/ui/pages/Login.tsx index b486b0f..28cc280 100644 --- a/src/ui/pages/Login.tsx +++ b/src/ui/pages/Login.tsx @@ -3,7 +3,6 @@ import { DefaultPageProps } from "../../types/DefaultPageProps"; import { Form } from "../elements/Form"; import { Margin } from "../elements/Margin"; import { MarginInline } from "../elements/MarginInline"; -import { PageState } from "../../state/PageState"; import { PageTitle } from "../elements/PageTitle"; import { lookupSelf } from "../../api/sys/lookupSelf"; import { route } from "preact-router"; @@ -11,7 +10,7 @@ import { setErrorText } from "../../pageUtils"; import { usernameLogin } from "../../api/auth/usernameLogin"; import i18next from "i18next"; -export class TokenLoginForm extends Component<{ state: PageState }, unknown> { +export class TokenLoginForm extends Component { constructor() { super(); } @@ -40,7 +39,7 @@ export class TokenLoginForm extends Component<{ state: PageState }, unknown> { async onSubmit(data: FormData): Promise { const token = data.get("token"); - this.props.state.token = token as string; + this.props.settings.token = token as string; try { await lookupSelf(); @@ -57,7 +56,7 @@ export class TokenLoginForm extends Component<{ state: PageState }, unknown> { } } -export class UsernameLoginForm extends Component<{ state: PageState }, unknown> { +export class UsernameLoginForm extends Component { constructor() { super(); } @@ -100,7 +99,7 @@ export class UsernameLoginForm extends Component<{ state: PageState }, unknown> data.get("username") as string, data.get("password") as string, ); - this.props.state.token = res; + this.props.settings.token = res; route("/"); } catch (e: unknown) { const error = e as Error; @@ -128,10 +127,10 @@ export class Login extends Component {

    • - +
    • - +
diff --git a/src/ui/pages/Me.tsx b/src/ui/pages/Me.tsx index df180f4..766a435 100644 --- a/src/ui/pages/Me.tsx +++ b/src/ui/pages/Me.tsx @@ -66,7 +66,7 @@ export class Me extends Component {
  • { - this.props.state.token = ""; + this.props.settings.token = ""; route("/"); }} > @@ -74,7 +74,7 @@ export class Me extends Component {
  • - +
  • { async componentDidMount() { - if (!(await prePageChecks(this.props.state))) return; + if (!(await prePageChecks(this.props.settings))) return; let policies = await getPolicies(); policies = policies.sort(); diff --git a/src/ui/pages/Policies/PolicyView.tsx b/src/ui/pages/Policies/PolicyView.tsx index e67c280..c33f7fc 100644 --- a/src/ui/pages/Policies/PolicyView.tsx +++ b/src/ui/pages/Policies/PolicyView.tsx @@ -14,7 +14,7 @@ export class PolicyView extends Component< { policy: string; policyName: string } > { async componentDidMount() { - if (!(await prePageChecks(this.props.state))) return; + if (!(await prePageChecks(this.props.settings))) return; console.log(this.props); const policyName = this.props.matches["policyName"]; const policy = await getPolicy(policyName); diff --git a/src/ui/pages/Secrets/KeyValue/KeyValueList.tsx b/src/ui/pages/Secrets/KeyValue/KeyValueList.tsx index 8d0ccef..f3838ab 100644 --- a/src/ui/pages/Secrets/KeyValue/KeyValueList.tsx +++ b/src/ui/pages/Secrets/KeyValue/KeyValueList.tsx @@ -10,7 +10,7 @@ import { route } from "preact-router"; import { setErrorText } from "../../../../pageUtils"; import i18next from "i18next"; -export type KVKeysListProps = DefaultPageProps & { +export type KVKeysListProps = { baseMount: string; secretPath: string[]; }; @@ -201,7 +201,7 @@ export class KeyValueList extends Component )}

    {this.state.mountType == "cubbyhole" &&

    {i18next.t("kv_view_cubbyhole_text")}

    } - + ); } diff --git a/src/ui/pages/Secrets/SecretsHome.tsx b/src/ui/pages/Secrets/SecretsHome.tsx index deaffa0..d302ee8 100644 --- a/src/ui/pages/Secrets/SecretsHome.tsx +++ b/src/ui/pages/Secrets/SecretsHome.tsx @@ -1,7 +1,6 @@ import { Component, JSX, render } from "preact"; import { DefaultPageProps } from "../../../types/DefaultPageProps"; import { MountType, getMounts } from "../../../api/sys/getMounts"; -import { PageState } from "../../../state/PageState"; import { PageTitle } from "../../elements/PageTitle"; import { getCapsPath } from "../../../api/sys/getCapabilities"; import { prePageChecks } from "../../../pageUtils"; @@ -20,7 +19,6 @@ export function isSupportedMount(mount: MountType): boolean { } export type MountLinkProps = { - state: PageState; mount: MountType; baseMount: string; }; @@ -61,7 +59,7 @@ type SecretsState = { export class Secrets extends Component { async componentDidMount() { - if (!(await prePageChecks(this.props.state))) return; + if (!(await prePageChecks(this.props.settings))) return; const mountsCapabilities = await getCapsPath("/sys/mounts"); const mounts = await getMounts(); @@ -102,7 +100,7 @@ export class Secrets extends Component { console.log(baseMount, mount); if (isSupportedMount(mount)) { return ( - + ); } })} diff --git a/src/ui/pages/SetLanguage.tsx b/src/ui/pages/SetLanguage.tsx index dcc04d5..84d8d00 100644 --- a/src/ui/pages/SetLanguage.tsx +++ b/src/ui/pages/SetLanguage.tsx @@ -44,10 +44,10 @@ export class SetLanguage extends Component { async onSubmit(data: FormData): Promise { const language = data.get("language") as string; - this.props.state.language = language; + this.props.settings.language = language; const t = await i18next.changeLanguage(language); - this.props.state.pageDirection = t("language_direction"); + this.props.settings.pageDirection = t("language_direction"); // TODO: make navbar somethingy //reloadNavBar(this.router); route("/"); diff --git a/src/ui/pages/SetVaultURL.tsx b/src/ui/pages/SetVaultURL.tsx index b2c1f8f..9b6356e 100644 --- a/src/ui/pages/SetVaultURL.tsx +++ b/src/ui/pages/SetVaultURL.tsx @@ -33,7 +33,7 @@ export class SetVaultURL extends Component { async onSubmit(data: FormData): Promise { // TODO: check if vault is actually working here. - this.props.state.apiURL = data.get("vaultURL") as string; + this.props.settings.apiURL = data.get("vaultURL") as string; route("/"); }