import React, { useState, useRef } from "react"; import { FileSpreadsheet, ListChecks, Upload, X, Loader2, Clock, FileText, ChevronRight, Brain, } from "lucide-react"; import MCQQuizPage from "../components/quize/mcq"; import API from "../api/api"; import * as pdfjsLib from "pdfjs-dist"; // Initialize PDF worker pdfjsLib.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjsLib.version}/build/pdf.worker.min.mjs`; const ResumeGeneratedQuize: React.FC = () => { const [showQuiz, setShowQuiz] = useState(false); const [quizType, setQuizType] = useState<"mcq" | null>("mcq"); const [uploadType, setUploadType] = useState<"resume" | "notes" | null>(null); const [uploadedFile, setUploadedFile] = useState(null); const [fileError, setFileError] = useState(null); const [fileObject, setFileObject] = useState(null); const [isProcessing, setIsProcessing] = useState(false); // Configuration State const [customPrompt, setCustomPrompt] = useState(""); const [duration, setDuration] = useState(15); const [quizData, setQuizData] = useState(null); const resumeInputRef = useRef(null); const notesInputRef = useRef(null); const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB const handleFileUpload = ( event: React.ChangeEvent, type: "resume" | "notes" ) => { const file = event.target.files?.[0]; if (!file) return; if (file.type !== "application/pdf") { setFileError("Please upload a PDF file"); return; } if (file.size > MAX_FILE_SIZE) { setFileError( `File size exceeds 10MB. Your file is ${( file.size / (1024 * 1024) ).toFixed(2)}MB` ); return; } setFileError(null); setUploadType(type); setUploadedFile(file.name); setFileObject(file); }; const handleResumeClick = () => resumeInputRef.current?.click(); const handleNotesClick = () => notesInputRef.current?.click(); const clearUpload = () => { setUploadedFile(null); setUploadType(null); setFileError(null); setFileObject(null); }; const extractTextFromPDF = async (file: File): Promise => { const arrayBuffer = await file.arrayBuffer(); const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise; let fullText = ""; for (let i = 1; i <= pdf.numPages; i++) { const page = await pdf.getPage(i); const textContent = await page.getTextContent(); const pageText = textContent.items.map((item: any) => item.str).join(" "); fullText += pageText + "\n"; } return fullText; }; const generateQuiz = async () => { if (!fileObject || !quizType || !uploadType) return; setIsProcessing(true); setFileError(null); try { console.log("Extracting text from PDF..."); const extractedText = await extractTextFromPDF(fileObject); if (!extractedText.trim()) { throw new Error( "Could not extract text from the PDF. It might be an image-only PDF." ); } const payload = { parsed_doc: extractedText.trim(), user_prompt: customPrompt.trim() || "Generate a quiz based on this content.", }; const endpoint = uploadType === "notes" ? "/quiz/notes" : "/quiz/resume"; console.log(`Sending to ${endpoint}...`); const response = await API.post(endpoint, payload); setQuizData(response.data); setShowQuiz(true); } catch (error: any) { console.error("Quiz Generation Error:", error); let errorMessage = "Failed to generate quiz. Check login status."; if (error.response?.data?.detail) { if (typeof error.response.data.detail === "string") { errorMessage = error.response.data.detail; } else if ( Array.isArray(error.response.data.detail) && error.response.data.detail.length > 0 ) { const firstError = error.response.data.detail[0]; errorMessage = `Validation Error: Field '${firstError.loc.join( " -> " )}' ${firstError.msg}`; } } else if (error.code === "ERR_BAD_REQUEST") { errorMessage = "Server rejected the data. Are you logged in?"; } setFileError(errorMessage); } finally { setIsProcessing(false); } }; if (showQuiz) { return ( setShowQuiz(false)} totalTimeSeconds={duration * 60} /> ); } // ---- UI COMPONENTS ---- const OutputTypeOption = ({ icon, title, desc, value }: any) => (
setQuizType(value)} className={`p-3 rounded-xl border-2 transition-all cursor-pointer flex flex-col gap-2 group h-full ${ quizType === value ? "bg-[#607B8F] border-[#F7E396] shadow-lg transform scale-[1.01]" : "bg-[#607B8F]/50 border-transparent hover:bg-[#607B8F] hover:border-[#E97F4A]/50" } `} >
{icon}

{title}

{desc}

); return ( // FIXED: h-screen + overflow-hidden to fix double scrollbars.
{/* Background Blobs */}
{/* Main Content Wrapper - Added 'pt-6' to fix header cut-off */}
{/* Header - Compacted margins */}

Smart AI-Powered Quiz Generator

Generate Quizzes from Resumes or Notes
{/* Content Grid - Compacted spacing */}
{/* LEFT COLUMN */}
{/* 1. Source Card - Compact Padding (p-5) */}

1. Select Quiz Source

Upload your material (Max 10MB PDF).

handleFileUpload(e, "resume")} className="hidden" /> handleFileUpload(e, "notes")} className="hidden" /> {fileError && (
{fileError}
)} {uploadedFile && (

File Uploaded

{uploadedFile}
)}
{/* 2. Output Type Selection */}

2. Choose Output Type

} title="Multiple Choice Quiz (MCQ)" desc="Generate a standardized multiple-choice assessment." />
{/* RIGHT COLUMN: Configuration */}

3. Configure

{/* Custom Prompt - Reduced height */}
{/* Duration Slider */}
{duration} min
setDuration(parseInt(e.target.value))} className="w-full h-1.5 bg-[#434E78] rounded-lg appearance-none cursor-pointer accent-[#F7E396]" />
5m 30m 60m
{/* Action Button - Reduced height */}
); }; export default ResumeGeneratedQuize;