From 0d57f3799f8d0fe11cacf6f46eb64a48c53a634c Mon Sep 17 00:00:00 2001 From: Farooq Karimi Zadeh Date: Wed, 14 Feb 2024 16:44:34 +0330 Subject: [PATCH] pretty pretty code --- src/App.tsx | 2 +- src/Login.tsx | 43 +++++++++++++++++++++------------ src/LoginHandler.ts | 35 +++++++++++++++------------ src/LoginWithQR/LoginWithQR.tsx | 24 +++++++++++------- src/RoomView/RoomView.tsx | 11 ++++++--- src/Settings.tsx | 6 ----- src/globals.d.ts | 2 +- 7 files changed, 70 insertions(+), 53 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 80b1f27..88e8308 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -38,7 +38,7 @@ class App extends Component<{}, AppState> { } }); localforage.getItem("well_known").then((well_known: unknown) => { - this.wellKnown = well_known as WellKnown; + this.wellKnown = well_known as WellKnown; }); localforage.getItem("guide").then((value: unknown) => { this.setState({ guide: Boolean(value) }); diff --git a/src/Login.tsx b/src/Login.tsx index c0f0005..90f7bc6 100644 --- a/src/Login.tsx +++ b/src/Login.tsx @@ -57,15 +57,19 @@ class Login extends Component<{}, LoginState> { rightCb = () => { switch (this.state.stage) { case 0: - this.loginHandler.findHomeserver(this.homeserverName).then( () => { - this.setState({ cursor: 0, stage: 1}) - }).catch((e: any) => { - window.alert("Could not connect to homeserver"); - console.log(e); - }); + this.loginHandler + .findHomeserver(this.homeserverName) + .then(() => { + this.setState({ cursor: 0, stage: 1 }); + }) + .catch((e: any) => { + window.alert("Could not connect to homeserver"); + console.log(e); + }); break; case 1: - this.selectedLoginFlow = this.loginHandler.loginFlows[this.state.cursor]; + this.selectedLoginFlow = + this.loginHandler.loginFlows[this.state.cursor]; if (this.selectedLoginFlow.type !== "m.login.password") { window.alert("The selected login method is not implemented, yet."); } else { @@ -73,13 +77,16 @@ class Login extends Component<{}, LoginState> { } break; case 2: - let loginData = {'username': this.username, 'password': this.password}; + let loginData = { username: this.username, password: this.password }; if (this.selectedLoginFlow !== undefined) { - this.loginHandler.doLogin(this.selectedLoginFlow, loginData).then(() => { - window.location = window.location; - }).catch((e) => alert(e.message)); + this.loginHandler + .doLogin(this.selectedLoginFlow, loginData) + .then(() => { + window.location = window.location; + }) + .catch((e) => alert(e.message)); } else { - throw new Error("Undefined selectedLoginFlow") + throw new Error("Undefined selectedLoginFlow"); } break; default: @@ -100,7 +107,7 @@ class Login extends Component<{}, LoginState> { this.username = ""; this.password = ""; this.stageNames = ["Login Info", "Login method", "Login"]; - this.loginHandler = new LoginHandler() + this.loginHandler = new LoginHandler(); this.state = { stage: 0, cursor: 0, @@ -155,9 +162,13 @@ class Login extends Component<{}, LoginState> { }); break; case 1: - listViewChildren = this.loginHandler.loginFlows.map((flow: LoginFlow) => { - return ; - }); + listViewChildren = this.loginHandler.loginFlows.map( + (flow: LoginFlow) => { + return ( + + ); + } + ); break; case 2: if (!this.selectedLoginFlow) { diff --git a/src/LoginHandler.ts b/src/LoginHandler.ts index 122e864..2603bb1 100644 --- a/src/LoginHandler.ts +++ b/src/LoginHandler.ts @@ -5,10 +5,9 @@ import * as localforage from "localforage"; import { createClient } from "matrix-js-sdk"; import shared from "./shared"; - export type LoginData = { - username: string, - password: string, + username: string; + password: string; }; export default class LoginHandler { @@ -41,16 +40,20 @@ export default class LoginHandler { switch (loginFlow.type) { case "m.login.password": let password: string = loginData.password; - loginResult = await shared.mClient - .loginWithPassword(username, password); + loginResult = await shared.mClient.loginWithPassword( + username, + password + ); break; default: throw new Error("Unsupported"); } if (loginResult.well_known) { - this.setWellKnown(loginResult.well_known) - console.log("Received a well_known from client login property. Updating previous settings.") - console.log(loginResult.well_known) + this.setWellKnown(loginResult.well_known); + console.log( + "Received a well_known from client login property. Updating previous settings." + ); + console.log(loginResult.well_known); } await localforage.setItem("login", loginResult); alert("Logged in as " + username); @@ -69,7 +72,7 @@ export default class LoginHandler { break; default: if (e.message === "Unsupported") { - message = "Login flow selected is unsupported" + message = "Login flow selected is unsupported"; } else if (e.errcode) { message = e.errcode; } else { @@ -77,7 +80,7 @@ export default class LoginHandler { } break; } - throw new Error(message) + throw new Error(message); } } @@ -87,7 +90,7 @@ export default class LoginHandler { this.homeserverName = name; let baseUrl: string = ""; let wellKnownUrl: string = `https://${name}/.well-known/matrix/client`; - try { + try { let r: Response = await fetch(wellKnownUrl); if (!r.ok) { throw new Error("404"); @@ -104,20 +107,20 @@ export default class LoginHandler { baseUrl: baseUrl, fetchFn: customFetch, }); - let result = await shared.mClient.loginFlows() + let result = await shared.mClient.loginFlows(); if (!result.flows) { throw new Error("Got no flows"); } this.loginFlows = result.flows; } catch (e) { - alert(`No server found at ${baseUrl}`) + alert(`No server found at ${baseUrl}`); console.error(e); } this.setWellKnown({ - "m.homeserver": {"base_url": baseUrl}, - "m.identity_server": {"base_url": "https://vector.im"}, + "m.homeserver": { base_url: baseUrl }, + "m.identity_server": { base_url: "https://vector.im" }, // TODO Where to infer this outside of actual .well-known? - }) + }); } } } diff --git a/src/LoginWithQR/LoginWithQR.tsx b/src/LoginWithQR/LoginWithQR.tsx index 64ca0ea..22d5105 100644 --- a/src/LoginWithQR/LoginWithQR.tsx +++ b/src/LoginWithQR/LoginWithQR.tsx @@ -30,10 +30,10 @@ class LoginWithQR extends Component { }; private readonly loginFlowsShort: Record = { - "PASS": "m.login.password" - } + PASS: "m.login.password", + }; - private async doLogin (data: string) { + private async doLogin(data: string) { let decodedParts: string[] = data.split(" ", 4); let flow = decodedParts[0]; const serverName = decodedParts[1]; @@ -41,15 +41,18 @@ class LoginWithQR extends Component { const start = flow.length + serverName.length + username.length + 3; let password: string = data.substring(start); // TODO implement more flows - if (window.confirm( - `Do you confirm? Flow: ${flow} | Server name: ${serverName} | Username: ${username}`)) { + if ( + window.confirm( + `Do you confirm? Flow: ${flow} | Server name: ${serverName} | Username: ${username}` + ) + ) { // users can either write the full m.login.password (or whatever other flow) or use a shorthand // This maps the shorthand to the actual flow identificator if (!flow.startsWith("m.login")) { flow = this.loginFlowsShort[flow]; } if (flow !== "m.login.password") { - alert("Password authentication is the only supported flow currently") + alert("Password authentication is the only supported flow currently"); return; } try { @@ -61,15 +64,18 @@ class LoginWithQR extends Component { } } if (selectedFlow !== undefined) { - const loginData: LoginData = {username: username, password: password}; - await this.loginHandler.doLogin(selectedFlow, loginData); + const loginData: LoginData = { + username: username, + password: password, + }; + await this.loginHandler.doLogin(selectedFlow, loginData); window.location = window.location; // restart the app } } catch (e) { alert(e); } } - }; + } constructor(props: any) { super(props); diff --git a/src/RoomView/RoomView.tsx b/src/RoomView/RoomView.tsx index 981de39..a6d8270 100644 --- a/src/RoomView/RoomView.tsx +++ b/src/RoomView/RoomView.tsx @@ -134,7 +134,7 @@ class RoomView extends Component { } if (this.state.imageViewer) { - this.handleImageViewerEvents(evt); + this.handleImageViewerEvents(evt); return; } const lastEventIndex = this.getVisibleEvents().length - 1; @@ -146,7 +146,9 @@ class RoomView extends Component { if (cursor === lastEventIndex) { this.setState({ textInputFocus: true }); } else { - this.setState({ cursor: Math.min(this.getVisibleEvents().length - 1, cursor + 1) }); + this.setState({ + cursor: Math.min(this.getVisibleEvents().length - 1, cursor + 1), + }); } } else if (evt.key === "ArrowUp") { if (textInputFocus) { @@ -156,7 +158,7 @@ class RoomView extends Component { } } }; - + handleImageViewerEvents = (evt: KeyboardEvent) => { if (!this.imageViewer) { alert("Image viewer is null! This is a bug. please report it :)"); @@ -523,7 +525,8 @@ class RoomView extends Component { return ( <> {imageViewer && - this.currentEvent?.getContent().msgtype === "m.image" && this.currentEvent?.getContent().url ? ( + this.currentEvent?.getContent().msgtype === "m.image" && + this.currentEvent?.getContent().url ? ( { this.imageViewer = ref; diff --git a/src/Settings.tsx b/src/Settings.tsx index 76063e1..f369afe 100644 --- a/src/Settings.tsx +++ b/src/Settings.tsx @@ -1,6 +1,3 @@ - - - interface Preferences { adShowFreq: "startup" | number; serversForPublicDirectory: string[]; @@ -8,9 +5,6 @@ interface Preferences { inviteAutoAccept: boolean; } - - - function Settings() { return (

diff --git a/src/globals.d.ts b/src/globals.d.ts index 308306c..8a33c9e 100644 --- a/src/globals.d.ts +++ b/src/globals.d.ts @@ -5,7 +5,7 @@ declare global { } interface Window { MozActivity: MozActivity; - getKaiAd?: KaiAdFunc + getKaiAd?: KaiAdFunc; } interface VolumeManager { requestShow: () => void;