import React, { useEffect, useRef } from 'react'; import useVideoTrack from '@dailyjs/shared/hooks/useVideoTrack'; import { ReactComponent as IconMicMute } from '@dailyjs/shared/icons/mic-off-sm.svg'; import classNames from 'classnames'; import PropTypes from 'prop-types'; import { DEFAULT_ASPECT_RATIO } from '../../constants'; import { Video } from './Video'; import { ReactComponent as Avatar } from './avatar.svg'; export const Tile = React.memo( ({ participant, mirrored = true, showName = true, showAvatar = true, aspectRatio = DEFAULT_ASPECT_RATIO, onVideoResize, ...props }) => { const videoTrack = useVideoTrack(participant); const videoEl = useRef(null); /** * Add optional event listener for resize event so the parent component * can know the video's native aspect ratio. */ useEffect(() => { const video = videoEl.current; if (!onVideoResize || !video) return false; const handleResize = () => { if (!video) return; const width = video?.videoWidth; const height = video?.videoHeight; if (width && height) { // Return the video's aspect ratio to the parent's handler onVideoResize(width / height); } }; handleResize(); video?.addEventListener('resize', handleResize); return () => video?.removeEventListener('resize', handleResize); }, [onVideoResize, videoEl, participant]); const cx = classNames('tile', { mirrored, avatar: showAvatar && !videoTrack, active: participant.isActiveSpeaker, }); return (
{showName && (
{participant.isMicMuted && } {participant.name}
)} {videoTrack ? (
); } ); Tile.propTypes = { participant: PropTypes.object.isRequired, mirrored: PropTypes.bool, showName: PropTypes.bool, showAvatar: PropTypes.bool, aspectRatio: PropTypes.number, onVideoResize: PropTypes.func, }; export default Tile;