import React, { useRef, useEffect, useState, useCallback, useMemo } from 'react'; import { useNavigate } from 'react-router-dom'; import { useVoiceStore } from '../../stores/voiceStore'; import { useChatStore } from '../../stores/chatStore'; import { useUIStore } from '../../stores/uiStore'; import { useServerStore } from '../../stores/serverStore'; import { Avatar } from '../ui/Avatar'; import type { ParticipantInfo } from '../../hooks/useLiveKit'; const PIP_WIDTH = 320; const PIP_HEIGHT = 180; const PIP_MARGIN = 16; const DRAG_THRESHOLD = 5; interface SelectedStream { participant: ParticipantInfo; track: MediaStreamTrack; type: 'screen' | 'camera'; } function selectPipStream( participants: ParticipantInfo[], focusedId: string | null, watchingStreams: Set, ): SelectedStream | null { // Priority 1: Screen share from a user we're watching const screenSharer = participants.find( p => p.screenTrack !== null && watchingStreams.has(p.userId), ); if (screenSharer?.screenTrack) { return { participant: screenSharer, track: screenSharer.screenTrack, type: 'screen' }; } // Priority 2: Focused participant with camera if (focusedId) { const focused = participants.find(p => p.identity === focusedId); if (focused?.videoTrack) { return { participant: focused, track: focused.videoTrack, type: 'camera' }; } } // Priority 3: Remote participant with camera const remoteWithCamera = participants.find(p => !p.isLocal && p.videoTrack !== null); if (remoteWithCamera?.videoTrack) { return { participant: remoteWithCamera, track: remoteWithCamera.videoTrack, type: 'camera' }; } // Priority 4: Local participant with camera const localWithCamera = participants.find(p => p.isLocal && p.videoTrack !== null); if (localWithCamera?.videoTrack) { return { participant: localWithCamera, track: localWithCamera.videoTrack, type: 'camera' }; } return null; } export function PictureInPicture() { const navigate = useNavigate(); const videoRef = useRef(null); const containerRef = useRef(null); // Store state const currentVoiceChannelId = useVoiceStore((s) => s.currentVoiceChannelId); const activeDmCall = useVoiceStore((s) => s.activeDmCall); const participants = useVoiceStore((s) => s.participants); const focusedParticipantId = useVoiceStore((s) => s.focusedParticipantId); const watchingStreams = useVoiceStore((s) => s.watchingStreams); const speakingParticipantIds = useVoiceStore((s) => s.speakingParticipantIds); const currentChannelId = useChatStore((s) => s.currentChannelId); const voiceFullscreen = useUIStore((s) => s.voiceFullscreen); const pipCollapsed = useUIStore((s) => s.pipCollapsed); const setPipCollapsed = useUIStore((s) => s.setPipCollapsed); const channelToServerMap = useServerStore((s) => s.channelToServerMap); const channels = useServerStore((s) => s.channels); // Drag state const [position, setPosition] = useState<{ x: number; y: number }>({ x: -1, y: -1 }); const [isDragging, setIsDragging] = useState(false); const dragOffset = useRef({ x: 0, y: 0 }); const dragStartPos = useRef({ x: 0, y: 0 }); const hasMoved = useRef(false); // Reset pipCollapsed when joining a new call const prevVoiceChannel = useRef(currentVoiceChannelId); const prevDmCall = useRef(activeDmCall?.dmChannelId ?? null); useEffect(() => { const voiceChanged = currentVoiceChannelId !== prevVoiceChannel.current; const dmChanged = (activeDmCall?.dmChannelId ?? null) !== prevDmCall.current; prevVoiceChannel.current = currentVoiceChannelId; prevDmCall.current = activeDmCall?.dmChannelId ?? null; if ((voiceChanged && currentVoiceChannelId) || (dmChanged && activeDmCall)) { setPipCollapsed(false); } }, [currentVoiceChannelId, activeDmCall, setPipCollapsed]); // Visibility const isInServerVoice = currentVoiceChannelId !== null && currentChannelId !== currentVoiceChannelId; const isInDmCall = activeDmCall !== null && currentChannelId !== activeDmCall.dmChannelId; const shouldShow = (isInServerVoice || isInDmCall) && !voiceFullscreen && !pipCollapsed; // Stream selection const selectedStream = useMemo( () => selectPipStream(participants, focusedParticipantId, watchingStreams), [participants, focusedParticipantId, watchingStreams], ); // Fallback participant for avatar (most relevant remote, or first participant) const fallbackParticipant = useMemo(() => { const speaking = participants.find(p => !p.isLocal && speakingParticipantIds.has(p.identity)); if (speaking) return speaking; const remote = participants.find(p => !p.isLocal); if (remote) return remote; return participants[0] ?? null; }, [participants, speakingParticipantIds]); // Channel name for display const channelName = useMemo(() => { if (currentVoiceChannelId) { const ch = channels.find(c => c.id === currentVoiceChannelId); return ch?.name ?? 'Voice'; } return 'Call'; }, [currentVoiceChannelId, channels]); // Derive the LiveKit Track from the selected stream's participant const lkTrack = selectedStream ? (selectedStream.type === 'screen' ? selectedStream.participant.lkScreenTrack : selectedStream.participant.lkVideoTrack) : null; // Video track attachment — use LiveKit's track.attach() to register the element // with the adaptive stream observer (enables SFU layer switching by viewport size) // shouldShow in deps ensures re-run when PiP becomes visible (videoRef was null before) useEffect(() => { const videoEl = videoRef.current; if (!videoEl) return; if (lkTrack) { lkTrack.attach(videoEl); return () => { lkTrack.detach(videoEl); }; } else { videoEl.srcObject = null; } }, [lkTrack, shouldShow]); // Initialize position to bottom-right useEffect(() => { if (shouldShow && position.x === -1) { setPosition({ x: window.innerWidth - PIP_WIDTH - PIP_MARGIN, y: window.innerHeight - PIP_HEIGHT - PIP_MARGIN, }); } }, [shouldShow, position.x]); // Window resize: keep PiP in bounds useEffect(() => { if (!shouldShow) return; const handleResize = () => { setPosition(prev => ({ x: Math.max(PIP_MARGIN, Math.min(window.innerWidth - PIP_WIDTH - PIP_MARGIN, prev.x)), y: Math.max(PIP_MARGIN, Math.min(window.innerHeight - PIP_HEIGHT - PIP_MARGIN, prev.y)), })); }; window.addEventListener('resize', handleResize); return () => window.removeEventListener('resize', handleResize); }, [shouldShow]); // Snap to nearest horizontal edge const snapToEdge = useCallback((currentX: number, currentY: number) => { const centerX = currentX + PIP_WIDTH / 2; const screenMidX = window.innerWidth / 2; const targetX = centerX < screenMidX ? PIP_MARGIN : window.innerWidth - PIP_WIDTH - PIP_MARGIN; const clampedY = Math.max(PIP_MARGIN, Math.min(window.innerHeight - PIP_HEIGHT - PIP_MARGIN, currentY)); setPosition({ x: targetX, y: clampedY }); }, []); // Drag handlers const handlePointerDown = useCallback((e: React.PointerEvent) => { if ((e.target as HTMLElement).closest('[data-pip-action]')) return; setIsDragging(true); hasMoved.current = false; dragOffset.current = { x: e.clientX - position.x, y: e.clientY - position.y }; dragStartPos.current = { x: e.clientX, y: e.clientY }; containerRef.current?.setPointerCapture(e.pointerId); }, [position]); const handlePointerMove = useCallback((e: React.PointerEvent) => { if (!isDragging) return; const dx = Math.abs(e.clientX - dragStartPos.current.x); const dy = Math.abs(e.clientY - dragStartPos.current.y); if (dx > DRAG_THRESHOLD || dy > DRAG_THRESHOLD) { hasMoved.current = true; } const newX = Math.max(PIP_MARGIN, Math.min(window.innerWidth - PIP_WIDTH - PIP_MARGIN, e.clientX - dragOffset.current.x)); const newY = Math.max(PIP_MARGIN, Math.min(window.innerHeight - PIP_HEIGHT - PIP_MARGIN, e.clientY - dragOffset.current.y)); setPosition({ x: newX, y: newY }); }, [isDragging]); const handlePointerUp = useCallback((e: React.PointerEvent) => { if (!isDragging) return; setIsDragging(false); containerRef.current?.releasePointerCapture(e.pointerId); if (!hasMoved.current) { // Click — navigate back to voice channel if (activeDmCall) { navigate(`/channels/@me/${activeDmCall.dmChannelId}`); } else if (currentVoiceChannelId) { const serverId = channelToServerMap.get(currentVoiceChannelId); if (serverId) { navigate(`/channels/${serverId}/${currentVoiceChannelId}`); } } } else { // Drag ended — snap to edge snapToEdge(position.x, position.y); } }, [isDragging, activeDmCall, currentVoiceChannelId, channelToServerMap, navigate, snapToEdge, position]); const handleClose = useCallback((e: React.MouseEvent) => { e.stopPropagation(); setPipCollapsed(true); }, [setPipCollapsed]); if (!shouldShow) return null; const displayParticipant = selectedStream?.participant ?? fallbackParticipant; const displayName = displayParticipant ? (displayParticipant.isLocal ? `${displayParticipant.username} (You)` : displayParticipant.username) : channelName; const hasVideo = selectedStream !== null; const isScreen = selectedStream?.type === 'screen'; return (
{/* Video or Avatar fallback */} {hasVideo ? (