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 (
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.
{error}
)}