import { useState, useEffect, useRef } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import { Button } from '../ui/button'; import { Badge } from '../ui/badge'; import { Textarea } from '../ui/textarea'; import { Video, VideoOff, Mic, MicOff, PhoneOff, MessageSquare, Wifi, AlertTriangle, Clock, } from 'lucide-react'; import { useAuth } from '../../lib/authContext'; import { useWebRTC } from '../../hooks/useWebRTC'; import { getApiBaseUrl } from '../../lib/api/config'; const BASE_URL = getApiBaseUrl(); /** * The sheikh's side of the verification interview. * Joins the same WebRTC room as the admin (room = "interview-{sheikhId}"). * No approve/reject logic โ€” just the video call. */ export function SheikhInterviewSession() { const { sheikhId } = useParams<{ sheikhId: string }>(); const navigate = useNavigate(); const { user } = useAuth(); const roomId = `interview-${sheikhId ?? 'unknown'}`; const [isVideoOn, setIsVideoOn] = useState(true); const [isAudioOn, setIsAudioOn] = useState(true); const [sessionTime, setSessionTime] = useState(0); const [newMessage, setNewMessage] = useState(''); const [showChat, setShowChat] = useState(false); const localVideoRef = useRef(null); const remoteVideoRef = useRef(null); const { localStream, remoteStream, isConnected, messages, sendChatMessage, toggleVideo, toggleAudio, connectionQuality, } = useWebRTC(roomId, user?.id ?? 'sheikh'); useEffect(() => { if (localVideoRef.current && localStream) localVideoRef.current.srcObject = localStream; }, [localStream]); useEffect(() => { if (remoteVideoRef.current && remoteStream) { remoteVideoRef.current.srcObject = remoteStream; } }, [remoteStream]); useEffect(() => { const timer = setInterval(() => setSessionTime(prev => prev + 1), 1000); return () => clearInterval(timer); }, []); // Poll every 10s for admin's decision โ€” redirect when approved or rejected useEffect(() => { const token = localStorage.getItem('authToken') ?? ''; if (!token) return; const checkDecision = async () => { try { const res = await fetch(`${BASE_URL}/api/auth/verify-profile`, { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, }); if (!res.ok) return; const data = await res.json(); const status: string = data.sheikhApprovalStatus ?? ''; if (status === 'APPROVED') { navigate('/sheikh/dashboard', { replace: true }); } else if (status === 'REJECTED') { const reason: string = data.rejectionReason ?? ''; navigate('/sheikh/interview', { replace: true, state: { rejected: true, reason }, }); } } catch { // silent โ€” background poll, don't disrupt UI } }; const timer = setInterval(checkDecision, 10_000); return () => clearInterval(timer); }, [navigate]); const formatTime = (seconds: number) => { const h = Math.floor(seconds / 3600); const m = Math.floor((seconds % 3600) / 60); const s = seconds % 60; return `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`; }; const handleSendMessage = () => { if (newMessage.trim()) { sendChatMessage(newMessage); setNewMessage(''); } }; const handleLeave = () => { navigate('/sheikh/interview'); }; const getConnectionColor = () => { switch (connectionQuality) { case 'good': return 'text-emerald-500'; case 'fair': return 'text-yellow-500'; case 'poor': return 'text-red-500'; default: return 'text-gray-500'; } }; const getConnectionLabel = () => { if (!isConnected) return 'Connecting...'; switch (connectionQuality) { case 'good': return 'Excellent'; case 'fair': return 'Stable'; case 'poor': return 'Poor Connection'; default: return 'Waiting for Admin...'; } }; return (
{/* Header */}

Verification Interview

With the review team

{connectionQuality === 'poor' ? : } {getConnectionLabel()}
{formatTime(sessionTime)}
LIVE
{/* Main Content */}
{/* Video Area */}
{/* Remote video always in DOM so ref is always attached */}
{/* Local PiP */}
{/* Chat Sidebar */} {showChat && (

Chat

{messages.map((msg, i) => (

{msg.message}

{msg.sender} ยท {msg.time}

))}