From 5971ae7bd814338cad12658100a56402f4ae72be Mon Sep 17 00:00:00 2001 From: Kitteh Date: Sat, 8 May 2021 02:48:20 +0100 Subject: [PATCH] Add typing to TOTPView.ts. --- src/allPages.ts | 2 +- src/pages/TOTP/{TOTPView.js => TOTPView.ts} | 44 +++++++++++++-------- 2 files changed, 28 insertions(+), 18 deletions(-) rename src/pages/TOTP/{TOTPView.js => TOTPView.ts} (66%) diff --git a/src/allPages.ts b/src/allPages.ts index 23fc0a2..a1058e8 100644 --- a/src/allPages.ts +++ b/src/allPages.ts @@ -12,7 +12,7 @@ import { Page } from "./types/Page"; import { PwGenPage } from "./pages/PwGen"; import { SetLanguagePage } from "./pages/SetLanguage"; import { SetVaultURLPage } from "./pages/SetVaultURL"; -import { TOTPViewPage } from "./pages/TOTP/TOTPView.js"; +import { TOTPViewPage } from "./pages/TOTP/TOTPView"; import { TransitDecryptPage } from "./pages/Transit/TransitDecrypt.js"; import { TransitEncryptPage } from "./pages/Transit/TransitEncrypt.js"; import { TransitViewPage } from "./pages/Transit/TransitView.js"; diff --git a/src/pages/TOTP/TOTPView.js b/src/pages/TOTP/TOTPView.ts similarity index 66% rename from src/pages/TOTP/TOTPView.js rename to src/pages/TOTP/TOTPView.ts index 7afa768..7115196 100644 --- a/src/pages/TOTP/TOTPView.js +++ b/src/pages/TOTP/TOTPView.ts @@ -6,18 +6,27 @@ import { getTOTPCode } from "../../api/getTOTPCode"; import { getTOTPKeys } from "../../api/getTOTPKeys"; import { makeElement } from "../../htmlUtils"; import { objectToMap } from "../../utils"; -import { pageState } from "../../globalPageState.ts"; +import { pageState } from "../../globalPageState"; import i18next from 'i18next'; +export interface TOTPListElement extends HTMLElement { + setCode(code: string): void; +} + + export class TOTPViewPage extends Page { constructor() { super(); this.refresher = undefined; this.totpListElements = {}; } - async render() { + + refresher: number; + totpListElements: Record; + + async render(): Promise { setTitleElement(pageState); - let totpList = makeElement({ tag: "div" }); + const totpList = makeElement({ tag: "div" }); setPageContent(makeElement({ tag: "div", children: [ @@ -38,9 +47,9 @@ export class TOTPViewPage extends Page { })); - getTOTPKeys(pageState.currentBaseMount, pageState.currentSecretPath).then(res => { + getTOTPKeys(pageState.currentBaseMount).then(res => { res.forEach(async function (totpKeyName) { - let totpListElement = this.makeTOTPListElement(totpKeyName); + const totpListElement = this.makeTOTPListElement(totpKeyName); totpList.appendChild(totpListElement); this.totpListElements[totpKeyName] = totpListElement; await this.updateTOTPElement(totpKeyName, totpListElement); @@ -48,46 +57,47 @@ export class TOTPViewPage extends Page { document.getElementById("loadingText").remove(); }).catch(e => { if (e == DoesNotExistError) { - let loadingText = document.getElementById("loadingText"); - loadingText.innerText = i18next.t("totp_view_empty"); + const loadingText = document.getElementById("loadingText"); + loadingText.innerText = i18next.t("totp_view_empty"); } else { setErrorText(e.message); } }); - let totpRefresher = async () => { + const totpRefresher = async () => { await Promise.all(Array.from(objectToMap(this.totpListElements)).map((kv) => { return this.updateTOTPElement(...kv); })) } await totpRefresher(); - this.refresher = setInterval(totpRefresher, 3000); + this.refresher = setInterval(totpRefresher, 3000) as unknown as number; } - cleanup() { + cleanup(): void { clearInterval(this.refresher); this.totpListElements = {}; } - async updateTOTPElement(totpKeyName, totpListElement) { + async updateTOTPElement(totpKeyName: string, totpListElement: TOTPListElement): Promise { totpListElement.setCode(await getTOTPCode(pageState.currentBaseMount, totpKeyName)); } - makeTOTPListElement(totpKeyName) { - let totpKeyBox = CopyableInputBox(totpKeyName, false); - let totpValueBox = CopyableInputBox(i18next.t("totp_view_loading_box")); + makeTOTPListElement(totpKeyName: string): TOTPListElement { + const totpKeyBox = CopyableInputBox(totpKeyName, false); + const totpValueBox = CopyableInputBox(i18next.t("totp_view_loading_box")); - let gridElement = makeElement({ + const gridElement = makeElement({ tag: "div", class: ["uk-grid", "uk-grid-small", "uk-text-expand"], children: [totpKeyBox, totpValueBox] - }); + }) as TOTPListElement; gridElement.setCode = totpValueBox.setText; return gridElement; } - get name() { + + get name(): string { return i18next.t("totp_view_title"); } } \ No newline at end of file