Spaces:
Sleeping
Sleeping
File size: 18,328 Bytes
eb6a2f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | 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, ClipboardEdit, Save, Wifi,
AlertTriangle, Clock, CheckCircle2, XCircle,
} from 'lucide-react';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '../ui/dialog';
import { Label } from '../ui/label';
import { useAuth } from '../../lib/authContext';
import { useWebRTC } from '../../hooks/useWebRTC';
import adminApi from '../../lib/api/adminApi';
import { toast } from 'sonner';
export function InterviewSession() {
const { sheikhId } = useParams<{ sheikhId: string }>();
const navigate = useNavigate();
const { user } = useAuth();
// Use "interview-{sheikhId}" as the WebRTC room ID
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 [showNotes, setShowNotes] = useState(false);
const [notes, setNotes] = useState('');
const [isSavingNotes, setIsSavingNotes] = useState(false);
const [lastSavedNotes, setLastSavedNotes] = useState('');
// Post-interview decision dialog
const [showDecision, setShowDecision] = useState(false);
const [decisionType, setDecisionType] = useState<'approve' | 'reject' | null>(null);
const [rejectionReason, setRejectionReason] = useState('');
const [processing, setProcessing] = useState(false);
const localVideoRef = useRef<HTMLVideoElement>(null);
const remoteVideoRef = useRef<HTMLVideoElement>(null);
const {
localStream, remoteStream, isConnected,
messages, sendChatMessage,
toggleVideo, toggleAudio, connectionQuality,
} = useWebRTC(roomId, user?.id ?? 'admin');
useEffect(() => {
if (localVideoRef.current && localStream) localVideoRef.current.srcObject = localStream;
}, [localStream]);
useEffect(() => {
if (remoteVideoRef.current && remoteStream) {
remoteVideoRef.current.srcObject = remoteStream;
}
}, [remoteStream]);
// Session timer
useEffect(() => {
const timer = setInterval(() => setSessionTime(prev => prev + 1), 1000);
return () => clearInterval(timer);
}, []);
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 handleSaveNotes = async (isAutoSave = false) => {
if (notes === lastSavedNotes) return;
if (!isAutoSave) setIsSavingNotes(true);
try {
// حفظ الملاحظات على السيرفر
await adminApi.saveInterviewNotes(sheikhId, notes);
setLastSavedNotes(notes);
if (!isAutoSave) toast.success('Notes saved successfully');
} catch (error) {
if (!isAutoSave) toast.error('Failed to save notes');
} finally {
if (!isAutoSave) setIsSavingNotes(false);
}
};
// const handleSaveNotes = async (isAutoSave = false) => {
// if (notes === lastSavedNotes) return;
// if (!isAutoSave) setIsSavingNotes(true);
// // Notes are local for now — just mark as saved
// setLastSavedNotes(notes);
// if (!isAutoSave) setIsSavingNotes(false);
// };
// Auto-save notes every 60s
useEffect(() => {
const timer = setInterval(() => handleSaveNotes(true), 60000);
return () => clearInterval(timer);
}, [notes, lastSavedNotes]);
const handleEndInterview = () => {
setShowDecision(true);
};
const handleDecisionConfirm = async () => {
if (!sheikhId || !decisionType || processing) return;
setProcessing(true);
try {
if (decisionType === 'approve') {
await adminApi.approveSheikh(Number(sheikhId));
} else {
await adminApi.rejectSheikh(Number(sheikhId), rejectionReason);
}
setShowDecision(false);
navigate('/admin/sheikh-approval');
} catch (err) {
console.error('Decision failed:', err);
const apiErr = err as { status?: number; message?: string };
if (apiErr?.status === 500 && apiErr?.message?.includes('already approved')) {
toast.info('Sheikh was already approved. Redirecting...');
setShowDecision(false);
navigate('/admin/sheikh-approval');
} else {
toast.error('Failed to process decision. Please try again.');
}
} finally {
setProcessing(false);
}
};
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 Sheikh...';
}
};
return (
<div className="h-screen bg-gray-900 flex flex-col">
{/* Header */}
<div className="bg-gray-800 border-b border-gray-700 px-6 py-3">
<div className="flex items-center justify-between">
<div>
<h2 className="text-white font-semibold">Verification Interview</h2>
<p className="text-gray-400 text-sm">Sheikh ID #{sheikhId}</p>
</div>
<div className="flex items-center gap-6">
<div className={`flex items-center gap-1.5 px-3 py-1 rounded-full bg-gray-700/50 border border-gray-600 ${getConnectionColor()}`}>
{connectionQuality === 'poor'
? <AlertTriangle className="h-3.5 w-3.5" />
: <Wifi className="h-3.5 w-3.5" />}
<span className="text-xs font-medium">{getConnectionLabel()}</span>
</div>
<div className="flex items-center gap-2 text-white border-l border-gray-700 pl-6">
<Clock className="h-4 w-4 text-emerald-500" />
<span className="font-mono text-sm">{formatTime(sessionTime)}</span>
</div>
<Badge variant="destructive" className="animate-pulse px-3">LIVE</Badge>
</div>
</div>
</div>
{/* Main Content */}
<div className="flex-1 flex overflow-hidden">
{/* Video Area */}
<div className="flex-1 relative bg-black">
{/* Remote Video (Sheikh) — always in DOM so ref is always attached */}
<div className="absolute inset-0 flex items-center justify-center">
<video
ref={remoteVideoRef}
autoPlay
playsInline
className={`w-full h-full object-cover ${remoteStream ? '' : 'hidden'}`}
/>
{!remoteStream && (
<div className="w-full h-full bg-gradient-to-br from-gray-800 to-gray-900 flex items-center justify-center">
<div className="text-center">
<div className="w-32 h-32 rounded-full bg-blue-600 flex items-center justify-center text-white text-4xl mx-auto mb-4">
S
</div>
<p className="text-white text-xl">Sheikh</p>
<p className="text-gray-400 mt-2">Waiting for sheikh to join...</p>
</div>
</div>
)}
</div>
{/* Local Video PiP (Admin) */}
<div className="absolute top-4 right-4 w-48 h-36 bg-black rounded-lg shadow-lg border-2 border-white overflow-hidden">
<video
ref={localVideoRef}
autoPlay
playsInline
muted
className={`w-full h-full object-cover ${!isVideoOn ? 'hidden' : ''}`}
/>
{!isVideoOn && (
<div className="w-full h-full flex items-center justify-center bg-gray-800">
<div className="text-center">
<div className="w-12 h-12 rounded-full bg-emerald-600 flex items-center justify-center text-white mx-auto mb-1">
A
</div>
<p className="text-white text-xs">You (Video Off)</p>
</div>
</div>
)}
</div>
</div>
{/* Chat Sidebar */}
{showChat && (
<div className="w-80 bg-gray-800 border-l border-gray-700 flex flex-col">
<div className="p-4 border-b border-gray-700">
<h3 className="text-white font-medium">Chat</h3>
</div>
<div className="flex-1 overflow-y-auto p-4 space-y-3">
{messages.map((msg, i) => (
<div key={i} className={`flex flex-col ${msg.sender === 'You' ? 'items-end' : 'items-start'}`}>
<div className={`max-w-[80%] rounded-lg p-3 ${msg.sender === 'You' ? 'bg-emerald-600' : 'bg-gray-700'}`}>
<p className="text-white text-sm">{msg.message}</p>
</div>
<p className="text-gray-400 text-xs mt-1">{msg.sender} · {msg.time}</p>
</div>
))}
</div>
<div className="p-4 border-t border-gray-700 flex gap-2">
<Textarea
value={newMessage}
onChange={e => setNewMessage(e.target.value)}
onKeyDown={e => e.key === 'Enter' && !e.shiftKey && (e.preventDefault(), handleSendMessage())}
placeholder="Type a message..."
rows={2}
className="bg-gray-700 border-gray-600 text-white"
/>
<Button onClick={handleSendMessage} size="sm">Send</Button>
</div>
</div>
)}
{/* Admin Notes Sidebar */}
{showNotes && (
<div className="w-96 bg-gray-800 border-l border-gray-700 flex flex-col">
<div className="p-4 border-b border-gray-700 flex justify-between items-center">
<h3 className="text-white font-medium flex items-center gap-2">
<ClipboardEdit className="h-5 w-5 text-emerald-500" />
Interview Notes
</h3>
<Badge variant="outline" className="text-gray-400 border-gray-600">Private</Badge>
</div>
<div className="flex-1 p-4 flex flex-col gap-4">
<div className="bg-gray-700/50 p-3 rounded-lg border border-gray-600">
<p className="text-sm text-gray-300">
Record your observations during the interview. These notes are private and will help you make your approval decision.
</p>
</div>
<Textarea
value={notes}
onChange={e => setNotes(e.target.value)}
placeholder="Tajweed level, credentials verified, communication skills..."
className="flex-1 bg-gray-700 border-gray-600 text-white resize-none"
/>
</div>
<div className="p-4 border-t border-gray-700">
<Button
onClick={() => handleSaveNotes(false)}
className="w-full bg-emerald-600 hover:bg-emerald-700 text-white"
disabled={isSavingNotes || notes.trim() === ''}
>
{isSavingNotes ? (
<span className="flex items-center gap-2">
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white" />
Saving...
</span>
) : (
<span className="flex items-center gap-2">
<Save className="h-4 w-4" />
Save Notes
</span>
)}
</Button>
</div>
</div>
)}
</div>
{/* Controls */}
<div className="bg-gray-800 border-t border-gray-700 px-6 py-4">
<div className="flex items-center justify-center gap-4">
<Button
variant={isVideoOn ? 'outline' : 'destructive'}
size="lg"
className={isVideoOn ? 'bg-gray-700 border-gray-600 hover:bg-gray-600 text-white' : ''}
onClick={() => { setIsVideoOn(!isVideoOn); toggleVideo(); }}
>
{isVideoOn ? <Video className="h-5 w-5" /> : <VideoOff className="h-5 w-5" />}
</Button>
<Button
variant={isAudioOn ? 'outline' : 'destructive'}
size="lg"
className={isAudioOn ? 'bg-gray-700 border-gray-600 hover:bg-gray-600 text-white' : ''}
onClick={() => { setIsAudioOn(!isAudioOn); toggleAudio(); }}
>
{isAudioOn ? <Mic className="h-5 w-5" /> : <MicOff className="h-5 w-5" />}
</Button>
<Button
variant={showChat ? 'default' : 'outline'}
size="lg"
className={showChat ? 'bg-emerald-600 text-white hover:bg-emerald-700' : 'bg-gray-700 border-gray-600 hover:bg-gray-600 text-white'}
onClick={() => { setShowChat(!showChat); if (!showChat) setShowNotes(false); }}
>
<MessageSquare className="h-5 w-5" />
</Button>
<Button
variant={showNotes ? 'default' : 'outline'}
size="lg"
className={showNotes ? 'bg-emerald-600 text-white hover:bg-emerald-700' : 'bg-gray-700 border-gray-600 hover:bg-gray-600 text-white'}
onClick={() => { setShowNotes(!showNotes); if (!showNotes) setShowChat(false); }}
>
<ClipboardEdit className="h-5 w-5" />
</Button>
<Button variant="destructive" size="lg" onClick={handleEndInterview}>
<PhoneOff className="h-5 w-5 mr-2" />
End Interview
</Button>
</div>
</div>
{/* Post-interview decision dialog */}
<Dialog open={showDecision} onOpenChange={(open) => { if (!processing) setShowDecision(open); }}>
<DialogContent>
<DialogHeader>
<DialogTitle>Interview Complete — Make a Decision</DialogTitle>
<DialogDescription>
Approve or reject this sheikh based on the interview.
</DialogDescription>
</DialogHeader>
<div className="space-y-4 pt-2">
{/* Decision buttons */}
<div className="grid grid-cols-2 gap-3">
<button
onClick={() => setDecisionType('approve')}
className={`flex flex-col items-center gap-2 p-4 rounded-xl border-2 transition-all ${
decisionType === 'approve'
? 'border-emerald-500 bg-emerald-50'
: 'border-slate-200 hover:border-emerald-300'
}`}
>
<CheckCircle2 className={`h-8 w-8 ${decisionType === 'approve' ? 'text-emerald-600' : 'text-slate-400'}`} />
<span className={`font-semibold ${decisionType === 'approve' ? 'text-emerald-700' : 'text-slate-600'}`}>
Approve
</span>
</button>
<button
onClick={() => setDecisionType('reject')}
className={`flex flex-col items-center gap-2 p-4 rounded-xl border-2 transition-all ${
decisionType === 'reject'
? 'border-red-500 bg-red-50'
: 'border-slate-200 hover:border-red-300'
}`}
>
<XCircle className={`h-8 w-8 ${decisionType === 'reject' ? 'text-red-600' : 'text-slate-400'}`} />
<span className={`font-semibold ${decisionType === 'reject' ? 'text-red-700' : 'text-slate-600'}`}>
Reject
</span>
</button>
</div>
{/* Rejection reason */}
{decisionType === 'reject' && (
<div className="space-y-2">
<Label htmlFor="reason">Rejection Reason</Label>
<Textarea
id="reason"
placeholder="Explain why the application is being rejected..."
value={rejectionReason}
onChange={e => setRejectionReason(e.target.value)}
rows={3}
/>
</div>
)}
{/* Notes summary */}
{notes.trim() && (
<div className="bg-slate-50 rounded-lg p-3 border border-slate-200">
<p className="text-xs text-slate-500 mb-1 font-medium uppercase tracking-wide">Your notes</p>
<p className="text-sm text-slate-700 line-clamp-3">{notes}</p>
</div>
)}
<div className="flex gap-2 pt-1">
<Button
variant="outline"
className="flex-1"
onClick={() => setShowDecision(false)}
disabled={processing}
>
Back to Interview
</Button>
<Button
className="flex-1"
variant={decisionType === 'reject' ? 'destructive' : 'default'}
disabled={
processing ||
!decisionType ||
(decisionType === 'reject' && !rejectionReason.trim())
}
onClick={handleDecisionConfirm}
>
{processing ? (
<span className="flex items-center gap-2">
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white" />
Processing...
</span>
) : (
`Confirm ${decisionType === 'approve' ? 'Approval' : 'Rejection'}`
)}
</Button>
</div>
</div>
</DialogContent>
</Dialog>
</div>
);
}
|