/* Copyright 2022 New Vector Ltd 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"; import { useHistory } from "react-router-dom"; import { randomString } from "matrix-js-sdk/src/randomstring"; import { Trans, useTranslation } from "react-i18next"; import { Heading } from "@vector-im/compound-web"; import { logger } from "matrix-js-sdk/src/logger"; import { useClient } from "../ClientContext"; import { Header, HeaderLogo, LeftNav, RightNav } from "../Header"; import { UserMenuContainer } from "../UserMenuContainer"; import { FieldRow, InputField, ErrorMessage } from "../input/Input"; import { Button } from "../button"; import { createRoom, getRelativeRoomUrl, roomAliasLocalpartFromRoomName, sanitiseRoomNameInput, } from "../matrix-utils"; import { useInteractiveRegistration } from "../auth/useInteractiveRegistration"; import { JoinExistingCallModal } from "./JoinExistingCallModal"; import { useRecaptcha } from "../auth/useRecaptcha"; import { Body, Caption, Link } from "../typography/Typography"; import { Form } from "../form/Form"; import styles from "./UnauthenticatedView.module.css"; import commonStyles from "./common.module.css"; import { generateRandomName } from "../auth/generateRandomName"; import { AnalyticsNotice } from "../analytics/AnalyticsNotice"; import { Config } from "../config/Config"; import { E2eeType } from "../e2ee/e2eeType"; import { useSetting, optInAnalytics as optInAnalyticsSetting, } from "../settings/settings"; export const UnauthenticatedView: FC = () => { const { setClient } = useClient(); const [loading, setLoading] = useState(false); const [error, setError] = useState(); const [optInAnalytics] = useSetting(optInAnalyticsSetting); const { recaptchaKey, register } = useInteractiveRegistration(); const { execute, reset, recaptchaId } = useRecaptcha(recaptchaKey); const [joinExistingCallModalOpen, setJoinExistingCallModalOpen] = useState(false); const onDismissJoinExistingCallModal = useCallback( () => setJoinExistingCallModalOpen(false), [setJoinExistingCallModalOpen], ); const [onFinished, setOnFinished] = useState<() => void>(); const history = useHistory(); const { t } = useTranslation(); const onSubmit: FormEventHandler = useCallback( (e) => { e.preventDefault(); const data = new FormData(e.target as HTMLFormElement); const roomName = sanitiseRoomNameInput(data.get("callName") as string); const displayName = data.get("displayName") as string; async function submit(): Promise { setError(undefined); setLoading(true); const recaptchaResponse = await execute(); const userName = generateRandomName(); const [client, session] = await register( userName, randomString(16), displayName, recaptchaResponse, true, ); let createRoomResult; try { createRoomResult = await createRoom( client, roomName, E2eeType.SHARED_KEY, ); } catch (error) { if (!setClient) { throw error; } // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore if (error.errcode === "M_ROOM_IN_USE") { setOnFinished(() => { setClient({ client, session }); const aliasLocalpart = roomAliasLocalpartFromRoomName(roomName); history.push(`/${aliasLocalpart}`); }); setLoading(false); setJoinExistingCallModalOpen(true); return; } else { throw error; } } // Only consider the registration successful if we managed to create the room, too if (!setClient) { throw new Error("setClient is undefined"); } if (!createRoomResult.password) throw new Error("Failed to create room with shared secret"); setClient({ client, session }); history.push( getRelativeRoomUrl( createRoomResult.roomId, { kind: E2eeType.SHARED_KEY, secret: createRoomResult.password }, roomName, ), ); } submit().catch((error) => { logger.error(error); setLoading(false); setError(error); reset(); }); }, [ register, reset, execute, history, setJoinExistingCallModalOpen, setClient, ], ); return ( <>
{t("start_new_call")}
{optInAnalytics === null && ( )} By clicking "Go", you agree to our{" "} End User Licensing Agreement (EULA) {error && ( )}
{t("unauthenticated_view_login_button")} Not registered yet?{" "} Create an account
{onFinished && ( )} ); };