import { useState, useRef, useEffect } from 'react'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faPhone, faCommentDots, faEllipsisVertical, faMessageDots } from '@fortawesome/pro-duotone-svg-icons'; import { useSip } from '@/providers/sip-provider'; import { notify } from '@/lib/toast'; import { cx } from '@/utils/cx'; type PhoneActionCellProps = { phoneNumber: string; displayNumber: string; leadId?: string; onDial?: () => void; }; export const PhoneActionCell = ({ phoneNumber, displayNumber, leadId: _leadId, onDial }: PhoneActionCellProps) => { const { isRegistered, isInCall, dialOutbound } = useSip(); const [menuOpen, setMenuOpen] = useState(false); const [dialing, setDialing] = useState(false); const menuRef = useRef(null); const touchTimer = useRef(null); // Close menu on click outside useEffect(() => { if (!menuOpen) return; const handleClick = (e: MouseEvent) => { if (menuRef.current && !menuRef.current.contains(e.target as Node)) { setMenuOpen(false); } }; document.addEventListener('mousedown', handleClick); return () => document.removeEventListener('mousedown', handleClick); }, [menuOpen]); const handleCall = async () => { if (!isRegistered || isInCall || dialing) return; setMenuOpen(false); setDialing(true); try { onDial?.(); await dialOutbound(phoneNumber); } catch { notify.error('Dial Failed', 'Could not place the call'); } finally { setDialing(false); } }; const handleSms = () => { setMenuOpen(false); window.open(`sms:+91${phoneNumber}`, '_self'); }; const handleWhatsApp = () => { setMenuOpen(false); window.open(`https://wa.me/91${phoneNumber}`, '_blank'); }; // Long-press for mobile const onTouchStart = () => { touchTimer.current = window.setTimeout(() => setMenuOpen(true), 500); }; const onTouchEnd = () => { if (touchTimer.current) { clearTimeout(touchTimer.current); touchTimer.current = null; } }; const canCall = isRegistered && !isInCall && !dialing; return (
{/* Clickable phone number — calls directly */} {/* Kebab menu trigger — SMS + WhatsApp */} {/* Context menu — SMS + WhatsApp only (dial is the primary click) */} {menuOpen && (
)}
); };