mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-19 06:23:32 +00:00
🪜 refactor: Move Row Mounting and the Scrollbar Gutter Into @librechat/chat (#17586)
* refactor: Move Row Mounting and the Scrollbar Gutter Into @librechat/chat * test: Cover Progressive Mounting of a Long Conversation
This commit is contained in:
parent
225810a16d
commit
096fdbfc85
17 changed files with 104 additions and 31 deletions
|
|
@ -8,12 +8,12 @@ import { ChatPane, getChatPaneView, QueuedTurnPortalProvider } from '@librechat/
|
|||
import {
|
||||
submissionByIndex,
|
||||
ChatSessionProvider,
|
||||
useScrollbarGutterSeed,
|
||||
ComposerRestoreProvider,
|
||||
} from '@librechat/chat/react';
|
||||
import type { TChatProject, TMessage } from 'librechat-data-provider';
|
||||
import type { ChatFormValues } from '~/common';
|
||||
import { AddedChatContext, ChatFormProvider, useChatContext, useFileMapContext } from '~/Providers';
|
||||
import { useScrollbarGutterSeed, useAddedResponse, useLocalize } from '~/hooks';
|
||||
import Footer, { useLandingFooter, useConfiguredFooter } from './Footer';
|
||||
import { useChatSettings } from '~/Providers/ChatSettingsContext';
|
||||
import ApprovalProvider from './Messages/Content/ApprovalContext';
|
||||
|
|
@ -21,6 +21,7 @@ import ConversationStarters from './Input/ConversationStarters';
|
|||
import { pendingApprovalActionFamily } from './approval/state';
|
||||
import ProjectBadge from '~/components/Projects/ProjectBadge';
|
||||
import { OwnerTextProvider } from './Messages/PrivateText';
|
||||
import { useAddedResponse, useLocalize } from '~/hooks';
|
||||
import { AskAnswerHostProvider } from './ask/state';
|
||||
import MessagesView from './Messages/MessagesView';
|
||||
import Presentation from './Presentation';
|
||||
|
|
|
|||
|
|
@ -1,18 +1,16 @@
|
|||
import { useState, useCallback, useEffect } from 'react';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { Constants } from 'librechat-data-provider';
|
||||
import { useIsSubmitting } from '@librechat/chat/react';
|
||||
import type { TMessage } from 'librechat-data-provider';
|
||||
import {
|
||||
useLocalize,
|
||||
useScreenshot,
|
||||
useIsSubmitting,
|
||||
RowMountProvider,
|
||||
useScrollbarGutter,
|
||||
useMessageScrolling,
|
||||
useConversationSeen,
|
||||
} from '~/hooks';
|
||||
useProgressiveRowMount,
|
||||
} from '@librechat/chat/react';
|
||||
import type { TMessage } from 'librechat-data-provider';
|
||||
import { MessagePartsHostProvider, appMessagePartsHost } from '~/Providers/MessagePartsHostContext';
|
||||
import { useLocalize, useScreenshot, useMessageScrolling, useConversationSeen } from '~/hooks';
|
||||
import { MessagesViewProvider, useChatContext, useFileMapContext } from '~/Providers';
|
||||
import { RowMountProvider, useProgressiveRowMount } from '~/hooks/Messages';
|
||||
import { useChatSurface } from '~/components/Chat/Subagents/surface';
|
||||
import useThreadRows from '~/hooks/Messages/useThreadRows';
|
||||
import PendingSteers from './Content/Parts/PendingSteers';
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { memo, useEffect, useRef, useCallback } from 'react';
|
||||
import { useAtom } from 'jotai';
|
||||
import { useRowMountWindow } from '@librechat/chat/react';
|
||||
import { isAssistantsEndpoint } from 'librechat-data-provider';
|
||||
import type { TMessage } from 'librechat-data-provider';
|
||||
import type { ReactElement } from 'react';
|
||||
|
|
@ -7,7 +8,6 @@ import type { TMessageProps } from '~/common';
|
|||
import EventSubagentActivityGroup from '~/components/Chat/Subagents/EventSubagentActivityGroup';
|
||||
import MessageContent from '~/components/Messages/MessageContent';
|
||||
import { siblingIdxFamily, siblingKey } from './Thread/state';
|
||||
import { useRowMountWindow } from '~/hooks/Messages';
|
||||
import { hasParallelLanes } from '~/utils/lanes';
|
||||
import MessageParts from './MessageParts';
|
||||
import Message from './Message';
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { memo } from 'react';
|
||||
import { useRowMountWindow } from '@librechat/chat/react';
|
||||
import type { ThreadRow } from '~/utils/thread';
|
||||
import type { TMessageProps } from '~/common';
|
||||
import { useRowMountWindow } from '~/hooks/Messages';
|
||||
import Row from './Row';
|
||||
|
||||
type ListProps = {
|
||||
|
|
|
|||
|
|
@ -32,7 +32,6 @@ jest.mock('~/hooks', () => ({
|
|||
debouncedHandleScroll: jest.fn(),
|
||||
handleNearBottomChange: jest.fn(),
|
||||
}),
|
||||
useScrollbarGutter: jest.fn(),
|
||||
useConversationSeen: () => jest.fn(),
|
||||
useLocalize: () => (key: string) => key,
|
||||
}));
|
||||
|
|
@ -43,7 +42,9 @@ jest.mock('~/Providers', () => ({
|
|||
useFileMapContext: () => ({}),
|
||||
}));
|
||||
|
||||
jest.mock('~/hooks/Messages', () => ({
|
||||
jest.mock('@librechat/chat/react', () => ({
|
||||
...jest.requireActual('@librechat/chat/react'),
|
||||
useScrollbarGutter: jest.fn(),
|
||||
RowMountProvider: ({ children }: { children: React.ReactNode }) => <>{children}</>,
|
||||
useProgressiveRowMount: () => null,
|
||||
}));
|
||||
|
|
|
|||
|
|
@ -386,7 +386,7 @@ describe('MultiMessage sibling selection', () => {
|
|||
|
||||
describe('MultiMessage row mount window', () => {
|
||||
const { RowMountProvider } =
|
||||
jest.requireActual<typeof import('~/hooks/Messages')>('~/hooks/Messages');
|
||||
jest.requireActual<typeof import('@librechat/chat/react')>('@librechat/chat/react');
|
||||
|
||||
const chain = (): TMessage => {
|
||||
const leaf = { ...msg('m2'), parentMessageId: 'm1', depth: 2 } as TMessage;
|
||||
|
|
|
|||
|
|
@ -48,7 +48,6 @@ jest.mock('@librechat/chat/react', () => {
|
|||
jest.mock('~/hooks', () => ({
|
||||
useLocalize: () => (key: string) => (key === 'com_ui_new_chat' ? 'New chat' : key),
|
||||
useAddedResponse: () => ({}),
|
||||
useScrollbarGutterSeed: jest.fn(),
|
||||
}));
|
||||
|
||||
jest.mock('../Presentation', () => ({
|
||||
|
|
|
|||
|
|
@ -47,7 +47,6 @@ jest.mock('@librechat/chat/react', () => {
|
|||
|
||||
jest.mock('~/hooks', () => ({
|
||||
useAddedResponse: () => ({}),
|
||||
useScrollbarGutterSeed: jest.fn(),
|
||||
useLocalize: () => (key: string) => key,
|
||||
}));
|
||||
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@ import download from 'downloadjs';
|
|||
import { toCanvas } from 'html-to-image';
|
||||
import { QueryKeys } from 'librechat-data-provider';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { completeProgressiveRowMounts } from '@librechat/chat/react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { MemoryRouter, Route, Routes, useNavigate, useParams } from 'react-router-dom';
|
||||
import type { TMessage, TConversation } from 'librechat-data-provider';
|
||||
import type { ReactNode } from 'react';
|
||||
import { completeProgressiveRowMounts } from '~/hooks/Messages/useProgressiveRowMount';
|
||||
import { ScreenshotProvider, useScreenshot } from '~/hooks/ScreenshotContext';
|
||||
import useExportConversation from '../useExportConversation';
|
||||
|
||||
|
|
@ -19,7 +19,8 @@ jest.mock('@librechat/client', () => ({
|
|||
jest.mock('~/hooks', () => ({ useLocalize: () => (key: string) => key }));
|
||||
jest.mock('downloadjs', () => jest.fn());
|
||||
jest.mock('html-to-image', () => ({ toCanvas: jest.fn() }));
|
||||
jest.mock('~/hooks/Messages/useProgressiveRowMount', () => ({
|
||||
jest.mock('@librechat/chat/react', () => ({
|
||||
...jest.requireActual('@librechat/chat/react'),
|
||||
completeProgressiveRowMounts: jest.fn(),
|
||||
}));
|
||||
|
||||
|
|
|
|||
|
|
@ -11,13 +11,6 @@ export type { ContentMetadataResult } from './useContentMetadata';
|
|||
export { default as useExpandCollapse } from './useExpandCollapse';
|
||||
export { default as useLazyCollapseBody } from './useLazyCollapseBody';
|
||||
export { default as useThrottledValue } from './useThrottledValue';
|
||||
export {
|
||||
RowMountProvider,
|
||||
useRowMountWindow,
|
||||
useProgressiveRowMount,
|
||||
completeProgressiveRowMounts,
|
||||
} from './useProgressiveRowMount';
|
||||
export type { RowMountWindow } from './useProgressiveRowMount';
|
||||
export { default as useMessageActions } from './useMessageActions';
|
||||
export { default as useMessagesRetention } from './useMessagesRetention';
|
||||
export { useLatestMessage, useLatestMessageId } from './useLatestMessage';
|
||||
|
|
@ -32,5 +25,4 @@ export {
|
|||
} from './useCopyToClipboard';
|
||||
export { default as useContentMetadata } from './useContentMetadata';
|
||||
export { default as useMessageScrolling } from './useMessageScrolling';
|
||||
export { default as useScrollbarGutter, useScrollbarGutterSeed } from './useScrollbarGutter';
|
||||
export { default as useSmoothStreaming } from './useSmoothStreaming';
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { createContext, useRef, useContext, RefObject, ReactNode } from 'react';
|
||||
import { toCanvas } from 'html-to-image';
|
||||
import { completeProgressiveRowMounts } from '@librechat/chat/react';
|
||||
import { ThemeContext, isDark, readThemeColor } from '@librechat/client';
|
||||
import { completeProgressiveRowMounts } from '~/hooks/Messages/useProgressiveRowMount';
|
||||
|
||||
type ScreenshotContextType = {
|
||||
ref?: RefObject<HTMLDivElement>;
|
||||
|
|
|
|||
65
e2e/specs/mock/scenarios/long-conversation-mount.spec.ts
Normal file
65
e2e/specs/mock/scenarios/long-conversation-mount.spec.ts
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import { randomUUID } from 'crypto';
|
||||
import { expect, test } from '@playwright/test';
|
||||
import {
|
||||
deleteConversations,
|
||||
deleteMessagesByConversation,
|
||||
seedConversations,
|
||||
seedMessages,
|
||||
} from '../db';
|
||||
import { getE2EUser } from '../../../setup/user';
|
||||
|
||||
/** Past the row count at which the thread mounts progressively instead of all at once. */
|
||||
const TURNS = 60;
|
||||
|
||||
async function seedLongChat(): Promise<string> {
|
||||
const conversationId = randomUUID();
|
||||
const { email } = getE2EUser();
|
||||
await seedConversations(email, [
|
||||
{ conversationId, title: 'Long conversation', updatedAt: new Date() },
|
||||
]);
|
||||
let parentMessageId = '00000000-0000-0000-0000-000000000000';
|
||||
const messages = Array.from({ length: TURNS }, (_, index) => {
|
||||
const messageId = randomUUID();
|
||||
const isCreatedByUser = index % 2 === 0;
|
||||
const message = {
|
||||
messageId,
|
||||
parentMessageId,
|
||||
text: `Turn ${index + 1} of the long conversation`,
|
||||
isCreatedByUser,
|
||||
sender: isCreatedByUser ? 'User' : 'Mock Provider A',
|
||||
};
|
||||
parentMessageId = messageId;
|
||||
return message;
|
||||
});
|
||||
await seedMessages(email, conversationId, messages);
|
||||
return conversationId;
|
||||
}
|
||||
|
||||
test.describe('long conversations', () => {
|
||||
let conversationId: string;
|
||||
|
||||
test.beforeEach(async () => {
|
||||
conversationId = await seedLongChat();
|
||||
});
|
||||
|
||||
test.afterEach(async () => {
|
||||
await deleteMessagesByConversation([conversationId]);
|
||||
await deleteConversations([conversationId]);
|
||||
});
|
||||
|
||||
test('a long conversation mounts every turn and its latest turn is reachable @scenario:long-conversation-mounts-every-turn', async ({
|
||||
page,
|
||||
}) => {
|
||||
await page.goto(`/c/${conversationId}`);
|
||||
|
||||
const thread = page.locator(`[data-conversation-id="${conversationId}"]`);
|
||||
await expect(
|
||||
thread.getByText('Turn 1 of the long conversation', { exact: true }),
|
||||
).toBeVisible();
|
||||
await expect(thread.locator('.message-render')).toHaveCount(TURNS);
|
||||
|
||||
const latest = thread.getByText(`Turn ${TURNS} of the long conversation`, { exact: true });
|
||||
await latest.scrollIntoViewIfNeeded();
|
||||
await expect(latest).toBeInViewport();
|
||||
});
|
||||
});
|
||||
|
|
@ -168,3 +168,15 @@ export { default as useAudioLevels, frameLevel } from './react/levels';
|
|||
export { default as useInitPopoverInput } from './react/popover';
|
||||
export { useComposerHost, ComposerHostProvider } from './react/input';
|
||||
export type { ComposerHost, ComposerShortcutAction } from './react/input';
|
||||
export {
|
||||
RowMountProvider,
|
||||
useRowMountWindow,
|
||||
useProgressiveRowMount,
|
||||
completeProgressiveRowMounts,
|
||||
} from './react/messages/useProgressiveRowMount';
|
||||
export type { RowMountWindow } from './react/messages/useProgressiveRowMount';
|
||||
export {
|
||||
default as useScrollbarGutter,
|
||||
useScrollbarGutterSeed,
|
||||
SCROLLBAR_GUTTER_PROPERTY,
|
||||
} from './react/messages/useScrollbarGutter';
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
import React from 'react';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import type { RowMountWindow } from '../useProgressiveRowMount';
|
||||
|
|
@ -1,3 +1,6 @@
|
|||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import useScrollbarGutter, { SCROLLBAR_GUTTER_PROPERTY } from '../useScrollbarGutter';
|
||||
|
||||
|
|
@ -26,7 +26,7 @@ export function RowMountProvider({
|
|||
}: {
|
||||
mountWindow: RowMountWindow;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
}): ReactNode {
|
||||
return <RowMountContext.Provider value={mountWindow}>{children}</RowMountContext.Provider>;
|
||||
}
|
||||
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import { useEffect, useLayoutEffect } from 'react';
|
||||
import type { RefObject } from 'react';
|
||||
|
||||
export const SCROLLBAR_GUTTER_PROPERTY = '--message-scrollbar-gutter';
|
||||
|
||||
|
|
@ -13,9 +14,7 @@ export const SCROLLBAR_GUTTER_PROPERTY = '--message-scrollbar-gutter';
|
|||
* The value is app-wide, so it lives on the document element and outlives any
|
||||
* one thread: a second chat column measures the same band, and dropping the
|
||||
* property when one unmounts would strand the other on the fallback. */
|
||||
export default function useScrollbarGutter(
|
||||
scrollableRef: React.RefObject<HTMLDivElement | null>,
|
||||
): void {
|
||||
export default function useScrollbarGutter(scrollableRef: RefObject<HTMLDivElement | null>): void {
|
||||
useEffect(() => {
|
||||
const element = scrollableRef.current;
|
||||
if (!element) {
|
||||
Loading…
Add table
Add a link
Reference in a new issue