import React, { useEffect, useState } from 'react'; import { useDaily } from '@daily-co/daily-react'; import { MainVideo } from './MainVideo'; import { PiPContainer } from './PiPContainer'; import { MicSelectBtn, CameraSelectBtn } from '../cvi/components/device-select'; import { SlidevControls } from '../SlidevControls'; import { useScreenshare } from '../../hooks/useScreenshare'; import styles from './ExtendedConversationBlock.module.css'; export interface ExtendedConversationBlockProps { /** Daily.co room URL to join */ conversationUrl: string; /** Demo session ID for Slidev generation */ sessionId: string; /** Callback when user leaves the conversation */ onLeave?: () => void; } /** * ExtendedConversationBlock is the main conversation UI for multi-participant demos. * * Integrates: * - MainVideo: Displays Tavus avatar (Discovery) or Slidev screenshare (Presentation) * - PiPContainer: Shows customer self-view and avatar PiP * - SlidevControls: Generate and control slide presentations * - Media Controls: Mic and camera selection, leave button * * Handles Daily.co room lifecycle (join on mount, leave on unmount/button). * * @example * ```tsx * navigate('/')} * /> * ``` */ export const ExtendedConversationBlock: React.FC = ({ conversationUrl, sessionId, onLeave, }) => { const daily = useDaily(); const [isJoining, setIsJoining] = useState(false); const [joinError, setJoinError] = useState(null); const { startSharing, isSharing } = useScreenshare(); // Join Daily.co room on mount useEffect(() => { if (!daily || !conversationUrl || isJoining) return; const joinRoom = async () => { setIsJoining(true); setJoinError(null); try { // Small delay to ensure previous leave() has completed await new Promise(resolve => setTimeout(resolve, 100)); await daily.join({ url: conversationUrl }); console.log('[ExtendedConversationBlock] Joined room:', conversationUrl); } catch (error) { console.error('[ExtendedConversationBlock] Failed to join room:', error); setJoinError(error instanceof Error ? error.message : 'Failed to join room'); } finally { setIsJoining(false); } }; void joinRoom(); // Leave room on unmount return () => { if (daily) { daily.leave().catch((err) => { console.error('[ExtendedConversationBlock] Error leaving room:', err); }); } }; }, [daily, conversationUrl, isJoining]); const handleLeave = async () => { if (daily) { try { await daily.leave(); console.log('[ExtendedConversationBlock] Left room'); } catch (error) { console.error('[ExtendedConversationBlock] Error leaving:', error); } } onLeave?.(); }; // Show error state if (joinError) { return (
⚠️

Unable to Join Demo

{joinError}

); } // Show loading state while joining if (isJoining) { return (

Connecting to demo...

); } // Handle deck generation completion - auto-trigger screenshare const handleDeckGenerated = async (deckUrl: string, deckId: string) => { console.log('[ExtendedConversationBlock] Deck generated:', deckUrl, deckId); // Automatically start screenshare when deck is ready try { await startSharing(deckUrl); console.log('[ExtendedConversationBlock] Screenshare started for deck'); } catch (error) { console.error('[ExtendedConversationBlock] Failed to start screenshare:', error); // Don't throw - SlidevControls will show the deck URL for manual opening } }; return (
{/* Slidev presentation controls */}
void handleDeckGenerated(deckUrl, deckId)} onError={(error) => { console.error('[ExtendedConversationBlock] Slidev error:', error); }} />
{isSharing && (
📺 Screensharing presentation
)}
); };