import { useEffect } from 'react'; import { useNavigate, useLocation } from 'react-router'; import { faPhone, faPhoneArrowDown, faPhoneXmark, faCircleCheck } from '@fortawesome/pro-duotone-svg-icons'; import { faIcon } from '@/lib/icon-wrapper'; const Phone01 = faIcon(faPhone); const PhoneIncoming01 = faIcon(faPhoneArrowDown); const PhoneX = faIcon(faPhoneXmark); const CheckCircle = faIcon(faCircleCheck); import { Button } from '@/components/base/buttons/button'; import { useSetAtom } from 'jotai'; import { sipCallStateAtom } from '@/state/sip-state'; import { useSip } from '@/providers/sip-provider'; import { cx } from '@/utils/cx'; const formatDuration = (seconds: number): string => { const m = Math.floor(seconds / 60).toString().padStart(2, '0'); const s = (seconds % 60).toString().padStart(2, '0'); return `${m}:${s}`; }; // CallWidget is a lightweight floating notification for calls outside the Call Desk. // It only handles: ringing (answer/decline) + auto-redirect to Call Desk. // All active call management (mute, hold, end, disposition) happens on the Call Desk via ActiveCallCard. export const CallWidget = () => { const { callState, callerNumber, callDuration, answer, reject } = useSip(); const setCallState = useSetAtom(sipCallStateAtom); const navigate = useNavigate(); const { pathname } = useLocation(); // Auto-navigate to Call Desk when a call becomes active or outbound ringing starts useEffect(() => { if (pathname === '/call-desk') return; if (callState === 'active' || callState === 'ringing-out') { console.log(`[CALL-WIDGET] Redirecting to Call Desk (state=${callState})`); navigate('/call-desk'); } }, [callState, pathname, navigate]); // Auto-dismiss ended/failed state after 3 seconds useEffect(() => { if (callState === 'ended' || callState === 'failed') { const timer = setTimeout(() => { console.log('[CALL-WIDGET] Auto-dismissing ended/failed state'); setCallState('idle'); }, 3000); return () => clearTimeout(timer); } }, [callState, setCallState]); // Log state changes useEffect(() => { if (callState !== 'idle') { console.log(`[CALL-WIDGET] State: ${callState} | caller=${callerNumber ?? 'none'}`); } }, [callState, callerNumber]); if (callState === 'idle') return null; // Ringing inbound — answer redirects to Call Desk if (callState === 'ringing-in') { return (