import { useState } from "react"; import { useMutation } from "@tanstack/react-query"; import { useLocation } from "react-router-dom"; import { Sheet, SheetContent, SheetHeader, SheetTitle } from "@/components/ui/sheet"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Sparkles, Send, Loader2 } from "lucide-react"; import { coachingService } from "@/services/coaching.service"; import { useToast } from "@/hooks/use-toast"; const quickActions = [ "Platform health summary", "Show dropout risks", "Compare course performance", "Suggest batch improvements", ]; export default function AiAssistantDrawer() { const [open, setOpen] = useState(false); const [messages, setMessages] = useState<{ role: "user" | "ai"; text: string }[]>([]); const [input, setInput] = useState(""); const location = useLocation(); const { toast } = useToast(); const chatMutation = useMutation({ mutationFn: (message: string) => coachingService.chat({ message, context: { page: location.pathname } }), onSuccess: (data) => { setMessages((prev) => [...prev, { role: "ai", text: data.reply }]); }, onError: (err: Error) => { toast({ variant: "destructive", title: "Could not get a reply", description: err.message || "Something went wrong. Try again.", }); setMessages((prev) => [ ...prev, { role: "ai", text: "Sorry, I could not reach the assistant. Please try again in a moment.", }, ]); }, }); const handleSend = (text: string) => { if (!text.trim() || chatMutation.isPending) return; const userMsg = text.trim(); setMessages((prev) => [...prev, { role: "user", text: userMsg }]); setInput(""); chatMutation.mutate(userMsg); }; return ( <> EnCoach AI Assistant
{quickActions.map((action) => ( ))}
{messages.length === 0 && (

Ask me anything about the platform,

or click a quick action above.

)} {messages.map((msg, i) => (
{msg.role === "ai" && ( )} {msg.text}
))} {chatMutation.isPending && (
Thinking...
)}
setInput(e.target.value)} onKeyDown={(e) => e.key === "Enter" && handleSend(input)} />
); }