import { useEffect } from "react"; import { useMutation } from "@tanstack/react-query"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Sparkles, RefreshCw, Loader2, Lightbulb } from "lucide-react"; import { coachingService } from "@/services/coaching.service"; import { useToast } from "@/hooks/use-toast"; export default function AiStudyCoach() { const { toast } = useToast(); const suggestMutation = useMutation({ mutationFn: () => coachingService.suggest(), onError: (err: Error) => { toast({ variant: "destructive", title: "Could not load coach tips", description: err.message || "Try refreshing in a moment.", }); }, }); useEffect(() => { suggestMutation.mutate(); // eslint-disable-next-line react-hooks/exhaustive-deps -- load once on mount }, []); const refresh = () => { suggestMutation.mutate(); }; const d = suggestMutation.data; const suggestions = d ? [d.suggestion, ...(d.focus_areas ?? []).map((a: string) => `Focus area: ${a}`)].filter(Boolean) : []; const planTips = d?.daily_plan?.length ? d.daily_plan.map((p: { activity: string; duration_min: number; skill: string }) => `${p.activity} (${p.duration_min}min — ${p.skill})`) : d?.motivation ? [d.motivation] : []; return (
Your AI Study Coach
{suggestMutation.isPending && !suggestMutation.data ? (
Analyzing your performance...
) : ( <> {suggestions.length > 0 && (

Suggestions

    {suggestions.map((s, i) => (
  • {s}
  • ))}
)} {planTips.length > 0 && (

Study plan tips

{planTips.map((tip, i) => (

{tip}

))}
)} {!suggestMutation.isPending && suggestions.length === 0 && planTips.length === 0 && (

No suggestions yet. Try refreshing.

)} )}
); }