mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
* refactor: Update Frontend logger function to enhance logging conditions
- Modified the logger function to check for logger enablement and development environment more robustly.
- Adjusted the condition to ensure logging occurs only when the logger is enabled or when the environment variable for logger is not set in development mode.
* fix: Add key prop to MeasuredRow components in Conversations for improved rendering
- Updated MeasuredRow components to include a key prop for better performance and to prevent rendering issues during list updates.
- Ensured consistent handling of item types within the Conversations component.
* refactor: Enhance ScrollToBottom component with forwardRef for improved functionality
- Updated ScrollToBottom component to use forwardRef, allowing it to accept a ref for better integration with parent components.
- Modified MessagesView to utilize the new ref for the ScrollToBottom button, improving scrolling behavior and performance.
* refactor: Enhance EndpointItem and renderEndpoints for improved model render keys
- Updated EndpointItem to accept an endpointIndex prop for better indexing of endpoints.
- Modified renderEndpoints to pass the endpointIndex to EndpointItem, improving the rendering of endpoint models.
- Adjusted renderEndpointModels to utilize the endpointIndex for unique key generation, enhancing performance and preventing rendering issues.
* refactor: Update BaseClient to handle non-ephemeral agents in conversation logic
- Added a check for non-ephemeral agents in BaseClient, modifying the exceptions set to include 'model' when applicable.
- Enhanced conversation handling to improve flexibility based on agent type.
* refactor: Optimize FavoritesList component for agent handling and loading states
- Updated FavoritesList to improve agent ID management by introducing combinedAgentsMap for better handling of missing agents.
- Refactored loading state logic to ensure accurate representation of agent loading status.
- Enhanced the use of useQueries for fetching missing agent data, streamlining the overall data retrieval process.
- Improved memoization of agent IDs and loading conditions for better performance and reliability.
* Revert "refactor: Update BaseClient to handle non-ephemeral agents in conversation logic"
This reverts commit 6738acbe04.
371 lines
11 KiB
TypeScript
371 lines
11 KiB
TypeScript
import { useMemo, memo, type FC, useCallback, useEffect, useRef } from 'react';
|
|
import throttle from 'lodash/throttle';
|
|
import { ChevronDown } from 'lucide-react';
|
|
import { useRecoilValue } from 'recoil';
|
|
import { Spinner, useMediaQuery } from '@librechat/client';
|
|
import { List, AutoSizer, CellMeasurer, CellMeasurerCache } from 'react-virtualized';
|
|
import type { TConversation } from 'librechat-data-provider';
|
|
import { useLocalize, TranslationKeys, useFavorites, useShowMarketplace } from '~/hooks';
|
|
import FavoritesList from '~/components/Nav/Favorites/FavoritesList';
|
|
import { groupConversationsByDate, cn } from '~/utils';
|
|
import Convo from './Convo';
|
|
import store from '~/store';
|
|
|
|
export type CellPosition = {
|
|
columnIndex: number;
|
|
rowIndex: number;
|
|
};
|
|
|
|
export type MeasuredCellParent = {
|
|
invalidateCellSizeAfterRender?: ((cell: CellPosition) => void) | undefined;
|
|
recomputeGridSize?: ((cell: CellPosition) => void) | undefined;
|
|
};
|
|
|
|
interface ConversationsProps {
|
|
conversations: Array<TConversation | null>;
|
|
moveToTop: () => void;
|
|
toggleNav: () => void;
|
|
containerRef: React.RefObject<List>;
|
|
loadMoreConversations: () => void;
|
|
isLoading: boolean;
|
|
isSearchLoading: boolean;
|
|
isChatsExpanded: boolean;
|
|
setIsChatsExpanded: (expanded: boolean) => void;
|
|
}
|
|
|
|
interface MeasuredRowProps {
|
|
cache: CellMeasurerCache;
|
|
rowKey: string;
|
|
parent: MeasuredCellParent;
|
|
index: number;
|
|
style: React.CSSProperties;
|
|
children: React.ReactNode;
|
|
}
|
|
|
|
/** Reusable wrapper for virtualized row measurement */
|
|
const MeasuredRow: FC<MeasuredRowProps> = memo(
|
|
({ cache, rowKey, parent, index, style, children }) => (
|
|
<CellMeasurer cache={cache} columnIndex={0} key={rowKey} parent={parent} rowIndex={index}>
|
|
{({ registerChild }) => (
|
|
<div ref={registerChild as React.LegacyRef<HTMLDivElement>} style={style}>
|
|
{children}
|
|
</div>
|
|
)}
|
|
</CellMeasurer>
|
|
),
|
|
);
|
|
|
|
MeasuredRow.displayName = 'MeasuredRow';
|
|
|
|
const LoadingSpinner = memo(() => {
|
|
const localize = useLocalize();
|
|
|
|
return (
|
|
<div className="mx-auto mt-2 flex items-center justify-center gap-2">
|
|
<Spinner className="text-text-primary" />
|
|
<span className="animate-pulse text-text-primary">{localize('com_ui_loading')}</span>
|
|
</div>
|
|
);
|
|
});
|
|
|
|
LoadingSpinner.displayName = 'LoadingSpinner';
|
|
|
|
interface ChatsHeaderProps {
|
|
isExpanded: boolean;
|
|
onToggle: () => void;
|
|
}
|
|
|
|
/** Collapsible header for the Chats section */
|
|
const ChatsHeader: FC<ChatsHeaderProps> = memo(({ isExpanded, onToggle }) => {
|
|
const localize = useLocalize();
|
|
return (
|
|
<button
|
|
onClick={onToggle}
|
|
className="group flex w-full items-center justify-between px-1 py-2 text-xs font-bold text-text-secondary"
|
|
type="button"
|
|
>
|
|
<span className="select-none">{localize('com_ui_chats')}</span>
|
|
<ChevronDown
|
|
className={cn('h-3 w-3 transition-transform duration-200', isExpanded ? 'rotate-180' : '')}
|
|
/>
|
|
</button>
|
|
);
|
|
});
|
|
|
|
ChatsHeader.displayName = 'ChatsHeader';
|
|
|
|
const DateLabel: FC<{ groupName: string; isFirst?: boolean }> = memo(({ groupName, isFirst }) => {
|
|
const localize = useLocalize();
|
|
return (
|
|
<h2
|
|
className={cn('pl-1 pt-1 text-text-secondary', isFirst === true ? 'mt-0' : 'mt-2')}
|
|
style={{ fontSize: '0.7rem' }}
|
|
>
|
|
{localize(groupName as TranslationKeys) || groupName}
|
|
</h2>
|
|
);
|
|
});
|
|
|
|
DateLabel.displayName = 'DateLabel';
|
|
|
|
type FlattenedItem =
|
|
| { type: 'favorites' }
|
|
| { type: 'chats-header' }
|
|
| { type: 'header'; groupName: string }
|
|
| { type: 'convo'; convo: TConversation }
|
|
| { type: 'loading' };
|
|
|
|
const MemoizedConvo = memo(
|
|
({
|
|
conversation,
|
|
retainView,
|
|
toggleNav,
|
|
}: {
|
|
conversation: TConversation;
|
|
retainView: () => void;
|
|
toggleNav: () => void;
|
|
}) => {
|
|
return <Convo conversation={conversation} retainView={retainView} toggleNav={toggleNav} />;
|
|
},
|
|
(prevProps, nextProps) => {
|
|
return (
|
|
prevProps.conversation.conversationId === nextProps.conversation.conversationId &&
|
|
prevProps.conversation.title === nextProps.conversation.title &&
|
|
prevProps.conversation.endpoint === nextProps.conversation.endpoint
|
|
);
|
|
},
|
|
);
|
|
|
|
const Conversations: FC<ConversationsProps> = ({
|
|
conversations: rawConversations,
|
|
moveToTop,
|
|
toggleNav,
|
|
containerRef,
|
|
loadMoreConversations,
|
|
isLoading,
|
|
isSearchLoading,
|
|
isChatsExpanded,
|
|
setIsChatsExpanded,
|
|
}) => {
|
|
const localize = useLocalize();
|
|
const search = useRecoilValue(store.search);
|
|
const { favorites, isLoading: isFavoritesLoading } = useFavorites();
|
|
const isSmallScreen = useMediaQuery('(max-width: 768px)');
|
|
const convoHeight = isSmallScreen ? 44 : 34;
|
|
const showAgentMarketplace = useShowMarketplace();
|
|
|
|
// Determine if FavoritesList will render content
|
|
const shouldShowFavorites =
|
|
!search.query && (isFavoritesLoading || favorites.length > 0 || showAgentMarketplace);
|
|
|
|
const filteredConversations = useMemo(
|
|
() => rawConversations.filter(Boolean) as TConversation[],
|
|
[rawConversations],
|
|
);
|
|
|
|
const groupedConversations = useMemo(
|
|
() => groupConversationsByDate(filteredConversations),
|
|
[filteredConversations],
|
|
);
|
|
|
|
const flattenedItems = useMemo(() => {
|
|
const items: FlattenedItem[] = [];
|
|
// Only include favorites row if FavoritesList will render content
|
|
if (shouldShowFavorites) {
|
|
items.push({ type: 'favorites' });
|
|
}
|
|
items.push({ type: 'chats-header' });
|
|
|
|
if (isChatsExpanded) {
|
|
groupedConversations.forEach(([groupName, convos]) => {
|
|
items.push({ type: 'header', groupName });
|
|
items.push(...convos.map((convo) => ({ type: 'convo' as const, convo })));
|
|
});
|
|
|
|
if (isLoading) {
|
|
items.push({ type: 'loading' } as any);
|
|
}
|
|
}
|
|
return items;
|
|
}, [groupedConversations, isLoading, isChatsExpanded, shouldShowFavorites]);
|
|
|
|
// Store flattenedItems in a ref for keyMapper to access without recreating cache
|
|
const flattenedItemsRef = useRef(flattenedItems);
|
|
flattenedItemsRef.current = flattenedItems;
|
|
|
|
// Create a stable cache that doesn't depend on flattenedItems
|
|
const cache = useMemo(
|
|
() =>
|
|
new CellMeasurerCache({
|
|
fixedWidth: true,
|
|
defaultHeight: convoHeight,
|
|
keyMapper: (index) => {
|
|
const item = flattenedItemsRef.current[index];
|
|
if (!item) {
|
|
return `unknown-${index}`;
|
|
}
|
|
if (item.type === 'favorites') {
|
|
return 'favorites';
|
|
}
|
|
if (item.type === 'chats-header') {
|
|
return 'chats-header';
|
|
}
|
|
if (item.type === 'header') {
|
|
return `header-${item.groupName}`;
|
|
}
|
|
if (item.type === 'convo') {
|
|
return `convo-${item.convo.conversationId}`;
|
|
}
|
|
if (item.type === 'loading') {
|
|
return 'loading';
|
|
}
|
|
return `unknown-${index}`;
|
|
},
|
|
}),
|
|
[convoHeight],
|
|
);
|
|
|
|
// Debounced function to clear cache and recompute heights
|
|
const clearFavoritesCache = useCallback(() => {
|
|
if (cache) {
|
|
cache.clear(0, 0);
|
|
if (containerRef.current && 'recomputeRowHeights' in containerRef.current) {
|
|
containerRef.current.recomputeRowHeights(0);
|
|
}
|
|
}
|
|
}, [cache, containerRef]);
|
|
|
|
// Clear cache when favorites change
|
|
useEffect(() => {
|
|
const frameId = requestAnimationFrame(() => {
|
|
clearFavoritesCache();
|
|
});
|
|
return () => cancelAnimationFrame(frameId);
|
|
}, [favorites.length, isFavoritesLoading, clearFavoritesCache]);
|
|
|
|
const rowRenderer = useCallback(
|
|
({ index, key, parent, style }) => {
|
|
const item = flattenedItems[index];
|
|
const rowProps = { cache, rowKey: key, parent, index, style };
|
|
|
|
if (item.type === 'loading') {
|
|
return (
|
|
<MeasuredRow key={key} {...rowProps}>
|
|
<LoadingSpinner />
|
|
</MeasuredRow>
|
|
);
|
|
}
|
|
|
|
if (item.type === 'favorites') {
|
|
return (
|
|
<MeasuredRow key={key} {...rowProps}>
|
|
<FavoritesList
|
|
isSmallScreen={isSmallScreen}
|
|
toggleNav={toggleNav}
|
|
onHeightChange={clearFavoritesCache}
|
|
/>
|
|
</MeasuredRow>
|
|
);
|
|
}
|
|
|
|
if (item.type === 'chats-header') {
|
|
return (
|
|
<MeasuredRow key={key} {...rowProps}>
|
|
<ChatsHeader
|
|
isExpanded={isChatsExpanded}
|
|
onToggle={() => setIsChatsExpanded(!isChatsExpanded)}
|
|
/>
|
|
</MeasuredRow>
|
|
);
|
|
}
|
|
|
|
if (item.type === 'header') {
|
|
// First date header index depends on whether favorites row is included
|
|
// With favorites: [favorites, chats-header, first-header] → index 2
|
|
// Without favorites: [chats-header, first-header] → index 1
|
|
const firstHeaderIndex = shouldShowFavorites ? 2 : 1;
|
|
return (
|
|
<MeasuredRow key={key} {...rowProps}>
|
|
<DateLabel groupName={item.groupName} isFirst={index === firstHeaderIndex} />
|
|
</MeasuredRow>
|
|
);
|
|
}
|
|
|
|
if (item.type === 'convo') {
|
|
return (
|
|
<MeasuredRow key={key} {...rowProps}>
|
|
<MemoizedConvo conversation={item.convo} retainView={moveToTop} toggleNav={toggleNav} />
|
|
</MeasuredRow>
|
|
);
|
|
}
|
|
|
|
return null;
|
|
},
|
|
[
|
|
cache,
|
|
flattenedItems,
|
|
moveToTop,
|
|
toggleNav,
|
|
clearFavoritesCache,
|
|
isSmallScreen,
|
|
isChatsExpanded,
|
|
setIsChatsExpanded,
|
|
shouldShowFavorites,
|
|
],
|
|
);
|
|
|
|
const getRowHeight = useCallback(
|
|
({ index }: { index: number }) => cache.getHeight(index, 0),
|
|
[cache],
|
|
);
|
|
|
|
const throttledLoadMore = useMemo(
|
|
() => throttle(loadMoreConversations, 300),
|
|
[loadMoreConversations],
|
|
);
|
|
|
|
const handleRowsRendered = useCallback(
|
|
({ stopIndex }: { stopIndex: number }) => {
|
|
if (stopIndex >= flattenedItems.length - 8) {
|
|
throttledLoadMore();
|
|
}
|
|
},
|
|
[flattenedItems.length, throttledLoadMore],
|
|
);
|
|
|
|
return (
|
|
<div className="relative flex h-full min-h-0 flex-col pb-2 text-sm text-text-primary">
|
|
{isSearchLoading ? (
|
|
<div className="flex flex-1 items-center justify-center">
|
|
<Spinner className="text-text-primary" />
|
|
<span className="ml-2 text-text-primary">{localize('com_ui_loading')}</span>
|
|
</div>
|
|
) : (
|
|
<div className="flex-1">
|
|
<AutoSizer>
|
|
{({ width, height }) => (
|
|
<List
|
|
ref={containerRef}
|
|
width={width}
|
|
height={height}
|
|
deferredMeasurementCache={cache}
|
|
rowCount={flattenedItems.length}
|
|
rowHeight={getRowHeight}
|
|
rowRenderer={rowRenderer}
|
|
overscanRowCount={10}
|
|
aria-readonly={false}
|
|
className="outline-none"
|
|
aria-label="Conversations"
|
|
onRowsRendered={handleRowsRendered}
|
|
tabIndex={-1}
|
|
style={{ outline: 'none', scrollbarGutter: 'stable' }}
|
|
/>
|
|
)}
|
|
</AutoSizer>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default memo(Conversations);
|