From ef75e8d0c22ed7a9509bab95a11a8d418b23bc7d Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Mon, 13 Apr 2026 23:57:48 -0700 Subject: [PATCH] fix: avoid stale worktree card remeasurement (#626) * fix: guard WorktreeList getItemKey against stale virtualizer cache entries When rows shrink (group collapse, worktree removal) while PR/issue data arrives asynchronously, the virtualizer's elementsCache can hold stale entries whose data-index exceeds the new rows length. measureElement calls getItemKey for those stale indices, causing "Cannot read properties of undefined (reading 'type')". Add a null guard matching the pattern already used by FileExplorer and Search virtualizers. * fix: avoid stale worktree card remeasurement --- src/renderer/src/components/sidebar/WorktreeList.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/components/sidebar/WorktreeList.tsx b/src/renderer/src/components/sidebar/WorktreeList.tsx index 33612f083..c44ee3f18 100644 --- a/src/renderer/src/components/sidebar/WorktreeList.tsx +++ b/src/renderer/src/components/sidebar/WorktreeList.tsx @@ -394,9 +394,17 @@ const WorktreeList = React.memo(function WorktreeList() { useLayoutEffect(() => { virtualizer.elementsCache.forEach((element) => { + // Why: elementsCache can hold stale DOM nodes whose data-index + // exceeds the current rows length (e.g. after a group collapse). + // Measuring a stale element feeds a detached node's size into the + // virtualizer, corrupting layout and causing overlapping cards. + const idx = parseInt(element.getAttribute('data-index') ?? '', 10) + if (Number.isNaN(idx) || idx >= rows.length) { + return + } virtualizer.measureElement(element) }) - }, [prCacheLen, issueCacheLen, virtualizer]) + }, [prCacheLen, issueCacheLen, virtualizer, rows.length]) const navigateWorktree = useCallback( (direction: 'up' | 'down') => {