import React, { useEffect, useState } from "react"; import Vapi from "@vapi-ai/web"; import { Mic, MicOff, PhoneOff, Send, Loader2, Volume2, ChevronDown, } from "lucide-react"; // Import useAuth to get dynamic username import { useAuth } from "../components/context/AuthContext"; // --- CONFIG --- const VAPI_PUBLIC_KEY = "6e393730-74a2-4690-8cb7-845ed3880488"; const BACKEND_URL = ""; const vapi = new Vapi(VAPI_PUBLIC_KEY); function AIInterview() { const { username } = useAuth(); const [viewState, setViewState] = useState<"config" | "interview">("config"); const [isSessionActive, setIsSessionActive] = useState(false); const [isSpeaking, setIsSpeaking] = useState(false); const [isMuted, setIsMuted] = useState(false); const [status, setStatus] = useState("Idle"); // Form State const [name, setName] = useState(username || "Candidate"); const [role, setRole] = useState("Senior Frontend Developer"); const [exp, setExp] = useState("5"); // UPDATED: Default difficulty const [difficulty, setDifficulty] = useState("Medium (Intermediate)"); useEffect(() => { if (username) setName(username); }, [username]); useEffect(() => { vapi.on("call-start", () => { setStatus("Connected"); setIsSessionActive(true); setViewState("interview"); setIsMuted(false); }); vapi.on("call-end", () => { setStatus("Call Ended"); setIsSessionActive(false); setIsSpeaking(false); setViewState("config"); }); vapi.on("speech-start", () => setIsSpeaking(true)); vapi.on("speech-end", () => setIsSpeaking(false)); vapi.on("error", (e) => { console.error("Vapi Error:", e); setStatus("Error connecting"); setIsSessionActive(false); setViewState("config"); }); return () => { vapi.stop(); vapi.removeAllListeners(); }; }, []); const startInterview = async () => { setStatus("Configuring..."); try { const response = await fetch( `${BACKEND_URL}/api/v1/interview/api/get-vapi-config`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name, job_role: role, experience: exp, level: difficulty, }), } ); const data = await response.json(); if (!data.assistantId || !data.overrides) throw new Error("Invalid config"); setStatus("Connecting..."); vapi.start(data.assistantId, data.overrides); } catch (err: any) { console.error("Error:", err); setStatus("Failed to start"); } }; const stopInterview = () => { vapi.stop(); }; const toggleMute = () => { const newMuteState = !isMuted; vapi.setMuted(newMuteState); setIsMuted(newMuteState); }; // --- RENDER --- return (
{viewState === "config" && (

Prep AI

Configure Your Interview
setName(e.target.value)} className="w-full p-4 rounded-lg bg-[#434E78]/50 border-2 border-transparent focus:border-[#F7E396] focus:ring-0 outline-none transition text-white font-medium placeholder-gray-300 shadow-inner" />
setRole(e.target.value)} className="w-full p-4 rounded-lg bg-[#434E78]/50 border-2 border-transparent focus:border-[#F7E396] focus:ring-0 outline-none transition text-white font-medium placeholder-gray-300 shadow-inner" />
setExp(e.target.value)} className="w-full p-4 rounded-lg bg-[#434E78]/50 border-2 border-transparent focus:border-[#F7E396] focus:ring-0 outline-none transition text-white font-medium placeholder-gray-300 shadow-inner" />
{/* UPDATED: Difficulty Selection Dropdown */}
)} {viewState === "interview" && (
{isSpeaking ? ( ) : (
)}

{isSpeaking ? "Prep AI is speaking..." : "Listening to you..."}

)}
); } export default AIInterview;