From ed981ad4d8948da138ee01ea44753d27f85b3609 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Tue, 14 Apr 2026 00:30:34 -0700 Subject: [PATCH] Fix sidebar virtualizer layout after worktree deletes (#627) --- .../src/components/sidebar/WorktreeList.tsx | 667 +++++++++--------- 1 file changed, 339 insertions(+), 328 deletions(-) diff --git a/src/renderer/src/components/sidebar/WorktreeList.tsx b/src/renderer/src/components/sidebar/WorktreeList.tsx index c44ee3f18..4b89a2110 100644 --- a/src/renderer/src/components/sidebar/WorktreeList.tsx +++ b/src/renderer/src/components/sidebar/WorktreeList.tsx @@ -44,6 +44,327 @@ function getWorktreeOptionId(worktreeId: string): string { return `worktree-list-option-${encodeURIComponent(worktreeId)}` } +type VirtualizedWorktreeViewportProps = { + rows: Row[] + activeWorktreeId: string | null + setActiveWorktree: (worktreeId: string | null) => void + groupBy: 'none' | 'repo' | 'pr-status' + toggleGroup: (key: string) => void + collapsedGroups: Set + handleCreateForRepo: (repoId: string) => void + hintByWorktreeId: Map | null + activeModal: string + pendingRevealWorktreeId: string | null + clearPendingRevealWorktreeId: () => void + worktrees: Worktree[] + repoMap: Map + prCache: Record | null +} + +const VirtualizedWorktreeViewport = React.memo(function VirtualizedWorktreeViewport({ + rows, + activeWorktreeId, + setActiveWorktree, + groupBy, + toggleGroup, + collapsedGroups, + handleCreateForRepo, + hintByWorktreeId, + activeModal, + pendingRevealWorktreeId, + clearPendingRevealWorktreeId, + worktrees, + repoMap, + prCache +}: VirtualizedWorktreeViewportProps) { + const scrollRef = useRef(null) + const activeWorktreeRowIndex = useMemo( + () => rows.findIndex((row) => row.type === 'item' && row.worktree.id === activeWorktreeId), + [rows, activeWorktreeId] + ) + + const virtualizer = useVirtualizer({ + count: rows.length, + getScrollElement: () => scrollRef.current, + estimateSize: () => 120, + overscan: 10, + gap: 6, + getItemKey: (index) => { + const row = rows[index] + if (!row) { + return `__stale_${index}` + } + return row.type === 'header' ? `hdr:${row.key}` : `wt:${row.worktree.id}` + } + }) + + React.useEffect(() => { + if (!pendingRevealWorktreeId) { + return + } + + if (groupBy !== 'none') { + const targetWorktree = worktrees.find((w) => w.id === pendingRevealWorktreeId) + if (targetWorktree) { + const groupKey = getGroupKeyForWorktree(groupBy, targetWorktree, repoMap, prCache) + if (groupKey && collapsedGroups.has(groupKey)) { + toggleGroup(groupKey) + } + } + } + + requestAnimationFrame(() => { + const targetIndex = rows.findIndex( + (row) => row.type === 'item' && row.worktree.id === pendingRevealWorktreeId + ) + if (targetIndex !== -1) { + virtualizer.scrollToIndex(targetIndex, { align: 'center' }) + } + clearPendingRevealWorktreeId() + }) + }, [ + pendingRevealWorktreeId, + groupBy, + worktrees, + repoMap, + prCache, + rows, + virtualizer, + clearPendingRevealWorktreeId, + toggleGroup, + collapsedGroups + ]) + + const prCacheLen = useAppStore((s) => Object.keys(s.prCache).length) + const issueCacheLen = useAppStore((s) => Object.keys(s.issueCache).length) + + useLayoutEffect(() => { + virtualizer.elementsCache.forEach((element) => { + const idx = parseInt(element.getAttribute('data-index') ?? '', 10) + if (Number.isNaN(idx) || idx >= rows.length) { + return + } + virtualizer.measureElement(element) + }) + }, [prCacheLen, issueCacheLen, virtualizer, rows.length]) + + const navigateWorktree = useCallback( + (direction: 'up' | 'down') => { + const worktreeRows = rows.filter( + (r): r is Extract => r.type === 'item' + ) + if (worktreeRows.length === 0) { + return + } + + let nextIndex = 0 + const currentIndex = worktreeRows.findIndex((r) => r.worktree.id === activeWorktreeId) + + if (currentIndex !== -1) { + if (direction === 'up') { + nextIndex = currentIndex - 1 + if (nextIndex < 0) { + nextIndex = worktreeRows.length - 1 + } + } else { + nextIndex = currentIndex + 1 + if (nextIndex >= worktreeRows.length) { + nextIndex = 0 + } + } + } + + const nextWorktreeId = worktreeRows[nextIndex].worktree.id + setActiveWorktree(nextWorktreeId) + + const rowIndex = rows.findIndex((r) => r.type === 'item' && r.worktree.id === nextWorktreeId) + if (rowIndex !== -1) { + virtualizer.scrollToIndex(rowIndex, { align: 'auto' }) + } + }, + [rows, activeWorktreeId, setActiveWorktree, virtualizer] + ) + + useEffect(() => { + const handleKeyDown = (e: KeyboardEvent) => { + if (activeModal !== 'none' || isEditableTarget(e.target)) { + return + } + + const mod = navigator.userAgent.includes('Mac') + ? e.metaKey && !e.ctrlKey + : e.ctrlKey && !e.metaKey + if (mod && !e.shiftKey && e.key === '0') { + scrollRef.current?.focus() + e.preventDefault() + return + } + + if (mod && e.shiftKey && (e.key === 'ArrowUp' || e.key === 'ArrowDown')) { + navigateWorktree(e.key === 'ArrowUp' ? 'up' : 'down') + e.preventDefault() + } + } + + window.addEventListener('keydown', handleKeyDown, { capture: true }) + return () => window.removeEventListener('keydown', handleKeyDown, { capture: true }) + }, [activeModal, navigateWorktree]) + + const handleContainerKeyDown = useCallback( + (e: React.KeyboardEvent) => { + if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { + if (e.target !== e.currentTarget) { + return + } + navigateWorktree(e.key === 'ArrowUp' ? 'up' : 'down') + e.preventDefault() + } else if (e.key === 'Enter') { + const helper = document.querySelector( + '.xterm-helper-textarea' + ) as HTMLTextAreaElement | null + if (helper) { + helper.focus() + } + e.preventDefault() + } + }, + [navigateWorktree] + ) + + const virtualItems = virtualizer.getVirtualItems() + const activeDescendantId = + activeWorktreeId != null && + activeWorktreeRowIndex !== -1 && + virtualItems.some((item) => item.index === activeWorktreeRowIndex) + ? getWorktreeOptionId(activeWorktreeId) + : undefined + + return ( +
+
+ {virtualItems.map((vItem) => { + const row = rows[vItem.index] + + if (row.type === 'header') { + return ( +
+ + + + {row.repo && !isGitRepoKind(row.repo) + ? `${row.label} is opened as a folder` + : `Create worktree for ${row.label}`} + + + ) : null} + +
+ +
+ +
+ ) + } + + return ( +
+ +
+ ) + })} +
+
+ ) +}) + const WorktreeList = React.memo(function WorktreeList() { // ── Granular selectors (each is a primitive or shallow-stable ref) ── const worktreesByRepo = useAppStore((s) => s.worktreesByRepo) @@ -126,7 +447,6 @@ const WorktreeList = React.memo(function WorktreeList() { return () => clearTimeout(timer) }, [sortEpoch, debouncedSortEpoch, worktreeCount]) - const scrollRef = useRef(null) // Why a latching ref: we need to distinguish "app just started, no PTYs // have spawned yet" from "user closed all terminals mid-session." The // former should use the persisted sortOrder; the latter should keep using @@ -269,6 +589,7 @@ const WorktreeList = React.memo(function WorktreeList() { () => buildRows(groupBy, worktrees, repoMap, prCache, collapsedGroups), [groupBy, worktrees, repoMap, prCache, collapsedGroups] ) + const viewportResetKey = `${groupBy}:${rows.length}` // Why: derive the rendered item order from the post-buildRows() row list, // not the flat `worktrees` array, because grouping (groupBy: 'repo' or @@ -282,11 +603,6 @@ const WorktreeList = React.memo(function WorktreeList() { .map((r) => r.worktree), [rows] ) - const activeWorktreeRowIndex = useMemo( - () => rows.findIndex((row) => row.type === 'item' && row.worktree.id === activeWorktreeId), - [rows, activeWorktreeId] - ) - // Why layout effect instead of effect: the global Cmd/Ctrl+1–9 key handler // can fire immediately after React commits the new grouped/collapsed order. // Publishing after paint leaves a brief window where the sidebar shows the @@ -307,194 +623,6 @@ const WorktreeList = React.memo(function WorktreeList() { return map }, [showHints, renderedWorktrees]) - // ── TanStack Virtual ────────────────────────────────────────── - const virtualizer = useVirtualizer({ - count: rows.length, - getScrollElement: () => scrollRef.current, - // Why a constant instead of per-row pixel math: the previous estimateSize - // tried to predict exact card heights from Tailwind class constants, but - // those estimates drifted from actual rendered sizes — causing overlapping - // cards whenever content (PR rows, comments, badges) didn't match the - // prediction. A generous constant that overshoots the tallest possible - // card means items may briefly have extra space before measureElement - // corrects them, which is invisible compared to overlapping. - estimateSize: () => 120, - overscan: 10, - 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}` - } - }) - - React.useEffect(() => { - if (!pendingRevealWorktreeId) { - return - } - - // Uncollapse the group containing the target worktree - if (groupBy !== 'none') { - const targetWorktree = worktrees.find((w) => w.id === pendingRevealWorktreeId) - if (targetWorktree) { - const groupKey = getGroupKeyForWorktree(groupBy, targetWorktree, repoMap, prCache) - if (groupKey) { - setCollapsedGroups((prev) => { - if (!prev.has(groupKey)) { - return prev - } - const next = new Set(prev) - next.delete(groupKey) - return next - }) - } - } - } - - // Scroll to the target after the group uncollapse re-render settles - requestAnimationFrame(() => { - const targetIndex = rows.findIndex( - (row) => row.type === 'item' && row.worktree.id === pendingRevealWorktreeId - ) - if (targetIndex !== -1) { - virtualizer.scrollToIndex(targetIndex, { align: 'center' }) - } - clearPendingRevealWorktreeId() - }) - }, [ - pendingRevealWorktreeId, - groupBy, - worktrees, - repoMap, - prCache, - rows, - virtualizer, - clearPendingRevealWorktreeId - ]) - - // ── Async content re-measurement ────────────────────────────── - // PR and issue data arrive asynchronously after cards mount. When a - // card gains an issue/PR row its height changes, and the virtualizer's - // ResizeObserver should reposition subsequent items. In practice the - // observer can miss the resize when it coincides with React's batched - // rendering — leaving stale measurements and overlapping cards. - // - // Why useLayoutEffect: runs after React commits the new DOM (cards now - // include the issue row) but before the browser paints, so the user - // never sees a frame of overlap. - const prCacheLen = useAppStore((s) => Object.keys(s.prCache).length) - const issueCacheLen = useAppStore((s) => Object.keys(s.issueCache).length) - - 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, rows.length]) - - const navigateWorktree = useCallback( - (direction: 'up' | 'down') => { - const worktreeRows = rows.filter( - (r): r is Extract => r.type === 'item' - ) - if (worktreeRows.length === 0) { - return - } - - let nextIndex = 0 - const currentIndex = worktreeRows.findIndex((r) => r.worktree.id === activeWorktreeId) - - if (currentIndex !== -1) { - if (direction === 'up') { - nextIndex = currentIndex - 1 - if (nextIndex < 0) { - nextIndex = worktreeRows.length - 1 - } - } else { - nextIndex = currentIndex + 1 - if (nextIndex >= worktreeRows.length) { - nextIndex = 0 - } - } - } - - const nextWorktreeId = worktreeRows[nextIndex].worktree.id - setActiveWorktree(nextWorktreeId) - - const rowIndex = rows.findIndex((r) => r.type === 'item' && r.worktree.id === nextWorktreeId) - if (rowIndex !== -1) { - virtualizer.scrollToIndex(rowIndex, { align: 'auto' }) - } - }, - [rows, activeWorktreeId, setActiveWorktree, virtualizer] - ) - - useEffect(() => { - const handleKeyDown = (e: KeyboardEvent) => { - // Why: these shortcuts are for the sidebar navigation surface. Once a - // modal opens or focus is inside an editor/input, the host should leave - // the keystroke alone so overlays and text editing keep native behavior. - if (activeModal !== 'none' || isEditableTarget(e.target)) { - return - } - - const mod = navigator.userAgent.includes('Mac') - ? e.metaKey && !e.ctrlKey - : e.ctrlKey && !e.metaKey - if (mod && !e.shiftKey && e.key === '0') { - scrollRef.current?.focus() - e.preventDefault() - return - } - - if (mod && e.shiftKey && (e.key === 'ArrowUp' || e.key === 'ArrowDown')) { - navigateWorktree(e.key === 'ArrowUp' ? 'up' : 'down') - e.preventDefault() - } - } - - window.addEventListener('keydown', handleKeyDown, { capture: true }) - return () => window.removeEventListener('keydown', handleKeyDown, { capture: true }) - }, [activeModal, navigateWorktree]) - - const handleContainerKeyDown = useCallback( - (e: React.KeyboardEvent) => { - if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { - // Why: only capture bare arrow keys when the list container itself is - // focused. If focus is on an inner input or button, arrow keys should - // perform their native function (e.g. cursor movement in text fields). - if (e.target !== e.currentTarget) { - return - } - navigateWorktree(e.key === 'ArrowUp' ? 'up' : 'down') - e.preventDefault() - } else if (e.key === 'Enter') { - const helper = document.querySelector( - '.xterm-helper-textarea' - ) as HTMLTextAreaElement | null - if (helper) { - helper.focus() - } - e.preventDefault() - } - }, - [navigateWorktree] - ) - const handleCreateForRepo = useCallback( (repoId: string) => { openModal('create-worktree', { preselectedRepoId: repoId }) @@ -530,141 +658,24 @@ const WorktreeList = React.memo(function WorktreeList() { ) } - const virtualItems = virtualizer.getVirtualItems() - const activeDescendantId = - activeWorktreeId != null && - activeWorktreeRowIndex !== -1 && - virtualItems.some((item) => item.index === activeWorktreeRowIndex) - ? getWorktreeOptionId(activeWorktreeId) - : undefined - return ( -
-
- {virtualItems.map((vItem) => { - const row = rows[vItem.index] - - if (row.type === 'header') { - return ( -
- - - - {row.repo && !isGitRepoKind(row.repo) - ? `${row.label} is opened as a folder` - : `Create worktree for ${row.label}`} - - - ) : null} - -
- -
- -
- ) - } - - return ( -
- -
- ) - })} -
-
+ ) })