const EliaChat = () => { const [open, setOpen] = React.useState(false); const [messages, setMessages] = React.useState([ { role: 'elia', text: '¡Hola! Soy Elia, tu asistente estratégica. ✦ ¿En qué puedo ayudarte hoy?' } ]); const [input, setInput] = React.useState(''); const [loading, setLoading] = React.useState(false); const scrollRef = React.useRef(); React.useEffect(() => { if (scrollRef.current) scrollRef.current.scrollTop = scrollRef.current.scrollHeight; }, [messages]); const send = async () => { if (!input.trim() || loading) return; const msg = input.trim(); setInput(''); setMessages(prev => [...prev, { role: 'user', text: msg }]); setLoading(true); try { const res = await API.post('/chat', { message: msg }); setMessages(prev => [...prev, { role: 'elia', text: res.response }]); } catch (e) { setMessages(prev => [...prev, { role: 'elia', text: 'Lo siento, hubo un error al procesar tu solicitud.' }]); } finally { setLoading(false); } }; const bubble = { padding: '10px 14px', borderRadius: 18, fontSize: 13, lineHeight: 1.5, maxWidth: '85%', position: 'relative', marginBottom: 8 }; return (
{/* Botón flotante */} {/* Ventana de chat */} {open && (
{/* Header */}
Elia ✨
Asistente Estratégica Nativa
{/* Messages */}
{messages.map((m, i) => (
{m.text.split('\n').map((line, idx) =>
{line}
)}
))} {loading &&
Elia está pensando...
}
{/* Input */}
setInput(e.target.value)} onKeyDown={e => e.key === 'Enter' && send()} placeholder="Pregúntame algo..." style={{ flex: 1, border: '1.5px solid #E0E8FF', borderRadius: 50, padding: '8px 16px', fontSize: 13, outline: 'none' }} />
)}
); }; window.EliaChat = EliaChat;