diff --git a/src/api/sys/sealVault.ts b/src/api/sys/sealVault.ts index c68b27e..be24431 100644 --- a/src/api/sys/sealVault.ts +++ b/src/api/sys/sealVault.ts @@ -5,9 +5,5 @@ export async function sealVault(): Promise { method: "PUT", headers: getHeaders(), }); - const resp = await fetch(request); - const data = (await resp.json()) as { errors?: string[] }; - if ("errors" in data) { - throw new Error(data.errors[0]); - } + await fetch(request); } diff --git a/src/pages/Me.ts b/src/pages/Me.ts deleted file mode 100644 index ddddc19..0000000 --- a/src/pages/Me.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { Page } from "../types/Page"; -import { addClipboardNotifications, prePageChecks, setErrorText } from "../pageUtils"; -import { getCapabilitiesPath } from "../api/sys/getCapabilities"; -import { makeElement } from "z-makeelement"; -import { renewSelf } from "../api/sys/renewSelf"; -import { sealVault } from "../api/sys/sealVault"; -import ClipboardJS from "clipboard"; -import i18next from "i18next"; - -export class MePage extends Page { - constructor() { - super(); - } - - async render(): Promise { - if (!(await prePageChecks(this.router))) return; - await this.router.setPageContent( - makeElement({ - tag: "ul", - class: "uk-nav", - children: [ - makeElement({ - tag: "li", - children: makeElement({ - tag: "a", - text: i18next.t("log_out_btn"), - onclick: async () => { - this.state.token = ""; - await this.router.changePage("HOME"); - }, - }), - }), - makeElement({ - tag: "li", - children: makeElement({ - tag: "a", - text: i18next.t("copy_token_btn"), - attributes: { - "data-clipboard-text": this.state.token, - }, - thenRun: (e) => { - const clipboard = new ClipboardJS(e); - addClipboardNotifications(clipboard); - }, - }), - }), - makeElement({ - tag: "li", - children: makeElement({ - tag: "a", - text: i18next.t("renew_lease_btn"), - onclick: async () => { - try { - await renewSelf(); - await this.router.changePage("HOME"); - } catch (e: unknown) { - const error = e as Error; - setErrorText(error.message); - } - }, - }), - }), - makeElement({ - tag: "li", - children: makeElement({ - tag: "a", - condition: await (async () => { - try { - const caps = await getCapabilitiesPath("sys/seal"); - return caps.includes("sudo") && caps.includes("update"); - } catch (e) { - return !true; - } - })(), - text: i18next.t("seal_vault_btn"), - onclick: async () => { - await sealVault(); - await this.router.changePage("UNSEAL_VAULT"); - }, - }), - }), - makeElement({ - tag: "li", - children: makeElement({ - tag: "a", - text: i18next.t("change_language_btn"), - onclick: async () => { - await this.router.changePage("SET_LANGUAGE"); - }, - }), - }), - ], - }), - ); - } - - get name(): string { - return i18next.t("me_page_title"); - } -} diff --git a/src/pages/Me.tsx b/src/pages/Me.tsx new file mode 100644 index 0000000..81dff94 --- /dev/null +++ b/src/pages/Me.tsx @@ -0,0 +1,102 @@ +import { Component, JSX, createRef, render } from "preact"; +import { Page } from "../types/Page"; +import { addClipboardNotifications, prePageChecks, setErrorText } from "../pageUtils"; +import { getCapabilitiesPath } from "../api/sys/getCapabilities"; +import { renewSelf } from "../api/sys/renewSelf"; +import { sealVault } from "../api/sys/sealVault"; +import ClipboardJS from "clipboard"; +import i18next from "i18next"; + +export class CopyLink extends Component<{ text: string; data: string }, unknown> { + linkRef = createRef(); + + componentDidMount(): void { + const clipboard = new ClipboardJS(this.linkRef.current); + addClipboardNotifications(clipboard, 600); + } + + render(): JSX.Element { + return ( + + {this.props.text} + + ); + } +} + +export class MePage extends Page { + constructor() { + super(); + } + + async render(): Promise { + if (!(await prePageChecks(this.router))) return; + + let canSealVault = false; + try { + const caps = await getCapabilitiesPath("sys/seal"); + canSealVault = caps.includes("sudo") && caps.includes("update"); + } catch (e) { + canSealVault = false; + } + + render( + , + this.router.pageContentElement, + ); + } + + get name(): string { + return i18next.t("me_page_title"); + } +}