From 1114c047d5c0dd2e8f5dcaa234fbdafdabe333ee Mon Sep 17 00:00:00 2001 From: Evie Gauthier Date: Fri, 27 Mar 2026 23:17:03 -0400 Subject: [PATCH] fix: resolve Copilot code review comments on PR #529 - Clear initialScrollTimerRef when room changes to prevent stale timer from firing in a new room after navigation (RoomTimeline.tsx) - Scope URL preview cache by MatrixClient to prevent cross-account deduplication when multiple clients/sessions exist (UrlPreviewCard.tsx) - Delete cache entries after promise settles (both success/rejection) to prevent unbounded cache growth over long sessions (UrlPreviewCard.tsx) - Fix pagination token direction check: use appropriate timeline end and token direction based on pagination direction (forward/backward) instead of always checking first timeline's backward token (useTimelineSync.ts) --- .../components/url-preview/UrlPreviewCard.tsx | 28 ++++++++++++++----- src/app/features/room/RoomTimeline.tsx | 4 +++ src/app/hooks/timeline/useTimelineSync.ts | 6 ++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/app/components/url-preview/UrlPreviewCard.tsx b/src/app/components/url-preview/UrlPreviewCard.tsx index edc3f0f9d..2b5d6b87b 100644 --- a/src/app/components/url-preview/UrlPreviewCard.tsx +++ b/src/app/components/url-preview/UrlPreviewCard.tsx @@ -16,10 +16,23 @@ const linkStyles = { color: color.Success.Main }; // Module-level in-flight deduplication: prevents N+1 concurrent requests when a // large event batch renders many UrlPreviewCard instances for the same URL. -// Keyed by URL only (not ts) — the same URL shows the same preview regardless -// of which message referenced it. Rejected promises are evicted so a later -// render can retry after network recovery. -const previewRequestCache = new Map>(); +// Scoped by MatrixClient to avoid cross-account dedup if multiple clients exist. +// Inner cache keyed by URL only (not ts) — the same URL shows the same preview +// regardless of which message referenced it. Promises are evicted after settling +// so a later render can retry after network recovery. +const previewRequestCache = new WeakMap< + any, + Map> +>(); + +const getClientCache = (mx: any): Map> => { + let clientCache = previewRequestCache.get(mx); + if (!clientCache) { + clientCache = new Map(); + previewRequestCache.set(mx, clientCache); + } + return clientCache; +}; const openMediaInNewTab = async (url: string | undefined) => { if (!url) { @@ -41,11 +54,12 @@ export const UrlPreviewCard = as<'div', { url: string; ts: number; mediaType?: s const [previewStatus, loadPreview] = useAsyncCallback( useCallback(() => { if (isDirect) return Promise.resolve(null); - const cached = previewRequestCache.get(url); + const clientCache = getClientCache(mx); + const cached = clientCache.get(url); if (cached !== undefined) return cached; const urlPreview = mx.getUrlPreview(url, ts); - previewRequestCache.set(url, urlPreview); - urlPreview.catch(() => previewRequestCache.delete(url)); + clientCache.set(url, urlPreview); + urlPreview.finally(() => clientCache.delete(url)); return urlPreview; }, [url, ts, mx, isDirect]) ); diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index 95e3f105a..63af8c960 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -224,6 +224,10 @@ export function RoomTimeline({ mountScrollWindowRef.current = Date.now() + 3000; currentRoomIdRef.current = room.roomId; pendingReadyRef.current = false; + if (initialScrollTimerRef.current !== undefined) { + clearTimeout(initialScrollTimerRef.current); + initialScrollTimerRef.current = undefined; + } setIsReady(false); } diff --git a/src/app/hooks/timeline/useTimelineSync.ts b/src/app/hooks/timeline/useTimelineSync.ts index 07abffb10..16127b6e5 100644 --- a/src/app/hooks/timeline/useTimelineSync.ts +++ b/src/app/hooks/timeline/useTimelineSync.ts @@ -192,9 +192,11 @@ const useTimelinePagination = ( const fetched = countAfter - countBefore; if (fetched > 0 && fetched < 5) { + const checkTimeline = backwards ? freshLTimelines[0] : freshLTimelines[freshLTimelines.length - 1]; + const checkDirection = backwards ? Direction.Backward : Direction.Forward; const stillHasToken = - typeof getLinkedTimelines(freshLTimelines[0])[0]?.getPaginationToken( - Direction.Backward + typeof getLinkedTimelines(checkTimeline)[0]?.getPaginationToken( + checkDirection ) === 'string'; if (stillHasToken) { // Release lock so inner paginate can claim it, then mark continuing