From 5bb03eb8f4390c42e32e0227bb2b2fbaaaad1eb2 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Mon, 13 Apr 2026 23:31:50 -0700 Subject: [PATCH] fix: guard WorktreeList getItemKey against stale virtualizer cache entries (#625) 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. --- src/renderer/src/components/sidebar/WorktreeList.tsx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/renderer/src/components/sidebar/WorktreeList.tsx b/src/renderer/src/components/sidebar/WorktreeList.tsx index f3ac586a9..33612f083 100644 --- a/src/renderer/src/components/sidebar/WorktreeList.tsx +++ b/src/renderer/src/components/sidebar/WorktreeList.tsx @@ -323,6 +323,14 @@ const WorktreeList = React.memo(function WorktreeList() { gap: 6, getItemKey: (index) => { const row = rows[index] + // Why: when rows shrink (group collapse, worktree removal) the + // virtualizer's elementsCache can still hold stale entries whose + // data-index exceeds the new rows length. measureElement calls + // getItemKey for those stale indices, so we need a fallback to + // avoid "Cannot read properties of undefined (reading 'type')". + if (!row) { + return `__stale_${index}` + } return row.type === 'header' ? `hdr:${row.key}` : `wt:${row.worktree.id}` } })