fix(unread): keep the read marker when a room opens before it hydrates

This commit is contained in:
Erwan Leboucher committed 2026-09-20 20:33:08 +02:00
1 parent d5a9da4eed
commit cb0a01b293
3 files changed
+49 -6

No files matched your search

@@ -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(<RoomTimeline room={room} editor={{} as Editor} />);
expect(processedTimelineOptions.current?.readUptoEventId).toBe('$read:example.org');
});
});
describe('unread read marker (sliding sync)', () => {
+12 -1
View File
@@ -424,6 +424,7 @@ export function RoomTimeline({
const readUptoEventIdRef = useRef<string | undefined>(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(
+9 -5
View File
@@ -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,
};
};