-
Notifications
You must be signed in to change notification settings - Fork 13.6k
feat: migrate MessageSearch to PaginatedVirtualList #40844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
srijnabhargav
wants to merge
3
commits into
RocketChat:develop
Choose a base branch
from
srijnabhargav:feat/tanstack-message-search
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...meteor/client/views/room/contextualBar/MessageSearchTab/components/MessageSearch.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import type { IMessage } from '@rocket.chat/core-typings'; | ||
| import { render, screen } from '@testing-library/react'; | ||
|
|
||
| import MessageSearch from './MessageSearch'; | ||
|
|
||
| const useMessageSearchQueryMock = jest.fn(); | ||
|
|
||
| jest.mock('../hooks/useMessageSearchQuery', () => ({ | ||
| useMessageSearchQuery: (...args: unknown[]) => useMessageSearchQueryMock(...args), | ||
| })); | ||
|
|
||
| jest.mock('../../../../../components/PaginatedVirtualList', () => ({ | ||
| PaginatedVirtualList: ({ items, renderItem }: { items: IMessage[]; renderItem: (item: IMessage, index: number) => React.ReactNode }) => ( | ||
| <div data-testid='message-search-list'> | ||
| {items.map((item, index) => ( | ||
| <div key={item._id}>{renderItem(item, index)}</div> | ||
| ))} | ||
| </div> | ||
| ), | ||
| })); | ||
|
|
||
| jest.mock('../../../../../components/message/variants/RoomMessage', () => ({ message }: { message: IMessage }) => ( | ||
| <div data-testid='room-message'>{message.msg}</div> | ||
| )); | ||
|
|
||
| jest.mock('../../../../../components/message/variants/SystemMessage', () => ({ message }: { message: IMessage }) => ( | ||
| <div data-testid='system-message'>{message.msg}</div> | ||
| )); | ||
|
|
||
| jest.mock('../../../../../hooks/useFormatDate', () => ({ | ||
| useFormatDate: () => () => 'formatted-date', | ||
| })); | ||
|
|
||
| jest.mock('../../../MessageList/MessageListErrorBoundary', () => ({ | ||
| __esModule: true, | ||
| default: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
|
srijnabhargav marked this conversation as resolved.
|
||
| })); | ||
|
|
||
| jest.mock('../../../MessageList/providers/MessageListProvider', () => ({ | ||
| __esModule: true, | ||
| default: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
| })); | ||
|
|
||
| jest.mock('../../../contexts/RoomContext', () => ({ | ||
| useRoomSubscription: () => ({ | ||
| tunread: ['message-1'], | ||
| tunreadUser: ['message-1'], | ||
| tunreadGroup: ['message-1'], | ||
| }), | ||
| })); | ||
|
|
||
| jest.mock('@rocket.chat/ui-contexts', () => ({ | ||
| ...jest.requireActual('@rocket.chat/ui-contexts'), | ||
| useTranslation: () => (key: string) => key, | ||
| useUserPreference: () => true, | ||
| })); | ||
|
|
||
| const createMessage = (id: string, overrides: Partial<IMessage> = {}): IMessage => | ||
| ({ | ||
| _id: id, | ||
| rid: 'room-id', | ||
| msg: `Message ${id}`, | ||
| ts: new Date('2026-03-22T10:00:00.000Z'), | ||
| u: { _id: 'user-id', username: 'testuser', name: 'Test User' }, | ||
| _updatedAt: new Date('2026-03-22T10:00:00.000Z'), | ||
| ...overrides, | ||
| }) as IMessage; | ||
|
|
||
| describe('MessageSearch', () => { | ||
| beforeEach(() => { | ||
| useMessageSearchQueryMock.mockReturnValue({ | ||
| isPending: false, | ||
| isSuccess: true, | ||
| data: { items: [], itemCount: 0 }, | ||
| fetchNextPage: jest.fn(), | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| useMessageSearchQueryMock.mockReset(); | ||
| }); | ||
|
|
||
| it('renders the empty state when no messages are returned', () => { | ||
| render(<MessageSearch searchText='hello' globalSearch={false} />); | ||
|
srijnabhargav marked this conversation as resolved.
Outdated
|
||
|
|
||
| expect(screen.getByText('No_results_found')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders nothing until the search query succeeds', () => { | ||
| useMessageSearchQueryMock.mockReturnValue({ | ||
| isPending: true, | ||
| isSuccess: false, | ||
| data: undefined, | ||
| fetchNextPage: jest.fn(), | ||
| }); | ||
|
|
||
| const { container } = render(<MessageSearch searchText='hello' globalSearch={false} />); | ||
|
|
||
| expect(container).toBeEmptyDOMElement(); | ||
| }); | ||
|
|
||
| it('renders room and system messages with date dividers', () => { | ||
| const roomMessage = createMessage('message-1'); | ||
| const systemMessage = createMessage('message-2', { | ||
| ts: new Date('2026-03-23T10:00:00.000Z'), | ||
| t: 'au', | ||
| msg: 'System event', | ||
| }); | ||
|
|
||
| useMessageSearchQueryMock.mockReturnValue({ | ||
| isPending: false, | ||
| isSuccess: true, | ||
| data: { items: [roomMessage, systemMessage], itemCount: 2 }, | ||
| fetchNextPage: jest.fn(), | ||
| }); | ||
|
|
||
| render(<MessageSearch searchText='hello' globalSearch={false} />); | ||
|
|
||
| expect(screen.getByTestId('room-message')).toHaveTextContent('Message message-1'); | ||
| expect(screen.getByTestId('system-message')).toHaveTextContent('System event'); | ||
| expect(screen.getAllByText('formatted-date')).toHaveLength(2); | ||
| }); | ||
| }); | ||
103 changes: 103 additions & 0 deletions
103
apps/meteor/client/views/room/contextualBar/MessageSearchTab/components/MessageSearch.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { Box, MessageDivider } from '@rocket.chat/fuselage'; | ||
| import { MessageTypes } from '@rocket.chat/message-types'; | ||
| import { ContextualbarEmptyContent } from '@rocket.chat/ui-client'; | ||
| import { useTranslation, useUserPreference } from '@rocket.chat/ui-contexts'; | ||
| import type { ReactElement } from 'react'; | ||
| import { Fragment, memo } from 'react'; | ||
|
|
||
| import { PaginatedVirtualList } from '../../../../../components/PaginatedVirtualList'; | ||
| import RoomMessage from '../../../../../components/message/variants/RoomMessage'; | ||
| import SystemMessage from '../../../../../components/message/variants/SystemMessage'; | ||
| import { useFormatDate } from '../../../../../hooks/useFormatDate'; | ||
| import MessageListErrorBoundary from '../../../MessageList/MessageListErrorBoundary'; | ||
| import { isMessageNewDay } from '../../../MessageList/lib/isMessageNewDay'; | ||
| import MessageListProvider from '../../../MessageList/providers/MessageListProvider'; | ||
| import { useRoomSubscription } from '../../../contexts/RoomContext'; | ||
| import type { MessageSearchItem } from '../hooks/useMessageSearchQuery'; | ||
| import { useMessageSearchQuery } from '../hooks/useMessageSearchQuery'; | ||
|
|
||
| type MessageSearchProps = { | ||
| searchText: string; | ||
| globalSearch: boolean; | ||
| }; | ||
|
|
||
| const MessageSearch = ({ searchText, globalSearch }: MessageSearchProps): ReactElement => { | ||
| const t = useTranslation(); | ||
| const formatDate = useFormatDate(); | ||
| const showUserAvatar = !!useUserPreference<boolean>('displayAvatars'); | ||
|
|
||
| const subscription = useRoomSubscription(); | ||
| const { isPending, isSuccess, data, fetchNextPage } = useMessageSearchQuery({ searchText, globalSearch }); | ||
| const items = data?.items || []; | ||
| const itemCount = data?.itemCount ?? 0; | ||
|
srijnabhargav marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (!isSuccess) { | ||
| return <></>; | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| {items.length === 0 && <ContextualbarEmptyContent title={t('No_results_found')} />} | ||
| {items.length > 0 && ( | ||
| <MessageListErrorBoundary> | ||
| <MessageListProvider> | ||
| <Box | ||
| is='section' | ||
| display='flex' | ||
| flexDirection='column' | ||
| flexGrow={1} | ||
| flexShrink={1} | ||
| flexBasis={0} | ||
| height='full' | ||
| overflow='hidden' | ||
| style={{ minHeight: 0 }} | ||
| > | ||
| <Box h='full' w='full' style={{ minHeight: 0 }}> | ||
| <PaginatedVirtualList | ||
| items={items} | ||
| totalCount={itemCount} | ||
| overscan={25} | ||
| onEndReached={isPending ? undefined : fetchNextPage} | ||
| renderItem={(message: MessageSearchItem, index) => { | ||
| const previous = items[index - 1]; | ||
|
|
||
| const newDay = isMessageNewDay(message, previous); | ||
|
|
||
| const system = MessageTypes.isSystemMessage(message); | ||
|
|
||
| const unread = subscription?.tunread?.includes(message._id) ?? false; | ||
| const mention = subscription?.tunreadUser?.includes(message._id) ?? false; | ||
| const all = subscription?.tunreadGroup?.includes(message._id) ?? false; | ||
|
|
||
| return ( | ||
| <Fragment key={message._id}> | ||
| {newDay && <MessageDivider>{formatDate(message.ts)}</MessageDivider>} | ||
|
|
||
| {system ? ( | ||
| <SystemMessage message={message} showUserAvatar={showUserAvatar} /> | ||
| ) : ( | ||
| <RoomMessage | ||
| message={message} | ||
| sequential={false} | ||
| unread={unread} | ||
| mention={mention} | ||
| all={all} | ||
| context='search' | ||
| searchText={searchText} | ||
| showUserAvatar={showUserAvatar} | ||
| /> | ||
| )} | ||
| </Fragment> | ||
| ); | ||
| }} | ||
| /> | ||
| </Box> | ||
| </Box> | ||
| </MessageListProvider> | ||
| </MessageListErrorBoundary> | ||
| )} | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default memo(MessageSearch); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.