element-call/src/Room.jsx

147 lines
4 KiB
React
Raw Normal View History

2021-07-27 19:27:59 +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-28 23:14:38 +00:00
import React, { useEffect, useMemo, useRef, useState } from "react";
2021-07-27 19:27:59 +00:00
import styles from "./Room.module.css";
2021-07-30 21:29:08 +00:00
import { useParams, useLocation, Link } from "react-router-dom";
2021-07-27 19:27:59 +00:00
import { useVideoRoom } from "./ConferenceCallManagerHooks";
2021-07-28 23:14:38 +00:00
import { DevTools } from "./DevTools";
import { VideoGrid } from "./GridDemo";
2021-07-28 23:14:38 +00:00
function useQuery() {
const location = useLocation();
return useMemo(() => new URLSearchParams(location.search), [location.search]);
}
2021-07-27 19:27:59 +00:00
export function Room({ manager }) {
const { roomId } = useParams();
2021-07-28 23:14:38 +00:00
const query = useQuery();
const {
loading,
joined,
joining,
room,
participants,
error,
joinCall,
leaveCall,
} = useVideoRoom(manager, roomId);
2021-07-29 18:24:34 +00:00
const debugStr = query.get("debug");
const [debug, setDebug] = useState(debugStr === "" || debugStr === "true");
2021-07-28 23:14:38 +00:00
useEffect(() => {
function onKeyDown(event) {
if (
document.activeElement.tagName !== "input" &&
event.code === "Backquote"
) {
setDebug((prevDebug) => !prevDebug);
}
}
window.addEventListener("keydown", onKeyDown);
return () => {
window.removeEventListener("keydown", onKeyDown);
};
}, []);
2021-07-27 19:27:59 +00:00
return (
<div className={styles.room}>
{!loading && room && (
<div className={styles.header}>
2021-07-30 21:29:08 +00:00
<div className={styles.backNav}>
<Link to="/">Back</Link>
</div>
2021-07-27 19:27:59 +00:00
<h3>{room.name}</h3>
<div className={styles.userNav}>
<h5>{manager.client.getUserId()}</h5>
</div>
</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}>
<h3>Members:</h3>
<ul>
{room.getMembers().map((member) => (
<li key={member.userId}>{member.name}</li>
))}
</ul>
<button disabled={joining} onClick={joinCall}>
Join Call
</button>
2021-07-27 19:27:59 +00:00
</div>
)}
{!loading && room && joined && participants.length === 0 && (
<div className={styles.centerMessage}>
<p>Waiting for other participants...</p>
</div>
)}
{!loading && room && joined && participants.length > 0 && (
<VideoGrid participants={participants} />
2021-07-27 19:27:59 +00:00
)}
2021-07-27 20:26:18 +00:00
{!loading && room && joined && (
<div className={styles.footer}>
<button className={styles.leaveButton} onClick={leaveCall}>
Leave Call
</button>
<button
className={styles.leaveButton}
onClick={() => setDebug((debug) => !debug)}
>
Toggle Debugger
</button>
2021-07-27 20:26:18 +00:00
</div>
)}
2021-07-28 23:14:38 +00:00
{debug && <DevTools manager={manager} />}
2021-07-27 19:27:59 +00:00
</div>
);
}
function Participant({ userId, stream, local }) {
2021-07-27 19:27:59 +00:00
const videoRef = useRef();
useEffect(() => {
if (stream) {
if (local) {
2021-07-27 19:27:59 +00:00
videoRef.current.muted = true;
}
videoRef.current.srcObject = stream;
2021-07-27 19:27:59 +00:00
videoRef.current.play();
2021-07-27 20:26:18 +00:00
} else {
videoRef.current.srcObject = null;
2021-07-27 19:27:59 +00:00
}
}, [stream]);
2021-07-27 19:27:59 +00:00
return (
<div className={styles.participant}>
2021-07-30 03:50:03 +00:00
<video ref={videoRef} playsInline></video>
2021-07-27 19:27:59 +00:00
<div className={styles.participantLabel}>
<p>
2021-07-27 20:26:18 +00:00
{userId} {local && "(You)"}
2021-07-27 19:27:59 +00:00
</p>
</div>
</div>
);
}