From 0f95604a67577ab92dc6b52516c970e2647ec669 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Thu, 9 Jan 2025 15:40:10 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20refactor:=20Optimize=20Ren?= =?UTF-8?q?dering=20Performance=20for=20Icons,=20Conversations=20(#5234)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: HoverButtons and Fork components to use explicit props * refactor: improve typing for Fork Component * fix: memoize SpecIcon to avoid unnecessary re-renders * feat: introduce URLIcon component and update SpecIcon for improved icon handling * WIP: optimizing icons * refactor: simplify modelLabel assignment in Message components * refactor: memoize ConvoOptions component to optimize rendering performance --- client/src/common/types.ts | 6 +++ .../Chat/Menus/Endpoints/UnknownIcon.tsx | 5 +- .../components/Chat/Menus/Models/SpecIcon.tsx | 27 ++++------- .../components/Chat/Messages/HoverButtons.tsx | 10 ++-- .../components/Chat/Messages/MessageIcon.tsx | 35 ++++++++------ .../components/Chat/Messages/MessageParts.tsx | 46 +++++++++--------- .../Chat/Messages/SearchMessage.tsx | 18 ++++--- .../Chat/Messages/ui/MessageRender.tsx | 22 ++++----- .../ConvoOptions/ConvoOptions.tsx | 6 ++- client/src/components/Conversations/Fork.tsx | 47 ++++++++++--------- .../src/components/Endpoints/ConvoIconURL.tsx | 40 +++++++--------- .../Endpoints/MessageEndpointIcon.tsx | 26 ++++++---- client/src/components/Endpoints/URLIcon.tsx | 21 +++++++++ .../src/components/Messages/ContentRender.tsx | 22 ++++----- client/src/hooks/Chat/useChatFunctions.ts | 2 + .../src/hooks/Messages/useMessageProcess.tsx | 4 +- client/src/hooks/useGenerationsByLatest.ts | 30 ++++++------ client/src/utils/endpoints.ts | 8 ++-- packages/data-provider/src/schemas.ts | 2 +- 19 files changed, 206 insertions(+), 171 deletions(-) create mode 100644 client/src/components/Endpoints/URLIcon.tsx diff --git a/client/src/common/types.ts b/client/src/common/types.ts index 7042e0794..f952cd283 100644 --- a/client/src/common/types.ts +++ b/client/src/common/types.ts @@ -307,6 +307,12 @@ export type TMessageProps = { setSiblingIdx?: ((value: number) => void | React.Dispatch>) | null; }; +export type TMessageIcon = { endpoint?: string | null; isCreatedByUser?: boolean } & Pick< + t.TConversation, + 'modelLabel' +> & + Pick; + export type TInitialProps = { text: string; edit: boolean; diff --git a/client/src/components/Chat/Menus/Endpoints/UnknownIcon.tsx b/client/src/components/Chat/Menus/Endpoints/UnknownIcon.tsx index a0a9ef220..04bac0bde 100644 --- a/client/src/components/Chat/Menus/Endpoints/UnknownIcon.tsx +++ b/client/src/components/Chat/Menus/Endpoints/UnknownIcon.tsx @@ -1,3 +1,4 @@ +import { memo } from 'react'; import { EModelEndpoint, KnownEndpoints } from 'librechat-data-provider'; import { CustomMinimalIcon } from '~/components/svg'; import { IconContext } from '~/common'; @@ -53,7 +54,7 @@ const getKnownClass = ({ return cn(match, defaultClass); }; -export default function UnknownIcon({ +function UnknownIcon({ className = '', endpoint: _endpoint, iconURL = '', @@ -93,3 +94,5 @@ export default function UnknownIcon({ /> ); } + +export default memo(UnknownIcon); diff --git a/client/src/components/Chat/Menus/Models/SpecIcon.tsx b/client/src/components/Chat/Menus/Models/SpecIcon.tsx index 5235dd486..9d1cb09e8 100644 --- a/client/src/components/Chat/Menus/Models/SpecIcon.tsx +++ b/client/src/components/Chat/Menus/Models/SpecIcon.tsx @@ -1,8 +1,9 @@ -import React from 'react'; +import React, { memo } from 'react'; import type { TModelSpec, TEndpointsConfig } from 'librechat-data-provider'; import type { IconMapProps } from '~/common'; import { getModelSpecIconURL, getIconKey, getEndpointField } from '~/utils'; import { icons } from '~/components/Chat/Menus/Endpoints/Icons'; +import { URLIcon } from '~/components/Endpoints/URLIcon'; interface SpecIconProps { currentSpec: TModelSpec; @@ -16,24 +17,12 @@ const SpecIcon: React.FC = ({ currentSpec, endpointsConfig }) => const iconKey = getIconKey({ endpoint, endpointsConfig, endpointIconURL }); let Icon: (props: IconMapProps) => React.JSX.Element; - if (!iconURL?.includes('http')) { + if (!iconURL.includes('http')) { Icon = icons[iconKey] ?? icons.unknown; + } else if (iconURL) { + return ; } else { - Icon = iconURL - ? () => ( -
- {currentSpec.name} -
- ) - : icons[endpoint ?? ''] ?? icons.unknown; + Icon = icons[endpoint ?? ''] ?? icons.unknown; } return ( @@ -42,9 +31,9 @@ const SpecIcon: React.FC = ({ currentSpec, endpointsConfig }) => endpoint={endpoint} context="menu-item" iconURL={endpointIconURL} - className="icon-lg mr-1 shrink-0 dark:text-white" + className="icon-lg mr-1 shrink-0 text-text-primary" /> ); }; -export default SpecIcon; +export default memo(SpecIcon); diff --git a/client/src/components/Chat/Messages/HoverButtons.tsx b/client/src/components/Chat/Messages/HoverButtons.tsx index fa7bd59ce..0b27fc376 100644 --- a/client/src/components/Chat/Messages/HoverButtons.tsx +++ b/client/src/components/Chat/Messages/HoverButtons.tsx @@ -50,9 +50,13 @@ export default function HoverButtons({ } = useGenerationsByLatest({ isEditing, isSubmitting, - message, + error: message.error, endpoint: endpoint ?? '', - latestMessage, + messageId: message.messageId, + searchResult: message.searchResult, + finish_reason: message.finish_reason, + isCreatedByUser: message.isCreatedByUser, + latestMessageId: latestMessage?.messageId, }); if (!conversation) { return null; @@ -146,7 +150,7 @@ export default function HoverButtons({ messageId={message.messageId} conversationId={conversation.conversationId} forkingSupported={forkingSupported} - latestMessage={latestMessage} + latestMessageId={latestMessage?.messageId} /> {continueSupported === true ? (