diff --git a/src/app/features/room/RoomTimeline.test.tsx b/src/app/features/room/RoomTimeline.test.tsx index 79f55f4ac..e3daf042e 100644 --- a/src/app/features/room/RoomTimeline.test.tsx +++ b/src/app/features/room/RoomTimeline.test.tsx @@ -1117,6 +1117,34 @@ describe('unread read marker (normal sync)', () => { await act(() => new Promise((resolve) => requestAnimationFrame(resolve))); expect(markAsReadMock).not.toHaveBeenCalled(); }); + + it('does not mark the room read before the initial scroll settles', async () => { + getRoomUnreadInfoMock.mockReturnValue(undefined); + windowFocused.current = true; + + renderTimeline(); + await act(() => new Promise((resolve) => requestAnimationFrame(resolve))); + + expect(markAsReadMock).not.toHaveBeenCalled(); + }); + + it('resolves the read marker when the boundary loads after mount', async () => { + getRoomUnreadInfoMock.mockReturnValue(undefined); + const { rerender } = renderTimeline(); + await settleInitialScroll(); + + expect(processedTimelineOptions.current?.readUptoEventId).toBeUndefined(); + + getRoomUnreadInfoMock.mockReturnValue({ + readUptoEventId: '$read:example.org', + inLiveTimeline: true, + scrollTo: false, + }); + timelineSync.eventsLength = 2; + rerender(); + + expect(processedTimelineOptions.current?.readUptoEventId).toBe('$read:example.org'); + }); }); describe('unread read marker (sliding sync)', () => { diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index 498fe6ad9..9632017a9 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -424,6 +424,7 @@ export function RoomTimeline({ const readUptoEventIdRef = useRef(undefined); if (unreadInfo) readUptoEventIdRef.current = unreadInfo.readUptoEventId; + const unreadResolvedRef = useRef(unreadInfo !== undefined); const hideReadsRef = useRef(hideReads); hideReadsRef.current = hideReads; @@ -895,6 +896,15 @@ export function RoomTimeline({ setAtBottom(true); }, [eventId, focusLiveTimeline, setAtBottom]); + useEffect(() => { + if (unreadResolvedRef.current) return; + const resolved = getRoomUnreadInfo(room, !isReady); + if (!resolved) return; + unreadResolvedRef.current = true; + readUptoEventIdRef.current = resolved.readUptoEventId; + setUnreadInfo(resolved); + }, [room, isReady, timelineSync.eventsLength]); + useEffect(() => { if (eventId) return; if (isReady) return; @@ -1048,6 +1058,7 @@ export function RoomTimeline({ const tryAutoMarkAsRead = useCallback(() => { if (isInactivePanel) return; // Don't clear unread while room is behind the list + if (!isReady) return; if (!atBottomRef.current) return; if (!readUptoEventIdRef.current) { requestAnimationFrame(() => markAsRead(mx, room.roomId, hideReads)); @@ -1058,7 +1069,7 @@ export function RoomTimeline({ if (latestTimeline === room.getLiveTimeline()) { requestAnimationFrame(() => markAsRead(mx, room.roomId, hideReads)); } - }, [mx, room, hideReads, isInactivePanel]); + }, [mx, room, hideReads, isInactivePanel, isReady]); useDocumentFocusChange( useCallback( diff --git a/src/app/utils/timeline.ts b/src/app/utils/timeline.ts index 1c4b6df15..6756b10be 100644 --- a/src/app/utils/timeline.ts +++ b/src/app/utils/timeline.ts @@ -1,6 +1,6 @@ import type { EventTimeline, MatrixEvent, Room } from '$types/matrix-sdk'; import { Direction } from '$types/matrix-sdk'; -import { roomHaveNotification, roomHaveUnread } from '$utils/room/unread'; +import { getFullyReadEventId, roomHaveNotification, roomHaveUnread } from '$utils/room/unread'; export const PAGINATION_LIMIT = 60; @@ -100,9 +100,8 @@ export const getEmptyTimeline = () => ({ }); export const getRoomUnreadInfo = (room: Room, scrollTo = false) => { - if (!roomHaveNotification(room) && !roomHaveUnread(room.client, room)) return undefined; - - const readUptoEventId = room.getEventReadUpTo(room.client.getUserId() ?? ''); + const readUptoEventId = + room.getEventReadUpTo(room.client.getUserId() ?? '') ?? getFullyReadEventId(room); if (!readUptoEventId) return undefined; const evtTimeline = getEventTimeline(room, readUptoEventId); @@ -116,9 +115,14 @@ export const getRoomUnreadInfo = (room: Room, scrollTo = false) => { } const latestTimeline = getFirstLinkedTimeline(evtTimeline, Direction.Forward); + const inLiveTimeline = latestTimeline === room.getLiveTimeline(); + if (inLiveTimeline && !roomHaveNotification(room) && !roomHaveUnread(room.client, room)) { + return undefined; + } + return { readUptoEventId, - inLiveTimeline: latestTimeline === room.getLiveTimeline(), + inLiveTimeline, scrollTo, }; };