1
0
Fork 0

Add typing to Unseal.ts.

This commit is contained in:
Kitteh 2021-05-08 02:09:08 +01:00
parent ab3cc5fa70
commit 22824d4b90
5 changed files with 49 additions and 58 deletions

View file

@ -17,7 +17,7 @@ import { TransitDecryptPage } from "./pages/Transit/TransitDecrypt.js";
import { TransitEncryptPage } from "./pages/Transit/TransitEncrypt.js"; import { TransitEncryptPage } from "./pages/Transit/TransitEncrypt.js";
import { TransitViewPage } from "./pages/Transit/TransitView.js"; import { TransitViewPage } from "./pages/Transit/TransitView.js";
import { TransitViewSecretPage } from "./pages/Transit/TransitViewSecret.js"; import { TransitViewSecretPage } from "./pages/Transit/TransitViewSecret.js";
import { UnsealPage } from "./pages/Unseal.js"; import { UnsealPage } from "./pages/Unseal";
type pagesList = { type pagesList = {
[key: string]: Page [key: string]: Page

View file

@ -1,6 +1,6 @@
import { appendAPIURL } from "./apiUtils"; import { appendAPIURL } from "./apiUtils";
type SealStatusType = { export type SealStatusType = {
progress: number; progress: number;
t: number; t: number;
sealed: boolean; sealed: boolean;

View file

@ -7,7 +7,7 @@ import QrScanner from 'qr-scanner';
import qrScannerWorkerSource from '!!raw-loader!qr-scanner/qr-scanner-worker.min.js'; import qrScannerWorkerSource from '!!raw-loader!qr-scanner/qr-scanner-worker.min.js';
QrScanner.WORKER_PATH = URL.createObjectURL(new Blob([qrScannerWorkerSource])); QrScanner.WORKER_PATH = URL.createObjectURL(new Blob([qrScannerWorkerSource]));
interface QRScannerType extends HTMLElement { export interface QRScannerType extends HTMLElement {
deinit(): void; deinit(): void;
} }

View file

@ -1,24 +0,0 @@
import { Page } from "../types/Page";
import { changePage, setPageContent, setTitleElement } from "../pageUtils";
import { makeElement } from "../htmlUtils";
import { pageState } from "../globalPageState.ts";
export class TemplatePage extends Page {
constructor() {
super();
}
goBack() {
changePage("HOME");
}
async render() {
setTitleElement(pageState);
setPageContent(makeElement({
tag: "p",
text: "[PLACEHOLDER]"
}));
}
get name() {
return "Template";
}
}

View file

@ -1,8 +1,8 @@
import { MarginInline } from "../elements/MarginInline"; import { MarginInline } from "../elements/MarginInline";
import { Page } from "../types/Page"; import { Page } from "../types/Page";
import { QRScanner } from "../elements/QRScanner"; import { QRScanner, QRScannerType } from "../elements/QRScanner";
import { SealStatusType, getSealStatus } from "../api/getSealStatus";
import { changePage, setErrorText, setPageContent } from "../pageUtils"; import { changePage, setErrorText, setPageContent } from "../pageUtils";
import { getSealStatus } from "../api/getSealStatus";
import { makeElement } from "../htmlUtils"; import { makeElement } from "../htmlUtils";
import { submitUnsealKey } from "../api/submitUnsealKey"; import { submitUnsealKey } from "../api/submitUnsealKey";
import i18next from 'i18next'; import i18next from 'i18next';
@ -18,12 +18,21 @@ export class UnsealPage extends Page {
super(); super();
this.mode = UnsealInputModes.FORM_INPUT; this.mode = UnsealInputModes.FORM_INPUT;
} }
cleanup() {
this.deinitWebcam() mode: string;
refresher: number;
qrScanner: QRScannerType;
unsealProgress: HTMLProgressElement;
unsealProgressText: HTMLParagraphElement;
unsealInputContent: HTMLElement;
unsealKeyForm: HTMLFormElement;
cleanup(): void {
this.deinitWebcam();
clearInterval(this.refresher); clearInterval(this.refresher);
} }
deinitWebcam() { deinitWebcam(): void {
try { try {
this.qrScanner.deinit(); this.qrScanner.deinit();
} catch (_) { } catch (_) {
@ -31,25 +40,25 @@ export class UnsealPage extends Page {
} }
} }
makeRefresher() { makeRefresher(): void {
this.refresher = setInterval(async function () { this.refresher = setInterval(async function () {
this.updateSealProgress(await getSealStatus()); this.updateSealProgress(await getSealStatus());
}.bind(this), 1000); }.bind(this), 1000) as unknown as number;
} }
async render() { async render(): Promise<void> {
this.unsealProgress = makeElement({ this.unsealProgress = makeElement({
tag: "progress", tag: "progress",
class: "uk-progress", class: "uk-progress",
attributes: { value: "0", max: "0" } attributes: { value: "0", max: "0" }
}); }) as HTMLProgressElement;
this.unsealProgressText = makeElement({ this.unsealProgressText = makeElement({
tag: "p", tag: "p",
text: i18next.t("unseal_keys_progress", { progress: "0", keys_needed: "0" }), text: i18next.t("unseal_keys_progress", { progress: "0", keys_needed: "0" }),
}); }) as HTMLParagraphElement;
this.unsealInputContent = makeElement({ this.unsealInputContent = makeElement({
tag: "div" tag: "div"
}) }) as HTMLElement;
setPageContent(makeElement({ setPageContent(makeElement({
tag: "div", tag: "div",
children: [ children: [
@ -68,11 +77,17 @@ export class UnsealPage extends Page {
this.makeRefresher(); this.makeRefresher();
} }
setButtons(method) { setButtons(method: string): void {
let newMethod; const newMethod: string =
let buttonText; method == UnsealInputModes.FORM_INPUT ?
newMethod = method == UnsealInputModes.FORM_INPUT ? UnsealInputModes.QR_INPUT : UnsealInputModes.FORM_INPUT; UnsealInputModes.QR_INPUT
buttonText = newMethod == UnsealInputModes.FORM_INPUT ? i18next.t("unseal_input_btn") : i18next.t("unseal_qr_btn"); :
UnsealInputModes.FORM_INPUT;
const buttonText: string =
newMethod == UnsealInputModes.FORM_INPUT ?
i18next.t("unseal_input_btn")
:
i18next.t("unseal_qr_btn");
this.unsealInputContent.appendChild(makeElement({ this.unsealInputContent.appendChild(makeElement({
tag: "button", tag: "button",
class: ["uk-button", "uk-button-primary"], class: ["uk-button", "uk-button-primary"],
@ -84,7 +99,7 @@ export class UnsealPage extends Page {
} }
switchInputMode(method) { switchInputMode(method: string): void {
this.deinitWebcam(); this.deinitWebcam();
this.unsealInputContent.querySelectorAll('*').forEach(n => n.remove()) this.unsealInputContent.querySelectorAll('*').forEach(n => n.remove())
if (method == UnsealInputModes.FORM_INPUT) this.makeUnsealForm(); if (method == UnsealInputModes.FORM_INPUT) this.makeUnsealForm();
@ -92,7 +107,7 @@ export class UnsealPage extends Page {
this.setButtons(method); this.setButtons(method);
} }
makeUnsealForm() { makeUnsealForm(): void {
this.unsealKeyForm = makeElement({ this.unsealKeyForm = makeElement({
tag: "form", tag: "form",
children: [ children: [
@ -112,7 +127,7 @@ export class UnsealPage extends Page {
text: i18next.t("submit_key_btn") text: i18next.t("submit_key_btn")
})), })),
] ]
}); }) as HTMLFormElement;
this.unsealInputContent.appendChild(this.unsealKeyForm); this.unsealInputContent.appendChild(this.unsealKeyForm);
this.unsealKeyForm.addEventListener("submit", function (e) { this.unsealKeyForm.addEventListener("submit", function (e) {
e.preventDefault(); e.preventDefault();
@ -120,23 +135,23 @@ export class UnsealPage extends Page {
}.bind(this)); }.bind(this));
} }
async makeQRInput() { async makeQRInput(): Promise<void> {
this.qrScanner = await QRScanner(function (code) { this.qrScanner = await QRScanner(function (code: string) {
this.submitKey(code); this.submitKey(code);
console.log('decoded qr code:', code) console.log('decoded qr code:', code)
}.bind(this)); }.bind(this));
this.unsealInputContent.appendChild(this.qrScanner); this.unsealInputContent.appendChild(this.qrScanner);
} }
updateSealProgress(data) { updateSealProgress(data: SealStatusType): void {
let progress = data.progress; const progress = data.progress;
let keysNeeded = data.t; const keysNeeded = data.t;
let text = this.unsealProgressText; const text = this.unsealProgressText;
text.innerText = i18next.t("unseal_keys_progress", { text.innerText = i18next.t("unseal_keys_progress", {
progress: String(progress), progress: String(progress),
keys_needed: String(keysNeeded) keys_needed: String(keysNeeded)
}); });
let progressBar = this.unsealProgress; const progressBar = this.unsealProgress;
progressBar.value = progress; progressBar.value = progress;
progressBar.max = keysNeeded; progressBar.max = keysNeeded;
if (!data.sealed) { if (!data.sealed) {
@ -145,7 +160,7 @@ export class UnsealPage extends Page {
} }
} }
submitKey(key) { submitKey(key: string): void {
submitUnsealKey(key).then(_ => { submitUnsealKey(key).then(_ => {
getSealStatus().then(data => { getSealStatus().then(data => {
this.updateSealProgress(data); this.updateSealProgress(data);
@ -155,12 +170,12 @@ export class UnsealPage extends Page {
}); });
} }
async handleKeySubmit() { async handleKeySubmit(): Promise<void> {
let formData = new FormData(this.unsealKeyForm); const formData = new FormData(this.unsealKeyForm);
this.submitKey(formData.get("key")) this.submitKey(formData.get("key") as string)
} }
get name() { get name(): string {
return i18next.t("unseal_vault_text"); return i18next.t("unseal_vault_text");
} }
} }