import { useState } from "react"; import { useMutation } from "@tanstack/react-query"; import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Sparkles, Loader2 } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; import { analyticsService } from "@/services/analytics.service"; interface Props { batchId?: number; } export default function AiBatchOptimizer({ batchId }: Props) { const [open, setOpen] = useState(false); const { toast } = useToast(); const mutation = useMutation({ mutationFn: (id: number) => analyticsService.getBatchOptimization(id), onError: (err: Error) => { toast({ title: "Optimization failed", description: err.message || "Could not analyze this batch.", variant: "destructive", }); }, }); const handleOpen = () => { if (batchId == null) { toast({ title: "No batch selected", description: "Choose a batch to analyze, or open this from a batch detail page.", variant: "destructive", }); return; } mutation.reset(); setOpen(true); mutation.mutate(batchId); }; const handleApply = () => { toast({ title: "Suggestion Applied", description: "Batch split recommendation has been saved successfully." }); setOpen(false); }; const onOpenChange = (next: boolean) => { setOpen(next); if (!next) mutation.reset(); }; const suggestions = mutation.data ?? []; const showResults = !mutation.isPending && !mutation.isError && suggestions.length > 0; const showEmpty = !mutation.isPending && !mutation.isError && mutation.isSuccess && suggestions.length === 0; return ( <> AI Suggest Batch Split AI Batch Optimization {mutation.isPending ? ( Analyzing batch data... ) : mutation.isError ? ( Something went wrong. Try again. ) : showResults ? ( {suggestions.map((s, i) => ( {s.impact} impact {s.suggestion} {s.details ? {s.details} : null} ))} Apply Suggestion onOpenChange(false)}> Dismiss ) : showEmpty ? ( No optimization suggestions for this batch. ) : null} > ); }
Something went wrong. Try again.
{s.impact} impact
{s.suggestion}
{s.details}
No optimization suggestions for this batch.