-
Notifications
You must be signed in to change notification settings - Fork 13.8k
fix: Message list scroll position lost after switching channels and returning #41805
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
Merged
Merged
Changes from all 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Fixes the issue where the message list kept jumping to the latest messages instead of restoring the previous position when switching channels. |
61 changes: 61 additions & 0 deletions
61
apps/meteor/client/views/room/MessageList/hooks/useKeepAtBottom.spec.ts
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,61 @@ | ||
| import { renderHook } from '@testing-library/react'; | ||
|
|
||
| import { useKeepAtBottom } from './useKeepAtBottom'; | ||
|
|
||
| let resizeCallbacks: ResizeObserverCallback[] = []; | ||
|
|
||
| class MockResizeObserver { | ||
| constructor(cb: ResizeObserverCallback) { | ||
| resizeCallbacks.push(cb); | ||
| } | ||
|
|
||
| observe = jest.fn(); | ||
|
|
||
| unobserve = jest.fn(); | ||
|
|
||
| disconnect = jest.fn(); | ||
| } | ||
|
|
||
| // Rows are measured asynchronously after the list remounts (avatars, images, reactions), | ||
| // so the observer fires repeatedly while the restored position is being applied. | ||
| const settleList = (times: number) => { | ||
| for (let i = 0; i < times; i++) { | ||
| resizeCallbacks.forEach((cb) => cb([], {} as ResizeObserver)); | ||
| } | ||
| }; | ||
|
|
||
| const mountList = (isAtBottom: { current: boolean }) => { | ||
| const { result } = renderHook(() => useKeepAtBottom(isAtBottom)); | ||
|
|
||
| const node = document.createElement('div'); | ||
| node.appendChild(document.createElement('div')); | ||
| result.current.keepAtBottomRef(node); | ||
|
|
||
| const scrollToEnd = jest.fn(); | ||
| result.current.setKeepAtBottom(scrollToEnd); | ||
|
|
||
| return scrollToEnd; | ||
| }; | ||
|
|
||
| describe('useKeepAtBottom', () => { | ||
| beforeEach(() => { | ||
| resizeCallbacks = []; | ||
| (global as any).ResizeObserver = MockResizeObserver; | ||
| }); | ||
|
|
||
| it('does not pull the list to the latest messages when the room was left mid-history', () => { | ||
| const scrollToEnd = mountList({ current: false }); | ||
|
|
||
| settleList(3); | ||
|
|
||
| expect(scrollToEnd).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('keeps the list at the bottom when the room was left at the bottom', () => { | ||
| const scrollToEnd = mountList({ current: true }); | ||
|
|
||
| settleList(3); | ||
|
|
||
| expect(scrollToEnd).toHaveBeenCalledTimes(3); | ||
| }); | ||
| }); |
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
40 changes: 40 additions & 0 deletions
40
apps/meteor/client/views/room/body/hooks/useIsAtBottomRef.spec.ts
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,40 @@ | ||
| import { renderHook } from '@testing-library/react'; | ||
|
|
||
| import { useIsAtBottomRef } from './useIsAtBottomRef'; | ||
| import { RoomManager } from '../../../../lib/RoomManager'; | ||
|
|
||
| jest.mock('../../../../lib/RoomManager', () => ({ | ||
| RoomManager: { getStore: jest.fn() }, | ||
| })); | ||
|
|
||
| const mockStore = (store: { atBottom: boolean } | undefined) => (RoomManager.getStore as jest.Mock).mockReturnValue(store); | ||
|
|
||
| describe('useIsAtBottomRef', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('starts at the bottom when the room has no stored position yet', () => { | ||
| mockStore(undefined); | ||
|
|
||
| const { result } = renderHook(() => useIsAtBottomRef('rid')); | ||
|
|
||
| expect(result.current.current).toBe(true); | ||
| }); | ||
|
|
||
| it('starts away from the bottom when the room was left scrolled mid-history', () => { | ||
| mockStore({ atBottom: false }); | ||
|
|
||
| const { result } = renderHook(() => useIsAtBottomRef('rid')); | ||
|
|
||
| expect(result.current.current).toBe(false); | ||
| }); | ||
|
|
||
| it('starts at the bottom when the room was left at the bottom', () => { | ||
| mockStore({ atBottom: true }); | ||
|
|
||
| const { result } = renderHook(() => useIsAtBottomRef('rid')); | ||
|
|
||
| expect(result.current.current).toBe(true); | ||
| }); | ||
| }); |
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,9 @@ | ||
| import type { RefObject } from 'react'; | ||
| import { useRef } from 'react'; | ||
|
|
||
| import { RoomManager } from '../../../../lib/RoomManager'; | ||
|
|
||
| /** Starting from `true` makes `useKeepAtBottom` pull a restored mid-history position down to the latest messages, so seed it from the position persisted for the room being opened. */ | ||
| export const useIsAtBottomRef = (rid: string): RefObject<boolean> => { | ||
| return useRef<boolean>(RoomManager.getStore(rid)?.atBottom ?? true); | ||
|
gabriellsh marked this conversation as resolved.
gabriellsh marked this conversation as resolved.
|
||
| }; | ||
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.