TypeScriptify LoginPage

This commit is contained in:
Robin Townsend 2022-05-27 10:00:14 -04:00
parent 5c4bab2a8a
commit 9a44790450

View file

@ -1,5 +1,5 @@
/* /*
Copyright 2021 New Vector Ltd Copyright 2021-2022 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,8 +14,16 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import React, { useCallback, useRef, useState, useMemo } from "react"; import React, {
FC,
FormEvent,
useCallback,
useRef,
useState,
useMemo,
} from "react";
import { useHistory, useLocation, Link } from "react-router-dom"; import { useHistory, useLocation, Link } from "react-router-dom";
import { ReactComponent as Logo } from "../icons/LogoLarge.svg"; import { ReactComponent as Logo } from "../icons/LogoLarge.svg";
import { useClient } from "../ClientContext"; import { useClient } from "../ClientContext";
import { FieldRow, InputField, ErrorMessage } from "../input/Input"; import { FieldRow, InputField, ErrorMessage } from "../input/Input";
@ -25,23 +33,23 @@ import styles from "./LoginPage.module.css";
import { useInteractiveLogin } from "./useInteractiveLogin"; import { useInteractiveLogin } from "./useInteractiveLogin";
import { usePageTitle } from "../usePageTitle"; import { usePageTitle } from "../usePageTitle";
export function LoginPage() { export const LoginPage: FC = () => {
usePageTitle("Login"); usePageTitle("Login");
const { setClient } = useClient(); const { setClient } = useClient();
const [_, login] = useInteractiveLogin(); const [, login] = useInteractiveLogin();
const [homeserver, setHomeServer] = useState(defaultHomeserver); const homeserver = defaultHomeserver; // TODO: Make this configurable
const usernameRef = useRef(); const usernameRef = useRef<HTMLInputElement>();
const passwordRef = useRef(); const passwordRef = useRef<HTMLInputElement>();
const history = useHistory(); const history = useHistory();
const location = useLocation(); const location = useLocation();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [error, setError] = useState(); const [error, setError] = useState<Error>();
// TODO: Handle hitting login page with authenticated client // TODO: Handle hitting login page with authenticated client
const onSubmitLoginForm = useCallback( const onSubmitLoginForm = useCallback(
(e) => { (e: FormEvent<HTMLFormElement>) => {
e.preventDefault(); e.preventDefault();
setLoading(true); setLoading(true);
@ -60,13 +68,13 @@ export function LoginPage() {
setLoading(false); setLoading(false);
}); });
}, },
[login, location, history, homeserver] [login, location, history, homeserver, setClient]
); );
const homeserverHost = useMemo(() => { const homeserverHost = useMemo(() => {
try { try {
return new URL(homeserver).host; return new URL(homeserver).host;
} catch (_error) { } catch (error) {
return defaultHomeserverHost; return defaultHomeserverHost;
} }
}, [homeserver]); }, [homeserver]);
@ -125,4 +133,4 @@ export function LoginPage() {
</div> </div>
</> </>
); );
} };