Skip to content

Commit 1a81668

Browse files
committed
fix(MessageList): prevent message pagination too early on mount
1 parent deda536 commit 1a81668

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { renderHook } from '@testing-library/react';
2+
import { afterEach, describe, expect, it, vi } from 'vitest';
3+
4+
const { updateScrollTop } = vi.hoisted(() => ({
5+
updateScrollTop: vi.fn(),
6+
}));
7+
8+
vi.mock('../useMessageListScrollManager', () => ({
9+
useMessageListScrollManager: vi.fn(() => updateScrollTop),
10+
}));
11+
12+
import { useScrollLocationLogic } from '../useScrollLocationLogic';
13+
14+
describe('useScrollLocationLogic', () => {
15+
afterEach(() => {
16+
vi.clearAllMocks();
17+
});
18+
19+
it('seeds scroll manager with the initial DOM scrollTop', () => {
20+
const listElement = document.createElement('div');
21+
listElement.scrollTop = 249;
22+
23+
renderHook(() =>
24+
useScrollLocationLogic({
25+
hasMoreNewer: true,
26+
listElement,
27+
loadMoreScrollThreshold: 250,
28+
messages: [],
29+
suppressAutoscroll: false,
30+
}),
31+
);
32+
33+
expect(updateScrollTop).toHaveBeenCalledWith(249, null);
34+
});
35+
});

src/components/MessageList/hooks/MessageList/useScrollLocationLogic.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,15 @@ export const useScrollLocationLogic = (params: UseScrollLocationLogicParams) =>
399399
showNewMessages: () => setHasNewMessages(true),
400400
});
401401

402+
useLayoutEffect(() => {
403+
if (!listElement) return;
404+
405+
const initialScrollTop = listElement.scrollTop;
406+
previousScrollTopRef.current = initialScrollTop;
407+
// Keep pagination mode selection in sync with the actual initial DOM position.
408+
updateScrollTop(initialScrollTop, null);
409+
}, [listElement, updateScrollTop]);
410+
402411
useLayoutEffect(() => {
403412
previousHasMoreNewerRef.current = hasMoreNewer;
404413
}, [hasMoreNewer]);

0 commit comments

Comments
 (0)