From fa41047197991978e6419a4be4f381d5019e8540 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Sun, 12 Apr 2026 22:29:24 -0700 Subject: [PATCH] fix: stop worktree cards from overlapping in sidebar (#573) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * wip * fix: replace fragile worktree card height estimation with constant The virtualizer's estimateSize tried to predict exact pixel heights from Tailwind class constants, which drifted from actual rendered sizes and caused overlapping cards. Replace with a generous constant (120px) — measureElement/ResizeObserver already corrects to real heights, so the estimate just needs to overshoot. Delete the now-dead estimate module. --- .../src/components/sidebar/WorktreeList.tsx | 31 ++--- .../sidebar/worktree-list-estimate.test.ts | 128 ------------------ .../sidebar/worktree-list-estimate.ts | 65 --------- 3 files changed, 11 insertions(+), 213 deletions(-) delete mode 100644 src/renderer/src/components/sidebar/worktree-list-estimate.test.ts delete mode 100644 src/renderer/src/components/sidebar/worktree-list-estimate.ts diff --git a/src/renderer/src/components/sidebar/WorktreeList.tsx b/src/renderer/src/components/sidebar/WorktreeList.tsx index 4e38a3e1e..5a614c825 100644 --- a/src/renderer/src/components/sidebar/WorktreeList.tsx +++ b/src/renderer/src/components/sidebar/WorktreeList.tsx @@ -12,7 +12,6 @@ import { isGitRepoKind } from '../../../../shared/repo-kind' import { buildWorktreeComparator } from './smart-sort' import { type Row, buildRows, getGroupKeyForWorktree } from './worktree-list-groups' import { computeVisibleWorktreeIds, setVisibleWorktreeIds } from './visible-worktrees' -import { estimateRowHeight } from './worktree-list-estimate' import { useModifierHint } from '@/hooks/useModifierHint' // How long to wait after a sortEpoch bump before actually re-sorting. @@ -70,8 +69,8 @@ const WorktreeList = React.memo(function WorktreeList() { const cardProps = useAppStore((s) => s.worktreeCardProperties) - // PR cache is needed for PR-status grouping, recent sorting, search, and - // estimateSize when the PR card property is visible. + // PR cache is needed for PR-status grouping, recent sorting, search, + // and when the PR card property is visible. const prCache = useAppStore((s) => groupBy === 'pr-status' || sortBy === 'recent' || searchQuery || cardProps.includes('pr') ? s.prCache @@ -265,7 +264,7 @@ const WorktreeList = React.memo(function WorktreeList() { }) }, []) - // Build flat row list for virtualizer + // Build flat row list for rendering const rows: Row[] = useMemo( () => buildRows(groupBy, worktrees, repoMap, prCache, collapsedGroups), [groupBy, worktrees, repoMap, prCache, collapsedGroups] @@ -312,13 +311,15 @@ const WorktreeList = React.memo(function WorktreeList() { const virtualizer = useVirtualizer({ count: rows.length, getScrollElement: () => scrollRef.current, - // Dynamic height estimate — pixel constants coupled to WorktreeCard's - // Tailwind classes (see coupling comment in WorktreeCard meta section). - estimateSize: (index) => estimateRowHeight(rows[index], cardProps, repoMap, prCache), + // 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, - // Why gap instead of padding on the wrapper: the virtualizer accounts for - // the gap in its position calculations, guaranteeing consistent spacing - // between cards regardless of estimation/measurement mismatches. gap: 6, getItemKey: (index) => { const row = rows[index] @@ -326,16 +327,6 @@ const WorktreeList = React.memo(function WorktreeList() { } }) - // Why no virtualizer.measure() on prCache / cardProps changes: - // measure() resets ALL cached DOM measurements back to estimateSize() - // predictions. Because measureElement uses a ResizeObserver, visible - // items already re-measure automatically when their content changes - // (e.g. a PR row appears, card properties toggle on/off). Calling - // measure() discards those accurate measurements and replaces them - // with imprecise estimates, causing cards to overlap until the next - // paint re-measures them — which is exactly the bug users see when - // clicking a card triggers a prCache refresh. - React.useEffect(() => { if (!pendingRevealWorktreeId) { return diff --git a/src/renderer/src/components/sidebar/worktree-list-estimate.test.ts b/src/renderer/src/components/sidebar/worktree-list-estimate.test.ts deleted file mode 100644 index 2ddf2aa71..000000000 --- a/src/renderer/src/components/sidebar/worktree-list-estimate.test.ts +++ /dev/null @@ -1,128 +0,0 @@ -import { describe, expect, it } from 'vitest' -import { estimateRowHeight } from './worktree-list-estimate' -import type { Repo, Worktree } from '../../../../shared/types' -import type { Row } from './worktree-list-groups' - -const repo: Repo = { - id: 'repo-1', - path: '/tmp/orca', - displayName: 'orca', - badgeColor: '#000000', - addedAt: 0 -} - -const worktree: Worktree = { - id: 'wt-1', - repoId: repo.id, - path: '/tmp/orca-feature', - branch: 'refs/heads/feature/cool', - head: 'abc123', - isBare: false, - isMainWorktree: false, - linkedIssue: null, - linkedPR: null, - isArchived: false, - comment: '', - isUnread: false, - displayName: 'feature/cool', - sortOrder: 0, - lastActivityAt: 0 -} - -const repoMap = new Map([[repo.id, repo]]) - -function itemRow(wt: Worktree): Row { - return { type: 'item', worktree: wt, repo } -} - -describe('estimateRowHeight', () => { - it('returns 38 for header rows', () => { - const header: Row = { - type: 'header', - key: 'test', - label: 'Test', - count: 1, - icon: () => null, - tone: '' - } - expect(estimateRowHeight(header, [], repoMap, null)).toBe(38) - }) - - it('returns base height (55) for items with no metadata', () => { - expect(estimateRowHeight(itemRow(worktree), [], repoMap, null)).toBe(55) - }) - - it('adds 24px for issue row when linkedIssue is set', () => { - const wt = { ...worktree, linkedIssue: 42 } - const base = estimateRowHeight(itemRow(worktree), ['issue'], repoMap, null) - const withIssue = estimateRowHeight(itemRow(wt), ['issue'], repoMap, null) - expect(withIssue - base).toBe(24) // 16px row + 8px meta-section spacing - }) - - it('does not add issue height when cardProps excludes issue', () => { - const wt = { ...worktree, linkedIssue: 42 } - expect(estimateRowHeight(itemRow(wt), [], repoMap, null)).toBe(55) - }) - - it('adds 24px for PR row when prCache has data', () => { - const prCache = { - '/tmp/orca::feature/cool': { data: { number: 1 } } - } - const base = estimateRowHeight(itemRow(worktree), ['pr'], repoMap, null) - const withPR = estimateRowHeight(itemRow(worktree), ['pr'], repoMap, prCache) - expect(withPR - base).toBe(24) // 16px row + 8px meta-section spacing - }) - - it('does not add PR height when prCache is null', () => { - expect(estimateRowHeight(itemRow(worktree), ['pr'], repoMap, null)).toBe(55) - }) - - it('does not add PR height when prCache entry has no data', () => { - const prCache = { - '/tmp/orca::feature/cool': { data: null } - } - expect(estimateRowHeight(itemRow(worktree), ['pr'], repoMap, prCache)).toBe(55) - }) - - it('estimates comment height as fixed 2-line clamp', () => { - const wt = { ...worktree, comment: 'todo: fix bug' } - const base = estimateRowHeight(itemRow(worktree), ['comment'], repoMap, null) - const withComment = estimateRowHeight(itemRow(wt), ['comment'], repoMap, null) - // 2 lines × ceil(16.5)=33 + 4px py-0.5 + 8px meta spacing = 45 - expect(withComment - base).toBe(45) - }) - - it('uses same fixed height for long comments (clamped to 2 lines)', () => { - const short = { ...worktree, comment: 'short' } - const long = { ...worktree, comment: 'first line\nsecond line\nthird line\nfourth line' } - const hShort = estimateRowHeight(itemRow(short), ['comment'], repoMap, null) - const hLong = estimateRowHeight(itemRow(long), ['comment'], repoMap, null) - // Both clamped to 2 lines, so same height - expect(hShort).toBe(hLong) - }) - - it('stacks all metadata lines correctly', () => { - const wt = { ...worktree, linkedIssue: 42, comment: 'note' } - const prCache = { - '/tmp/orca::feature/cool': { data: { number: 1 } } - } - const h = estimateRowHeight(itemRow(wt), ['issue', 'pr', 'comment'], repoMap, prCache) - // 55 base + 16 issue + 16 pr + (33+4) comment + 8 meta spacing + 2×3 gaps = 138 - expect(h).toBe(138) - }) - - it('strips refs/heads/ prefix when building PR cache key', () => { - const wt = { ...worktree, branch: 'refs/heads/my-branch' } - const prCache = { - '/tmp/orca::my-branch': { data: { number: 5 } } - } - const h = estimateRowHeight(itemRow(wt), ['pr'], repoMap, prCache) - expect(h).toBe(79) // 55 + 16 + 8 - }) - - it('adds 1px when both status and unread are in cardProps', () => { - expect(estimateRowHeight(itemRow(worktree), ['status', 'unread'], repoMap, null)).toBe(56) - expect(estimateRowHeight(itemRow(worktree), ['status'], repoMap, null)).toBe(55) - expect(estimateRowHeight(itemRow(worktree), ['unread'], repoMap, null)).toBe(55) - }) -}) diff --git a/src/renderer/src/components/sidebar/worktree-list-estimate.ts b/src/renderer/src/components/sidebar/worktree-list-estimate.ts deleted file mode 100644 index 8a7639086..000000000 --- a/src/renderer/src/components/sidebar/worktree-list-estimate.ts +++ /dev/null @@ -1,65 +0,0 @@ -import type { Repo } from '../../../../shared/types' -import type { Row } from './worktree-list-groups' - -// Estimate the pixel height of a virtualizer row based on which metadata lines -// will render. All pixel constants are coupled to WorktreeCard's Tailwind -// classes (see the coupling comment in WorktreeCard's meta section). The -// inter-card gap is handled by the virtualizer's `gap` option, not here. -// -// Uses prCache (not wt.linkedPR) because prCache is the actual data source -// WorktreeCard checks when deciding to show the PR row. -export function estimateRowHeight( - row: Row, - cardProps: string[], - repoMap: Map, - prCache: Record | null -): number { - if (row.type === 'header') { - return 38 - } - const wt = row.worktree - - // Base: border(2) + py-2(16) + title leading-tight(15) + gap-1.5(6) - // + subtitle row with badges(16) = 55. - // Why 55 not 52: the old value omitted the 2px border and used 14px for - // the title instead of the actual 15px (12px × 1.25 leading-tight). - // When both status and unread indicators are visible, the left column - // (pt-[2px] + StatusIndicator h-3 + gap-2 + unread button size-4 = 38px) - // is 1px taller than the content column (37px). - let h = 55 - if (cardProps.includes('status') && cardProps.includes('unread')) { - h += 1 - } - - // Count meta rows (issue, PR, comment) to add per-section spacing once. - let metaCount = 0 - - if (cardProps.includes('issue') && wt.linkedIssue) { - h += 16 // py-0.5(4) + icon size-3(12) - metaCount++ - } - if (cardProps.includes('pr')) { - const repo = repoMap.get(wt.repoId) - const branch = wt.branch.replace(/^refs\/heads\//, '') - const prKey = repo && branch ? `${repo.path}::${branch}` : '' - if (prKey && prCache?.[prKey]?.data) { - h += 16 // py-0.5(4) + icon size-3(12) - metaCount++ - } - } - if (cardProps.includes('comment') && wt.comment) { - // Comment is clamped to 2 lines (line-clamp-2) in the card. Full content - // is shown in a hover card. Height: 2 lines × 16.5px + py-0.5(4px). - h += Math.ceil(2 * 16.5) + 4 - metaCount++ - } - - if (metaCount > 0) { - // Spacing before the meta section: parent flex gap-1.5(6) + mt-0.5(2). - h += 8 - // gap-[3px] between sibling meta rows. - h += (metaCount - 1) * 3 - } - - return h -}