From 8f2e91bf6bb688be5ab1a3ea410a72e960e7a3ba Mon Sep 17 00:00:00 2001 From: Kitteh Date: Sat, 22 May 2021 13:31:22 +0100 Subject: [PATCH] Rework TOTPView.tsx. --- src/pages/Secrets/TOTP/TOTPView.tsx | 119 +++++++++++----------------- 1 file changed, 45 insertions(+), 74 deletions(-) diff --git a/src/pages/Secrets/TOTP/TOTPView.tsx b/src/pages/Secrets/TOTP/TOTPView.tsx index 87e932d..6f06b1b 100644 --- a/src/pages/Secrets/TOTP/TOTPView.tsx +++ b/src/pages/Secrets/TOTP/TOTPView.tsx @@ -4,37 +4,36 @@ import { Page } from "../../../types/Page"; import { SecretTitleElement } from "../SecretTitleElement"; import { getTOTPCode } from "../../../api/totp/getTOTPCode"; import { getTOTPKeys } from "../../../api/totp/getTOTPKeys"; -import { makeElement } from "z-makeelement"; -import { objectToMap } from "../../../utils"; import { setErrorText } from "../../../pageUtils"; import i18next from "i18next"; -import { render, JSX } from "preact"; +import { render, JSX, Component } from "preact"; -export interface TOTPListElement extends HTMLElement { - setCode(code: string): void; -} -function quickHash(str: string): string { - var hash = 0; - if (str.length == 0) { - return String(hash).toString(); + +export class RefreshingTOTPGridItem extends Component<{ baseMount: string; totpKey: string }, { totpValue: string }> { + constructor() { + super(); + this.state = { totpValue: "" }; } - for (var i = 0; i < str.length; i++) { - var char = str.charCodeAt(i); - hash = ((hash << 5) - hash) + char; - hash = hash & hash; + timer: unknown; + + componentDidMount(): void { + this.timer = setInterval(() => { + getTOTPCode(this.props.baseMount, this.props.totpKey).then((code) => { + this.setState({ totpValue: code }); + }) + this.setState({}); + }, 3000); } - return String(hash).toString(); -} - -export function TOTPGridItem(props: { item_key: string, item_value: string }): JSX.Element { - return ( -
- - -
- ); + render(): JSX.Element { + return ( +
+ + +
+ ); + } } export class TOTPViewPage extends Page { @@ -50,8 +49,6 @@ export class TOTPViewPage extends Page { } async render(): Promise { - console.log(quickHash("abc")); - render((
-

{i18next.t("totp_view_loading")}



-
+
+ {await (async () => { + + + try { + const elem = await Promise.all(Array.from(await getTOTPKeys(this.state.baseMount)).map(async (key) => + + )) + return elem; + } catch (e: unknown) { + const error = e as Error; + if (error == DoesNotExistError) { + return

{i18next.t("totp_view_empty")}

+ } else { + setErrorText(error.message); + } + } + })()} +
), this.router.pageContentElement) - try { - await this.updateTOTPElements(); - document.getElementById("loadingText").remove(); - } catch (e: unknown) { - const error = e as Error; - if (error == DoesNotExistError) { - const loadingText = document.getElementById("loadingText"); - loadingText.innerText = i18next.t("totp_view_empty"); - } else { - setErrorText(error.message); - } - } - - const totpRefresher = async () => { - await this.updateTOTPElements(); - }; - - this.refresher = setInterval(async () => { - await totpRefresher(); - }, 3000) as unknown as number; } - async cleanup(): Promise { - clearInterval(this.refresher); - } - async updateTOTPElements(): Promise { - render(( - <> - {await Promise.all(Array.from(await getTOTPKeys(this.state.baseMount)).map(async (key) => - - ))} - - ), document.querySelector("#totpList")) - } - - makeTOTPListElement(totpKeyName: string): TOTPListElement { - - const gridElement = makeElement({ - tag: "div", - class: ["uk-grid", "uk-grid-small", "uk-text-expand"], - }) as TOTPListElement; - - - return gridElement; - } async getPageTitle(): Promise { return await SecretTitleElement(this.router);