Files
element-call/src/room/LobbyView.tsx
T

246 lines
7.7 KiB
TypeScript
Raw Normal View History

2022-05-04 17:09:48 +01:00
/*
Copyright 2022-2024 New Vector Ltd.
2022-05-04 17:09:48 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
2022-05-04 17:09:48 +01:00
*/
import { type FC, useCallback, useMemo, useState, type JSX } from "react";
2023-09-18 15:45:48 -04:00
import { useTranslation } from "react-i18next";
2025-03-13 13:58:43 +01:00
import { type MatrixClient } from "matrix-js-sdk";
import { Button } from "@vector-im/compound-web";
2023-09-18 15:45:48 -04:00
import classNames from "classnames";
2025-03-13 13:58:43 +01:00
import { logger } from "matrix-js-sdk/lib/logger";
2024-10-28 17:29:26 -04:00
import { usePreviewTracks } from "@livekit/components-react";
2024-12-06 18:12:51 +01:00
import {
type CreateLocalTracksOptions,
type LocalVideoTrack,
2024-12-06 18:12:51 +01:00
Track,
} from "livekit-client";
2024-10-28 17:29:26 -04:00
import { useObservable } from "observable-hooks";
import { map } from "rxjs";
2025-01-06 18:00:20 +01:00
import { useNavigate } from "react-router-dom";
2022-08-02 00:46:16 +02:00
2023-09-18 15:45:48 -04:00
import inCallStyles from "./InCallView.module.css";
2024-04-23 15:15:13 +02:00
import styles from "./LobbyView.module.css";
2022-01-05 13:09:12 -08:00
import { Header, LeftNav, RightNav, RoomHeaderInfo } from "../Header";
import { type MatrixInfo, VideoPreview } from "./VideoPreview";
import { type MuteStates } from "./MuteStates";
2023-09-27 15:17:04 -04:00
import { InviteButton } from "../button/InviteButton";
2023-09-18 15:45:48 -04:00
import {
EndCallButton,
2023-09-18 15:45:48 -04:00
MicButton,
SettingsButton,
2024-10-28 17:29:26 -04:00
SwitchCameraButton,
2023-09-18 15:45:48 -04:00
VideoButton,
} from "../button/Button";
2023-12-01 17:43:09 -05:00
import { SettingsModal, defaultSettingsTab } from "../settings/SettingsModal";
2023-09-18 15:45:48 -04:00
import { useMediaQuery } from "../useMediaQuery";
2024-04-23 15:15:13 +02:00
import { E2eeType } from "../e2ee/e2eeType";
import { Link } from "../button/Link";
2024-10-28 17:29:26 -04:00
import { useMediaDevices } from "../livekit/MediaDevicesContext";
import { useInitial } from "../useInitial";
2024-11-20 17:22:24 +01:00
import { useSwitchCamera as useShowSwitchCamera } from "./useSwitchCamera";
2024-12-06 18:12:51 +01:00
import {
useTrackProcessor,
useTrackProcessorSync,
} from "../livekit/TrackProcessorContext";
import { usePageTitle } from "../usePageTitle";
2022-01-05 13:09:12 -08:00
2022-08-02 00:46:16 +02:00
interface Props {
client: MatrixClient;
2023-05-26 20:41:32 +02:00
matrixInfo: MatrixInfo;
2023-08-02 15:29:37 -04:00
muteStates: MuteStates;
2023-08-08 13:46:51 +02:00
onEnter: () => void;
2024-04-23 15:15:13 +02:00
enterLabel?: JSX.Element | string;
2023-09-18 20:47:47 -04:00
confineToRoom: boolean;
hideHeader: boolean;
2024-04-23 15:15:13 +02:00
participantCount: number | null;
onShareClick: (() => void) | null;
2024-04-23 15:15:13 +02:00
waitingForInvite?: boolean;
2022-08-02 00:46:16 +02:00
}
2023-08-02 15:29:37 -04:00
export const LobbyView: FC<Props> = ({
client,
2023-08-02 15:29:37 -04:00
matrixInfo,
muteStates,
onEnter,
2024-04-23 15:15:13 +02:00
enterLabel,
2023-09-18 20:47:47 -04:00
confineToRoom,
2023-08-02 15:29:37 -04:00
hideHeader,
participantCount,
onShareClick,
2024-04-23 15:15:13 +02:00
waitingForInvite,
2023-08-02 15:29:37 -04:00
}) => {
2023-05-26 20:41:32 +02:00
const { t } = useTranslation();
usePageTitle(matrixInfo.roomName);
2022-02-04 12:38:40 -08:00
2023-09-18 15:45:48 -04:00
const onAudioPress = useCallback(
() => muteStates.audio.setEnabled?.((e) => !e),
2023-10-11 10:42:04 -04:00
[muteStates],
2023-09-18 15:45:48 -04:00
);
const onVideoPress = useCallback(
() => muteStates.video.setEnabled?.((e) => !e),
2023-10-11 10:42:04 -04:00
[muteStates],
2023-09-18 15:45:48 -04:00
);
const [settingsModalOpen, setSettingsModalOpen] = useState(false);
2023-12-01 17:43:09 -05:00
const [settingsTab, setSettingsTab] = useState(defaultSettingsTab);
2023-09-18 15:45:48 -04:00
const openSettings = useCallback(
() => setSettingsModalOpen(true),
2023-10-11 10:42:04 -04:00
[setSettingsModalOpen],
2023-09-18 15:45:48 -04:00
);
const closeSettings = useCallback(
() => setSettingsModalOpen(false),
2023-10-11 10:42:04 -04:00
[setSettingsModalOpen],
2023-09-18 15:45:48 -04:00
);
2025-01-06 18:00:20 +01:00
const navigate = useNavigate();
const onLeaveClick = useCallback(() => {
navigate("/")?.catch((error) => {
logger.error("Failed to navigate to /", error);
});
}, [navigate]);
2023-09-18 15:45:48 -04:00
const recentsButtonInFooter = useMediaQuery("(max-height: 500px)");
2023-09-18 20:47:47 -04:00
const recentsButton = !confineToRoom && (
<Link className={styles.recents} to="/">
2023-11-20 13:00:43 +00:00
{t("lobby.leave_button")}
2023-09-18 15:45:48 -04:00
</Link>
);
2022-02-04 12:38:40 -08:00
2024-10-28 17:29:26 -04:00
const devices = useMediaDevices();
// Capture the audio options as they were when we first mounted, because
// we're not doing anything with the audio anyway so we don't need to
// re-open the devices when they change (see below).
const initialAudioOptions = useInitial(
() =>
muteStates.audio.enabled && { deviceId: devices.audioInput.selectedId },
);
const { processor } = useTrackProcessor();
2024-12-06 18:12:51 +01:00
const initialProcessor = useInitial(() => processor);
const localTrackOptions = useMemo<CreateLocalTracksOptions>(
2024-10-28 17:29:26 -04:00
() => ({
// The only reason we request audio here is to get the audio permission
// request over with at the same time. But changing the audio settings
// shouldn't cause this hook to recreate the track, which is why we
// reference the initial values here.
// We also pass in a clone because livekit mutates the object passed in,
// which would cause the devices to be re-opened on the next render.
audio: Object.assign({}, initialAudioOptions),
video: muteStates.video.enabled && {
deviceId: devices.videoInput.selectedId,
2024-12-06 18:12:51 +01:00
processor: initialProcessor,
2024-10-28 17:29:26 -04:00
},
}),
[
initialAudioOptions,
muteStates.video.enabled,
2024-11-20 17:22:24 +01:00
devices.videoInput.selectedId,
2024-12-06 18:12:51 +01:00
initialProcessor,
2024-10-28 17:29:26 -04:00
],
);
const onError = useCallback(
(error: Error) => {
logger.error("Error while creating preview Tracks:", error);
muteStates.audio.setEnabled?.(false);
muteStates.video.setEnabled?.(false);
},
[muteStates.audio, muteStates.video],
);
const tracks = usePreviewTracks(localTrackOptions, onError);
2024-12-06 18:12:51 +01:00
const videoTrack = useMemo(() => {
const track = tracks?.find((t) => t.kind === Track.Kind.Video);
return track as LocalVideoTrack | null;
}, [tracks]);
useTrackProcessorSync(videoTrack);
2024-11-20 17:22:24 +01:00
const showSwitchCamera = useShowSwitchCamera(
2024-10-28 17:29:26 -04:00
useObservable(
(inputs$) => inputs$.pipe(map(([video]) => video)),
2024-10-28 17:29:26 -04:00
[videoTrack],
),
);
2023-09-18 15:45:48 -04:00
// TODO: Unify this component with InCallView, so we can get slick joining
// animations and don't have to feel bad about reusing its CSS
2022-01-05 13:09:12 -08:00
return (
2023-09-18 15:45:48 -04:00
<>
<div className={classNames(styles.room, inCallStyles.inRoom)}>
{!hideHeader && (
<Header>
<LeftNav>
<RoomHeaderInfo
id={matrixInfo.roomId}
name={matrixInfo.roomName}
avatarUrl={matrixInfo.roomAvatar}
2024-04-23 15:15:13 +02:00
encrypted={matrixInfo.e2eeSystem.kind !== E2eeType.NONE}
participantCount={participantCount}
2023-09-18 15:45:48 -04:00
/>
</LeftNav>
<RightNav>
2023-09-27 15:17:04 -04:00
{onShareClick !== null && <InviteButton onClick={onShareClick} />}
2023-09-18 15:45:48 -04:00
</RightNav>
</Header>
)}
<div className={styles.content}>
2024-10-28 17:29:26 -04:00
<VideoPreview
matrixInfo={matrixInfo}
muteStates={muteStates}
videoTrack={videoTrack}
>
2022-10-24 10:17:12 -04:00
<Button
2024-04-23 15:15:13 +02:00
className={classNames(styles.join, {
[styles.wait]: waitingForInvite,
})}
size={waitingForInvite ? "sm" : "lg"}
onClick={() => {
if (!waitingForInvite) onEnter();
}}
data-testid="lobby_joinCall"
2022-10-24 10:17:12 -04:00
>
2024-04-23 15:15:13 +02:00
{enterLabel ?? t("lobby.join_button")}
2022-10-24 10:17:12 -04:00
</Button>
2023-09-18 15:45:48 -04:00
</VideoPreview>
{!recentsButtonInFooter && recentsButton}
</div>
<div className={inCallStyles.footer}>
{recentsButtonInFooter && recentsButton}
<div className={inCallStyles.buttons}>
<MicButton
muted={!muteStates.audio.enabled}
onClick={onAudioPress}
2023-09-18 15:45:48 -04:00
disabled={muteStates.audio.setEnabled === null}
/>
2023-09-25 10:39:18 -04:00
<VideoButton
muted={!muteStates.video.enabled}
onClick={onVideoPress}
2023-09-25 10:39:18 -04:00
disabled={muteStates.video.setEnabled === null}
/>
2024-11-20 17:22:24 +01:00
{showSwitchCamera && (
<SwitchCameraButton onClick={showSwitchCamera} />
)}
<SettingsButton onClick={openSettings} />
{!confineToRoom && <EndCallButton onClick={onLeaveClick} />}
2023-09-18 15:45:48 -04:00
</div>
2022-01-05 13:09:12 -08:00
</div>
</div>
2023-09-18 15:45:48 -04:00
{client && (
<SettingsModal
client={client}
open={settingsModalOpen}
onDismiss={closeSettings}
2023-12-01 17:43:09 -05:00
tab={settingsTab}
onTabChange={setSettingsTab}
2023-09-18 15:45:48 -04:00
/>
)}
</>
2022-01-05 13:09:12 -08:00
);
2023-08-02 15:29:37 -04:00
};