import React, { useMemo } from 'react'; import { Pressable, type StyleProp, StyleSheet, View, type ViewStyle, } from 'react-native'; import { Z_INDEX } from '../../../constants'; import { ComponentTestIds } from '../../../constants/TestIds'; import { VideoSlash } from '../../../icons'; import FloatingView from './FloatingView'; import { type CallContentProps, type CallParticipantsListComponentProps, } from '../../Call'; import { FloatingViewAlignment } from './FloatingView/common'; import { ParticipantView as DefaultParticipantView, type ParticipantViewComponentProps, type ParticipantViewProps, } from '../ParticipantView'; import { useTheme } from '../../../contexts/ThemeContext'; import { useFloatingVideoDimensions } from './useFloatingVideoDimensions'; import { type StreamVideoParticipant } from '@stream-io/video-client'; export type FloatingParticipantViewAlignment = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'; /** * Props to be passed for the LocalVideoView component. */ export type FloatingParticipantViewProps = ParticipantViewComponentProps & Pick & Pick & Pick & { /** * Determines where the floating participant video will be placed. */ alignment?: FloatingParticipantViewAlignment; /** * The participant to be rendered in the FloatingParticipantView */ participant?: StreamVideoParticipant; /** * Custom style to be merged with the container of the participant view. */ participantViewStyle?: StyleProp; /** * Custom style to be merged with the absolute container of the floating participant view. * This is the container that holds the participant view and the whole of its draggable area. */ draggableContainerStyle?: StyleProp; /** * Handler used to handle actions on click of the participant view in FloatingParticipantView. * Eg: Can be used to handle participant switch on click. */ onPressHandler?: () => void; }; const floatingAlignmentMap: Record< FloatingParticipantViewAlignment, FloatingViewAlignment > = { 'top-left': FloatingViewAlignment.topLeft, 'top-right': FloatingViewAlignment.topRight, 'bottom-left': FloatingViewAlignment.bottomLeft, 'bottom-right': FloatingViewAlignment.bottomRight, }; const DefaultLocalParticipantViewVideoFallback = () => { const { theme: { colors, floatingParticipantsView, variants: { iconSizes }, defaults, }, } = useTheme(); return ( ); }; /** * A component to render the floating participant's video. */ export const FloatingParticipantView = ({ alignment = 'top-right', onPressHandler, participant, participantViewStyle, draggableContainerStyle, ParticipantView = DefaultParticipantView, ParticipantNetworkQualityIndicator, ParticipantVideoFallback = DefaultLocalParticipantViewVideoFallback, ParticipantReaction, VideoRenderer, supportedReactions, videoZOrder = 1, objectFit, mirror, }: FloatingParticipantViewProps) => { const { theme: { colors, floatingParticipantsView, variants: { spacingSizes }, }, } = useTheme(); const [containerDimensions, setContainerDimensions] = React.useState<{ width: number; height: number; }>(); const floatingVideoDimensions = useFloatingVideoDimensions( containerDimensions, participant, 'videoTrack', ); const participantViewProps: ParticipantViewComponentProps = useMemo( () => ({ ParticipantLabel: null, ParticipantNetworkQualityIndicator, ParticipantReaction, ParticipantVideoFallback, VideoRenderer, }), [ ParticipantNetworkQualityIndicator, ParticipantReaction, ParticipantVideoFallback, VideoRenderer, ], ); if (!participant) { return null; } return ( { const { width, height } = event.nativeEvent.layout; setContainerDimensions((prev) => { if (prev && prev.width === width && prev.height === height) { return prev; } return { width: width, height: height, }; }); }} > {containerDimensions && floatingVideoDimensions && ( {ParticipantView && ( )} )} ); }; const styles = StyleSheet.create({ container: { // Needed to make the view on top and draggable zIndex: Z_INDEX.IN_MIDDLE, flex: 1, }, participantViewContainer: { shadowOffset: { width: 0, height: 2, }, shadowOpacity: 0.23, shadowRadius: 2.62, elevation: 4, }, videoFallback: { alignItems: 'center', justifyContent: 'center', }, });