import { useEffect, useMemo } from "react"; import { useMutation } from "@tanstack/react-query"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Sparkles, TrendingUp, AlertTriangle, Info, Loader2 } from "lucide-react"; import { analyticsService, type AiInsightItem } from "@/services/analytics.service"; import { useToast } from "@/hooks/use-toast"; const EMPTY_PAYLOAD: Record = {}; function insightIcon(severity: AiInsightItem["severity"]) { switch (severity) { case "critical": return AlertTriangle; case "warning": return TrendingUp; default: return Info; } } function insightColor(severity: AiInsightItem["severity"]) { switch (severity) { case "critical": return "text-destructive"; case "warning": return "text-warning"; default: return "text-primary"; } } interface Props { data?: Record; } export default function AiInsightsPanel({ data = EMPTY_PAYLOAD }: Props) { const { toast } = useToast(); const payloadKey = useMemo(() => JSON.stringify(data), [data]); const mutation = useMutation({ mutationFn: (payload: Record) => analyticsService.getInsights(payload), onError: (err: Error) => { toast({ title: "Insights unavailable", description: err.message || "Could not load AI insights.", variant: "destructive", }); }, }); useEffect(() => { mutation.mutate(data); // eslint-disable-next-line react-hooks/exhaustive-deps }, [payloadKey]); const items = mutation.data?.insights ?? []; return ( AI Platform Insights {mutation.isPending && (
Loading insights…
)} {mutation.isError && !mutation.isPending && (

Could not load insights.

)} {mutation.isSuccess && items.length === 0 && (

No insights available for this view.

)} {!mutation.isPending && items.length > 0 && (
{items.map((item, idx) => { const Icon = insightIcon(item.severity); const color = insightColor(item.severity); return (
{item.title}

{item.description}

{item.recommendation && (

{item.recommendation}

)}
); })}
)}
); }