Refactor ChatWindow to ChatHistory component
Browse files
frontend/src/components/ChatHistory.tsx
CHANGED
|
@@ -1,109 +1,28 @@
|
|
| 1 |
-
import 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-
|
| 70 |
-
<
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 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
|
|
|
|
| 1 |
+
import React from 'react';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
function ChatHistory({ chats, currentChatId, onSelectChat, onNewChat }: any) {
|
| 4 |
return (
|
| 5 |
+
<div className="chat-history">
|
| 6 |
+
<button className="new-chat-btn" onClick={onNewChat}>
|
| 7 |
+
+ Nuevo chat
|
| 8 |
+
</button>
|
| 9 |
+
<div className="chat-list">
|
| 10 |
+
{Object.entries(chats).map(([chatId, messages]: any) => (
|
| 11 |
+
<div
|
| 12 |
+
key={chatId}
|
| 13 |
+
className={`chat-item ${chatId === currentChatId ? 'active' : ''}`}
|
| 14 |
+
onClick={() => onSelectChat(chatId)}
|
| 15 |
+
>
|
| 16 |
+
{chatId === currentChatId ? '● ' : ''}
|
| 17 |
+
{messages && messages.length > 0
|
| 18 |
+
? messages[0].content.substring(0, 30) + '...'
|
| 19 |
+
: `Chat ${chatId}`
|
| 20 |
+
}
|
|
|
|
|
|
|
| 21 |
</div>
|
| 22 |
))}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
</div>
|
| 24 |
</div>
|
| 25 |
);
|
| 26 |
}
|
| 27 |
|
| 28 |
+
export default ChatHistory;
|