mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
feat: Add RQ to Conversation component, create temp Chat component with RQ for compare and debugging
This commit is contained in:
parent
39f53e6ddf
commit
c6d3bcd457
3 changed files with 114 additions and 12 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import React, { useState, useRef } from 'react';
|
import { useState, useRef, useEffect} from 'react';
|
||||||
import { useRecoilState, useSetRecoilState } from 'recoil';
|
import { useRecoilState, useSetRecoilState } from 'recoil';
|
||||||
|
import { useUpdateConversationMutation } from '~/data-provider';
|
||||||
import RenameButton from './RenameButton';
|
import RenameButton from './RenameButton';
|
||||||
import DeleteButton from './DeleteButton';
|
import DeleteButton from './DeleteButton';
|
||||||
import ConvoIcon from '../svg/ConvoIcon';
|
import ConvoIcon from '../svg/ConvoIcon';
|
||||||
|
|
@ -15,6 +15,8 @@ export default function Conversation({ conversation, retainView }) {
|
||||||
const { refreshConversations } = store.useConversations();
|
const { refreshConversations } = store.useConversations();
|
||||||
const { switchToConversation } = store.useConversation();
|
const { switchToConversation } = store.useConversation();
|
||||||
|
|
||||||
|
const updateConvoMutation = useUpdateConversationMutation(currentConversation?.conversationId);
|
||||||
|
|
||||||
const [renaming, setRenaming] = useState(false);
|
const [renaming, setRenaming] = useState(false);
|
||||||
const inputRef = useRef(null);
|
const inputRef = useRef(null);
|
||||||
|
|
||||||
|
|
@ -59,15 +61,30 @@ export default function Conversation({ conversation, retainView }) {
|
||||||
if (titleInput === title) {
|
if (titleInput === title) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
rename.trigger({ conversationId, title: titleInput }).then(() => {
|
updateConvoMutation.mutate({ arg: { conversationId, title: titleInput }});
|
||||||
|
|
||||||
|
// rename.trigger({ conversationId, title: titleInput }).then(() => {
|
||||||
|
// refreshConversations();
|
||||||
|
// if (conversationId == currentConversation?.conversationId)
|
||||||
|
// setCurrentConversation(prevState => ({
|
||||||
|
// ...prevState,
|
||||||
|
// title: titleInput
|
||||||
|
// }));
|
||||||
|
// });
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (updateConvoMutation.isSuccess) {
|
||||||
|
// debugger;
|
||||||
refreshConversations();
|
refreshConversations();
|
||||||
if (conversationId == currentConversation?.conversationId)
|
if (conversationId == currentConversation?.conversationId)
|
||||||
|
|
||||||
setCurrentConversation(prevState => ({
|
setCurrentConversation(prevState => ({
|
||||||
...prevState,
|
...prevState,
|
||||||
title: titleInput
|
title: titleInput
|
||||||
}));
|
}));
|
||||||
});
|
}
|
||||||
};
|
}, [updateConvoMutation.isSuccess]);
|
||||||
|
|
||||||
const handleKeyDown = e => {
|
const handleKeyDown = e => {
|
||||||
if (e.key === 'Enter') {
|
if (e.key === 'Enter') {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ import {
|
||||||
} from "@tanstack/react-query";
|
} from "@tanstack/react-query";
|
||||||
import * as t from "./types";
|
import * as t from "./types";
|
||||||
import * as dataService from "./data-service";
|
import * as dataService from "./data-service";
|
||||||
|
import { useRecoilState, useResetRecoilState, useSetRecoilState } from 'recoil';
|
||||||
|
import store from '~/store';
|
||||||
|
|
||||||
export enum QueryKeys {
|
export enum QueryKeys {
|
||||||
messages = "messsages",
|
messages = "messsages",
|
||||||
|
|
@ -66,13 +68,17 @@ export const useUpdateConversationMutation = (
|
||||||
t.TUpdateConversationRequest,
|
t.TUpdateConversationRequest,
|
||||||
unknown
|
unknown
|
||||||
> => {
|
> => {
|
||||||
|
const [conversation, setConversation] = useRecoilState(store.conversation);
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
return useMutation(
|
return useMutation(
|
||||||
(payload: t.TUpdateConversationRequest) =>
|
(payload: t.TUpdateConversationRequest) =>
|
||||||
dataService.updateConversation(payload),
|
dataService.updateConversation(payload),
|
||||||
{
|
{
|
||||||
onSuccess: () => {
|
onSuccess: (res) => {
|
||||||
|
console.log('res', res);
|
||||||
|
setConversation(res);
|
||||||
queryClient.invalidateQueries([QueryKeys.conversation, id]);
|
queryClient.invalidateQueries([QueryKeys.conversation, id]);
|
||||||
|
queryClient.invalidateQueries([QueryKeys.allConversations, id]);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
||||||
79
client/src/routes/ChatRQTemp.jsx
Normal file
79
client/src/routes/ChatRQTemp.jsx
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
import React, { useEffect } from 'react';
|
||||||
|
import { useNavigate, useParams } from 'react-router-dom';
|
||||||
|
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
|
||||||
|
|
||||||
|
import Landing from '../components/ui/Landing';
|
||||||
|
import Messages from '../components/Messages';
|
||||||
|
import TextChat from '../components/Input';
|
||||||
|
|
||||||
|
import store from '~/store';
|
||||||
|
import { useGetMessagesByConvoId, useGetConversationByIdQuery } from '~/data-provider';
|
||||||
|
|
||||||
|
export default function Chat() {
|
||||||
|
const searchQuery = useRecoilValue(store.searchQuery);
|
||||||
|
const [conversation, setConversation] = useRecoilState(store.conversation);
|
||||||
|
const setMessages = useSetRecoilState(store.messages);
|
||||||
|
const messagesTree = useRecoilValue(store.messagesTree);
|
||||||
|
const { newConversation } = store.useConversation();
|
||||||
|
const { conversationId } = useParams();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
//disabled by default, we only enable it when messagesTree is null
|
||||||
|
const messagesQuery = useGetMessagesByConvoId(conversation?.conversationId, { enabled: false });
|
||||||
|
|
||||||
|
const conversationQuery = useGetConversationByIdQuery(
|
||||||
|
conversation?.conversationId, {
|
||||||
|
enabled: !!conversation?.conversationId &&
|
||||||
|
conversation?.conversationId !== 'search' &&
|
||||||
|
conversation?.conversationId !== 'new'
|
||||||
|
});
|
||||||
|
|
||||||
|
// when conversation changed or conversationId (in url) changed
|
||||||
|
useEffect(() => {
|
||||||
|
if (conversation === null) {
|
||||||
|
// no current conversation, we need to do something
|
||||||
|
if (conversationId === 'new') {
|
||||||
|
// create new
|
||||||
|
newConversation();
|
||||||
|
} else if (conversationQuery.data) {
|
||||||
|
// fetch it from server
|
||||||
|
setConversation(conversationQuery.data);
|
||||||
|
setMessages(null);
|
||||||
|
} else if (conversationQuery.isError) {
|
||||||
|
console.error('failed to fetch the conversation');
|
||||||
|
console.error(conversationQuery.error);
|
||||||
|
newConversation();
|
||||||
|
} else {
|
||||||
|
navigate(`/chat/new`);
|
||||||
|
}
|
||||||
|
} else if (conversation?.conversationId === 'search') {
|
||||||
|
// jump to search page
|
||||||
|
navigate(`/search/${searchQuery}`);
|
||||||
|
} else if (conversation?.conversationId !== conversationId) {
|
||||||
|
// conversationId (in url) should always follow conversation?.conversationId, unless conversation is null
|
||||||
|
navigate(`/chat/${conversation?.conversationId}`);
|
||||||
|
}
|
||||||
|
}, [conversation, conversationId, newConversation, navigate, searchQuery, setMessages, conversationQuery]);
|
||||||
|
|
||||||
|
// when messagesTree is null (<=> messages is null)
|
||||||
|
// we need to fetch message list from server
|
||||||
|
useEffect(() => {
|
||||||
|
if (messagesTree === null) {
|
||||||
|
messagesQuery.refetch(conversation?.conversationId);
|
||||||
|
}
|
||||||
|
}, [conversation?.conversationId, messagesQuery, messagesTree]);
|
||||||
|
|
||||||
|
// if not a conversation
|
||||||
|
if (conversation?.conversationId === 'search') return null;
|
||||||
|
// if conversationId not match
|
||||||
|
if (conversation?.conversationId !== conversationId) return null;
|
||||||
|
// if conversationId is null
|
||||||
|
if (!conversationId) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{conversationId === 'new' && !messagesTree?.length ? <Landing /> : <Messages />}
|
||||||
|
<TextChat />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue