Spaces:
Sleeping
Sleeping
File size: 7,187 Bytes
8421ec4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | import React, { useState, useEffect, useCallback } from 'react';
import { Chat, Message, ChatState } from './types';
import { createChat, getChats, getMessages, sendMessage, deleteChat } from './services/api';
import ChatSidebar from './components/ChatSidebar';
import ChatWindow from './components/ChatWindow';
import DocumentsModal from './components/DocumentsModal';
const App: React.FC = () => {
const [state, setState] = useState<ChatState>({
chats: [],
activeChat: null,
messages: [],
isLoading: false,
isSending: false,
error: null,
});
const [isDocsModalOpen, setIsDocsModalOpen] = useState(false);
// Listen for custom event to open docs modal
useEffect(() => {
const handleOpenModal = () => setIsDocsModalOpen(true);
window.addEventListener('open-documents-modal', handleOpenModal);
return () => window.removeEventListener('open-documents-modal', handleOpenModal);
}, []);
// Load chats on mount
useEffect(() => {
loadChats();
}, []);
// Load all chats from API
const loadChats = async () => {
try {
setState(prev => ({ ...prev, isLoading: true, error: null }));
const chats = await getChats();
setState(prev => ({ ...prev, chats, isLoading: false }));
} catch (err) {
setState(prev => ({
...prev,
isLoading: false,
error: err instanceof Error ? err.message : 'Failed to load chats',
}));
}
};
// Load messages for a chat
const loadMessages = async (chatId: string) => {
try {
setState(prev => ({ ...prev, isLoading: true, error: null }));
const messages = await getMessages(chatId);
setState(prev => ({ ...prev, messages, isLoading: false }));
} catch (err) {
setState(prev => ({
...prev,
isLoading: false,
error: err instanceof Error ? err.message : 'Failed to load messages',
}));
}
};
// Handle selecting a chat
const handleSelectChat = useCallback(async (chat: Chat) => {
setState(prev => ({ ...prev, activeChat: chat, messages: [] }));
await loadMessages(chat.id);
}, []);
// Handle creating a new chat
const handleNewChat = useCallback(async () => {
try {
setState(prev => ({ ...prev, isLoading: true, error: null }));
const chat = await createChat();
setState(prev => ({
...prev,
chats: [chat, ...prev.chats],
activeChat: chat,
messages: [],
isLoading: false,
}));
} catch (err) {
setState(prev => ({
...prev,
isLoading: false,
error: err instanceof Error ? err.message : 'Failed to create chat',
}));
}
}, []);
// Handle deleting a chat
const handleDeleteChat = useCallback(async (chatId: string) => {
try {
await deleteChat(chatId);
setState(prev => {
const newChats = prev.chats.filter(c => c.id !== chatId);
const newActiveChat = prev.activeChat?.id === chatId ? null : prev.activeChat;
const newMessages = prev.activeChat?.id === chatId ? [] : prev.messages;
return {
...prev,
chats: newChats,
activeChat: newActiveChat,
messages: newMessages,
};
});
} catch (err) {
setState(prev => ({
...prev,
error: err instanceof Error ? err.message : 'Failed to delete chat',
}));
}
}, []);
// Handle sending a message
const handleSendMessage = useCallback(async (content: string) => {
if (!state.activeChat) return;
const chatId = state.activeChat.id;
// Optimistically add user message
const tempUserMsg: Message = {
id: `temp-${Date.now()}`,
chat_id: chatId,
role: 'user',
content,
timestamp: new Date().toISOString(),
};
setState(prev => ({
...prev,
messages: [...prev.messages, tempUserMsg],
isSending: true,
error: null,
}));
try {
const response = await sendMessage(chatId, content);
setState(prev => {
// Replace temp message with real messages
const messagesWithoutTemp = prev.messages.filter(m => m.id !== tempUserMsg.id);
const newMessages = [
...messagesWithoutTemp,
response.user_message,
response.assistant_message,
];
// Update chat title if it changed (first message)
const updatedChats = prev.chats.map(chat => {
if (chat.id === chatId && chat.title === 'New Chat') {
const newTitle = content.length > 50 ? content.slice(0, 50) + '...' : content;
return { ...chat, title: newTitle };
}
return chat;
});
// Update active chat title too
const updatedActiveChat = prev.activeChat && prev.activeChat.title === 'New Chat'
? { ...prev.activeChat, title: content.length > 50 ? content.slice(0, 50) + '...' : content }
: prev.activeChat;
return {
...prev,
messages: newMessages,
chats: updatedChats,
activeChat: updatedActiveChat,
isSending: false,
};
});
} catch (err) {
setState(prev => ({
...prev,
messages: prev.messages.filter(m => m.id !== tempUserMsg.id),
isSending: false,
error: err instanceof Error ? err.message : 'Failed to send message',
}));
}
}, [state.activeChat]);
return (
<div className="h-screen flex bg-gray-100 overflow-hidden">
{/* Sidebar */}
<ChatSidebar
chats={state.chats}
activeChat={state.activeChat}
onSelectChat={handleSelectChat}
onNewChat={handleNewChat}
onDeleteChat={handleDeleteChat}
isLoading={state.isLoading}
/>
{/* Main Chat Area */}
<ChatWindow
activeChat={state.activeChat}
messages={state.messages}
onSendMessage={handleSendMessage}
isSending={state.isSending}
isLoading={state.isLoading}
/>
{/* Error Toast */}
{state.error && (
<div className="fixed bottom-4 right-4 max-w-md bg-red-500 text-white px-6 py-4 rounded-xl shadow-2xl flex items-center gap-3 animate-in slide-in-from-bottom-4 duration-300">
<svg className="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
<span className="text-sm font-medium">{state.error}</span>
<button
onClick={() => setState(prev => ({ ...prev, error: null }))}
className="ml-2 hover:bg-red-600 p-1 rounded transition-colors"
>
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
)}
{/* Documents Management Modal */}
<DocumentsModal
isOpen={isDocsModalOpen}
onClose={() => setIsDocsModalOpen(false)}
/>
</div>
);
};
export default App;
|