"use client"; import { useState, useRef, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Sheet, SheetContent, SheetHeader, SheetTitle } from "@/components/ui/sheet"; import { MessageCircle, Send, Bot, User } from "lucide-react"; import ReactMarkdown from "react-markdown"; import { cn } from "@/lib/utils"; type Message = { role: "user" | "assistant"; content: string; }; type Props = { recipeId: string; recipeTitle: string; }; export function RecipeChatPanel({ recipeId, recipeTitle }: Props) { const [open, setOpen] = useState(false); const [messages, setMessages] = useState([]); const [input, setInput] = useState(""); const [loading, setLoading] = useState(false); const bottomRef = useRef(null); useEffect(() => { if (open && bottomRef.current) { bottomRef.current.scrollIntoView({ behavior: "smooth" }); } }, [messages, open]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); const question = input.trim(); if (!question || loading) return; setInput(""); setMessages((prev) => [...prev, { role: "user", content: question }]); setLoading(true); try { const res = await fetch("/api/v1/ai/recipe-chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ recipeId, question }), }); const data = await res.json() as { answer?: string; error?: string }; setMessages((prev) => [ ...prev, { role: "assistant", content: data.answer ?? "Sorry, I couldn't answer that." }, ]); } catch { setMessages((prev) => [ ...prev, { role: "assistant", content: "Something went wrong. Please try again." }, ]); } finally { setLoading(false); } }; const suggestions = [ "Can I substitute any ingredients?", "How do I know when it's done?", "Can I make this ahead of time?", "What can I serve with this?", ]; return ( <> Ask about this recipe

{recipeTitle}

{messages.length === 0 && (

Ask anything about this recipe — ingredients, techniques, substitutions, timing…

{suggestions.map((s) => ( ))}
)} {messages.map((msg, i) => (
{msg.role === "user" ? : }
{msg.role === "assistant" ? ( {msg.content} ) : ( msg.content )}
))} {loading && (
)}
setInput(e.target.value)} placeholder="Ask a question…" disabled={loading} className="flex-1" autoComplete="off" />
); }