/* 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, MicOffSolidIcon, MicOffIcon, OverflowHorizontalIcon, VolumeOnIcon, VolumeOffIcon, VisibilityOnIcon, UserProfileIcon, ExpandIcon, } from "@vector-im/compound-design-tokens/assets/web/icons"; import { ContextMenu, MenuItem, ToggleMenuItem, Menu, } from "@vector-im/compound-web"; import { useObservableEagerState } from "observable-hooks"; import styles from "./GridTile.module.css"; import { 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; displayName: string; nameTag: string; showSpeakingIndicators: boolean; } interface UserMediaTileProps extends TileProps { vm: UserMediaViewModel; mirror: boolean; menuStart?: ReactNode; menuEnd?: ReactNode; } const UserMediaTile = forwardRef( ( { vm, showSpeakingIndicators, menuStart, menuEnd, className, nameTag, ...props }, ref, ) => { const { t } = useTranslation(); const video = useObservableEagerState(vm.video); const unencryptedWarning = useObservableEagerState(vm.unencryptedWarning); 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; } 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; } 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 GridTileProps { vm: UserMediaViewModel; onOpenProfile: () => void; targetWidth: number; targetHeight: number; className?: string; style?: ComponentProps["style"]; showSpeakingIndicators: boolean; } export const GridTile = forwardRef( ({ vm, onOpenProfile, ...props }, ref) => { const nameData = useNameData(vm); if (vm instanceof LocalUserMediaViewModel) { return ( ); } else { return ; } }, ); GridTile.displayName = "GridTile";