mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
fix: force navigation to /chat/new on endpoint change and conversation deletions (#1141)
This commit is contained in:
parent
0886441461
commit
a2ee57568a
7 changed files with 76 additions and 61 deletions
|
|
@ -1,13 +1,16 @@
|
||||||
import { useState, useRef, useEffect } from 'react';
|
import { useState, useRef } from 'react';
|
||||||
import { useRecoilState, useSetRecoilState } from 'recoil';
|
import { useRecoilState, useSetRecoilState } from 'recoil';
|
||||||
import { useUpdateConversationMutation } from 'librechat-data-provider';
|
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 { 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';
|
import store from '~/store';
|
||||||
|
|
||||||
export default function Conversation({ conversation, retainView }) {
|
export default function Conversation({ conversation, retainView }) {
|
||||||
|
const { showToast } = useToastContext();
|
||||||
const [currentConversation, setCurrentConversation] = useRecoilState(store.conversation);
|
const [currentConversation, setCurrentConversation] = useRecoilState(store.conversation);
|
||||||
const setSubmission = useSetRecoilState(store.submission);
|
const setSubmission = useSetRecoilState(store.submission);
|
||||||
|
|
||||||
|
|
@ -63,7 +66,28 @@ export default function Conversation({ conversation, retainView }) {
|
||||||
if (titleInput === title) {
|
if (titleInput === title) {
|
||||||
return;
|
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({
|
const icon = MinimalIcon({
|
||||||
|
|
@ -74,19 +98,6 @@ export default function Conversation({ conversation, retainView }) {
|
||||||
className: 'mr-0',
|
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) => {
|
const handleKeyDown = (e) => {
|
||||||
if (e.key === 'Enter') {
|
if (e.key === 'Enter') {
|
||||||
onRename(e);
|
onRename(e);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import { useEffect } from 'react';
|
|
||||||
import TrashIcon from '../svg/TrashIcon';
|
import TrashIcon from '../svg/TrashIcon';
|
||||||
import CrossIcon from '../svg/CrossIcon';
|
import CrossIcon from '../svg/CrossIcon';
|
||||||
import { useRecoilValue } from 'recoil';
|
import { useRecoilValue } from 'recoil';
|
||||||
|
|
@ -13,24 +12,25 @@ export default function DeleteButton({ conversationId, renaming, retainView, tit
|
||||||
const currentConversation = useRecoilValue(store.conversation) || {};
|
const currentConversation = useRecoilValue(store.conversation) || {};
|
||||||
const { newConversation } = useConversation();
|
const { newConversation } = useConversation();
|
||||||
const { refreshConversations } = useConversations();
|
const { refreshConversations } = useConversations();
|
||||||
|
|
||||||
const confirmDelete = () => {
|
|
||||||
deleteConvoMutation.mutate({ conversationId, source: 'button' });
|
|
||||||
};
|
|
||||||
|
|
||||||
const deleteConvoMutation = useDeleteConversationMutation(conversationId);
|
const deleteConvoMutation = useDeleteConversationMutation(conversationId);
|
||||||
|
|
||||||
useEffect(() => {
|
const confirmDelete = () => {
|
||||||
if (deleteConvoMutation.isSuccess) {
|
deleteConvoMutation.mutate(
|
||||||
if ((currentConversation as { conversationId?: string }).conversationId == conversationId) {
|
{ conversationId, source: 'button' },
|
||||||
newConversation();
|
{
|
||||||
}
|
onSuccess: () => {
|
||||||
|
if (
|
||||||
|
(currentConversation as { conversationId?: string }).conversationId == conversationId
|
||||||
|
) {
|
||||||
|
newConversation();
|
||||||
|
}
|
||||||
|
|
||||||
refreshConversations();
|
refreshConversations();
|
||||||
retainView();
|
retainView();
|
||||||
}
|
},
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
},
|
||||||
}, [deleteConvoMutation.isSuccess]);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog>
|
<Dialog>
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,7 @@ export default function NewConversationMenu() {
|
||||||
if (!newEndpoint) {
|
if (!newEndpoint) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
newConversation({}, { endpoint: newEndpoint });
|
newConversation(null, { endpoint: newEndpoint });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { useState, useEffect, useCallback } from 'react';
|
import { useState } from 'react';
|
||||||
import { Dialog } from '~/components/ui/';
|
import { Dialog } from '~/components/ui/';
|
||||||
import DialogTemplate from '~/components/ui/DialogTemplate';
|
import DialogTemplate from '~/components/ui/DialogTemplate';
|
||||||
import { ClearChatsButton } from './SettingsTabs/';
|
import { ClearChatsButton } from './SettingsTabs/';
|
||||||
|
|
@ -13,23 +13,23 @@ const ClearConvos = ({ open, onOpenChange }) => {
|
||||||
const localize = useLocalize();
|
const localize = useLocalize();
|
||||||
|
|
||||||
// Clear all conversations
|
// Clear all conversations
|
||||||
const clearConvos = useCallback(() => {
|
const clearConvos = () => {
|
||||||
if (confirmClear) {
|
if (confirmClear) {
|
||||||
console.log('Clearing conversations...');
|
console.log('Clearing conversations...');
|
||||||
clearConvosMutation.mutate({});
|
clearConvosMutation.mutate(
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
onSuccess: () => {
|
||||||
|
newConversation();
|
||||||
|
refreshConversations();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
setConfirmClear(false);
|
setConfirmClear(false);
|
||||||
} else {
|
} else {
|
||||||
setConfirmClear(true);
|
setConfirmClear(true);
|
||||||
}
|
}
|
||||||
}, [confirmClear, clearConvosMutation]);
|
};
|
||||||
|
|
||||||
// Refresh conversations after clearing
|
|
||||||
useEffect(() => {
|
|
||||||
if (clearConvosMutation.isSuccess) {
|
|
||||||
refreshConversations();
|
|
||||||
newConversation();
|
|
||||||
}
|
|
||||||
}, [clearConvosMutation.isSuccess, newConversation, refreshConversations]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import { useGetUserBalance, useGetStartupConfig } from 'librechat-data-provider'
|
||||||
import type { TConversation } from 'librechat-data-provider';
|
import type { TConversation } from 'librechat-data-provider';
|
||||||
import { Menu, Transition } from '@headlessui/react';
|
import { Menu, Transition } from '@headlessui/react';
|
||||||
import { ExportModel } from './ExportConversation';
|
import { ExportModel } from './ExportConversation';
|
||||||
import ClearConvos from './ClearConvos';
|
|
||||||
import Settings from './Settings';
|
import Settings from './Settings';
|
||||||
import NavLink from './NavLink';
|
import NavLink from './NavLink';
|
||||||
import Logout from './Logout';
|
import Logout from './Logout';
|
||||||
|
|
@ -23,7 +22,6 @@ export default function NavLinks() {
|
||||||
enabled: !!isAuthenticated && startupConfig?.checkBalance,
|
enabled: !!isAuthenticated && startupConfig?.checkBalance,
|
||||||
});
|
});
|
||||||
const [showExports, setShowExports] = useState(false);
|
const [showExports, setShowExports] = useState(false);
|
||||||
const [showClearConvos, setShowClearConvos] = useState(false);
|
|
||||||
const [showSettings, setShowSettings] = useState(false);
|
const [showSettings, setShowSettings] = useState(false);
|
||||||
const localize = useLocalize();
|
const localize = useLocalize();
|
||||||
|
|
||||||
|
|
@ -125,7 +123,6 @@ export default function NavLinks() {
|
||||||
)}
|
)}
|
||||||
</Menu>
|
</Menu>
|
||||||
{showExports && <ExportModel open={showExports} onOpenChange={setShowExports} />}
|
{showExports && <ExportModel open={showExports} onOpenChange={setShowExports} />}
|
||||||
{showClearConvos && <ClearConvos open={showClearConvos} onOpenChange={setShowClearConvos} />}
|
|
||||||
{showSettings && <Settings open={showSettings} onOpenChange={setShowSettings} />}
|
{showSettings && <Settings open={showSettings} onOpenChange={setShowSettings} />}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { useRecoilState } from 'recoil';
|
import { useRecoilState } from 'recoil';
|
||||||
import * as Tabs from '@radix-ui/react-tabs';
|
import * as Tabs from '@radix-ui/react-tabs';
|
||||||
import React, { useState, useContext, useEffect, useCallback, useRef } from 'react';
|
import React, { useState, useContext, useCallback, useRef } from 'react';
|
||||||
import { useClearConversationsMutation } from 'librechat-data-provider';
|
import { useClearConversationsMutation } from 'librechat-data-provider';
|
||||||
import {
|
import {
|
||||||
ThemeContext,
|
ThemeContext,
|
||||||
|
|
@ -116,22 +116,23 @@ function General() {
|
||||||
const contentRef = useRef(null);
|
const contentRef = useRef(null);
|
||||||
useOnClickOutside(contentRef, () => confirmClear && setConfirmClear(false), []);
|
useOnClickOutside(contentRef, () => confirmClear && setConfirmClear(false), []);
|
||||||
|
|
||||||
useEffect(() => {
|
const clearConvos = () => {
|
||||||
if (clearConvosMutation.isSuccess) {
|
|
||||||
newConversation();
|
|
||||||
refreshConversations();
|
|
||||||
}
|
|
||||||
}, [clearConvosMutation.isSuccess, newConversation, refreshConversations]);
|
|
||||||
|
|
||||||
const clearConvos = useCallback(() => {
|
|
||||||
if (confirmClear) {
|
if (confirmClear) {
|
||||||
console.log('Clearing conversations...');
|
console.log('Clearing conversations...');
|
||||||
clearConvosMutation.mutate({});
|
|
||||||
setConfirmClear(false);
|
setConfirmClear(false);
|
||||||
|
clearConvosMutation.mutate(
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
onSuccess: () => {
|
||||||
|
newConversation();
|
||||||
|
refreshConversations();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
setConfirmClear(true);
|
setConfirmClear(true);
|
||||||
}
|
}
|
||||||
}, [confirmClear, clearConvosMutation]);
|
};
|
||||||
|
|
||||||
const changeTheme = useCallback(
|
const changeTheme = useCallback(
|
||||||
(value: string) => {
|
(value: string) => {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { useSetRecoilState, useResetRecoilState, useRecoilCallback } from 'recoil';
|
import { useSetRecoilState, useResetRecoilState, useRecoilCallback } from 'recoil';
|
||||||
import { useGetEndpointsQuery } from 'librechat-data-provider';
|
import { useGetEndpointsQuery } from 'librechat-data-provider';
|
||||||
import type {
|
import type {
|
||||||
|
|
@ -12,6 +13,7 @@ import { buildDefaultConvo, getDefaultEndpoint } from '~/utils';
|
||||||
import store from '~/store';
|
import store from '~/store';
|
||||||
|
|
||||||
const useConversation = () => {
|
const useConversation = () => {
|
||||||
|
const navigate = useNavigate();
|
||||||
const setConversation = useSetRecoilState(store.conversation);
|
const setConversation = useSetRecoilState(store.conversation);
|
||||||
const setMessages = useSetRecoilState<TMessagesAtom>(store.messages);
|
const setMessages = useSetRecoilState<TMessagesAtom>(store.messages);
|
||||||
const setSubmission = useSetRecoilState<TSubmission | null>(store.submission);
|
const setSubmission = useSetRecoilState<TSubmission | null>(store.submission);
|
||||||
|
|
@ -48,6 +50,10 @@ const useConversation = () => {
|
||||||
setMessages(messages);
|
setMessages(messages);
|
||||||
setSubmission({} as TSubmission);
|
setSubmission({} as TSubmission);
|
||||||
resetLatestMessage();
|
resetLatestMessage();
|
||||||
|
|
||||||
|
if (conversation.conversationId === 'new') {
|
||||||
|
navigate('/chat/new');
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[endpointsConfig],
|
[endpointsConfig],
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue