Spaces:
Sleeping
Sleeping
File size: 22,827 Bytes
a0da743 | 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 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 | 'use client';
import { useState, useEffect, useRef, useCallback } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { useRouter } from 'next/navigation';
import { useInterview } from '@/context/InterviewContext';
import LoadingScreen from '@/components/LoadingScreen';
import ScoreRing from '@/components/ScoreRing';
import { Question, ChatMessage, Evaluation } from '@/types/interview';
import {
Send,
Mic,
MicOff,
Clock,
ChevronRight,
CheckCircle2,
AlertCircle,
Lightbulb,
XCircle,
SkipForward,
Trophy,
MessageSquare,
Volume2,
} from 'lucide-react';
export default function SessionPage() {
const router = useRouter();
const {
session,
setQuestions,
addMessage,
submitAnswer,
addEvaluation,
nextQuestion,
setStatus,
completeSession,
} = useInterview();
const [input, setInput] = useState('');
const [isLoading, setIsLoading] = useState(true);
const [isEvaluating, setIsEvaluating] = useState(false);
const [isGeneratingSummary, setIsGeneratingSummary] = useState(false);
const [showEvaluation, setShowEvaluation] = useState<Evaluation | null>(null);
const [elapsedTime, setElapsedTime] = useState(0);
const [isListening, setIsListening] = useState(false);
const [error, setError] = useState<string | null>(null);
const messagesEndRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLTextAreaElement>(null);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const recognitionRef = useRef<any>(null);
// Redirect if no session
useEffect(() => {
if (!session) {
router.push('/interview');
}
}, [session, router]);
// Timer
useEffect(() => {
if (session?.status === 'in-progress') {
const interval = setInterval(() => {
const elapsed = Math.floor((Date.now() - session.startedAt) / 1000);
setElapsedTime(elapsed);
// Auto-end if time limit is set and exceeded
if (session.config.timeLimit > 0) {
const limitSeconds = session.config.timeLimit * 60;
if (elapsed >= limitSeconds) {
clearInterval(interval);
handleEndInterview();
}
}
}, 1000);
return () => clearInterval(interval);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [session?.status, session?.startedAt]);
// Auto scroll to bottom
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [session?.messages]);
// Generate questions on mount
useEffect(() => {
if (session && session.questions.length === 0 && isLoading) {
generateQuestions();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [session]);
const generateQuestions = async () => {
if (!session) return;
setError(null);
try {
const res = await fetch('/api/generate-questions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-gemini-api-key': localStorage.getItem('gemini-api-key') || ''
},
body: JSON.stringify({
role: session.config.role,
difficulty: session.config.difficulty,
questionCount: session.config.questionCount,
}),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error);
const questions: Question[] = data.questions;
setQuestions(questions);
// Add welcome message
addMessage({
id: 'welcome',
role: 'ai',
content: `Welcome to your **${session.config.role.title}** mock interview! π―\n\nI'll be asking you **${questions.length} questions** at **${session.config.difficulty}** difficulty.\n\nTake your time to think before answering. Let's begin!`,
timestamp: Date.now(),
});
// Add first question
setTimeout(() => {
addMessage({
id: `q-${questions[0].id}`,
role: 'ai',
content: `**Question 1/${questions.length}** β’ _${questions[0].category}_\n\n${questions[0].text}`,
timestamp: Date.now(),
questionId: questions[0].id,
});
setIsLoading(false);
}, 800);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to generate questions');
setIsLoading(false);
}
};
const handleSubmitAnswer = async () => {
if (!session || !input.trim() || isEvaluating) return;
const currentQ = session.questions[session.currentQuestionIndex];
if (!currentQ) return;
const answerText = input.trim();
setInput('');
// Add user message
addMessage({
id: `a-${currentQ.id}`,
role: 'user',
content: answerText,
timestamp: Date.now(),
questionId: currentQ.id,
});
// Submit answer
submitAnswer({
questionId: currentQ.id,
text: answerText,
timestamp: Date.now(),
});
// Evaluate
setIsEvaluating(true);
try {
const res = await fetch('/api/evaluate-answer', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-gemini-api-key': localStorage.getItem('gemini-api-key') || ''
},
body: JSON.stringify({
question: currentQ.text,
answer: answerText,
role: session.config.role.title,
difficulty: session.config.difficulty,
}),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error);
const evaluation: Evaluation = {
...data.evaluation,
questionId: currentQ.id,
};
addEvaluation(evaluation);
setShowEvaluation(evaluation);
// Add AI feedback message
addMessage({
id: `eval-${currentQ.id}`,
role: 'ai',
content: buildEvaluationMessage(evaluation),
timestamp: Date.now(),
questionId: currentQ.id,
evaluation,
});
} catch (err) {
addMessage({
id: `err-${currentQ.id}`,
role: 'ai',
content: `β οΈ I couldn't evaluate that response. ${err instanceof Error ? err.message : 'Please try again.'}`,
timestamp: Date.now(),
});
}
setIsEvaluating(false);
};
const handleNextQuestion = useCallback(() => {
if (!session) return;
setShowEvaluation(null);
const nextIndex = session.currentQuestionIndex + 1;
if (nextIndex >= session.questions.length) {
// End of interview
handleEndInterview();
return;
}
nextQuestion();
const nextQ = session.questions[nextIndex];
addMessage({
id: `q-${nextQ.id}`,
role: 'ai',
content: `**Question ${nextIndex + 1}/${session.questions.length}** β’ _${nextQ.category}_\n\n${nextQ.text}`,
timestamp: Date.now(),
questionId: nextQ.id,
});
inputRef.current?.focus();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [session, nextQuestion, addMessage]);
const handleEndInterview = async () => {
if (!session) return;
setIsGeneratingSummary(true);
try {
const res = await fetch('/api/session-summary', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-gemini-api-key': localStorage.getItem('gemini-api-key') || ''
},
body: JSON.stringify({
role: session.config.role.title,
difficulty: session.config.difficulty,
evaluations: session.evaluations,
questions: session.questions,
answers: session.answers,
}),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error);
completeSession(data.summary);
router.push('/dashboard');
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to generate summary');
setIsGeneratingSummary(false);
}
};
const handleSkipQuestion = () => {
if (!session) return;
const currentQ = session.questions[session.currentQuestionIndex];
addMessage({
id: `skip-${currentQ.id}`,
role: 'user',
content: '_Skipped this question_',
timestamp: Date.now(),
questionId: currentQ.id,
});
submitAnswer({
questionId: currentQ.id,
text: '[SKIPPED]',
timestamp: Date.now(),
});
handleNextQuestion();
};
// Voice input
const toggleVoiceInput = () => {
if (isListening) {
recognitionRef.current?.stop();
setIsListening(false);
return;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const win = window as any;
const SpeechRecognitionAPI = win.SpeechRecognition || win.webkitSpeechRecognition;
if (!SpeechRecognitionAPI) {
setError('Voice input is not supported in your browser.');
return;
}
const recognition = new SpeechRecognitionAPI();
recognition.continuous = true;
recognition.interimResults = true;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
recognition.onresult = (event: any) => {
let transcript = '';
for (let i = 0; i < event.results.length; i++) {
transcript += event.results[i][0].transcript;
}
setInput(transcript);
};
recognition.onerror = () => {
setIsListening(false);
};
recognition.onend = () => {
setIsListening(false);
};
recognition.start();
recognitionRef.current = recognition;
setIsListening(true);
};
const formatTime = (seconds: number) => {
const m = Math.floor(seconds / 60);
const s = seconds % 60;
return `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
};
if (!session) return null;
if (isLoading) {
return <LoadingScreen message="AI is preparing your interview questions..." />;
}
if (isGeneratingSummary) {
return <LoadingScreen message="Generating your performance summary..." />;
}
const progress = session.questions.length > 0
? ((session.currentQuestionIndex + (session.evaluations.length > session.currentQuestionIndex ? 1 : 0)) / session.questions.length) * 100
: 0;
const timeLimitSeconds = session.config.timeLimit * 60;
const remainingSeconds = session.config.timeLimit > 0 ? Math.max(0, timeLimitSeconds - elapsedTime) : 0;
const isRunningOut = session.config.timeLimit > 0 && remainingSeconds <= 120;
const displayTime = session.config.timeLimit > 0 ? remainingSeconds : elapsedTime;
return (
<div className="min-h-screen flex flex-col">
{/* Top Bar */}
<div className="glass sticky top-16 z-40" style={{ borderBottom: '1px solid var(--border)' }}>
<div className="max-w-5xl mx-auto px-4 py-3 flex items-center justify-between">
<div className="flex items-center gap-4">
<span className="text-xl">{session.config.role.icon}</span>
<div>
<h2 className="font-semibold text-sm">{session.config.role.title}</h2>
<p className="text-xs capitalize" style={{ color: 'var(--muted)' }}>
{session.config.difficulty} β’ Q{session.currentQuestionIndex + 1}/{session.questions.length}
</p>
</div>
</div>
<div className="flex items-center gap-4">
{/* Progress bar */}
<div className="hidden sm:block w-32">
<div className="progress-bar">
<div className="progress-bar-fill" style={{ width: `${progress}%` }} />
</div>
</div>
{/* Timer */}
<div className="flex items-center gap-1.5 px-3 py-1.5 rounded-lg transition-colors duration-300" style={{
background: isRunningOut ? 'rgba(255, 107, 107, 0.1)' : 'var(--card)',
border: `1px solid ${isRunningOut ? 'rgba(255, 107, 107, 0.3)' : 'var(--border)'}`
}}>
<Clock className="w-3.5 h-3.5" style={{ color: isRunningOut ? 'var(--danger)' : 'var(--muted)' }} />
<span className="text-sm font-mono" style={{ color: isRunningOut ? 'var(--danger)' : 'var(--foreground)' }}>
{formatTime(displayTime)}
</span>
</div>
{/* End button */}
<button
onClick={handleEndInterview}
className="px-3 py-1.5 rounded-lg text-sm font-medium transition-all duration-300 cursor-pointer"
style={{
background: 'rgba(255, 107, 107, 0.1)',
border: '1px solid rgba(255, 107, 107, 0.2)',
color: 'var(--danger)',
}}
>
End
</button>
</div>
</div>
</div>
{/* Error bar */}
{error && (
<div className="max-w-5xl mx-auto px-4 mt-4">
<div className="flex items-center gap-3 p-4 rounded-xl" style={{ background: 'rgba(255, 107, 107, 0.1)', border: '1px solid rgba(255, 107, 107, 0.2)' }}>
<AlertCircle className="w-5 h-5 shrink-0" style={{ color: 'var(--danger)' }} />
<p className="text-sm" style={{ color: 'var(--danger)' }}>{error}</p>
<button onClick={() => setError(null)} className="ml-auto cursor-pointer">
<XCircle className="w-4 h-4" style={{ color: 'var(--danger)' }} />
</button>
</div>
</div>
)}
{/* Messages */}
<div className="flex-1 overflow-y-auto py-6 px-4">
<div className="max-w-3xl mx-auto space-y-4">
<AnimatePresence initial={false}>
{session.messages.map((msg) => (
<ChatBubble key={msg.id} message={msg} roleColor={session.config.role.color} />
))}
</AnimatePresence>
{/* Evaluating indicator */}
{isEvaluating && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
className="flex items-start gap-3"
>
<div className="w-8 h-8 rounded-full flex items-center justify-center shrink-0" style={{ background: 'var(--gradient-primary)' }}>
<span className="text-sm">π€</span>
</div>
<div className="glass-light px-4 py-3 rounded-2xl rounded-tl-md">
<div className="typing-dots flex gap-1">
<span></span>
<span></span>
<span></span>
</div>
</div>
</motion.div>
)}
<div ref={messagesEndRef} />
</div>
</div>
{/* Evaluation Drawer */}
<AnimatePresence>
{showEvaluation && (
<motion.div
initial={{ y: 100, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
exit={{ y: 100, opacity: 0 }}
className="glass sticky bottom-0 z-30"
style={{ borderTop: '1px solid var(--border)' }}
>
<div className="max-w-3xl mx-auto p-4">
<div className="flex items-center justify-between mb-4">
<div className="flex items-center gap-3">
<ScoreRing score={showEvaluation.score} maxScore={10} size={56} strokeWidth={5} showPercentage={false} />
<div>
<p className="font-semibold text-sm">Score: {showEvaluation.score}/10</p>
<div className="flex gap-3 mt-1">
{[
{ label: 'Relevance', value: showEvaluation.relevance },
{ label: 'Clarity', value: showEvaluation.clarity },
{ label: 'Accuracy', value: showEvaluation.technicalAccuracy },
].map((m) => (
<span key={m.label} className="text-xs" style={{ color: 'var(--muted)' }}>
{m.label}: <span style={{ color: m.value >= 7 ? 'var(--success)' : m.value >= 4 ? 'var(--warning)' : 'var(--danger)' }}>{m.value}/10</span>
</span>
))}
</div>
</div>
</div>
<button
onClick={handleNextQuestion}
className="btn-primary flex items-center gap-2 text-sm cursor-pointer"
>
{session.currentQuestionIndex + 1 >= session.questions.length ? (
<>
<Trophy className="w-4 h-4" />
Finish
</>
) : (
<>
Next Question
<ChevronRight className="w-4 h-4" />
</>
)}
</button>
</div>
</div>
</motion.div>
)}
</AnimatePresence>
{/* Input Area */}
{!showEvaluation && session.status !== 'completed' && (
<div className="glass sticky bottom-0 z-20" style={{ borderTop: '1px solid var(--border)' }}>
<div className="max-w-3xl mx-auto p-4">
<div className="flex items-end gap-3">
<div className="flex-1 relative">
<textarea
ref={inputRef}
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSubmitAnswer();
}
}}
placeholder="Type your answer... (Shift+Enter for new line)"
rows={2}
className="w-full resize-none rounded-xl px-4 py-3 text-sm focus:outline-none transition-all duration-300"
style={{
background: 'var(--card)',
border: '1px solid var(--border)',
color: 'var(--foreground)',
}}
onFocus={(e) => {
e.currentTarget.style.borderColor = 'var(--primary)';
e.currentTarget.style.boxShadow = '0 0 20px rgba(108, 92, 231, 0.1)';
}}
onBlur={(e) => {
e.currentTarget.style.borderColor = 'var(--border)';
e.currentTarget.style.boxShadow = 'none';
}}
disabled={isEvaluating}
/>
</div>
{/* Voice Button */}
<button
onClick={toggleVoiceInput}
className="w-10 h-10 rounded-xl flex items-center justify-center transition-all duration-300 shrink-0 cursor-pointer"
style={{
background: isListening ? 'var(--danger)' : 'var(--card)',
border: `1px solid ${isListening ? 'var(--danger)' : 'var(--border)'}`,
color: isListening ? 'white' : 'var(--muted)',
}}
title={isListening ? 'Stop listening' : 'Voice input'}
>
{isListening ? <MicOff className="w-4 h-4" /> : <Mic className="w-4 h-4" />}
</button>
{/* Skip Button */}
<button
onClick={handleSkipQuestion}
className="w-10 h-10 rounded-xl flex items-center justify-center transition-all duration-300 shrink-0 cursor-pointer"
style={{
background: 'var(--card)',
border: '1px solid var(--border)',
color: 'var(--muted)',
}}
title="Skip question"
>
<SkipForward className="w-4 h-4" />
</button>
{/* Send Button */}
<button
onClick={handleSubmitAnswer}
disabled={!input.trim() || isEvaluating}
className="w-10 h-10 rounded-xl flex items-center justify-center transition-all duration-300 shrink-0 cursor-pointer disabled:opacity-40"
style={{
background: input.trim() ? 'var(--gradient-primary)' : 'var(--card)',
border: `1px solid ${input.trim() ? 'transparent' : 'var(--border)'}`,
color: input.trim() ? 'white' : 'var(--muted)',
}}
>
<Send className="w-4 h-4" />
</button>
</div>
</div>
</div>
)}
</div>
);
}
function buildEvaluationMessage(evaluation: Evaluation): string {
const scoreEmoji = evaluation.score >= 8 ? 'π' : evaluation.score >= 6 ? 'π' : evaluation.score >= 4 ? 'π‘' : 'π';
let msg = `${scoreEmoji} **Score: ${evaluation.score}/10**\n\n`;
msg += `π Relevance: ${evaluation.relevance}/10 β’ Clarity: ${evaluation.clarity}/10 β’ Accuracy: ${evaluation.technicalAccuracy}/10 β’ Confidence: ${evaluation.confidence}/10\n\n`;
if (evaluation.strengths.length > 0) {
msg += `β
**Strengths:**\n${evaluation.strengths.map(s => `β’ ${s}`).join('\n')}\n\n`;
}
if (evaluation.weaknesses.length > 0) {
msg += `β οΈ **Areas to Improve:**\n${evaluation.weaknesses.map(w => `β’ ${w}`).join('\n')}\n\n`;
}
if (evaluation.suggestions.length > 0) {
msg += `π‘ **Suggestions:**\n${evaluation.suggestions.map(s => `β’ ${s}`).join('\n')}\n\n`;
}
if (evaluation.sampleAnswer) {
msg += `π **Sample Answer:**\n_${evaluation.sampleAnswer}_`;
}
return msg;
}
function ChatBubble({ message, roleColor }: { message: ChatMessage; roleColor: string }) {
const isAI = message.role === 'ai';
return (
<motion.div
initial={{ opacity: 0, y: 10, scale: 0.98 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
transition={{ duration: 0.3 }}
className={`flex items-start gap-3 ${isAI ? '' : 'flex-row-reverse'}`}
>
{/* Avatar */}
<div
className="w-8 h-8 rounded-full flex items-center justify-center shrink-0 text-sm"
style={{
background: isAI ? 'var(--gradient-primary)' : roleColor,
}}
>
{isAI ? 'π€' : 'π€'}
</div>
{/* Bubble */}
<div
className={`max-w-[80%] px-4 py-3 rounded-2xl text-sm leading-relaxed ${isAI ? 'rounded-tl-md' : 'rounded-tr-md'}`}
style={{
background: isAI ? 'var(--card)' : `${roleColor}15`,
border: `1px solid ${isAI ? 'var(--border)' : `${roleColor}30`}`,
}}
>
<div
dangerouslySetInnerHTML={{
__html: formatMessage(message.content),
}}
/>
</div>
</motion.div>
);
}
function formatMessage(text: string): string {
return text
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/_(.*?)_/g, '<em>$1</em>')
.replace(/\n/g, '<br/>');
}
|