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.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-06-30 18:21:18 -04:00
|
|
|
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
|
|
|
|
2022-05-17 12:34:05 -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";
|
2023-02-28 13:50:24 +00:00
|
|
|
import {
|
|
|
|
|
createRoom,
|
2023-09-19 18:55:33 +01:00
|
|
|
getRelativeRoomUrl,
|
2023-02-28 13:50:24 +00:00
|
|
|
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";
|
2023-03-13 18:40:16 -04:00
|
|
|
import { AnalyticsNotice } from "../analytics/AnalyticsNotice";
|
2023-08-11 17:00:05 +02:00
|
|
|
import { useEnableE2EE, useOptInAnalytics } from "../settings/useSetting";
|
2023-07-06 08:53:57 +02:00
|
|
|
import { Config } from "../config/Config";
|
2022-01-04 16:00:13 -08:00
|
|
|
|
2022-08-18 18:48:24 -04:00
|
|
|
export const UnauthenticatedView: FC = () => {
|
2022-05-17 12:34:05 -04:00
|
|
|
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>();
|
2023-03-13 18:40:16 -04:00
|
|
|
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);
|
2022-05-17 12:34:05 -04:00
|
|
|
|
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>();
|
2022-05-19 15:59:02 -04:00
|
|
|
const history = useHistory();
|
2022-10-10 09:19:10 -04:00
|
|
|
const { t } = useTranslation();
|
2022-05-19 15:59:02 -04:00
|
|
|
|
2023-08-11 17:00:05 +02:00
|
|
|
const [e2eeEnabled] = useEnableE2EE();
|
|
|
|
|
|
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);
|
2023-02-28 13:50:24 +00:00
|
|
|
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();
|
2022-05-17 12:34:05 -04:00
|
|
|
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;
|
2022-05-19 15:59:02 -04:00
|
|
|
try {
|
2023-10-06 16:15:16 +01:00
|
|
|
createRoomResult = await createRoom(
|
2023-10-05 16:44:31 +01:00
|
|
|
client,
|
|
|
|
|
roomName,
|
2023-10-11 10:42:04 -04:00
|
|
|
e2eeEnabled ?? false,
|
2023-10-05 16:44:31 +01:00
|
|
|
);
|
2022-05-19 15:59:02 -04:00
|
|
|
} 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
|
2022-05-19 15:59:02 -04:00
|
|
|
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}`);
|
2022-05-19 15:59:02 -04:00
|
|
|
});
|
2022-05-17 12:34:05 -04:00
|
|
|
|
2022-05-19 15:59:02 -04:00
|
|
|
setLoading(false);
|
2023-09-17 14:35:35 -04:00
|
|
|
setJoinExistingCallModalOpen(true);
|
2022-05-19 15:59:02 -04:00
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2022-01-04 16:00:13 -08:00
|
|
|
}
|
2022-05-19 15:59:02 -04: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);
|
2022-05-19 15:59:02 -04:00
|
|
|
setLoading(false);
|
|
|
|
|
setError(error);
|
|
|
|
|
reset();
|
2022-01-04 16:00:13 -08:00
|
|
|
});
|
|
|
|
|
},
|
2023-08-11 17:00:05 +02:00
|
|
|
[
|
|
|
|
|
register,
|
|
|
|
|
reset,
|
|
|
|
|
execute,
|
|
|
|
|
history,
|
2023-09-17 14:35:35 -04:00
|
|
|
setJoinExistingCallModalOpen,
|
2023-08-11 17:00:05 +02:00
|
|
|
setClient,
|
|
|
|
|
e2eeEnabled,
|
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">
|
|
|
|
|
{t("Start new call")}
|
|
|
|
|
</Heading>
|
2022-01-04 16:00:13 -08:00
|
|
|
<Form className={styles.form} onSubmit={onSubmit}>
|
|
|
|
|
<FieldRow>
|
|
|
|
|
<InputField
|
|
|
|
|
id="callName"
|
|
|
|
|
name="callName"
|
2023-09-18 11:06:06 -04:00
|
|
|
label={t("Name of call")}
|
|
|
|
|
placeholder={t("Name of call")}
|
2022-01-04 16:00:13 -08:00
|
|
|
type="text"
|
|
|
|
|
required
|
|
|
|
|
autoComplete="off"
|
2023-04-19 13:47:05 +01:00
|
|
|
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"
|
2022-10-10 09:19:10 -04:00
|
|
|
label={t("Display name")}
|
|
|
|
|
placeholder={t("Display name")}
|
2022-01-04 16:00:13 -08:00
|
|
|
type="text"
|
|
|
|
|
required
|
2023-04-19 13:47:05 +01:00
|
|
|
data-testid="home_displayName"
|
2022-01-04 16:00:13 -08:00
|
|
|
autoComplete="off"
|
|
|
|
|
/>
|
|
|
|
|
</FieldRow>
|
2023-03-13 18:40:16 -04:00
|
|
|
{optInAnalytics === null && (
|
|
|
|
|
<Caption className={styles.notice}>
|
|
|
|
|
<AnalyticsNotice />
|
|
|
|
|
</Caption>
|
|
|
|
|
)}
|
|
|
|
|
<Caption className={styles.notice}>
|
2022-10-10 09:19:10 -04:00
|
|
|
<Trans>
|
|
|
|
|
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"
|
|
|
|
|
>
|
2022-10-10 09:19:10 -04:00
|
|
|
{loading ? t("Loading…") : t("Go")}
|
2022-01-04 16:00:13 -08:00
|
|
|
</Button>
|
|
|
|
|
<div id={recaptchaId} />
|
|
|
|
|
</Form>
|
|
|
|
|
</main>
|
|
|
|
|
<footer className={styles.footer}>
|
|
|
|
|
<Body className={styles.mobileLoginLink}>
|
2023-05-02 18:05:13 +01:00
|
|
|
<Link color="primary" to="/login" data-testid="home_login">
|
2022-10-10 09:19:10 -04:00
|
|
|
{t("Login to your account")}
|
2022-01-04 16:00:13 -08:00
|
|
|
</Link>
|
|
|
|
|
</Body>
|
|
|
|
|
<Body>
|
2022-10-10 09:19:10 -04:00
|
|
|
<Trans>
|
|
|
|
|
Not registered yet?{" "}
|
2023-05-02 18:05:13 +01:00
|
|
|
<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
|
|
|
};
|