Files
element-call/src/home/UnauthenticatedView.tsx
T

243 lines
7.7 KiB
TypeScript
Raw Normal View History

2022-05-04 17:09:48 +01:00
/*
2023-01-03 16:55:26 +00:00
Copyright 2022 New Vector Ltd
2022-05-04 17:09:48 +01:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { FC, useCallback, useState, FormEventHandler } from "react";
2022-08-18 18:48:24 -04:00
import { useHistory } from "react-router-dom";
import { randomString } from "matrix-js-sdk/src/randomstring";
2022-10-10 09:19:10 -04:00
import { Trans, useTranslation } from "react-i18next";
2023-09-18 11:06:06 -04:00
import { Heading } from "@vector-im/compound-web";
2023-09-25 18:04:34 +01:00
import { logger } from "matrix-js-sdk/src/logger";
2022-08-18 18:48:24 -04:00
import { useClient } from "../ClientContext";
2022-01-04 16:00:13 -08:00
import { Header, HeaderLogo, LeftNav, RightNav } from "../Header";
import { UserMenuContainer } from "../UserMenuContainer";
2022-01-05 17:27:01 -08:00
import { FieldRow, InputField, ErrorMessage } from "../input/Input";
2022-01-04 16:00:13 -08:00
import { Button } from "../button";
import {
createRoom,
getRelativeRoomUrl,
roomAliasLocalpartFromRoomName,
sanitiseRoomNameInput,
} from "../matrix-utils";
2022-01-05 16:47:53 -08:00
import { useInteractiveRegistration } from "../auth/useInteractiveRegistration";
2022-01-05 17:00:02 -08:00
import { JoinExistingCallModal } from "./JoinExistingCallModal";
2022-01-05 16:34:01 -08:00
import { useRecaptcha } from "../auth/useRecaptcha";
2022-08-18 18:48:24 -04:00
import { Body, Caption, Link } from "../typography/Typography";
2022-01-04 16:00:13 -08:00
import { Form } from "../form/Form";
import styles from "./UnauthenticatedView.module.css";
2022-01-04 17:09:27 -08:00
import commonStyles from "./common.module.css";
2022-02-15 12:46:58 -08:00
import { generateRandomName } from "../auth/generateRandomName";
import { AnalyticsNotice } from "../analytics/AnalyticsNotice";
2023-10-13 10:30:06 +01:00
import { useOptInAnalytics } from "../settings/useSetting";
2023-07-06 08:53:57 +02:00
import { Config } from "../config/Config";
2023-10-23 12:10:25 +01:00
import { E2eeType } from "../e2ee/e2eeType";
2022-01-04 16:00:13 -08:00
2022-08-18 18:48:24 -04:00
export const UnauthenticatedView: FC = () => {
const { setClient } = useClient();
2022-01-04 16:00:13 -08:00
const [loading, setLoading] = useState(false);
2022-08-18 18:48:24 -04:00
const [error, setError] = useState<Error>();
const [optInAnalytics] = useOptInAnalytics();
2023-07-06 08:53:57 +02:00
const { recaptchaKey, register } = useInteractiveRegistration();
2022-01-04 16:00:13 -08:00
const { execute, reset, recaptchaId } = useRecaptcha(recaptchaKey);
2023-09-17 14:35:35 -04:00
const [joinExistingCallModalOpen, setJoinExistingCallModalOpen] =
useState(false);
const onDismissJoinExistingCallModal = useCallback(
() => setJoinExistingCallModalOpen(false),
2023-10-11 10:42:04 -04:00
[setJoinExistingCallModalOpen],
2023-09-17 14:35:35 -04:00
);
2022-08-18 18:48:24 -04:00
const [onFinished, setOnFinished] = useState<() => void>();
const history = useHistory();
2022-10-10 09:19:10 -04:00
const { t } = useTranslation();
2022-08-18 18:48:24 -04:00
const onSubmit: FormEventHandler<HTMLFormElement> = useCallback(
2022-01-04 16:00:13 -08:00
(e) => {
e.preventDefault();
2022-08-18 18:48:24 -04:00
const data = new FormData(e.target as HTMLFormElement);
const roomName = sanitiseRoomNameInput(data.get("callName") as string);
2022-08-18 18:48:24 -04:00
const displayName = data.get("displayName") as string;
2022-01-04 16:00:13 -08:00
2023-09-22 18:05:13 -04:00
async function submit(): Promise<void> {
2022-01-04 16:00:13 -08:00
setError(undefined);
setLoading(true);
const recaptchaResponse = await execute();
2022-02-15 12:46:58 -08:00
const userName = generateRandomName();
const [client, session] = await register(
2022-01-04 16:00:13 -08:00
userName,
randomString(16),
2022-02-15 12:46:58 -08:00
displayName,
2022-01-04 16:00:13 -08:00
recaptchaResponse,
2023-10-11 10:42:04 -04:00
true,
2022-01-04 16:00:13 -08:00
);
2023-10-06 16:15:16 +01:00
let createRoomResult;
try {
2023-10-23 12:10:25 +01:00
createRoomResult = await createRoom(
client,
roomName,
E2eeType.SHARED_KEY,
);
} catch (error) {
2023-06-30 16:43:28 +01:00
if (!setClient) {
throw error;
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (error.errcode === "M_ROOM_IN_USE") {
2022-08-18 18:48:24 -04:00
setOnFinished(() => {
2023-06-30 16:43:28 +01:00
setClient({ client, session });
2022-06-01 09:29:47 +01:00
const aliasLocalpart = roomAliasLocalpartFromRoomName(roomName);
2023-07-03 16:20:19 +02:00
history.push(`/${aliasLocalpart}`);
});
setLoading(false);
2023-09-17 14:35:35 -04:00
setJoinExistingCallModalOpen(true);
return;
} else {
throw error;
}
2022-01-04 16:00:13 -08:00
}
// Only consider the registration successful if we managed to create the room, too
2023-06-30 16:43:28 +01:00
if (!setClient) {
throw new Error("setClient is undefined");
}
setClient({ client, session });
2023-10-05 16:44:31 +01:00
history.push(
getRelativeRoomUrl(
2023-10-06 16:15:16 +01:00
createRoomResult.roomId,
2023-10-05 16:44:31 +01:00
roomName,
2023-10-11 10:42:04 -04:00
createRoomResult.password,
),
2023-10-05 16:44:31 +01:00
);
2022-01-04 16:00:13 -08:00
}
submit().catch((error) => {
2023-09-25 18:04:34 +01:00
logger.error(error);
setLoading(false);
setError(error);
reset();
2022-01-04 16:00:13 -08:00
});
},
[
register,
reset,
execute,
history,
2023-09-17 14:35:35 -04:00
setJoinExistingCallModalOpen,
setClient,
2023-10-11 10:42:04 -04:00
],
2022-01-04 16:00:13 -08:00
);
return (
<>
2022-01-04 17:09:27 -08:00
<div className={commonStyles.container}>
2023-09-18 16:47:15 -04:00
<Header>
<LeftNav>
<HeaderLogo />
</LeftNav>
<RightNav hideMobile>
<UserMenuContainer />
</RightNav>
</Header>
2022-01-04 17:09:27 -08:00
<main className={commonStyles.main}>
<HeaderLogo className={commonStyles.logo} />
2023-09-18 11:06:06 -04:00
<Heading size="lg" weight="semibold">
2023-11-20 13:00:43 +00:00
{t("start_new_call")}
2023-09-18 11:06:06 -04:00
</Heading>
2022-01-04 16:00:13 -08:00
<Form className={styles.form} onSubmit={onSubmit}>
<FieldRow>
<InputField
id="callName"
name="callName"
2023-11-20 13:00:43 +00:00
label={t("call_name")}
placeholder={t("call_name")}
2022-01-04 16:00:13 -08:00
type="text"
required
autoComplete="off"
data-testid="home_callName"
2022-01-04 16:00:13 -08:00
/>
</FieldRow>
<FieldRow>
<InputField
2022-02-15 12:46:58 -08:00
id="displayName"
name="displayName"
label={t("common.display_name")}
placeholder={t("common.display_name")}
2022-01-04 16:00:13 -08:00
type="text"
required
data-testid="home_displayName"
2022-01-04 16:00:13 -08:00
autoComplete="off"
/>
</FieldRow>
{optInAnalytics === null && (
<Caption className={styles.notice}>
<AnalyticsNotice />
</Caption>
)}
<Caption className={styles.notice}>
2023-11-20 13:00:43 +00:00
<Trans i18nKey="unauthenticated_view_eula_caption">
2022-10-10 09:19:10 -04:00
By clicking "Go", you agree to our{" "}
2023-07-06 08:53:57 +02:00
<Link href={Config.get().eula}>
2023-07-06 08:43:00 +02:00
End User Licensing Agreement (EULA)
</Link>
2022-10-10 09:19:10 -04:00
</Trans>
2022-01-04 16:00:13 -08:00
</Caption>
{error && (
<FieldRow>
2022-10-10 09:19:10 -04:00
<ErrorMessage error={error} />
2022-01-04 16:00:13 -08:00
</FieldRow>
)}
2023-05-02 17:33:56 +01:00
<Button
type="submit"
size="lg"
disabled={loading}
data-testid="home_go"
>
{loading ? t("common.loading") : t("action.go")}
2022-01-04 16:00:13 -08:00
</Button>
<div id={recaptchaId} />
</Form>
</main>
<footer className={styles.footer}>
<Body className={styles.mobileLoginLink}>
<Link color="primary" to="/login" data-testid="home_login">
2023-11-20 13:00:43 +00:00
{t("unauthenticated_view_login_button")}
2022-01-04 16:00:13 -08:00
</Link>
</Body>
<Body>
2023-11-20 13:00:43 +00:00
<Trans i18nKey="unauthenticated_view_body">
2022-10-10 09:19:10 -04:00
Not registered yet?{" "}
<Link color="primary" to="/register" data-testid="home_register">
2022-10-10 09:19:10 -04:00
Create an account
</Link>
</Trans>
2022-01-04 16:00:13 -08:00
</Body>
</footer>
</div>
2023-09-17 14:35:35 -04:00
{onFinished && (
<JoinExistingCallModal
onJoin={onFinished}
open={joinExistingCallModalOpen}
onDismiss={onDismissJoinExistingCallModal}
/>
2022-01-04 16:00:13 -08:00
)}
</>
);
2022-08-18 18:48:24 -04:00
};