import React, { useEffect, useRef } from 'react'; import { useRecoilState } from 'recoil'; import { Phone, PhoneOff, AlertCircle, Mic, MicOff, Volume2, VolumeX, Activity, } from 'lucide-react'; import { OGDialog, OGDialogContent, Button } from '~/components'; import { useWebSocket, useCall } from '~/hooks'; import { CallState } from '~/common'; import store from '~/store'; export const Call: React.FC = () => { const { isConnected } = useWebSocket(); const { callState, error, startCall, hangUp, isConnecting, localStream, remoteStream, connectionQuality, isMuted, toggleMute, } = useCall(); const [open, setOpen] = useRecoilState(store.callDialogOpen(0)); const [eventLog, setEventLog] = React.useState([]); const [isAudioEnabled, setIsAudioEnabled] = React.useState(true); const remoteAudioRef = useRef(null); const logEvent = (message: string) => { console.log(message); setEventLog((prev) => [...prev, `${new Date().toISOString()}: ${message}`]); }; useEffect(() => { if (remoteAudioRef.current && remoteStream) { remoteAudioRef.current.srcObject = remoteStream; remoteAudioRef.current.play().catch((err) => console.error('Error playing audio:', err)); } }, [remoteStream]); useEffect(() => { if (localStream) { localStream.getAudioTracks().forEach((track) => { track.enabled = !isMuted; }); } }, [localStream, isMuted]); useEffect(() => { if (isConnected) { logEvent('Connected to server.'); } else { logEvent('Disconnected from server.'); } }, [isConnected]); useEffect(() => { if (error) { logEvent(`Error: ${error.message} (${error.code})`); } }, [error]); useEffect(() => { logEvent(`Call state changed to: ${callState}`); }, [callState]); const handleStartCall = () => { logEvent('Attempting to start call...'); startCall(); }; const handleHangUp = () => { logEvent('Attempting to hang up call...'); hangUp(); }; const handleToggleMute = () => { toggleMute(); logEvent(`Microphone ${isMuted ? 'unmuted' : 'muted'}`); }; const toggleAudio = () => { setIsAudioEnabled((prev) => !prev); if (remoteAudioRef.current) { remoteAudioRef.current.muted = !isAudioEnabled; } logEvent(`Speaker ${isAudioEnabled ? 'disabled' : 'enabled'}`); }; const isActive = callState === CallState.ACTIVE; const isError = callState === CallState.ERROR; // TESTS useEffect(() => { if (remoteAudioRef.current && remoteStream) { console.log('Setting up remote audio:', { tracks: remoteStream.getTracks().length, active: remoteStream.active, }); remoteAudioRef.current.srcObject = remoteStream; remoteAudioRef.current.muted = false; remoteAudioRef.current.volume = 1.0; const playPromise = remoteAudioRef.current.play(); if (playPromise) { playPromise.catch((err) => { console.error('Error playing audio:', err); // Retry play on user interaction document.addEventListener( 'click', () => { remoteAudioRef.current?.play(); }, { once: true }, ); }); } } }, [remoteStream]); return (
{/* Connection Status */}
{isConnected ? 'Connected' : 'Disconnected'}
{isActive && (
{connectionQuality} Quality
)}
{/* Error Display */} {error && (
{error.message}
)} {/* Call Controls */}
{isActive && ( <> )} {isActive ? ( ) : ( )}
{/* Event Log */}

Event Log

    {eventLog.map((log, index) => (
  • {log}
  • ))}
{/* Hidden Audio Element */}
); };