fix(threads): thread drawer empty with classic sync

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.
This commit is contained in:
Evie Gauthier committed 2026-03-17 16:17:47 -04:00
1 parent ab138a11b5
commit 2a63f596fe
1 file changed
+37 -2
+37 -2
View File
@@ -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()