"use client"; import { useState } from "react"; import { useTranslations } from "next-intl"; import { Wand2, Loader2 } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from "@/components/ui/dialog"; import { Textarea } from "@/components/ui/textarea"; import { Label } from "@/components/ui/label"; import type { RegeneratedRecipe } from "@/lib/ai/features/regenerate-recipe"; type CurrentRecipe = { title: string; description?: string; baseServings: number; difficulty?: "easy" | "medium" | "hard" | ""; ingredients: Array<{ rawName: string; quantity?: string; unit?: string }>; steps: Array<{ instruction: string }>; }; export function RegenerateRecipeButton({ current, onRegenerated, }: { current: CurrentRecipe; onRegenerated: (recipe: RegeneratedRecipe) => void; }) { const t = useTranslations("recipe"); const [open, setOpen] = useState(false); const [instruction, setInstruction] = useState(""); const [regenerating, setRegenerating] = useState(false); async function handleRegenerate() { if (!instruction.trim()) return; setRegenerating(true); try { const res = await fetch("/api/v1/ai/regenerate", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title: current.title, description: current.description || undefined, baseServings: current.baseServings, difficulty: current.difficulty || undefined, ingredients: current.ingredients, steps: current.steps, instruction: instruction.trim(), }), }); if (!res.ok) { const err = await res.json() as { error?: string }; toast.error(err.error ?? t("regenerateFailed")); return; } const regenerated = await res.json() as RegeneratedRecipe; onRegenerated(regenerated); toast.success(t("regenerated")); setOpen(false); setInstruction(""); } catch { toast.error(t("regenerateFailed")); } finally { setRegenerating(false); } } return ( <> setOpen(true)} className="gap-1.5"> {t("regenerateWithAi")} { setOpen(v); if (!v) setInstruction(""); }}> {t("regenerateTitle")} {t("regenerateDescription")} {t("regenerateInstructionLabel")} setInstruction(e.target.value)} placeholder={t("regenerateInstructionPlaceholder")} rows={3} maxLength={500} disabled={regenerating} /> setOpen(false)} disabled={regenerating}> {t("regenerateCancel")} {regenerating ? <>{t("regenerating")}> : <>{t("regenerateApply")}> } > ); }