Implement ChatWindow component with message handling
Browse files
frontend/src/components/ChatWindow.tsx
CHANGED
|
@@ -1 +1,109 @@
|
|
|
|
|
| 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useEffect } from 'react';
|
| 2 |
|
| 3 |
+
interface Message {
|
| 4 |
+
id: string;
|
| 5 |
+
role: 'user' | 'assistant' | 'error';
|
| 6 |
+
content: string;
|
| 7 |
+
avatar: string;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
function ChatWindow({ chatId, onUpdateChat }: any) {
|
| 11 |
+
const [messages, setMessages] = useState<Message[]>([]);
|
| 12 |
+
const [input, setInput] = useState('');
|
| 13 |
+
const [loading, setLoading] = useState(false);
|
| 14 |
+
|
| 15 |
+
useEffect(() => {
|
| 16 |
+
setMessages([]);
|
| 17 |
+
}, [chatId]);
|
| 18 |
+
|
| 19 |
+
const handleSendMessage = async () => {
|
| 20 |
+
if (!input.trim()) return;
|
| 21 |
+
|
| 22 |
+
const userMessage: Message = {
|
| 23 |
+
id: Date.now().toString(),
|
| 24 |
+
role: 'user',
|
| 25 |
+
content: input,
|
| 26 |
+
avatar: '👤',
|
| 27 |
+
};
|
| 28 |
+
|
| 29 |
+
setMessages(prev => [...prev, userMessage]);
|
| 30 |
+
setInput('');
|
| 31 |
+
setLoading(true);
|
| 32 |
+
|
| 33 |
+
try {
|
| 34 |
+
const response = await fetch('/api/chat/send', {
|
| 35 |
+
method: 'POST',
|
| 36 |
+
headers: { 'Content-Type': 'application/json' },
|
| 37 |
+
body: JSON.stringify({
|
| 38 |
+
chat_id: chatId,
|
| 39 |
+
content: input,
|
| 40 |
+
}),
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
if (!response.ok) throw new Error('Error sending message');
|
| 44 |
+
|
| 45 |
+
const data = await response.json();
|
| 46 |
+
const assistantMessage: Message = {
|
| 47 |
+
id: (Date.now() + 1).toString(),
|
| 48 |
+
role: 'assistant',
|
| 49 |
+
content: data.response,
|
| 50 |
+
avatar: '🤖',
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
setMessages(prev => [...prev, assistantMessage]);
|
| 54 |
+
onUpdateChat([...messages, userMessage, assistantMessage]);
|
| 55 |
+
} catch (error) {
|
| 56 |
+
console.error('Error:', error);
|
| 57 |
+
setMessages(prev => [...prev, {
|
| 58 |
+
id: Date.now().toString(),
|
| 59 |
+
role: 'error',
|
| 60 |
+
content: 'Error sending message',
|
| 61 |
+
avatar: '⚠️',
|
| 62 |
+
}]);
|
| 63 |
+
} finally {
|
| 64 |
+
setLoading(false);
|
| 65 |
+
}
|
| 66 |
+
};
|
| 67 |
+
|
| 68 |
+
return (
|
| 69 |
+
<div className="chat-window">
|
| 70 |
+
<div className="messages">
|
| 71 |
+
{messages.length === 0 && (
|
| 72 |
+
<div className="initial-menu">
|
| 73 |
+
<h1>Email Story Creator</h1>
|
| 74 |
+
<p>✉️ Experto en emails narrativos que conectan historias con ventas</p>
|
| 75 |
+
<div className="example-buttons">
|
| 76 |
+
<button className="example-btn">Definir audiencia 🎯</button>
|
| 77 |
+
<button className="example-btn">Propuesta de valor 💎</button>
|
| 78 |
+
<button className="example-btn">CTA que convierte 🚀</button>
|
| 79 |
+
<button className="example-btn">Asunto + gancho ✉️</button>
|
| 80 |
+
</div>
|
| 81 |
+
</div>
|
| 82 |
+
)}
|
| 83 |
+
{messages.map(msg => (
|
| 84 |
+
<div key={msg.id} className={`message ${msg.role}`}>
|
| 85 |
+
<span className="avatar">{msg.avatar}</span>
|
| 86 |
+
<div className="content">{msg.content}</div>
|
| 87 |
+
</div>
|
| 88 |
+
))}
|
| 89 |
+
{loading && <div className="message loading">🤖 Escribiendo...</div>}
|
| 90 |
+
</div>
|
| 91 |
+
|
| 92 |
+
<div className="input-area">
|
| 93 |
+
<input
|
| 94 |
+
type="text"
|
| 95 |
+
value={input}
|
| 96 |
+
onChange={(e) => setInput(e.target.value)}
|
| 97 |
+
onKeyPress={(e) => e.key === 'Enter' && handleSendMessage()}
|
| 98 |
+
placeholder="Escribe aquí tus instrucciones"
|
| 99 |
+
disabled={loading}
|
| 100 |
+
/>
|
| 101 |
+
<button onClick={handleSendMessage} disabled={loading}>
|
| 102 |
+
Enviar
|
| 103 |
+
</button>
|
| 104 |
+
</div>
|
| 105 |
+
</div>
|
| 106 |
+
);
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
export default ChatWindow;
|