fix: force navigation to /chat/new on endpoint change and conversation deletions (#1141)

This commit is contained in:
Danny Avila 2023-11-04 20:33:24 -04:00 committed by GitHub
parent 0886441461
commit a2ee57568a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 76 additions and 61 deletions

View file

@ -1,13 +1,16 @@
import { useState, useRef, useEffect } from 'react';
import { useState, useRef } from 'react';
import { useRecoilState, useSetRecoilState } from 'recoil';
import { useUpdateConversationMutation } from 'librechat-data-provider';
import RenameButton from './RenameButton';
import DeleteButton from './DeleteButton';
import { MinimalIcon } from '~/components/Endpoints';
import { useConversations, useConversation } from '~/hooks';
import { MinimalIcon } from '~/components/Endpoints';
import { NotificationSeverity } from '~/common';
import { useToastContext } from '~/Providers';
import DeleteButton from './DeleteButton';
import RenameButton from './RenameButton';
import store from '~/store';
export default function Conversation({ conversation, retainView }) {
const { showToast } = useToastContext();
const [currentConversation, setCurrentConversation] = useRecoilState(store.conversation);
const setSubmission = useSetRecoilState(store.submission);
@ -63,7 +66,28 @@ export default function Conversation({ conversation, retainView }) {
if (titleInput === title) {
return;
}
updateConvoMutation.mutate({ conversationId, title: titleInput });
updateConvoMutation.mutate(
{ conversationId, title: titleInput },
{
onSuccess: () => {
refreshConversations();
if (conversationId == currentConversation?.conversationId) {
setCurrentConversation((prevState) => ({
...prevState,
title: titleInput,
}));
}
},
onError: () => {
setTitleInput(title);
showToast({
message: 'Failed to rename conversation',
severity: NotificationSeverity.ERROR,
showIcon: true,
});
},
},
);
};
const icon = MinimalIcon({
@ -74,19 +98,6 @@ export default function Conversation({ conversation, retainView }) {
className: 'mr-0',
});
useEffect(() => {
if (updateConvoMutation.isSuccess) {
refreshConversations();
if (conversationId == currentConversation?.conversationId) {
setCurrentConversation((prevState) => ({
...prevState,
title: titleInput,
}));
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [updateConvoMutation.isSuccess]);
const handleKeyDown = (e) => {
if (e.key === 'Enter') {
onRename(e);

View file

@ -1,4 +1,3 @@
import { useEffect } from 'react';
import TrashIcon from '../svg/TrashIcon';
import CrossIcon from '../svg/CrossIcon';
import { useRecoilValue } from 'recoil';
@ -13,24 +12,25 @@ export default function DeleteButton({ conversationId, renaming, retainView, tit
const currentConversation = useRecoilValue(store.conversation) || {};
const { newConversation } = useConversation();
const { refreshConversations } = useConversations();
const confirmDelete = () => {
deleteConvoMutation.mutate({ conversationId, source: 'button' });
};
const deleteConvoMutation = useDeleteConversationMutation(conversationId);
useEffect(() => {
if (deleteConvoMutation.isSuccess) {
if ((currentConversation as { conversationId?: string }).conversationId == conversationId) {
newConversation();
}
const confirmDelete = () => {
deleteConvoMutation.mutate(
{ conversationId, source: 'button' },
{
onSuccess: () => {
if (
(currentConversation as { conversationId?: string }).conversationId == conversationId
) {
newConversation();
}
refreshConversations();
retainView();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [deleteConvoMutation.isSuccess]);
refreshConversations();
retainView();
},
},
);
};
return (
<Dialog>