2021-08-19 17:49:45 -07:00
|
|
|
/*
|
|
|
|
|
Copyright 2021 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-09 12:58:30 -08:00
|
|
|
import React, { useCallback } from "react";
|
2021-12-07 17:59:55 -08:00
|
|
|
import { useHistory } from "react-router-dom";
|
2021-10-01 16:17:47 -07:00
|
|
|
import {
|
2021-12-09 12:58:30 -08:00
|
|
|
useClient,
|
2021-10-01 16:17:47 -07:00
|
|
|
useGroupCallRooms,
|
|
|
|
|
usePublicRooms,
|
2021-12-09 12:58:30 -08:00
|
|
|
useCreateRoom,
|
2021-10-01 16:17:47 -07:00
|
|
|
} from "./ConferenceCallManagerHooks";
|
2021-12-06 18:00:34 -08:00
|
|
|
import { Header, HeaderLogo, LeftNav, RightNav } from "./Header";
|
2021-08-19 17:49:45 -07:00
|
|
|
import styles from "./Home.module.css";
|
2021-12-07 11:59:57 -08:00
|
|
|
import { FieldRow, InputField, ErrorMessage } from "./Input";
|
2021-12-06 18:00:34 -08:00
|
|
|
import { UserMenu } from "./UserMenu";
|
2021-12-07 11:59:57 -08:00
|
|
|
import { Button } from "./button";
|
2021-12-09 12:58:30 -08:00
|
|
|
import { CallList } from "./CallList";
|
|
|
|
|
import classNames from "classnames";
|
|
|
|
|
import { ErrorModal } from "./ErrorModal";
|
|
|
|
|
|
|
|
|
|
export function Home() {
|
|
|
|
|
const { isAuthenticated, isGuest, loading, error, client } = useClient();
|
|
|
|
|
|
2021-08-19 17:49:45 -07:00
|
|
|
const history = useHistory();
|
2021-12-10 10:54:18 -08:00
|
|
|
const { createRoomError, creatingRoom, createRoom } = useCreateRoom();
|
2021-08-19 17:49:45 -07:00
|
|
|
|
|
|
|
|
const onCreateRoom = useCallback(
|
|
|
|
|
(e) => {
|
|
|
|
|
e.preventDefault();
|
2021-11-17 14:24:52 -08:00
|
|
|
const data = new FormData(e.target);
|
|
|
|
|
const roomName = data.get("roomName");
|
2021-12-09 12:58:30 -08:00
|
|
|
const userName = data.get("userName");
|
|
|
|
|
|
|
|
|
|
createRoom(roomName, userName).then((roomIdOrAlias) => {
|
2021-12-10 10:54:18 -08:00
|
|
|
if (roomIdOrAlias) {
|
|
|
|
|
history.push(`/room/${roomIdOrAlias}`);
|
|
|
|
|
}
|
2021-12-09 12:58:30 -08:00
|
|
|
});
|
2021-08-19 17:49:45 -07:00
|
|
|
},
|
2021-12-09 12:58:30 -08:00
|
|
|
[history]
|
2021-08-19 17:49:45 -07:00
|
|
|
);
|
|
|
|
|
|
2021-12-07 17:59:55 -08:00
|
|
|
const onJoinRoom = useCallback(
|
|
|
|
|
(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const data = new FormData(e.target);
|
|
|
|
|
const roomId = data.get("roomId");
|
|
|
|
|
history.push(`/room/${roomId}`);
|
|
|
|
|
},
|
|
|
|
|
[history]
|
|
|
|
|
);
|
|
|
|
|
|
2021-12-10 10:54:18 -08:00
|
|
|
if (loading) {
|
|
|
|
|
return <div>Loading...</div>;
|
|
|
|
|
} else if (error) {
|
|
|
|
|
return <ErrorModal error={error} />;
|
|
|
|
|
} else if (!isAuthenticated || isGuest) {
|
|
|
|
|
return (
|
|
|
|
|
<UnregisteredView
|
|
|
|
|
onCreateRoom={onCreateRoom}
|
|
|
|
|
createRoomError={createRoomError}
|
|
|
|
|
creatingRoom={creatingRoom}
|
|
|
|
|
onJoinRoom={onJoinRoom}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return (
|
|
|
|
|
<RegisteredView
|
|
|
|
|
client={client}
|
|
|
|
|
onCreateRoom={onCreateRoom}
|
|
|
|
|
createRoomError={createRoomError}
|
|
|
|
|
creatingRoom={creatingRoom}
|
|
|
|
|
onJoinRoom={onJoinRoom}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function UnregisteredView({
|
|
|
|
|
onCreateRoom,
|
|
|
|
|
createRoomError,
|
|
|
|
|
creatingRoom,
|
|
|
|
|
onJoinRoom,
|
|
|
|
|
}) {
|
2021-08-19 17:49:45 -07:00
|
|
|
return (
|
2021-12-10 10:54:18 -08:00
|
|
|
<div className={classNames(styles.home, styles.fullWidth)}>
|
2021-12-10 12:46:18 -08:00
|
|
|
<Header className={styles.header}>
|
2021-12-10 10:54:18 -08:00
|
|
|
<LeftNav>
|
|
|
|
|
<HeaderLogo />
|
|
|
|
|
</LeftNav>
|
|
|
|
|
<RightNav>
|
|
|
|
|
<UserMenu />
|
|
|
|
|
</RightNav>
|
|
|
|
|
</Header>
|
|
|
|
|
<div className={styles.splitContainer}>
|
|
|
|
|
<div className={styles.left}>
|
|
|
|
|
<div className={styles.content}>
|
|
|
|
|
<div className={styles.centered}>
|
|
|
|
|
<form onSubmit={onJoinRoom}>
|
|
|
|
|
<h1>Join a call</h1>
|
2021-12-07 17:59:55 -08:00
|
|
|
<FieldRow className={styles.fieldRow}>
|
2021-12-10 10:54:18 -08:00
|
|
|
<InputField
|
|
|
|
|
id="roomId"
|
|
|
|
|
name="roomId"
|
|
|
|
|
label="Call ID"
|
|
|
|
|
type="text"
|
|
|
|
|
required
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
placeholder="Call ID"
|
|
|
|
|
/>
|
2021-08-23 12:32:32 -07:00
|
|
|
</FieldRow>
|
2021-12-10 10:54:18 -08:00
|
|
|
<FieldRow className={styles.fieldRow}>
|
|
|
|
|
<Button className={styles.button} type="submit">
|
|
|
|
|
Join call
|
|
|
|
|
</Button>
|
|
|
|
|
</FieldRow>
|
|
|
|
|
</form>
|
|
|
|
|
<hr />
|
|
|
|
|
<form onSubmit={onCreateRoom}>
|
|
|
|
|
<h1>Create a call</h1>
|
|
|
|
|
<FieldRow className={styles.fieldRow}>
|
|
|
|
|
<InputField
|
|
|
|
|
id="userName"
|
|
|
|
|
name="userName"
|
|
|
|
|
label="Username"
|
|
|
|
|
type="text"
|
|
|
|
|
required
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
placeholder="Username"
|
|
|
|
|
/>
|
|
|
|
|
</FieldRow>
|
|
|
|
|
<FieldRow className={styles.fieldRow}>
|
|
|
|
|
<InputField
|
|
|
|
|
id="roomName"
|
|
|
|
|
name="roomName"
|
|
|
|
|
label="Room Name"
|
|
|
|
|
type="text"
|
|
|
|
|
required
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
placeholder="Room Name"
|
|
|
|
|
/>
|
|
|
|
|
</FieldRow>
|
|
|
|
|
{createRoomError && (
|
|
|
|
|
<FieldRow className={styles.fieldRow}>
|
|
|
|
|
<ErrorMessage>{createRoomError.message}</ErrorMessage>
|
|
|
|
|
</FieldRow>
|
|
|
|
|
)}
|
|
|
|
|
<FieldRow className={styles.fieldRow}>
|
|
|
|
|
<Button
|
|
|
|
|
className={styles.button}
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={creatingRoom}
|
|
|
|
|
>
|
|
|
|
|
{creatingRoom ? "Creating call..." : "Create call"}
|
|
|
|
|
</Button>
|
|
|
|
|
</FieldRow>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
2021-12-07 17:59:55 -08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2021-12-09 12:58:30 -08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 10:54:18 -08:00
|
|
|
function RegisteredView({
|
|
|
|
|
client,
|
|
|
|
|
onCreateRoom,
|
|
|
|
|
createRoomError,
|
|
|
|
|
creatingRoom,
|
|
|
|
|
onJoinRoom,
|
|
|
|
|
}) {
|
2021-12-09 12:58:30 -08:00
|
|
|
const publicRooms = usePublicRooms(
|
|
|
|
|
client,
|
|
|
|
|
import.meta.env.VITE_PUBLIC_SPACE_ROOM_ID
|
|
|
|
|
);
|
|
|
|
|
const recentRooms = useGroupCallRooms(client);
|
|
|
|
|
|
|
|
|
|
const hideCallList = publicRooms.length === 0 && recentRooms.length === 0;
|
|
|
|
|
|
|
|
|
|
return (
|
2021-12-10 10:54:18 -08:00
|
|
|
<div
|
|
|
|
|
className={classNames(styles.home, {
|
|
|
|
|
[styles.fullWidth]: hideCallList,
|
|
|
|
|
})}
|
|
|
|
|
>
|
2021-12-10 12:46:18 -08:00
|
|
|
<Header className={styles.header}>
|
|
|
|
|
<LeftNav className={styles.leftNav}>
|
2021-12-10 10:54:18 -08:00
|
|
|
<HeaderLogo />
|
|
|
|
|
</LeftNav>
|
|
|
|
|
<RightNav>
|
|
|
|
|
<UserMenu />
|
|
|
|
|
</RightNav>
|
|
|
|
|
</Header>
|
|
|
|
|
<div className={styles.splitContainer}>
|
|
|
|
|
<div className={styles.left}>
|
|
|
|
|
<div className={styles.content}>
|
|
|
|
|
<div className={styles.centered}>
|
|
|
|
|
<form onSubmit={onJoinRoom}>
|
|
|
|
|
<h1>Join a call</h1>
|
2021-12-09 12:58:30 -08:00
|
|
|
<FieldRow className={styles.fieldRow}>
|
2021-12-10 10:54:18 -08:00
|
|
|
<InputField
|
|
|
|
|
id="roomId"
|
|
|
|
|
name="roomId"
|
|
|
|
|
label="Call ID"
|
|
|
|
|
type="text"
|
|
|
|
|
required
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
placeholder="Call ID"
|
|
|
|
|
/>
|
2021-12-09 12:58:30 -08:00
|
|
|
</FieldRow>
|
2021-12-10 10:54:18 -08:00
|
|
|
<FieldRow className={styles.fieldRow}>
|
|
|
|
|
<Button className={styles.button} type="submit">
|
|
|
|
|
Join call
|
|
|
|
|
</Button>
|
|
|
|
|
</FieldRow>
|
|
|
|
|
</form>
|
|
|
|
|
<hr />
|
|
|
|
|
<form onSubmit={onCreateRoom}>
|
|
|
|
|
<h1>Create a call</h1>
|
|
|
|
|
<FieldRow className={styles.fieldRow}>
|
|
|
|
|
<InputField
|
|
|
|
|
id="roomName"
|
|
|
|
|
name="roomName"
|
|
|
|
|
label="Room Name"
|
|
|
|
|
type="text"
|
|
|
|
|
required
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
placeholder="Room Name"
|
|
|
|
|
/>
|
|
|
|
|
</FieldRow>
|
|
|
|
|
{createRoomError && (
|
|
|
|
|
<FieldRow className={styles.fieldRow}>
|
|
|
|
|
<ErrorMessage>{createRoomError.message}</ErrorMessage>
|
|
|
|
|
</FieldRow>
|
|
|
|
|
)}
|
|
|
|
|
<FieldRow className={styles.fieldRow}>
|
|
|
|
|
<Button
|
|
|
|
|
className={styles.button}
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={creatingRoom}
|
|
|
|
|
>
|
|
|
|
|
{creatingRoom ? "Creating call..." : "Create call"}
|
|
|
|
|
</Button>
|
|
|
|
|
</FieldRow>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
2021-12-07 17:59:55 -08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2021-12-10 10:54:18 -08:00
|
|
|
{!hideCallList && (
|
|
|
|
|
<div className={styles.right}>
|
|
|
|
|
<div className={styles.content}>
|
|
|
|
|
{publicRooms.length > 0 && (
|
|
|
|
|
<CallList title="Public Calls" rooms={publicRooms} />
|
|
|
|
|
)}
|
|
|
|
|
{recentRooms.length > 0 && (
|
|
|
|
|
<CallList title="Recent Calls" rooms={recentRooms} />
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2021-12-09 12:58:30 -08:00
|
|
|
</div>
|
2021-12-10 10:54:18 -08:00
|
|
|
)}
|
|
|
|
|
</div>
|
2021-12-07 17:59:55 -08:00
|
|
|
</div>
|
2021-08-19 17:49:45 -07:00
|
|
|
);
|
|
|
|
|
}
|