import React, { useState } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "./ui/dialog"; import { Button } from "./ui/button"; import { Textarea } from "./ui/textarea"; import { Loader } from "lucide-react"; import { toast } from "sonner"; import { useCreateAnimation } from "@/hooks/useCreateAnimation"; import { AnimationRequest, RobotAnimationConfig } from "@/lib/types"; import { useUrdf } from "@/hooks/useUrdf"; interface AnimationDialogProps { open: boolean; onOpenChange: (open: boolean) => void; robotName: string; } const AnimationDialog: React.FC = ({ open, onOpenChange, robotName, }) => { const [animationPrompt, setAnimationPrompt] = useState(""); const [isGeneratingAnimation, setIsGeneratingAnimation] = useState(false); const { createAnimation, clearError } = useCreateAnimation(); const { urdfContent, currentRobotData, setCurrentAnimationConfig } = useUrdf(); // Handle dialog close - clear error states const handleOpenChange = (open: boolean) => { if (!open) { clearError(); } onOpenChange(open); }; const handleAnimationRequest = async () => { if (!animationPrompt.trim()) { toast.error("Please enter an animation description"); return; } if (!urdfContent) { toast.error("No URDF content available", { description: "Please upload a URDF model first.", }); return; } setIsGeneratingAnimation(true); try { // Create the animation request const request: AnimationRequest = { robotName: currentRobotData?.name || robotName, urdfContent, description: animationPrompt, }; // Call the createAnimation function const animationResult = await createAnimation(request); setIsGeneratingAnimation(false); onOpenChange(false); // Reset the prompt for next time setAnimationPrompt(""); // Store animation in the context instead of using the onAnimationApplied callback if (animationResult) { setCurrentAnimationConfig(animationResult); // Show success message toast.success("Animation applied successfully", { description: `Applied: "${animationPrompt}"`, duration: 2000, }); } } catch (error) { setIsGeneratingAnimation(false); // The error toast is already handled in the hook console.error("Animation generation failed:", error); } }; return ( Animation Request for {currentRobotData?.name || robotName}