import React, { useEffect, useCallback, useState } from "react"; import { DailyAudio, DailyVideo, useLocalSessionId, useMeetingState, useParticipantIds, useScreenVideoTrack, useVideoTrack } from "@daily-co/daily-react"; import { logger } from "@buzztrail/shared-utils"; import { useLocalCamera } from "../cvi/hooks/use-local-camera"; import { useLocalMicrophone } from "../cvi/hooks/use-local-microphone"; import { useLocalScreenshare } from "../cvi/hooks/use-local-screenshare"; import { useReplicaIDs } from "../cvi/hooks/use-replica-ids"; import { useCVICall } from "../cvi/hooks/use-cvi-call"; import { AudioWave } from "../audio-wave"; import { HairCheck } from "../hair-check"; import styles from "./conversation.module.css"; interface ConversationProps { onLeave: () => void; onTimeout?: () => void; // Callback for when conversation times out conversationUrl: string; showSelfView?: boolean; // Control self-view visibility compactMode?: boolean; // Enable compact mode for smaller self-view maxDuration?: number; // Maximum duration in milliseconds (default: 15000) showHairCheck?: boolean; // Show hair check interface for device testing presentationMode?: 'auto' | 'fullscreen' | 'split'; // Control presentation mode behavior onPresentationStart?: () => void; // Callback when presentation mode starts onPresentationEnd?: () => void; // Callback when presentation mode ends } const VideoPreview = React.memo(({ id, compactMode = false }: { id: string; compactMode?: boolean }) => { const videoState = useVideoTrack(id); const widthVideo = videoState.track?.getSettings()?.width; const heightVideo = videoState.track?.getSettings()?.height; const isVertical = widthVideo && heightVideo ? widthVideo < heightVideo : false; return (
); }); const PreviewVideos = React.memo(({ showSelfView = true, compactMode = false }: { showSelfView?: boolean; compactMode?: boolean }) => { const localId = useLocalSessionId(); const { isScreenSharing } = useLocalScreenshare(); const replicaIds = useReplicaIDs(); const replicaId = replicaIds[0]; if (!showSelfView) { return null; } return ( <> {isScreenSharing && replicaId && } ); }); const MainVideo = React.memo(({ isPresenting, screenshareBotId }: { isPresenting: boolean; screenshareBotId?: string; }) => { const replicaIds = useReplicaIDs(); const localId = useLocalSessionId(); const replicaId = replicaIds[0]; const videoState = useVideoTrack(replicaId ?? ""); const screenVideoState = useScreenVideoTrack(localId); const isScreenSharing = !screenVideoState.isOff; logger.debug('[MainVideo] Video state', { replicaIds, localId, replicaId, videoState: { isOff: videoState.isOff, track: !!videoState.track }, isScreenSharing, isPresenting, screenshareBotId }); // This is one-to-one call, so we can use the first replica id // Already derived above if (!replicaId) { logger.debug('[MainVideo] No replicaId, showing connecting screen'); return (

Connecting...

); } // In presentation mode, show screenshare bot's screen if (isPresenting && screenshareBotId) { return (
); } // Switching between replica video and screen sharing video (original 2-participant logic) return (
); }); export const Conversation = React.memo(({ onLeave, onTimeout, conversationUrl, showSelfView = true, compactMode = false, maxDuration = 15000, showHairCheck = false, presentationMode = 'auto', onPresentationStart, onPresentationEnd }: ConversationProps) => { logger.debug('[Conversation] Component mounted with props', { conversationUrl, showSelfView, compactMode, maxDuration, showHairCheck, presentationMode }); const { joinCall, leaveCall } = useCVICall(); const meetingState = useMeetingState(); const { isMicReady } = useLocalMicrophone(); const { isCamReady } = useLocalCamera(); const [isHairCheckComplete, setIsHairCheckComplete] = useState(false); // Detect participants for 3-participant presentation mode const participantIds = useParticipantIds({ filter: 'remote' }); const localId = useLocalSessionId(); const replicaIds = useReplicaIDs(); const replicaId = replicaIds[0]; // Find screenshare bot participant (3rd participant) // The screenshare bot will be a remote participant that's not the replica const screenshareBotId = participantIds.find(id => id !== replicaId && id !== localId ); // Check if screenshare bot is actively sharing const screenshareBotVideoState = useScreenVideoTrack(screenshareBotId ?? ""); const isPresenting = !!screenshareBotId && !screenshareBotVideoState.isOff; logger.debug('[Conversation] Current state', { meetingState, isMicReady, isCamReady, isHairCheckComplete, participantCount: participantIds.length, screenshareBotId, isPresenting }); useEffect(() => { if (meetingState === 'error') { onLeave(); } }, [meetingState, onLeave]); // Handle presentation mode start/end callbacks useEffect(() => { if (isPresenting && onPresentationStart) { logger.debug('[Conversation] Presentation mode started'); onPresentationStart(); } else if (!isPresenting && onPresentationEnd) { logger.debug('[Conversation] Presentation mode ended'); onPresentationEnd(); } }, [isPresenting, onPresentationStart, onPresentationEnd]); // Initialize call when conversation is available useEffect(() => { if (!showHairCheck || isHairCheckComplete) { joinCall({ url: conversationUrl }); } }, [conversationUrl, joinCall, showHairCheck, isHairCheckComplete]); // Auto-end conversation after maxDuration useEffect((): (() => void) | undefined => { if (meetingState === 'joined-meeting') { logger.debug('[Conversation] Starting auto-end timer', { maxDuration }); const timer = setTimeout(() => { logger.debug('[Conversation] Auto-ending conversation', { maxDuration }); leaveCall(); // Call onTimeout if provided, otherwise fall back to onLeave if (onTimeout) { onTimeout(); } else { onLeave(); } }, maxDuration); return () => { logger.debug('[Conversation] Clearing auto-end timer'); clearTimeout(timer); }; } return undefined; }, [meetingState, maxDuration, leaveCall, onLeave, onTimeout]); const handleLeave = useCallback(() => { leaveCall(); onLeave(); }, [leaveCall, onLeave]); return (
{showHairCheck && !isHairCheckComplete ? ( // Show HairCheck interface for device testing
setIsHairCheckComplete(true)} onCancel={onLeave} />
) : ( // Show regular conversation interface
{ !isMicReady && !isCamReady && (

Camera or microphone access denied. Please check your settings and try again.

)} {/* Main video */}
{/* Presentation mode: Show thumbnails in corner */} {isPresenting && replicaId && (
{/* Avatar thumbnail */}
{/* Customer thumbnail */}
)} {/* Discovery mode: Show self view (original 2-participant) */} {!isPresenting && (
)}
)} {(!showHairCheck || isHairCheckComplete) && (
)}
); });