import { useState, useRef, useEffect } from 'react'; import { Send, Loader2, Bot, User } from 'lucide-react'; import { API_BASE_URL } from '../services/api'; export const ChatPanel = () => { const [messages, setMessages] = useState<{ sender: 'bot' | 'user'; text: string }[]>([ { sender: 'bot', text: 'Ask me anything about your notes, tasks, and transcripts.' } ]); const [input, setInput] = useState(''); const [loading, setLoading] = useState(false); const messagesEndRef = useRef(null); useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages]); const handleSend = async (e: React.FormEvent) => { e.preventDefault(); if (!input.trim() || loading) return; const userMessage = input; setInput(''); setMessages(prev => [...prev, { sender: 'user', text: userMessage }]); setLoading(true); try { const response = await fetch(`${API_BASE_URL}/chat`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ prompt: userMessage }), }); if (!response.ok) throw new Error('Chat failed'); const data = await response.json(); setMessages(prev => [...prev, { sender: 'bot', text: data.response || 'No response.' }]); } catch (err) { setTimeout(() => { setMessages(prev => [...prev, { sender: 'bot', text: `[Offline] Found 3 items related to "${userMessage}" in local records.` }]); }, 800); } finally { setLoading(false); } }; return (
{messages.map((m, idx) => (
{m.sender === 'bot' ? : } {m.text}
))} {loading && (
AI reading context...
)}
setInput(e.target.value)} placeholder="Ask about notes/tasks..." className="input" disabled={loading} />
); };