element-call/src/App.jsx

281 lines
7.3 KiB
React
Raw Normal View History

2021-07-16 21:38:44 +00: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-07-19 19:55:30 +00:00
import React, { useCallback, useEffect, useRef, useState } from "react";
2021-07-26 23:58:31 +00:00
import styles from "./App.module.css";
2021-07-19 19:55:30 +00:00
import {
BrowserRouter as Router,
Switch,
Route,
useHistory,
useParams,
Link,
Redirect,
} from "react-router-dom";
import {
useConferenceCallManager,
useVideoRoom,
2021-07-27 18:55:45 +00:00
useRooms,
} from "./ConferenceCallManagerHooks";
2021-07-16 21:22:03 +00:00
export default function App() {
const { protocol, host } = window.location;
// Assume homeserver is hosted on same domain (proxied in development by vite)
const homeserverUrl = `${protocol}//${host}`;
const { loading, authenticated, error, manager, login, register } =
useConferenceCallManager(homeserverUrl);
2021-07-16 21:22:03 +00:00
return (
2021-07-19 19:55:30 +00:00
<Router>
2021-07-26 23:58:31 +00:00
<div className={styles.app}>
2021-07-16 21:22:03 +00:00
{error && <p>{error.message}</p>}
{loading ? (
<p>Loading...</p>
) : (
2021-07-19 19:55:30 +00:00
<Switch>
<Route exact path="/">
{authenticated ? (
<JoinOrCreateRoom manager={manager} />
2021-07-19 19:55:30 +00:00
) : (
2021-07-27 18:55:45 +00:00
<LoginOrRegister onRegister={register} onLogin={login} />
2021-07-19 19:55:30 +00:00
)}
</Route>
<Route path="/room/:roomId">
{!authenticated ? (
<Redirect to="/" />
) : (
<Room manager={manager} />
)}
2021-07-19 19:55:30 +00:00
</Route>
</Switch>
2021-07-16 21:22:03 +00:00
)}
</div>
2021-07-19 19:55:30 +00:00
</Router>
2021-07-16 21:22:03 +00:00
);
}
2021-07-27 18:55:45 +00:00
function LoginOrRegister({ onRegister, onLogin }) {
const registerUsernameRef = useRef();
const registerPasswordRef = useRef();
const loginUsernameRef = useRef();
const loginPasswordRef = useRef();
2021-07-16 21:22:03 +00:00
2021-07-27 18:55:45 +00:00
const onSubmitRegisterForm = useCallback((e) => {
2021-07-16 21:22:03 +00:00
e.preventDefault();
onRegister(usernameRef.current.value, passwordRef.current.value);
});
2021-07-27 18:55:45 +00:00
const onSubmitLoginForm = useCallback((e) => {
2021-07-16 21:22:03 +00:00
e.preventDefault();
onLogin(usernameRef.current.value, passwordRef.current.value);
});
return (
2021-07-27 18:55:45 +00:00
<div className={styles.page}>
<h1>Matrix Video Chat</h1>
<h2>Register</h2>
<form onSubmit={onSubmitRegisterForm}>
<input
type="text"
ref={registerUsernameRef}
placeholder="Username"
></input>
<input
type="password"
ref={registerPasswordRef}
placeholder="Password"
></input>
<button type="submit">Register</button>
</form>
2021-07-26 23:58:31 +00:00
<h2>Login</h2>
2021-07-27 18:55:45 +00:00
<form onSubmit={onSubmitLoginForm}>
<input
type="text"
ref={loginUsernameRef}
placeholder="Username"
></input>
<input
type="password"
ref={loginPasswordRef}
placeholder="Password"
></input>
2021-07-26 23:58:31 +00:00
<button type="submit">Login</button>
</form>
2021-07-27 18:55:45 +00:00
</div>
2021-07-16 21:22:03 +00:00
);
}
function JoinOrCreateRoom({ manager }) {
2021-07-19 19:55:30 +00:00
const history = useHistory();
2021-07-16 21:22:03 +00:00
const roomNameRef = useRef();
const roomIdRef = useRef();
const [createRoomError, setCreateRoomError] = useState();
const [joinRoomError, setJoinRoomError] = useState();
2021-07-27 18:55:45 +00:00
const rooms = useRooms(manager);
2021-07-16 21:22:03 +00:00
const onCreateRoom = useCallback(
(e) => {
e.preventDefault();
setCreateRoomError(undefined);
manager.client
2021-07-16 21:22:03 +00:00
.createRoom({
visibility: "private",
2021-07-19 19:55:30 +00:00
preset: "public_chat",
2021-07-16 21:22:03 +00:00
name: roomNameRef.current.value,
})
.then(({ room_id }) => {
2021-07-26 19:20:11 +00:00
history.push(`/room/${room_id}`);
2021-07-16 21:22:03 +00:00
})
.catch(setCreateRoomError);
},
[manager]
2021-07-16 21:22:03 +00:00
);
const onJoinRoom = useCallback(
(e) => {
e.preventDefault();
setJoinRoomError(undefined);
manager.client
2021-07-16 21:22:03 +00:00
.joinRoom(roomIdRef.current.value)
.then(({ roomId }) => {
2021-07-26 19:20:11 +00:00
history.push(`/room/${roomId}`);
2021-07-16 21:22:03 +00:00
})
.catch(setJoinRoomError);
},
[manager]
2021-07-16 21:22:03 +00:00
);
return (
2021-07-26 23:58:31 +00:00
<div className={styles.page}>
<h1>Matrix Video Chat</h1>
2021-07-16 21:22:03 +00:00
<form onSubmit={onCreateRoom}>
2021-07-26 23:58:31 +00:00
<h2>Create New Room</h2>
2021-07-16 21:22:03 +00:00
<input
id="roomName"
name="roomName"
type="text"
required
autoComplete="off"
placeholder="Room Name"
ref={roomNameRef}
></input>
{createRoomError && <p>{createRoomError.message}</p>}
<button type="submit">Create Room</button>
</form>
<form onSubmit={onJoinRoom}>
2021-07-26 23:58:31 +00:00
<h2>Join Existing Room</h2>
2021-07-16 21:22:03 +00:00
<input
id="roomId"
name="roomId"
type="text"
required
autoComplete="off"
placeholder="Room ID"
ref={roomIdRef}
></input>
{joinRoomError && <p>{joinRoomError.message}</p>}
<button type="submit">Join Room</button>
</form>
2021-07-26 23:58:31 +00:00
<h2>Rooms:</h2>
2021-07-19 19:55:30 +00:00
<ul>
{rooms.map((room) => (
<li key={room.roomId}>
<Link to={`/room/${room.roomId}`}>{room.name}</Link>
</li>
))}
</ul>
2021-07-16 21:22:03 +00:00
</div>
);
}
function Room({ manager }) {
2021-07-19 19:55:30 +00:00
const { roomId } = useParams();
2021-07-22 06:28:01 +00:00
const { loading, joined, room, participants, error, joinCall } = useVideoRoom(
manager,
2021-07-22 06:28:01 +00:00
roomId
);
2021-07-16 21:22:03 +00:00
return (
2021-07-26 23:58:31 +00:00
<div className={styles.room}>
2021-07-19 19:55:30 +00:00
{!loading && room && (
2021-07-26 23:58:31 +00:00
<div className={styles.header}>
<h3>{room.name}</h3>
2021-07-27 00:03:20 +00:00
<div className={styles.userNav}>
<h5>{manager.client.getUserId()}</h5>
2021-07-27 00:03:20 +00:00
</div>
2021-07-26 23:58:31 +00:00
</div>
)}
{loading && (
<div className={styles.centerMessage}>
<p>Loading room...</p>
</div>
)}
{error && <div className={styles.centerMessage}>{error.message}</div>}
{!loading && room && !joined && (
<div className={styles.joinRoom}>
2021-07-19 19:55:30 +00:00
<h3>Members:</h3>
<ul>
{room.getMembers().map((member) => (
<li key={member.userId}>{member.name}</li>
))}
</ul>
2021-07-26 23:58:31 +00:00
<button onClick={joinCall}>Join Call</button>
</div>
)}
{!loading && room && joined && participants.length === 0 && (
<div className={styles.centerMessage}>
<p>Waiting for other participants...</p>
</div>
)}
{!loading && room && joined && participants.length > 0 && (
<div className={styles.roomContainer}>
{participants.map((participant) => (
<Participant key={participant.userId} participant={participant} />
))}
</div>
2021-07-19 19:55:30 +00:00
)}
2021-07-16 21:22:03 +00:00
</div>
);
}
2021-07-22 06:28:01 +00:00
function Participant({ participant }) {
const videoRef = useRef();
useEffect(() => {
if (participant.feed) {
if (participant.muted) {
videoRef.current.muted = true;
}
videoRef.current.srcObject = participant.feed.stream;
videoRef.current.play();
}
}, [participant.feed]);
2021-07-22 23:41:57 +00:00
return (
2021-07-26 23:58:31 +00:00
<div className={styles.participant}>
2021-07-22 23:41:57 +00:00
<video ref={videoRef}></video>
2021-07-26 23:58:31 +00:00
<div className={styles.participantLabel}>
<p>
{participant.userId} {participant.local && "(You)"}
</p>
</div>
</div>
2021-07-22 23:41:57 +00:00
);
2021-07-22 06:28:01 +00:00
}