From 2a63f596fe4a129f1ce4488f65abe52bdd434720 Mon Sep 17 00:00:00 2001 From: Evie Gauthier Date: Tue, 17 Mar 2026 15:20:51 -0400 Subject: [PATCH] fix(threads): thread drawer empty with classic sync MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With classic sync, RoomViewHeader creates Thread objects via room.createThread(id, rootEvent, [], false) — passing no initialEvents. This means thread.events starts as just [rootEvent] (or empty). Two bugs resulted: 1. The gate `if (fromThread.length > 0)` blocked the live-timeline fallback. Since thread.events = [rootEvent], length was 1 (truthy), but after filtering the root out the array was empty — yielding zero replies even though the events were present in the main room timeline. Fix: compute the filtered array first, then gate on its length so the fallback is reached when the thread object has no actual replies yet. 2. Even after #1, subsequent renders and read-receipt logic used thread.events (empty) rather than the live timeline. Fix: add a mount-time useEffect that backfills matching events from the unfiltered live timeline into the Thread object via thread.addEvents() so the authoritative source is populated for future interactions. --- src/app/features/room/ThreadDrawer.tsx | 39 ++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/app/features/room/ThreadDrawer.tsx b/src/app/features/room/ThreadDrawer.tsx index 00957bc3..3a9d6bb9 100644 --- a/src/app/features/room/ThreadDrawer.tsx +++ b/src/app/features/room/ThreadDrawer.tsx @@ -398,6 +398,33 @@ export function ThreadDrawer({ room, threadRootId, onClose, overlay }: ThreadDra const rootEvent = room.findEventById(threadRootId); + // When the drawer is opened with classic sync, room.createThread() may have + // been called with empty initialEvents so thread.events only has the root. + // Backfill events from the main room timeline into the Thread object so the + // authoritative source is populated for subsequent renders and receipts. + useEffect(() => { + const thread = room.getThread(threadRootId); + if (!thread) return; + const hasRepliesInThread = thread.events.some( + (ev) => ev.getId() !== threadRootId && !reactionOrEditEvent(ev) + ); + if (hasRepliesInThread) return; // already populated, nothing to do + + const liveEvents = room + .getUnfilteredTimelineSet() + .getLiveTimeline() + .getEvents() + .filter( + (ev) => + ev.threadRootId === threadRootId && + ev.getId() !== threadRootId && + !reactionOrEditEvent(ev) + ); + if (liveEvents.length > 0) { + thread.addEvents(liveEvents, false); + } + }, [room, threadRootId]); + // Re-render when new thread events arrive (including reactions via ThreadEvent.Update). useEffect(() => { const isEventInThread = (mEvent: MatrixEvent): boolean => { @@ -484,11 +511,19 @@ export function ThreadDrawer({ room, threadRootId, onClose, overlay }: ThreadDra // Use the Thread object if available (authoritative source with full history). // Fall back to scanning the live room timeline for local echoes and the // window before the Thread object is registered by the SDK. + // NOTE: With classic sync the Thread object is created via room.createThread() + // with empty initialEvents, so thread.events may only contain the root event. + // We must filter first, then decide whether to fall back — otherwise a thread + // whose events array consists solely of the root event (length === 1) prevents + // the live-timeline fallback from running, and the drawer shows nothing. const replyEvents: MatrixEvent[] = (() => { const thread = room.getThread(threadRootId); const fromThread = thread?.events ?? []; - if (fromThread.length > 0) { - return fromThread.filter((ev) => ev.getId() !== threadRootId && !reactionOrEditEvent(ev)); + const filteredFromThread = fromThread.filter( + (ev) => ev.getId() !== threadRootId && !reactionOrEditEvent(ev) + ); + if (filteredFromThread.length > 0) { + return filteredFromThread; } return room .getUnfilteredTimelineSet()