2021-08-17 16:57:25 -07:00
|
|
|
import React, { useCallback, useRef, useState } from "react";
|
2021-08-11 16:02:40 -07:00
|
|
|
import styles from "./GridDemo.module.css";
|
2021-08-17 16:57:25 -07:00
|
|
|
import { VideoGrid } from "./VideoGrid";
|
2021-08-17 16:46:32 -07:00
|
|
|
|
|
|
|
|
export function GridDemo() {
|
|
|
|
|
const participantKey = useRef(0);
|
|
|
|
|
const [stream, setStream] = useState();
|
|
|
|
|
const [participants, setParticipants] = useState([]);
|
|
|
|
|
|
|
|
|
|
const startWebcam = useCallback(async () => {
|
|
|
|
|
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
|
|
|
|
|
setStream(stream);
|
|
|
|
|
setParticipants([{ stream, userId: participantKey.current++ }]);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const addParticipant = useCallback(() => {
|
|
|
|
|
setParticipants((participants) => [
|
|
|
|
|
...participants,
|
|
|
|
|
{ stream: stream.clone(), userId: participantKey.current++ },
|
|
|
|
|
]);
|
|
|
|
|
}, [stream]);
|
|
|
|
|
|
2021-08-26 11:03:48 -07:00
|
|
|
const togglePresenter = useCallback((selectedParticipant) => {
|
|
|
|
|
setParticipants((participants) =>
|
|
|
|
|
participants.map((participant) =>
|
|
|
|
|
participant === selectedParticipant
|
|
|
|
|
? {
|
|
|
|
|
...participant,
|
|
|
|
|
presenter: !participant.presenter,
|
|
|
|
|
}
|
|
|
|
|
: participant
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}, []);
|
|
|
|
|
|
2021-08-17 16:46:32 -07:00
|
|
|
const removeParticipant = useCallback((key) => {
|
|
|
|
|
setParticipants((participants) =>
|
|
|
|
|
participants.filter((participant) => participant.userId !== key)
|
|
|
|
|
);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={styles.gridDemo}>
|
|
|
|
|
<div className={styles.buttons}>
|
|
|
|
|
{!stream && <button onClick={startWebcam}>Start Webcam</button>}
|
|
|
|
|
{stream && participants.length < 12 && (
|
|
|
|
|
<button onClick={addParticipant}>Add Tile</button>
|
|
|
|
|
)}
|
|
|
|
|
{stream && participants.length > 0 && (
|
|
|
|
|
<button
|
|
|
|
|
onClick={() =>
|
|
|
|
|
removeParticipant(participants[participants.length - 1].userId)
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
Remove Tile
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2021-08-26 11:03:48 -07:00
|
|
|
<VideoGrid participants={participants} onClickNameTag={togglePresenter} />
|
2021-08-17 16:46:32 -07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|