-
Notifications
You must be signed in to change notification settings - Fork 13.4k
chore: Virtualized main room message list #39490
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
Draft
MartinSchoeler
wants to merge
1
commit into
develop
Choose a base branch
from
poc/virtualize-msg-list
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.
+182
−30
Draft
Changes from all commits
Commits
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
43 changes: 15 additions & 28 deletions
43
apps/meteor/client/views/room/MessageList/MessageList.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
129 changes: 129 additions & 0 deletions
129
apps/meteor/client/views/room/MessageList/VirtualizedMessageList.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,129 @@ | ||
| import type { IRoom, IMessage, ISubscription } from '@rocket.chat/core-typings'; | ||
| import { isThreadMessage } from '@rocket.chat/core-typings'; | ||
| import { Box } from '@rocket.chat/fuselage'; | ||
| import { MessageTypes } from '@rocket.chat/message-types'; | ||
| import { useVirtualizer, type VirtualItem } from '@tanstack/react-virtual'; | ||
| import type { MutableRefObject } from 'react'; | ||
| import { useEffect, useRef } from 'react'; | ||
|
|
||
| import { MessageListItem } from './MessageListItem'; | ||
| import { isMessageSequential } from './lib/isMessageSequential'; | ||
| import { RoomManager } from '../../../lib/RoomManager'; | ||
|
|
||
| const ESTIMATE_SIZE = 84; | ||
| const OVERSCAN = 5; | ||
| const DEFAULT_MAX_RENDERED = 50; | ||
|
|
||
| type VirtualizedMessageListProps = { | ||
| rid: IRoom['_id']; | ||
| messages: IMessage[]; | ||
| scrollContainerRef?: MutableRefObject<HTMLElement | null>; | ||
| messageGroupingPeriod: number; | ||
| firstUnreadMessageId: string | undefined; | ||
| showUserAvatar: boolean; | ||
| subscription: ISubscription | undefined; | ||
| maxRenderedMessages?: number; | ||
| }; | ||
|
|
||
| export function VirtualizedMessageList({ | ||
| rid, | ||
| messages, | ||
| scrollContainerRef, | ||
| messageGroupingPeriod, | ||
| firstUnreadMessageId, | ||
| showUserAvatar, | ||
| subscription, | ||
| maxRenderedMessages = DEFAULT_MAX_RENDERED, | ||
| }: VirtualizedMessageListProps) { | ||
| const hasRestoredScrollRef = useRef(false); | ||
| // Use setting to limit overscan so we rarely render more than maxRenderedMessages; avoid slicing | ||
| // virtualItems so we never unmount rows (unmount clears measurements and causes scroll jump). | ||
| const maxRendered = Math.max(1, Math.floor(Number(maxRenderedMessages))); | ||
| const overscan = Math.min(OVERSCAN, Math.max(0, Math.floor(maxRendered / 2) - 2)); | ||
|
|
||
| const virtualizer = useVirtualizer({ | ||
| count: messages.length, | ||
| getScrollElement: () => scrollContainerRef?.current ?? null, | ||
| estimateSize: () => ESTIMATE_SIZE, | ||
| overscan, | ||
| getItemKey: (index: number) => messages[index]?._id ?? index, | ||
| }); | ||
|
|
||
| const virtualItems = virtualizer.getVirtualItems(); | ||
| const totalSize = virtualizer.getTotalSize(); | ||
|
|
||
| // Restore scroll position when returning to a channel (after virtual list has laid out). | ||
| useEffect(() => { | ||
| if (hasRestoredScrollRef.current || totalSize <= 0) { | ||
| return; | ||
| } | ||
| const store = RoomManager.getStore(rid); | ||
| if (store?.scroll == null || store.atBottom) { | ||
| return; | ||
| } | ||
| hasRestoredScrollRef.current = true; | ||
| virtualizer.scrollToOffset(store.scroll, { align: 'start' }); | ||
| }, [rid, totalSize, virtualizer]); | ||
|
|
||
| if (messages.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <Box | ||
| is='li' | ||
| style={{ | ||
| listStyle: 'none', | ||
| position: 'relative', | ||
| display: 'block', | ||
| width: '100%', | ||
| }} | ||
| > | ||
| <Box | ||
| style={{ | ||
| height: `${totalSize}px`, | ||
| width: '100%', | ||
| position: 'relative', | ||
| }} | ||
| > | ||
| {virtualItems.map((virtualRow: VirtualItem) => { | ||
| const message = messages[virtualRow.index]; | ||
| if (!message) { | ||
| return null; | ||
| } | ||
| const previous = messages[virtualRow.index - 1]; | ||
| const sequential = isMessageSequential(message, previous, messageGroupingPeriod); | ||
| const showUnreadDivider = firstUnreadMessageId === message._id; | ||
| const system = MessageTypes.isSystemMessage(message); | ||
| const visible = !isThreadMessage(message) && !system; | ||
|
|
||
| return ( | ||
| <Box | ||
| key={virtualRow.key} | ||
| data-index={virtualRow.index} | ||
| ref={virtualizer.measureElement} | ||
| style={{ | ||
| position: 'absolute', | ||
| top: 0, | ||
| left: 0, | ||
| width: '100%', | ||
| transform: `translateY(${virtualRow.start}px)`, | ||
| }} | ||
| > | ||
| <MessageListItem | ||
| message={message} | ||
| previous={previous} | ||
| showUnreadDivider={showUnreadDivider} | ||
| showUserAvatar={showUserAvatar} | ||
| sequential={sequential} | ||
| visible={visible} | ||
| subscription={subscription} | ||
| system={system} | ||
| /> | ||
| </Box> | ||
| ); | ||
| })} | ||
| </Box> | ||
| </Box> | ||
| ); | ||
| } |
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
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👎