import { useEffect, useRef, useState } from 'react'; import { SendHorizontal, Sparkles } from 'lucide-react'; import { ChatMessage } from './ChatMessage'; import { Spinner } from './Atoms'; import { api } from '../api/client'; const SUGGESTIONS = [ 'Recommend a low sugar biscuit', 'What snacks come in family packs?', 'Show me something under ₹20', 'Any options with no added preservatives?', ]; export function ChatPanel({ brand, onSelectSource }) { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const scrollRef = useRef(null); useEffect(() => { scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight, behavior: 'smooth' }); }, [messages, loading]); async function send(text) { const query = (text ?? input).trim(); if (!query || loading) return; setInput(''); setError(null); const history = messages.map((m) => ({ role: m.role, content: m.content })); setMessages((prev) => [...prev, { role: 'user', content: query }]); setLoading(true); try { const res = await api.chat({ query, brand: brand || undefined, history }); setMessages((prev) => [...prev, { role: 'assistant', content: res.answer, sources: res.sources, detectedCategory: res.detected_category }]); } catch (err) { setError(err.message); } finally { setLoading(false); } } return (
{messages.length === 0 && (

Ask about the catalog

I'll answer using only the products actually in the catalog {brand ? ` for ${brand}` : ''}, and show you exactly which ones I used.

{SUGGESTIONS.map((s) => ( ))}
)} {messages.map((m, i) => ( ))} {loading && (
)} {error && (

{error}

)}
{ e.preventDefault(); send(); }} className="flex items-center gap-2 border-t border-ink-900/10 bg-paper-50 px-4 py-3" > setInput(e.target.value)} placeholder={brand ? `Ask about ${brand}'s products…` : 'Ask about any product in the catalog…'} className="flex-1 rounded-full border border-ink-900/10 bg-white px-4 py-2.5 text-sm focus:border-amber-500 focus:outline-none focus:ring-2 focus:ring-amber-500/20" />
); }