-
Notifications
You must be signed in to change notification settings - Fork 13.6k
feat: add shared VirtualList component; migrate DiscussionsList to virtua #39394
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
6
commits into
RocketChat:develop
Choose a base branch
from
srijnabhargav:feat/tanstack-virtual-poc
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.
+1,473
−50
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3e40663
feat: add VirtualList component powered by virtua
srijnabhargav dde4c10
feat: migrate DiscussionsList from react-virtuoso to VirtualList
srijnabhargav 4f26f66
test: add VirtualList spec and update DiscussionsList tests for virtua
srijnabhargav 7c5264d
fix: refresh VirtualList end-reach lock on dataset changes
srijnabhargav f613e4c
fix: provide date formatter in discussions item stories
srijnabhargav 4ba2638
fix: address paginated virtual list review
srijnabhargav 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
24 changes: 24 additions & 0 deletions
24
apps/meteor/client/components/VirtualList/VirtuaListContainer.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,24 @@ | ||
| import type { CSSProperties, HTMLAttributes, ReactNode } from 'react'; | ||
| import { forwardRef } from 'react'; | ||
|
|
||
| const listResetStyle = { | ||
| margin: 0, | ||
| padding: 0, | ||
| listStyle: 'none', | ||
| } as const; | ||
|
|
||
| export type VirtuaListContainerProps = { | ||
| children: ReactNode; | ||
| style: CSSProperties; | ||
| } & Omit<HTMLAttributes<HTMLUListElement>, 'children' | 'style'>; | ||
|
|
||
| export const VirtuaListContainer = forwardRef<HTMLUListElement, VirtuaListContainerProps>(function VirtuaListContainer( | ||
| { children, style, ...props }, | ||
| ref, | ||
| ) { | ||
| return ( | ||
| <ul {...props} ref={ref} style={{ ...listResetStyle, ...style }}> | ||
| {children} | ||
| </ul> | ||
| ); | ||
| }); |
216 changes: 216 additions & 0 deletions
216
apps/meteor/client/components/VirtualList/VirtualList.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,216 @@ | ||
| import type { UseInfiniteQueryResult } from '@tanstack/react-query'; | ||
| import { act, fireEvent, render, screen } from '@testing-library/react'; | ||
| import { axe } from 'jest-axe'; | ||
| import type { CSSProperties, HTMLAttributes, ReactNode } from 'react'; | ||
| import * as React from 'react'; | ||
| import { Children, forwardRef, isValidElement } from 'react'; | ||
|
|
||
| import PaginatedVirtualList from './VirtualList'; | ||
|
|
||
| const mockVirtualizerHandle = { | ||
| scrollToIndex: jest.fn(), | ||
| scrollTo: jest.fn(), | ||
| findItemIndex: jest.fn((offset: number) => offset), | ||
| scrollOffset: 0, | ||
| scrollSize: 1000, | ||
| viewportSize: 300, | ||
| }; | ||
|
|
||
| type MockVListProps = { | ||
| children: ReactNode; | ||
| bufferSize?: number; | ||
| onScroll?: (offset: number) => void; | ||
| as?: React.ElementType; | ||
| item?: React.ElementType; | ||
| style?: CSSProperties; | ||
| className?: string; | ||
| }; | ||
|
|
||
| jest.mock('virtua', () => { | ||
| return { | ||
| Virtualizer: React.forwardRef( | ||
| ( | ||
| { children, bufferSize, onScroll, as: asRoot = 'div', item: asItem = 'div', style, className }: MockVListProps, | ||
| ref: React.Ref<unknown>, | ||
| ) => { | ||
| React.useImperativeHandle(ref, () => mockVirtualizerHandle); | ||
| const Root = asRoot; | ||
| const Item = asItem; | ||
| const wrapped = Children.map(children, (child, index) => { | ||
| const key = isValidElement(child) && child.key != null ? String(child.key) : `row-${index}`; | ||
| return <Item key={key}>{child}</Item>; | ||
| }); | ||
|
|
||
| return ( | ||
| <Root | ||
| className={className} | ||
| data-buffer-size={bufferSize} | ||
| data-testid='virtual-list' | ||
| style={style ?? { height: '100%' }} | ||
| onScroll={() => onScroll?.(mockVirtualizerHandle.scrollOffset)} | ||
| > | ||
| {wrapped} | ||
| </Root> | ||
| ); | ||
| }, | ||
| ), | ||
| }; | ||
| }); | ||
|
|
||
| jest.mock('@rocket.chat/ui-client', () => ({ | ||
| ...jest.requireActual('@rocket.chat/ui-client'), | ||
| CustomVirtuaScrollbars: forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement>>(function CustomVirtuaScrollbars( | ||
| { children, ...props }, | ||
| ref, | ||
| ) { | ||
| // eslint-disable-next-line testing-library/no-node-access | ||
| const content = isValidElement<{ children?: ReactNode }>(children) && children.type === 'div' ? children.props.children : children; | ||
|
|
||
| return ( | ||
| <div ref={ref} {...props}> | ||
| {content} | ||
| </div> | ||
| ); | ||
| }), | ||
| })); | ||
|
|
||
| const items = Array.from({ length: 10 }, (_, index) => ({ _id: `${index}` })); | ||
|
|
||
| type VirtualListTestItem = (typeof items)[number]; | ||
|
|
||
| const renderVirtualList = ( | ||
| props: Partial<{ | ||
| items: VirtualListTestItem[]; | ||
| totalCount: number; | ||
| renderItem: (item: VirtualListTestItem, index: number) => ReactNode; | ||
| overscan?: number; | ||
| onEndReached?: UseInfiniteQueryResult['fetchNextPage']; | ||
| }> = {}, | ||
| ) => render(<PaginatedVirtualList items={items} totalCount={20} renderItem={(item) => <div>{item._id}</div>} {...props} />); | ||
|
|
||
| const advanceDebouncedScroll = async () => { | ||
| await act(async () => { | ||
| await jest.advanceTimersByTimeAsync(300); | ||
| }); | ||
| }; | ||
|
|
||
| describe('PaginatedVirtualList', () => { | ||
| beforeEach(() => { | ||
| mockVirtualizerHandle.scrollOffset = 0; | ||
| mockVirtualizerHandle.scrollSize = 1000; | ||
| mockVirtualizerHandle.viewportSize = 300; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| it('has no accessibility violations', async () => { | ||
| const { container } = renderVirtualList(); | ||
| expect(await axe(container)).toHaveNoViolations(); | ||
| }); | ||
|
|
||
| it('calls onEndReached when scrolled near the bottom', async () => { | ||
| jest.useFakeTimers(); | ||
| const onEndReached = jest.fn().mockResolvedValue(undefined); | ||
|
|
||
| renderVirtualList({ onEndReached }); | ||
| expect(onEndReached).not.toHaveBeenCalled(); | ||
|
|
||
| mockVirtualizerHandle.scrollOffset = 700; | ||
| fireEvent.scroll(screen.getByTestId('virtual-list')); | ||
| await advanceDebouncedScroll(); | ||
|
|
||
| expect(onEndReached).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not call onEndReached when all items are loaded', () => { | ||
| const onEndReached = jest.fn().mockResolvedValue(undefined); | ||
|
|
||
| renderVirtualList({ onEndReached, totalCount: items.length }); | ||
| mockVirtualizerHandle.scrollOffset = 700; | ||
| fireEvent.scroll(screen.getByTestId('virtual-list')); | ||
|
|
||
| expect(onEndReached).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not call onEndReached repeatedly for the same item count', async () => { | ||
| jest.useFakeTimers(); | ||
| const onEndReached = jest.fn().mockResolvedValue(undefined); | ||
|
|
||
| renderVirtualList({ onEndReached }); | ||
| mockVirtualizerHandle.scrollOffset = 700; | ||
| fireEvent.scroll(screen.getByTestId('virtual-list')); | ||
| fireEvent.scroll(screen.getByTestId('virtual-list')); | ||
| await advanceDebouncedScroll(); | ||
|
|
||
| expect(onEndReached).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('calls onEndReached after a same-size dataset reset', async () => { | ||
| jest.useFakeTimers(); | ||
| const onEndReached = jest.fn().mockResolvedValue(undefined); | ||
| const { rerender } = renderVirtualList({ onEndReached }); | ||
| mockVirtualizerHandle.scrollOffset = 700; | ||
| fireEvent.scroll(screen.getByTestId('virtual-list')); | ||
| await advanceDebouncedScroll(); | ||
|
|
||
| const resetItems = Array.from({ length: 10 }, (_, index) => ({ _id: `reset-${index}` })); | ||
| rerender( | ||
| <PaginatedVirtualList items={resetItems} totalCount={20} renderItem={(item) => <div>{item._id}</div>} onEndReached={onEndReached} />, | ||
| ); | ||
| fireEvent.scroll(screen.getByTestId('virtual-list')); | ||
| await advanceDebouncedScroll(); | ||
|
|
||
| expect(onEndReached).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('passes overscan through to virtua buffer size', () => { | ||
| renderVirtualList({ overscan: 25 }); | ||
|
|
||
| expect(screen.getByTestId('virtual-list')).toHaveAttribute('data-buffer-size', '25'); | ||
| }); | ||
|
|
||
| it('allows onEndReached to retry after a failed load', async () => { | ||
| jest.useFakeTimers(); | ||
| const onEndReached = jest.fn().mockRejectedValue(new Error('failed to load more items')); | ||
|
|
||
| renderVirtualList({ onEndReached }); | ||
| mockVirtualizerHandle.scrollOffset = 700; | ||
| fireEvent.scroll(screen.getByTestId('virtual-list')); | ||
| await advanceDebouncedScroll(); | ||
|
|
||
| fireEvent.scroll(screen.getByTestId('virtual-list')); | ||
| await advanceDebouncedScroll(); | ||
|
|
||
| expect(onEndReached).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('allows onEndReached to retry after a synchronous throw', async () => { | ||
| jest.useFakeTimers(); | ||
| const onEndReached = jest | ||
| .fn() | ||
| .mockImplementationOnce(() => { | ||
| throw new Error('failed to load more items'); | ||
| }) | ||
| .mockImplementation(() => undefined); | ||
|
|
||
| renderVirtualList({ onEndReached }); | ||
| mockVirtualizerHandle.scrollOffset = 700; | ||
| fireEvent.scroll(screen.getByTestId('virtual-list')); | ||
| await advanceDebouncedScroll(); | ||
| fireEvent.scroll(screen.getByTestId('virtual-list')); | ||
| await advanceDebouncedScroll(); | ||
|
|
||
| expect(onEndReached).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('calls onEndReached when the viewport is underfilled', () => { | ||
| const onEndReached = jest.fn().mockResolvedValue(undefined); | ||
| mockVirtualizerHandle.scrollSize = 200; | ||
|
|
||
| renderVirtualList({ onEndReached }); | ||
|
|
||
| expect(onEndReached).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
109 changes: 109 additions & 0 deletions
109
apps/meteor/client/components/VirtualList/VirtualList.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,109 @@ | ||
| import { useDebouncedCallback } from '@rocket.chat/fuselage-hooks'; | ||
| import { CustomVirtuaScrollbars } from '@rocket.chat/ui-client'; | ||
| import type { UseInfiniteQueryResult } from '@tanstack/react-query'; | ||
| import type { ReactNode } from 'react'; | ||
| import { useCallback, useLayoutEffect, useRef } from 'react'; | ||
| import type { VirtualizerHandle } from 'virtua'; | ||
| import { Virtualizer } from 'virtua'; | ||
|
|
||
| import { VirtuaListContainer } from './VirtuaListContainer'; | ||
|
|
||
| const NEAR_BOTTOM_THRESHOLD = -20; | ||
|
|
||
| const scrollViewportStyle = { | ||
| height: '100%', | ||
| width: '100%', | ||
| overflow: 'auto', | ||
| } as const; | ||
|
|
||
| type PaginatedVirtualListProps<T extends { _id: string }> = { | ||
| items: T[]; | ||
| totalCount: number; | ||
| renderItem: (item: T, index: number) => ReactNode; | ||
| overscan?: number; | ||
| onEndReached?: UseInfiniteQueryResult['fetchNextPage']; | ||
| }; | ||
|
|
||
| function PaginatedVirtualList<T extends { _id: string }>({ | ||
| items, | ||
| totalCount, | ||
| renderItem, | ||
| overscan, | ||
| onEndReached, | ||
| }: PaginatedVirtualListProps<T>) { | ||
| const virtualizerRef = useRef<VirtualizerHandle | null>(null); | ||
| const isEndReachedLockedRef = useRef(false); | ||
| const firstItemId = items[0]?._id ?? ''; | ||
| const lastItemId = items[items.length - 1]?._id ?? ''; | ||
|
|
||
| useLayoutEffect(() => { | ||
| isEndReachedLockedRef.current = false; | ||
| }, [firstItemId, items.length, lastItemId, totalCount]); | ||
|
|
||
| const checkEndReached = useCallback( | ||
| (offset: number) => { | ||
| const handle = virtualizerRef.current; | ||
| if (!handle || !onEndReached) { | ||
| return; | ||
| } | ||
|
|
||
| const { scrollSize, viewportSize } = handle; | ||
| if (viewportSize <= 0) { | ||
| return; | ||
| } | ||
|
|
||
| if (items.length >= totalCount) { | ||
| return; | ||
| } | ||
|
|
||
| const nearBottom = offset - scrollSize + viewportSize >= NEAR_BOTTOM_THRESHOLD; | ||
| if (!nearBottom) { | ||
| return; | ||
| } | ||
|
|
||
| if (isEndReachedLockedRef.current) { | ||
| return; | ||
| } | ||
| isEndReachedLockedRef.current = true; | ||
|
|
||
| try { | ||
| void onEndReached().catch(() => { | ||
| isEndReachedLockedRef.current = false; | ||
| }); | ||
| } catch { | ||
| isEndReachedLockedRef.current = false; | ||
| } | ||
| }, | ||
| [items.length, onEndReached, totalCount], | ||
| ); | ||
|
|
||
| const handleScroll = useDebouncedCallback( | ||
| (offset: number) => { | ||
| checkEndReached(offset); | ||
| }, | ||
| 300, | ||
| [checkEndReached], | ||
| ); | ||
|
|
||
| useLayoutEffect(() => { | ||
| const handle = virtualizerRef.current; | ||
| if (!handle) { | ||
| return; | ||
| } | ||
| checkEndReached(handle.scrollOffset); | ||
| }, [checkEndReached, firstItemId, items.length, lastItemId, totalCount]); | ||
|
|
||
| return ( | ||
| <CustomVirtuaScrollbars> | ||
| <div style={scrollViewportStyle}> | ||
| <Virtualizer ref={virtualizerRef} as={VirtuaListContainer} item='li' bufferSize={overscan} onScroll={handleScroll}> | ||
| {items.map((item, index) => ( | ||
| <div key={item._id}>{renderItem(item, index)}</div> | ||
| ))} | ||
| </Virtualizer> | ||
| </div> | ||
| </CustomVirtuaScrollbars> | ||
| ); | ||
| } | ||
|
|
||
| export default PaginatedVirtualList; | ||
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 @@ | ||
| export { default as PaginatedVirtualList } from './VirtualList'; |
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.
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.