make page checks better than the previous hack
This commit is contained in:
parent
13e437f2bd
commit
26fa908648
18
src/main.tsx
18
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<void> {
|
||||
document.documentElement.dir = settings.pageDirection;
|
||||
|
@ -53,6 +52,8 @@ async function onLoad(): Promise<void> {
|
|||
document.body,
|
||||
);
|
||||
|
||||
pageChecks(getCurrentUrl(), api, settings);
|
||||
|
||||
if (process.env.NODE_ENV == "development") {
|
||||
await playground();
|
||||
}
|
||||
|
@ -71,19 +72,6 @@ async function onLoad(): Promise<void> {
|
|||
}
|
||||
}
|
||||
}, 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(
|
||||
|
|
|
@ -5,24 +5,34 @@ import ClipboardJS from "clipboard";
|
|||
import UIkit from "uikit";
|
||||
import i18next from "i18next";
|
||||
|
||||
async function prePageChecksReal(api: API, settings: Settings): Promise<boolean> {
|
||||
export async function pageChecks(url: string, api: API, settings: Settings): Promise<boolean> {
|
||||
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<boolean>
|
|||
return false;
|
||||
}
|
||||
|
||||
// ?????????????????????????????
|
||||
// return trues if checks failed
|
||||
// return false if checks passed
|
||||
// ?????????????????????????????
|
||||
export async function prePageChecks(api: API, settings: Settings): Promise<boolean> {
|
||||
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"), {
|
||||
|
|
|
@ -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 = () => (
|
||||
<Router>
|
||||
<Router onChange={(e) => {
|
||||
pageChecks(e.url, api, settings);
|
||||
}}>
|
||||
<Home path="/" settings={settings} api={api} />
|
||||
<Me path="/me" settings={settings} api={api} />
|
||||
<Login path="/login" settings={settings} api={api} />
|
||||
|
|
|
@ -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<DefaultPageProps> {
|
||||
async componentDidMount() {
|
||||
if (!(await prePageChecks(this.props.api, this.props.settings))) return;
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<>
|
||||
|
|
|
@ -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<DefaultPageProps, HomeState> {
|
||||
async componentDidMount() {
|
||||
if (await pageChecks("/home", this.props.api, this.props.settings)) return;
|
||||
|
||||
let selfTokenInfo: TokenInfo;
|
||||
try {
|
||||
selfTokenInfo = await this.props.api.lookupSelf();
|
||||
|
|
|
@ -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<DefaultPageProps, { policies: string[] }> {
|
||||
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) {
|
||||
|
|
|
@ -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({
|
||||
|
|
|
@ -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<DefaultPageProps, SecretsState> {
|
||||
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.
|
||||
|
|
Loading…
Reference in a new issue