"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Link2, 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 { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; type ImportedRecipe = { title: string; ingredients: Array<{ rawName: string; quantity?: string; unit?: string }>; steps: Array<{ instruction: string }>; }; export function UrlImportDialog({ open, onOpenChange, }: { open: boolean; onOpenChange: (open: boolean) => void; }) { const router = useRouter(); const [url, setUrl] = useState(""); const [importing, setImporting] = useState(false); async function handleImport() { if (!url.trim()) return; setImporting(true); try { const res = await fetch("/api/v1/ai/import-url", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ url: url.trim() }), }); if (!res.ok) { const err = await res.json() as { error?: string }; toast.error(err.error ?? "Failed to import recipe"); return; } const imported = await res.json() as ImportedRecipe; const saveRes = await fetch("/api/v1/recipes", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...imported, visibility: "private", sourceUrl: url.trim(), }), }); if (!saveRes.ok) { toast.error("Failed to save imported recipe"); return; } const saved = await saveRes.json() as { id: string }; toast.success("Recipe imported! Review before publishing."); onOpenChange(false); router.push(`/recipes/${saved.id}/edit`); } finally { setImporting(false); } } return ( Import recipe from URL Paste a recipe URL and AI will extract the ingredients and instructions.
setUrl(e.target.value)} placeholder="https://..." disabled={importing} onKeyDown={(e) => e.key === "Enter" && handleImport()} />
); }