From b01c744eb8ba7eb86a120553dd02ff02e85be5c5 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Mon, 6 Jan 2025 10:32:44 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B5=20fix:=20Prevent=20Unnecessary=20R?= =?UTF-8?q?e-renders=20when=20Loading=20Chats=20(#5189)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: typing * chore: typing * fix: enhance message scrolling logic to handle empty messages tree and ref checks * fix: optimize message selection logic with useCallback for better performance * chore: typing * refactor: optimize icon rendering * refactor: further optimize chat props * fix: remove unnecessary console log in useQueryParams cleanup * refactor: add queryClient to reset message data on new conversation initiation * refactor: update data-testid attributes for consistency and improve code readability * refactor: integrate queryClient to reset message data on new conversation initiation --- client/src/components/Chat/ChatView.tsx | 14 +++--- .../components/Chat/Menus/HeaderNewChat.tsx | 20 ++++++--- .../components/Chat/Messages/MessageIcon.tsx | 43 +++++++------------ .../components/Chat/Messages/MessageParts.tsx | 32 ++++++++++---- .../components/Chat/Messages/MessagesView.tsx | 2 +- .../Chat/Messages/SearchMessage.tsx | 17 +++++++- .../Chat/Messages/ui/MessageRender.tsx | 25 ++++++++++- client/src/components/Endpoints/ConvoIcon.tsx | 3 +- .../src/components/Endpoints/ConvoIconURL.tsx | 10 ++--- .../src/components/Endpoints/EndpointIcon.tsx | 6 +-- .../src/components/Messages/ContentRender.tsx | 30 ++++++++++--- client/src/components/Nav/MobileNav.tsx | 14 +++++- client/src/components/Nav/NewChat.tsx | 18 ++++++-- client/src/components/Share/Message.tsx | 2 +- client/src/components/Share/MessageIcon.tsx | 13 +++--- client/src/hooks/Chat/useChatHelpers.ts | 2 +- client/src/hooks/Input/useQueryParams.ts | 1 - .../src/hooks/Messages/useMessageScrolling.ts | 14 ++++-- client/src/store/families.ts | 2 +- e2e/specs/messages.spec.ts | 4 +- 20 files changed, 184 insertions(+), 88 deletions(-) diff --git a/client/src/components/Chat/ChatView.tsx b/client/src/components/Chat/ChatView.tsx index ecbc790911..0ee64ef62b 100644 --- a/client/src/components/Chat/ChatView.tsx +++ b/client/src/components/Chat/ChatView.tsx @@ -1,8 +1,9 @@ -import { memo } from 'react'; +import { memo, useCallback } from 'react'; import { useRecoilValue } from 'recoil'; import { useForm } from 'react-hook-form'; import { useParams } from 'react-router-dom'; import { useGetMessagesByConvoId } from 'librechat-data-provider/react-query'; +import type { TMessage } from 'librechat-data-provider'; import type { ChatFormValues } from '~/common'; import { ChatContext, AddedChatContext, useFileMapContext, ChatFormProvider } from '~/Providers'; import { useChatHelpers, useAddedResponse, useSSE } from '~/hooks'; @@ -24,10 +25,13 @@ function ChatView({ index = 0 }: { index?: number }) { const fileMap = useFileMapContext(); const { data: messagesTree = null, isLoading } = useGetMessagesByConvoId(conversationId ?? '', { - select: (data) => { - const dataTree = buildTree({ messages: data, fileMap }); - return dataTree?.length === 0 ? null : dataTree ?? null; - }, + select: useCallback( + (data: TMessage[]) => { + const dataTree = buildTree({ messages: data, fileMap }); + return dataTree?.length === 0 ? null : dataTree ?? null; + }, + [fileMap], + ), enabled: !!fileMap, }); diff --git a/client/src/components/Chat/Menus/HeaderNewChat.tsx b/client/src/components/Chat/Menus/HeaderNewChat.tsx index 5a9fc403a7..3e29190e7c 100644 --- a/client/src/components/Chat/Menus/HeaderNewChat.tsx +++ b/client/src/components/Chat/Menus/HeaderNewChat.tsx @@ -1,9 +1,13 @@ +import { useQueryClient } from '@tanstack/react-query'; +import { QueryKeys, Constants } from 'librechat-data-provider'; +import type { TMessage } from 'librechat-data-provider'; +import { useMediaQuery, useLocalize } from '~/hooks'; import { NewChatIcon } from '~/components/svg'; import { useChatContext } from '~/Providers'; -import { useMediaQuery, useLocalize } from '~/hooks'; export default function HeaderNewChat() { - const { newConversation } = useChatContext(); + const queryClient = useQueryClient(); + const { conversation, newConversation } = useChatContext(); const isSmallScreen = useMediaQuery('(max-width: 768px)'); const localize = useLocalize(); if (isSmallScreen) { @@ -12,10 +16,16 @@ export default function HeaderNewChat() { return (

- {title || localize('com_ui_new_chat')} + {title ?? localize('com_ui_new_chat')}