diff --git a/src/main.tsx b/src/main.tsx index faf4ab8..81440cb 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -25,7 +25,6 @@ import translations from "./translations/index.mjs"; // Actual Imports import { formatDistance } from "./formatDistance"; -//import { pageList } from "./allPages"; import { Main } from "./pages"; import { NavBar } from "./ui/elements/NavBar"; import { ThemeLoader } from "./ThemeLoader"; @@ -35,7 +34,7 @@ import { playground } from "./playground"; import { render } from "preact"; import { settings } from "./globalSettings"; import i18next from "i18next"; -import { prePageChecks } from "./pageUtils"; +import { pageChecks } from "./pageUtils"; async function onLoad(): Promise { document.documentElement.dir = settings.pageDirection; @@ -53,6 +52,8 @@ async function onLoad(): Promise { document.body, ); + pageChecks(getCurrentUrl(), api, settings); + if (process.env.NODE_ENV == "development") { await playground(); } @@ -71,19 +72,6 @@ async function onLoad(): Promise { } } }, 5000); - - - // DO NOT DO THIS - // THIS IS BAD - // I DO NOT RECOMMEND THIS - // WE NEED A BETTER WAY - // THIS IS **TRASH** - // DO NOT - // BAD - setInterval(async () => { - if (["/set_language", "/set_vault_url", "/unseal", "/login", "/me", "/pw_gen"].includes(getCurrentUrl())) return; - await prePageChecks(api, settings); - }, 1000) } document.addEventListener( diff --git a/src/pageUtils.ts b/src/pageUtils.ts index 84b6590..c5980a1 100644 --- a/src/pageUtils.ts +++ b/src/pageUtils.ts @@ -5,24 +5,34 @@ import ClipboardJS from "clipboard"; import UIkit from "uikit"; import i18next from "i18next"; -async function prePageChecksReal(api: API, settings: Settings): Promise { +export async function pageChecks(url: string, api: API, settings: Settings): Promise { + if (url.startsWith("/set_language")) return; + if (settings.language.length == 0) { console.log("set languge") route("/set_language", true); return true } + if (url.startsWith("/me") || url.startsWith("/pw_gen")) return; + + if (url.startsWith("/set_vault_url")) return; + if (settings.apiURL.length == 0) { route("/set_vault_url", false); return true } + if (url.startsWith("/unseal")) return; + const sealStatus = await api.getSealStatus(); if (sealStatus.sealed) { route("/unseal", true); return true } + if (url.startsWith("/login")) return; + try { await api.lookupSelf(); } catch (e) { @@ -32,22 +42,6 @@ async function prePageChecksReal(api: API, settings: Settings): Promise return false; } -// ????????????????????????????? -// return trues if checks failed -// return false if checks passed -// ????????????????????????????? -export async function prePageChecks(api: API, settings: Settings): Promise { - try { - if (await prePageChecksReal(api, settings)) { - return false; - } - } catch (e) { - console.log("OHNO", e); - return false; - } - return true; -} - export function addClipboardNotifications(clipboard: ClipboardJS, timeout = 1000): void { clipboard.on("success", () => { UIkit.notification(i18next.t("notification_copy_success"), { diff --git a/src/pages.tsx b/src/pages.tsx index 2714999..d3c2c2b 100644 --- a/src/pages.tsx +++ b/src/pages.tsx @@ -1,6 +1,7 @@ import { api } from "./globalAPI"; import { settings } from "./globalSettings"; import Router from "preact-router"; +import { pageChecks } from "./pageUtils"; import { AccessHomePage } from "./ui/pages/Access/AccessHome"; import { AuthHome } from "./ui/pages/Access/Auth/AuthHome"; @@ -46,7 +47,9 @@ import { UserPassUserView } from "./ui/pages/Access/Auth/userpass/UserPassUserVi import { UserPassUsersList } from "./ui/pages/Access/Auth/userpass/UserPassUsersList"; export const Main = () => ( - + { + pageChecks(e.url, api, settings); + }}> diff --git a/src/ui/pages/Access/AccessHome.tsx b/src/ui/pages/Access/AccessHome.tsx index 9772a86..8a8c5b3 100644 --- a/src/ui/pages/Access/AccessHome.tsx +++ b/src/ui/pages/Access/AccessHome.tsx @@ -3,15 +3,11 @@ import { DefaultPageProps } from "../../../types/DefaultPageProps"; import { Grid, GridSizes } from "../../elements/Grid"; import { PageTitle } from "../../elements/PageTitle"; import { Tile } from "../../elements/Tile"; -import { notImplemented, prePageChecks } from "../../../pageUtils"; +import { notImplemented } from "../../../pageUtils"; import { route } from "preact-router"; import i18next from "i18next"; export class AccessHomePage extends Component { - async componentDidMount() { - if (!(await prePageChecks(this.props.api, this.props.settings))) return; - } - render() { return ( <> diff --git a/src/ui/pages/Home.tsx b/src/ui/pages/Home.tsx index 2608557..beb9c1e 100644 --- a/src/ui/pages/Home.tsx +++ b/src/ui/pages/Home.tsx @@ -5,7 +5,7 @@ import { Margin } from "../elements/Margin"; import { PageTitle } from "../elements/PageTitle"; import { Tile } from "../elements/Tile"; import { TokenInfo } from "../../api/types/token"; -import { prePageChecks, setErrorText } from "../../pageUtils"; +import { pageChecks, setErrorText } from "../../pageUtils"; import { route } from "preact-router"; import i18next from "i18next"; @@ -17,6 +17,8 @@ type HomeState = { export class Home extends Component { async componentDidMount() { + if (await pageChecks("/home", this.props.api, this.props.settings)) return; + let selfTokenInfo: TokenInfo; try { selfTokenInfo = await this.props.api.lookupSelf(); diff --git a/src/ui/pages/Policies/PoliciesHome.tsx b/src/ui/pages/Policies/PoliciesHome.tsx index 31312a6..9ab41a5 100644 --- a/src/ui/pages/Policies/PoliciesHome.tsx +++ b/src/ui/pages/Policies/PoliciesHome.tsx @@ -3,14 +3,11 @@ import { DefaultPageProps } from "../../../types/DefaultPageProps"; import { Margin } from "../../elements/Margin"; import { PageTitle } from "../../elements/PageTitle"; import { policyNewURL, policyViewURL } from "../pageLinks"; -import { prePageChecks } from "../../../pageUtils"; import { route } from "preact-router"; import i18next from "i18next"; export class PoliciesHome extends Component { async componentDidMount() { - if (!(await prePageChecks(this.props.api, this.props.settings))) return; - let policies = await this.props.api.getPolicies(); policies = policies.sort(); policies = policies.filter(function (policy_name) { diff --git a/src/ui/pages/Policies/PolicyView.tsx b/src/ui/pages/Policies/PolicyView.tsx index 3abdd90..2d82235 100644 --- a/src/ui/pages/Policies/PolicyView.tsx +++ b/src/ui/pages/Policies/PolicyView.tsx @@ -4,7 +4,6 @@ import { DefaultPageProps } from "../../../types/DefaultPageProps"; import { Margin } from "../../elements/Margin"; import { PageTitle } from "../../elements/PageTitle"; import { policyDeleteURL, policyEditURL } from "../pageLinks"; -import { prePageChecks } from "../../../pageUtils"; import { route } from "preact-router"; import i18next from "i18next"; @@ -13,8 +12,6 @@ export class PolicyView extends Component< { policy: string; policyName: string } > { async componentDidMount() { - if (!(await prePageChecks(this.props.api, this.props.settings))) return; - console.log(this.props); const policyName = this.props.matches["policyName"]; const policy = await this.props.api.getPolicy(policyName); this.setState({ diff --git a/src/ui/pages/Secrets/SecretsHome.tsx b/src/ui/pages/Secrets/SecretsHome.tsx index 9d97b2c..cc85fa5 100644 --- a/src/ui/pages/Secrets/SecretsHome.tsx +++ b/src/ui/pages/Secrets/SecretsHome.tsx @@ -2,7 +2,6 @@ import { Component, JSX } from "preact"; import { DefaultPageProps } from "../../../types/DefaultPageProps"; import { MountType } from "../../../api/types/mount"; import { PageTitle } from "../../elements/PageTitle"; -import { prePageChecks } from "../../../pageUtils"; import { route } from "preact-router"; import { sortedObjectMap } from "../../../utils"; import i18next from "i18next"; @@ -58,8 +57,6 @@ type SecretsState = { export class Secrets extends Component { async componentDidMount() { - if (!(await prePageChecks(this.props.api, this.props.settings))) return; - const mountsCapabilities = await this.props.api.getCapsPath("/sys/mounts"); const mounts = await this.props.api.getMounts(); // sort it by secretPath so it's in alphabetical order consistantly.