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(); const [isMobile, setIsMobile] = React.useState(window.innerWidth < 768); React.useEffect(() => { const handle = () => setIsMobile(window.innerWidth < 768); window.addEventListener('resize', handle); return () => window.removeEventListener('resize', handle); }, []); React.useEffect(() => { if (scrollRef.current) scrollRef.current.scrollTop = scrollRef.current.scrollHeight; }, [messages]); // Prevent body scroll when chat is open on mobile React.useEffect(() => { if (open && isMobile) { document.body.style.overflow = 'hidden'; return () => { document.body.style.overflow = ''; }; } }, [open, isMobile]); 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 { 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.6, maxWidth: '85%', position: 'relative', marginBottom: 8, wordBreak: 'break-word' }; // ── MOBILE: Fullscreen overlay ── if (isMobile) { return ( <> {/* Floating button — positioned above bottom nav */} {!open && ( )} {/* Fullscreen chat panel */} {open && (
{/* Header */}
Elia { e.target.style.display='none'; }} />
Elia ✨
Asistente Estratégica
{/* 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: '10px 16px', fontSize: 14, outline: 'none', minWidth: 0, fontFamily: 'inherit' }} />
)} ); } // ── DESKTOP: Floating panel ── return (
{open && (
{/* Header */}
Elia { e.target.style.display='none'; }} />
Elia ✨
Asistente Estratégica
{/* 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: '9px 16px', fontSize: 13, outline: 'none', minWidth: 0 }} />
)}
); }; window.EliaChat = EliaChat;