import { useState } from "react"; import { useMutation } from "@tanstack/react-query"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Sparkles, Loader2 } from "lucide-react"; import { coachingService } from "@/services/coaching.service"; import { useToast } from "@/hooks/use-toast"; export default function AiGradeExplainer({ studentName, scores, }: { studentName: string; scores?: Record; }) { const [open, setOpen] = useState(false); const { toast } = useToast(); const explainMutation = useMutation({ mutationFn: () => coachingService.explain({ score_data: scores ?? {}, student_context: `IELTS / course grades for student: ${studentName}. Summarize what the scores mean and what to focus on next.`, }), onError: (err: Error) => { toast({ variant: "destructive", title: "Could not explain grades", description: err.message || "Try again in a moment.", }); }, }); const handleOpen = () => { setOpen(true); explainMutation.reset(); explainMutation.mutate(); }; return ( <> { setOpen(v); if (!v) explainMutation.reset(); }} > AI Grade Explanation — {studentName} {explainMutation.isPending ? (
Analyzing grades...
) : explainMutation.isError ? (

Something went wrong. Close and try again.

) : (

{explainMutation.data?.explanation}

)}
); }