element-call/src/home/RegisteredView.jsx

121 lines
3.7 KiB
React
Raw Normal View History

2022-01-04 17:09:27 -08:00
import React, { useState, useCallback } from "react";
2022-01-05 17:19:03 -08:00
import { createRoom, roomAliasFromRoomName } from "../matrix-utils";
import { useGroupCallRooms } from "./useGroupCallRooms";
2022-01-04 16:00:13 -08:00
import { Header, HeaderLogo, LeftNav, RightNav } from "../Header";
2022-01-04 17:09:27 -08:00
import commonStyles from "./common.module.css";
import styles from "./RegisteredView.module.css";
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";
2022-01-04 17:09:27 -08:00
import { CallList } from "./CallList";
2022-01-04 16:00:13 -08:00
import { UserMenuContainer } from "../UserMenuContainer";
2022-01-04 17:09:27 -08:00
import { useModalTriggerState } from "../Modal";
2022-01-05 17:00:02 -08:00
import { JoinExistingCallModal } from "./JoinExistingCallModal";
2022-01-04 17:09:27 -08:00
import { useHistory } from "react-router-dom";
import { Headline, Title } from "../typography/Typography";
import { Form } from "../form/Form";
2022-01-04 16:00:13 -08:00
2022-01-04 17:09:27 -08:00
export function RegisteredView({ client }) {
const [loading, setLoading] = useState(false);
const [error, setError] = useState();
const onSubmit = useCallback(
(e) => {
e.preventDefault();
const data = new FormData(e.target);
2022-01-04 17:13:45 -08:00
const roomName = data.get("callName");
2022-01-04 17:09:27 -08:00
async function submit() {
setError(undefined);
setLoading(true);
const roomIdOrAlias = await createRoom(client, roomName);
if (roomIdOrAlias) {
history.push(`/room/${roomIdOrAlias}`);
}
}
submit().catch((error) => {
if (error.errcode === "M_ROOM_IN_USE") {
setExistingRoomId(roomAliasFromRoomName(roomName));
2022-01-05 11:52:50 -08:00
setLoading(false);
2022-01-04 17:09:27 -08:00
setError(undefined);
modalState.open();
} else {
console.error(error);
setLoading(false);
setError(error);
reset();
}
});
},
[client]
2022-01-04 16:00:13 -08:00
);
2022-01-04 17:09:27 -08:00
2022-01-04 16:00:13 -08:00
const recentRooms = useGroupCallRooms(client);
2022-01-04 17:09:27 -08:00
const { modalState, modalProps } = useModalTriggerState();
const [existingRoomId, setExistingRoomId] = useState();
const history = useHistory();
const onJoinExistingRoom = useCallback(() => {
history.push(`/${existingRoomId}`);
}, [history, existingRoomId]);
2022-01-04 16:00:13 -08:00
return (
2022-01-04 17:09:27 -08:00
<>
<Header>
<LeftNav>
2022-01-04 16:00:13 -08:00
<HeaderLogo />
</LeftNav>
<RightNav>
<UserMenuContainer />
</RightNav>
</Header>
2022-01-04 17:09:27 -08:00
<div className={commonStyles.container}>
<main className={commonStyles.main}>
<HeaderLogo className={commonStyles.logo} />
<Headline className={commonStyles.headline}>
Enter a call name
</Headline>
<Form className={styles.form} onSubmit={onSubmit}>
<FieldRow className={styles.fieldRow}>
<InputField
id="callName"
name="callName"
label="Call name"
placeholder="Call name"
type="text"
required
autoComplete="off"
/>
2022-01-04 17:13:45 -08:00
<Button
type="submit"
size="lg"
className={styles.button}
disabled={loading}
>
2022-01-04 17:09:27 -08:00
{loading ? "Loading..." : "Go"}
</Button>
</FieldRow>
{error && (
2022-01-04 17:13:45 -08:00
<FieldRow className={styles.fieldRow}>
2022-01-04 17:09:27 -08:00
<ErrorMessage>{error.message}</ErrorMessage>
</FieldRow>
)}
</Form>
{recentRooms.length > 0 && (
<>
<Title className={styles.recentCallsTitle}>
Your recent Calls
</Title>
<CallList rooms={recentRooms} client={client} />
</>
)}
</main>
2022-01-04 16:00:13 -08:00
</div>
2022-01-04 17:09:27 -08:00
{modalState.isOpen && (
<JoinExistingCallModal onJoin={onJoinExistingRoom} {...modalProps} />
)}
</>
2022-01-04 16:00:13 -08:00
);
}