/* Copyright 2022-2024 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 { ComponentProps, ReactNode, forwardRef, useCallback, useState, } from "react"; import { animated } from "@react-spring/web"; import classNames from "classnames"; import { useTranslation } from "react-i18next"; import MicOnSolidIcon from "@vector-im/compound-design-tokens/icons/mic-on-solid.svg?react"; import MicOffSolidIcon from "@vector-im/compound-design-tokens/icons/mic-off-solid.svg?react"; import MicOffIcon from "@vector-im/compound-design-tokens/icons/mic-off.svg?react"; import OverflowHorizontalIcon from "@vector-im/compound-design-tokens/icons/overflow-horizontal.svg?react"; import VolumeOnIcon from "@vector-im/compound-design-tokens/icons/volume-on.svg?react"; import VolumeOffIcon from "@vector-im/compound-design-tokens/icons/volume-off.svg?react"; import VisibilityOnIcon from "@vector-im/compound-design-tokens/icons/visibility-on.svg?react"; import UserProfileIcon from "@vector-im/compound-design-tokens/icons/user-profile.svg?react"; import ExpandIcon from "@vector-im/compound-design-tokens/icons/expand.svg?react"; import CollapseIcon from "@vector-im/compound-design-tokens/icons/collapse.svg?react"; import { ContextMenu, MenuItem, ToggleMenuItem, Menu, } from "@vector-im/compound-web"; import { useObservableEagerState } from "observable-hooks"; import styles from "./GridTile.module.css"; import { ScreenShareViewModel, MediaViewModel, UserMediaViewModel, useNameData, LocalUserMediaViewModel, RemoteUserMediaViewModel, } from "../state/MediaViewModel"; import { Slider } from "../Slider"; import { MediaView } from "./MediaView"; import { useLatest } from "../useLatest"; interface TileProps { className?: string; style?: ComponentProps["style"]; targetWidth: number; targetHeight: number; maximised: boolean; displayName: string; nameTag: string; } interface MediaTileProps extends TileProps, Omit, "className"> { vm: MediaViewModel; videoEnabled: boolean; videoFit: "contain" | "cover"; mirror: boolean; nameTagLeadingIcon?: ReactNode; primaryButton: ReactNode; secondaryButton?: ReactNode; } const MediaTile = forwardRef( ({ vm, className, maximised, ...props }, ref) => { const video = useObservableEagerState(vm.video); const unencryptedWarning = useObservableEagerState(vm.unencryptedWarning); return ( ); }, ); MediaTile.displayName = "MediaTile"; interface UserMediaTileProps extends TileProps { vm: UserMediaViewModel; mirror: boolean; showSpeakingIndicators: boolean; menuStart?: ReactNode; menuEnd?: ReactNode; } const UserMediaTile = forwardRef( ( { vm, showSpeakingIndicators, menuStart, menuEnd, className, nameTag, ...props }, ref, ) => { const { t } = useTranslation(); const audioEnabled = useObservableEagerState(vm.audioEnabled); const videoEnabled = useObservableEagerState(vm.videoEnabled); const speaking = useObservableEagerState(vm.speaking); const cropVideo = useObservableEagerState(vm.cropVideo); const onChangeFitContain = useCallback(() => vm.toggleFitContain(), [vm]); const onSelectFitContain = useCallback( (e: Event) => e.preventDefault(), [], ); const MicIcon = audioEnabled ? MicOnSolidIcon : MicOffSolidIcon; const [menuOpen, setMenuOpen] = useState(false); const menu = ( <> {menuStart} {menuEnd} ); const tile = ( } nameTag={nameTag} primaryButton={ } side="left" align="start" > {menu} } {...props} /> ); return ( {menu} ); }, ); UserMediaTile.displayName = "UserMediaTile"; interface LocalUserMediaTileProps extends TileProps { vm: LocalUserMediaViewModel; onOpenProfile: () => void; showSpeakingIndicators: boolean; } const LocalUserMediaTile = forwardRef( ({ vm, onOpenProfile, ...props }, ref) => { const { t } = useTranslation(); const mirror = useObservableEagerState(vm.mirror); const alwaysShow = useObservableEagerState(vm.alwaysShow); const latestAlwaysShow = useLatest(alwaysShow); const onSelectAlwaysShow = useCallback( (e: Event) => e.preventDefault(), [], ); const onChangeAlwaysShow = useCallback( () => vm.setAlwaysShow(!latestAlwaysShow.current), [vm, latestAlwaysShow], ); return ( } menuEnd={ } {...props} /> ); }, ); LocalUserMediaTile.displayName = "LocalUserMediaTile"; interface RemoteUserMediaTileProps extends TileProps { vm: RemoteUserMediaViewModel; showSpeakingIndicators: boolean; } const RemoteUserMediaTile = forwardRef< HTMLDivElement, RemoteUserMediaTileProps >(({ vm, ...props }, ref) => { const { t } = useTranslation(); const locallyMuted = useObservableEagerState(vm.locallyMuted); const localVolume = useObservableEagerState(vm.localVolume); const onChangeMute = useCallback(() => vm.toggleLocallyMuted(), [vm]); const onSelectMute = useCallback((e: Event) => e.preventDefault(), []); const onChangeLocalVolume = useCallback( (v: number) => vm.setLocalVolume(v), [vm], ); const VolumeIcon = locallyMuted ? VolumeOffIcon : VolumeOnIcon; return ( {/* TODO: Figure out how to make this slider keyboard accessible */} } {...props} /> ); }); RemoteUserMediaTile.displayName = "RemoteUserMediaTile"; interface ScreenShareTileProps extends TileProps { vm: ScreenShareViewModel; fullscreen: boolean; onToggleFullscreen: (itemId: string) => void; } const ScreenShareTile = forwardRef( ({ vm, fullscreen, onToggleFullscreen, ...props }, ref) => { const { t } = useTranslation(); const onClickFullScreen = useCallback( () => onToggleFullscreen(vm.id), [onToggleFullscreen, vm], ); const FullScreenIcon = fullscreen ? CollapseIcon : ExpandIcon; return ( ) } {...props} /> ); }, ); ScreenShareTile.displayName = "ScreenShareTile"; interface GridTileProps { vm: MediaViewModel; maximised: boolean; fullscreen: boolean; onToggleFullscreen: (itemId: string) => void; onOpenProfile: () => void; targetWidth: number; targetHeight: number; className?: string; style?: ComponentProps["style"]; showSpeakingIndicators: boolean; } export const GridTile = forwardRef( ({ vm, fullscreen, onToggleFullscreen, onOpenProfile, ...props }, ref) => { const nameData = useNameData(vm); if (vm instanceof LocalUserMediaViewModel) { return ( ); } else if (vm instanceof RemoteUserMediaViewModel) { return ; } else { return ( ); } }, ); GridTile.displayName = "GridTile";