diff --git a/src/App.jsx b/src/App.jsx index 65dc0c0..b60a9e2 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -28,6 +28,7 @@ import { import { useConferenceCallManager, useVideoRoom, + useRooms, } from "./ConferenceCallManagerHooks"; export default function App() { @@ -49,13 +50,7 @@ export default function App() { {authenticated ? ( ) : ( - <> -
-

Matrix Video Chat

- - -
- + )} @@ -72,45 +67,54 @@ export default function App() { ); } -function Register({ onRegister }) { - const usernameRef = useRef(); - const passwordRef = useRef(); +function LoginOrRegister({ onRegister, onLogin }) { + const registerUsernameRef = useRef(); + const registerPasswordRef = useRef(); + const loginUsernameRef = useRef(); + const loginPasswordRef = useRef(); - const onSubmit = useCallback((e) => { + const onSubmitRegisterForm = 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) => { + const onSubmitLoginForm = useCallback((e) => { e.preventDefault(); onLogin(usernameRef.current.value, passwordRef.current.value); }); return ( - <> +
+

Matrix Video Chat

+

Register

+
+ + + +

Login

-
- - + + +
- +
); } @@ -120,21 +124,7 @@ function JoinOrCreateRoom({ manager }) { 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 rooms = useRooms(manager); const onCreateRoom = useCallback( (e) => { diff --git a/src/ConferenceCallManagerHooks.js b/src/ConferenceCallManagerHooks.js index bf13d0b..570d921 100644 --- a/src/ConferenceCallManagerHooks.js +++ b/src/ConferenceCallManagerHooks.js @@ -187,3 +187,23 @@ export function useVideoRoom(manager, roomId, timeout = 5000) { return { loading, joined, room, participants, error, joinCall }; } + +export function useRooms(manager) { + const [rooms, setRooms] = useState([]); + + useEffect(() => { + function updateRooms() { + setRooms(manager.client.getRooms()); + } + + updateRooms(); + + manager.client.on("Room", updateRooms); + + return () => { + manager.client.removeListener("Room", updateRooms); + }; + }, []); + + return rooms; +}