Skip to content

Commit

Permalink
Fix issue with deleting chat history not updating current chat ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Royal-lobster committed Oct 1, 2023
1 parent 9e32da0 commit 2c85d8f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/components/Sidebar/chat/ChatHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ const ChatHistory = () => {

const handleChatDelete = (id: string) => {
deleteChatHistory(id);
if (id === currentChatId) {
setCurrentChatId(history[0].id);
}
};

return (
Expand Down Expand Up @@ -58,6 +55,7 @@ const ChatHistory = () => {
<DropdownMenu.Item
key={chat.id}
onSelect={() => {
alert("CLICKED")
setCurrentChatId(chat.id);
}}
className={`cdx-px-3 cdx-py-1.5 cdx-relative cdx-flex cdx-gap-3 cdx-justify-between cdx-items-center cdx-border-b dark:cdx-border-b-[#2F2F2F] ${
Expand Down
10 changes: 8 additions & 2 deletions src/hooks/useChatHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ export const useChatHistory = () => {
return newId;
};

const deleteChatHistory = (id: string | null) => {
const deleteChatHistory = async (id: string | null) => {
if (!id) return;
setHistory((prev) => prev.filter((h) => h.id !== id));
const newCurrentChatId = history.find((h) => h.id !== id)?.id ?? null;
setCurrentChatId(newCurrentChatId); // FIXME: this is not working

//TODO: This is a work around to make sure the current chat id is updated
// when message is deleted from the chat history dropdown menu cuz onSelect
// will also trigger when the user clicks on the delete button. we need
// to rethink UI/UX for this
await new Promise((resolve) => setTimeout(resolve, 1000));
setCurrentChatId(newCurrentChatId);
};

const getChatHistory = (id: string) => {
Expand Down

0 comments on commit 2c85d8f

Please sign in to comment.