diff --git a/components/header/Header.tsx b/components/header/Header.tsx index ddd19ed..00a3df7 100644 --- a/components/header/Header.tsx +++ b/components/header/Header.tsx @@ -27,7 +27,7 @@ const Header: React.FC = ({ setSidebarOpen }) => { -
+
-
+
Your chats
@@ -90,7 +87,6 @@ const SidebarList: React.FC = ({ 0 ? updatedAllChats[0].chatId : ''; + const deletedChatIndex = allChats.findIndex( + (chat) => chat.chatId === chatIdToDelete, + ); + let newSelectedChatId = ''; + if (updatedAllChats[deletedChatIndex]) { + newSelectedChatId = updatedAllChats[deletedChatIndex].chatId; + } else if (deletedChatIndex > 0) { + newSelectedChatId = updatedAllChats[deletedChatIndex - 1].chatId; + } setSelectedChatId(newSelectedChatId); } } diff --git a/pages/_document.tsx b/pages/_document.tsx index ec2b311..909994e 100644 --- a/pages/_document.tsx +++ b/pages/_document.tsx @@ -4,6 +4,7 @@ export default function Document() { return ( +
diff --git a/pages/index.tsx b/pages/index.tsx index 9ea9c0a..535d4ef 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -82,16 +82,11 @@ export default function Home() { const textAreaRef = useRef(null); const fetchChatHistory = useCallback(() => { + setLoading(true); + try { const conversations = getConversation(selectedChatId); - console.log( - 'Chat ID:', - selectedChatId, - 'History:', - conversations.history, - ); - if (!conversations || !conversations.messages) { console.error('Failed to fetch chat history: No conversations found.'); return; @@ -121,14 +116,22 @@ export default function Home() { })); } catch (error) { console.error('Failed to fetch chat history:', error); + } finally { + setLoading(false); } }, [selectedChatId, getConversation]); useEffect(() => { - if (selectedNamespace && chatList.length > 0) { + if (selectedNamespace && chatList.length > 0 && !selectedChatId) { setSelectedChatId(chatList[0].chatId); } - }, [selectedNamespace, chatList, setSelectedChatId]); + }, [selectedNamespace, chatList, selectedChatId, setSelectedChatId]); + + useEffect(() => { + if (chatList.length > 0) { + setSelectedChatId(chatList[chatList.length - 1].chatId); + } + }, [selectedNamespace, setSelectedChatId, chatList]); useEffect(() => { if (selectedChatId) { @@ -165,12 +168,6 @@ export default function Home() { setQuery(''); const conversation = getConversation(selectedChatId); - console.log( - 'Sending to API - Chat ID:', - selectedChatId, - 'History:', - conversation.history, - ); const response = await fetch('/api/chat', { method: 'POST', @@ -235,7 +232,7 @@ export default function Home() { return ( <> - {status === 'loading' ? ( + {loading ? ( ) : (
@@ -298,7 +295,6 @@ export default function Home() { (chat) => chat.chatId, )} selectedChatId={selectedChatId} - setChatId={setSelectedChatId} setSelectedChatId={setSelectedChatId} chatNames={chatNames} updateChatName={updateChatName} @@ -326,7 +322,6 @@ export default function Home() { namespaces={namespaces} filteredChatList={filteredChatList.map((chat) => chat.chatId)} selectedChatId={selectedChatId} - setChatId={setSelectedChatId} setSelectedChatId={setSelectedChatId} chatNames={chatNames} updateChatName={updateChatName}