2023-07-03 16:00:04 -04:00
|
|
|
import { useState, useEffect } from 'react';
|
2023-03-28 22:39:27 +08:00
|
|
|
import { useNavigate, useParams } from 'react-router-dom';
|
|
|
|
|
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-03-28 22:39:27 +08:00
|
|
|
import Landing from '../components/ui/Landing';
|
|
|
|
|
import Messages from '../components/Messages';
|
|
|
|
|
import TextChat from '../components/Input';
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-03-28 22:39:27 +08:00
|
|
|
import store from '~/store';
|
2023-06-15 09:36:34 -07:00
|
|
|
import {
|
|
|
|
|
useGetMessagesByConvoId,
|
|
|
|
|
useGetConversationByIdMutation,
|
|
|
|
|
useGetStartupConfig
|
2023-07-04 12:47:41 -07:00
|
|
|
} from '@librechat/data-provider';
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
export default function Chat() {
|
2023-07-03 16:00:04 -04:00
|
|
|
const [shouldNavigate, setShouldNavigate] = useState(true);
|
2023-03-29 00:08:02 +08:00
|
|
|
const searchQuery = useRecoilValue(store.searchQuery);
|
2023-03-28 20:36:21 +08:00
|
|
|
const [conversation, setConversation] = useRecoilState(store.conversation);
|
|
|
|
|
const setMessages = useSetRecoilState(store.messages);
|
|
|
|
|
const messagesTree = useRecoilValue(store.messagesTree);
|
2023-07-03 16:00:04 -04:00
|
|
|
const isSubmitting = useRecoilValue(store.isSubmitting);
|
2023-03-28 20:36:21 +08:00
|
|
|
const { newConversation } = store.useConversation();
|
|
|
|
|
const { conversationId } = useParams();
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
2023-04-03 16:46:35 -07:00
|
|
|
//disabled by default, we only enable it when messagesTree is null
|
|
|
|
|
const messagesQuery = useGetMessagesByConvoId(conversationId, { enabled: false });
|
2023-04-10 14:55:39 -07:00
|
|
|
const getConversationMutation = useGetConversationByIdMutation(conversationId);
|
2023-06-15 09:36:34 -07:00
|
|
|
const { data: config } = useGetStartupConfig();
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-07-03 16:00:04 -04:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isSubmitting && !shouldNavigate) {
|
|
|
|
|
setShouldNavigate(true);
|
|
|
|
|
}
|
|
|
|
|
}, [shouldNavigate, isSubmitting]);
|
|
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
// when conversation changed or conversationId (in url) changed
|
|
|
|
|
useEffect(() => {
|
2023-07-03 16:00:04 -04:00
|
|
|
// No current conversation and conversationId is 'new'
|
|
|
|
|
if (conversation === null && conversationId === 'new') {
|
|
|
|
|
newConversation();
|
|
|
|
|
setShouldNavigate(true);
|
|
|
|
|
}
|
|
|
|
|
// No current conversation and conversationId exists
|
|
|
|
|
else if (conversation === null && conversationId) {
|
|
|
|
|
getConversationMutation.mutate(conversationId, {
|
|
|
|
|
onSuccess: (data) => {
|
|
|
|
|
console.log('Conversation fetched successfully');
|
|
|
|
|
setConversation(data);
|
|
|
|
|
setShouldNavigate(true);
|
|
|
|
|
},
|
|
|
|
|
onError: (error) => {
|
|
|
|
|
console.error('Failed to fetch the conversation');
|
|
|
|
|
console.error(error);
|
|
|
|
|
navigate(`/chat/new`);
|
|
|
|
|
newConversation();
|
|
|
|
|
setShouldNavigate(true);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
setMessages(null);
|
|
|
|
|
}
|
|
|
|
|
// No current conversation and no conversationId
|
|
|
|
|
else if (conversation === null) {
|
|
|
|
|
navigate(`/chat/new`);
|
|
|
|
|
setShouldNavigate(true);
|
|
|
|
|
}
|
|
|
|
|
// Current conversationId is 'search'
|
|
|
|
|
else if (conversation?.conversationId === 'search') {
|
|
|
|
|
navigate(`/search/${searchQuery}`);
|
|
|
|
|
setShouldNavigate(true);
|
|
|
|
|
}
|
|
|
|
|
// Conversation change and isSubmitting
|
|
|
|
|
else if (conversation?.conversationId !== conversationId && isSubmitting) {
|
|
|
|
|
setShouldNavigate(false);
|
|
|
|
|
}
|
|
|
|
|
// conversationId (in url) should always follow conversation?.conversationId, unless conversation is null
|
|
|
|
|
else if (conversation?.conversationId !== conversationId) {
|
|
|
|
|
if (shouldNavigate) {
|
|
|
|
|
navigate(`/chat/${conversation?.conversationId}`);
|
2023-03-29 00:08:02 +08:00
|
|
|
} else {
|
2023-07-03 16:00:04 -04:00
|
|
|
setShouldNavigate(true);
|
2023-03-28 20:36:21 +08:00
|
|
|
}
|
2023-03-29 00:08:02 +08:00
|
|
|
}
|
2023-06-15 09:36:34 -07:00
|
|
|
document.title = conversation?.title || config?.appTitle || 'Chat';
|
|
|
|
|
}, [conversation, conversationId, config]);
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-04-03 16:46:35 -07:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (messagesTree === null && conversation?.conversationId) {
|
|
|
|
|
messagesQuery.refetch(conversation?.conversationId);
|
|
|
|
|
}
|
|
|
|
|
}, [conversation?.conversationId, messagesQuery, messagesTree]);
|
2023-05-18 11:09:31 -07:00
|
|
|
|
2023-04-03 16:46:35 -07:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (messagesQuery.data) {
|
|
|
|
|
setMessages(messagesQuery.data);
|
2023-05-18 11:09:31 -07:00
|
|
|
} else if (messagesQuery.isError) {
|
2023-04-03 16:46:35 -07:00
|
|
|
console.error('failed to fetch the messages');
|
|
|
|
|
console.error(messagesQuery.error);
|
|
|
|
|
setMessages(null);
|
2023-03-28 20:36:21 +08:00
|
|
|
}
|
2023-04-03 16:46:35 -07:00
|
|
|
}, [messagesQuery.data, messagesQuery.isError, setMessages]);
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-03-29 00:08:02 +08:00
|
|
|
// if not a conversation
|
|
|
|
|
if (conversation?.conversationId === 'search') return null;
|
|
|
|
|
// if conversationId not match
|
2023-07-03 16:00:04 -04:00
|
|
|
if (conversation?.conversationId !== conversationId && !conversation) return null;
|
2023-03-29 00:08:02 +08:00
|
|
|
// if conversationId is null
|
|
|
|
|
if (!conversationId) return null;
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-07-03 16:00:04 -04:00
|
|
|
if (conversationId && !messagesTree) {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Messages />
|
|
|
|
|
<TextChat />
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
return (
|
|
|
|
|
<>
|
2023-03-29 06:44:18 +08:00
|
|
|
{conversationId === 'new' && !messagesTree?.length ? <Landing /> : <Messages />}
|
2023-03-28 20:36:21 +08:00
|
|
|
<TextChat />
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|