/* 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. */ import React, { useCallback, useEffect, useRef, useState } from "react"; import styles from "./App.module.css"; import { BrowserRouter as Router, Switch, Route, useHistory, useParams, Link, Redirect, } from "react-router-dom"; import { useConferenceCallManager, useVideoRoom, } from "./ConferenceCallManagerHooks"; 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); return (
{error &&

{error.message}

} {loading ? (

Loading...

) : ( {authenticated ? ( ) : ( <>

Matrix Video Chat

)}
{!authenticated ? ( ) : ( )}
)}
); } function Register({ onRegister }) { const usernameRef = useRef(); const passwordRef = useRef(); const onSubmit = useCallback((e) => { e.preventDefault(); onRegister(usernameRef.current.value, passwordRef.current.value); }); return ( <>

Register

); } function Login({ onLogin }) { const usernameRef = useRef(); const passwordRef = useRef(); const onSubmit = useCallback((e) => { e.preventDefault(); onLogin(usernameRef.current.value, passwordRef.current.value); }); return ( <>

Login

); } function JoinOrCreateRoom({ manager }) { const history = useHistory(); const roomNameRef = useRef(); const roomIdRef = useRef(); const [createRoomError, setCreateRoomError] = useState(); const [joinRoomError, setJoinRoomError] = useState(); const [rooms, setRooms] = useState([]); useEffect(() => { function updateRooms() { setRooms(manager.client.getRooms()); } updateRooms(); manager.client.on("Room", updateRooms); return () => { manager.client.removeListener("Room", updateRooms); }; }, []); const onCreateRoom = useCallback( (e) => { e.preventDefault(); setCreateRoomError(undefined); manager.client .createRoom({ visibility: "private", preset: "public_chat", name: roomNameRef.current.value, }) .then(({ room_id }) => { history.push(`/room/${room_id}`); }) .catch(setCreateRoomError); }, [manager] ); const onJoinRoom = useCallback( (e) => { e.preventDefault(); setJoinRoomError(undefined); manager.client .joinRoom(roomIdRef.current.value) .then(({ roomId }) => { history.push(`/room/${roomId}`); }) .catch(setJoinRoomError); }, [manager] ); return (

Matrix Video Chat

Create New Room

{createRoomError &&

{createRoomError.message}

}

Join Existing Room

{joinRoomError &&

{joinRoomError.message}

}

Rooms:

); } function Room({ manager }) { const { roomId } = useParams(); const { loading, joined, room, participants, error, joinCall } = useVideoRoom( manager, roomId ); return (
{!loading && room && (

{room.name}

{manager.client.getUserId()}
)} {loading && (

Loading room...

)} {error &&
{error.message}
} {!loading && room && !joined && (

Members:

)} {!loading && room && joined && participants.length === 0 && (

Waiting for other participants...

)} {!loading && room && joined && participants.length > 0 && (
{participants.map((participant) => ( ))}
)}
); } 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]); return (

{participant.userId} {participant.local && "(You)"}

); }