Skip to content

refactor(sidebar): extract WorktreeList into focused modules - #14443

Closed
AmethystLiang wants to merge 4 commits into
mainfrom
pure-extract-worktree-list
Closed

AmethystLiang wants to merge 4 commits into
mainfrom
pure-extract-worktree-list

Conversation

@AmethystLiang

Copy link
Copy Markdown
Contributor

Problem

WorktreeList.tsx has grown to ~2000 lines, mixing concerns across hooks, utilities, components, and state logic. This makes the file hard to navigate, test, and maintain.

Solution

Extract WorktreeList into ~30 focused modules:

  • Custom hooks (use-worktree-*) for specific behaviors
  • Utility files (worktree-list-*) for model logic
  • React components (WorktreeList*) for rendering
  • Helper files for DOM activation, drag operations, and keyboard handling

No behavior changes; same API and output. Tests updated to read from multiple files where needed.

Testing

  • Tests updated for multi-file test fixtures (project-group-header-dom, worktree-keyboard-cycle, worktree-list-groups)
  • All changes are extractions with no logic modifications
  • Verify pnpm typecheck and pnpm test pass

Move useReusedArrayIdentity to its own module to reduce duplication across
WorktreeList.tsx and use-worktree-list-smart-order.ts. Also removes unused
ref assignments in the viewport function and adds a TODO for scroll-origin
migration work.
@coderabbitai

coderabbitai Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds a virtualized worktree sidebar implementation. It introduces row and section-header models, folder-path status handling, workspace and auxiliary rows, keyboard navigation, reveal workflows, smart ordering, review refresh tracking, and row measurement. It also adds native and pointer drag interactions for reordering, status moves, pinning, lineage changes, workspace-board drops, previews, autoscrolling, and cleanup.

Merge Risk: 🟡 Moderate · up to 820bf

This refactor still has a concrete accessibility issue that can cause screen readers to misidentify the host actions menu, along with a smaller transient active-row rendering bug; unresolved localization, highlight cleanup, and avoidable refresh work add bounded follow-up risk. The PR should not merge until the accessibility issue is fixed or explicitly accepted.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 1.30% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and clearly describes the main refactoring activity: extracting a large monolithic WorktreeList component into focused, modular files.
Description check ✅ Passed The description includes ELI5, Problem, Solution, and Testing sections. It clearly explains the refactoring scope and motivation. However, Visual Proof and the checklist are incomplete or unchecked.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 2
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 9

🧹 Nitpick comments (8)
src/renderer/src/components/sidebar/use-worktree-list-workspace-reveal.ts (1)

184-204: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Duplicated reveal retry policy in both reveal hooks. Both hooks implement the same retry counter, the same hardcoded cap of 8, and the same tick increment, differing only in the ref key field (worktreeId versus rowKey). Extract one helper with a named limit constant so the two paths cannot drift.

  • src/renderer/src/components/sidebar/use-worktree-list-workspace-reveal.ts#L184-L204: replace the inline counter with a call to the shared retry helper keyed by worktreeId.
  • src/renderer/src/components/sidebar/use-worktree-list-sidebar-row-reveal.ts#L88-L105: replace the inline counter with the same helper keyed by rowKey.
src/renderer/src/components/sidebar/use-worktree-list-virtualizer.ts (1)

138-145: 📐 Maintainability & Code Quality | 🔵 Trivial

Track the scroll-origin migration TODO.

The TODO records that wall-clock suppression misclassifies under jank. Do you want me to open an issue to track the migration to programmaticScrollMarks and restoreSignal?

src/renderer/src/components/sidebar/use-worktree-virtual-row-measurement.ts (1)

24-28: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low value

Drop the redundant renderRowKeySignature dependency.

activeRenderRowKeys is recreated whenever renderRows identity changes, so it already retriggers the layout effect. renderRowKeySignature adds a full string join per rows change without adding retrigger coverage.

♻️ Proposed refactor
-  const renderRowKeySignature = useMemo(
-    () => renderRows.map(getRenderRowKey).join('\n'),
-    [renderRows]
-  )
   const activeRenderRowKeys = useMemo(() => new Set(renderRows.map(getRenderRowKey)), [renderRows])
   }, [
     activeRenderRowKeys,
     prCacheLen,
     issueCacheLen,
     measureMountedRows,
-    renderRowKeySignature,
     virtualizer
   ])

Also applies to: 55-62

src/renderer/src/components/sidebar/use-worktree-pointer-drag-flush.ts (1)

118-131: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Extract the repeated setWorktreeDragState reset into a local function.

Four blocks repeat the same reset logic and the same equality check. Three more blocks (lines 186-196, 234-244, 270-280) repeat the same preview-apply logic. The duplication invites drift between paths, and the file exists to reduce the size of WorktreeList.tsx.

Define two module-level helpers and call them from each path.

♻️ Proposed extraction
// Add near the top of the module.
function resetWorktreeDragRowState(
  pointerY: number
): (prev: WorktreeRowDragState) => WorktreeRowDragState {
  return (prev) =>
    prev.dropIndex === null &&
    prev.dropIndicatorY === null &&
    prev.pointerY === pointerY &&
    prev.previewOffsetsByWorktreeId.size === 0
      ? prev
      : {
          ...prev,
          dropIndex: null,
          dropIndicatorY: null,
          previewOffsetsByWorktreeId: EMPTY_WORKTREE_DRAG_PREVIEW_OFFSETS,
          pointerY
        }
}

function applyWorktreeDropPreview(
  drop: WorktreeSidebarDropPreview,
  pointerY: number
): (prev: WorktreeRowDragState) => WorktreeRowDragState {
  return (prev) =>
    prev.dropIndex === drop.dropIndex &&
    prev.dropIndicatorY === drop.dropIndicatorY &&
    prev.pointerY === pointerY &&
    areWorktreeDragPreviewOffsetsEqual(
      prev.previewOffsetsByWorktreeId,
      drop.previewOffsetsByWorktreeId
    )
      ? prev
      : { ...prev, ...drop, pointerY }
}

Each call site then becomes one line:

-      setWorktreeDragState((prev) =>
-        prev.dropIndex === null &&
-        prev.dropIndicatorY === null &&
-        prev.pointerY === drag.currentY &&
-        prev.previewOffsetsByWorktreeId.size === 0
-          ? prev
-          : {
-              ...prev,
-              dropIndex: null,
-              dropIndicatorY: null,
-              previewOffsetsByWorktreeId: EMPTY_WORKTREE_DRAG_PREVIEW_OFFSETS,
-              pointerY: drag.currentY
-            }
-      )
+      setWorktreeDragState(resetWorktreeDragRowState(drag.currentY))

The same two helpers also apply to the duplicated blocks in use-worktree-native-drag-handlers.ts and use-worktree-native-drag-start.ts, but those variants omit the pointerY comparison. Align them only after you confirm the intended behavior.

Also applies to: 151-164, 202-215, 250-263

src/renderer/src/components/sidebar/WorktreeListWorktreeRow.tsx (1)

32-32: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Use React.RefObject<WorktreePointerDrag | null> for this React 19 prop.

MutableRefObject is deprecated in the locked @types/react@19.2.17. The row only reads current, so RefObject is the appropriate prop type.

src/renderer/src/components/sidebar/worktree-list-viewport-types.ts (1)

112-113: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Replace React.MutableRefObject with React.RefObject for these props. The project uses React 19, where MutableRefObject is deprecated.

src/renderer/src/components/sidebar/worktree-list-groups.test.ts (1)

67-73: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Rename the source loader and document its coverage.

readWorktreeListSource concatenates three modules. Rename it to readWorktreeListSectionHeaderSources and add a brief comment that the list must be updated when section-header code moves to another module. Keep the narrower loader in project-group-header-dom.test.ts.

src/renderer/src/components/sidebar/use-worktree-visible-review-refresh.ts (1)

20-20: 🗄️ Data Integrity & Integration | 🔵 Trivial | 💤 Low value

Rename now to generation.

The coordinator uses this value for per-window generation ordering. It does not compare it with prVisibleRefreshGeneration. Both hook call sites pass Date.now(), so the rename aligns the hook with the store and main-process contracts.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3782d439-3e4b-4370-a3a4-32af89064e50

📥 Commits

Reviewing files that changed from the base of the PR and between 4221f8d and 36ef2be.

📒 Files selected for processing (40)
  • src/renderer/src/components/sidebar/FolderWorkspacePathStatusIndicator.tsx
  • src/renderer/src/components/sidebar/WorktreeList.tsx
  • src/renderer/src/components/sidebar/WorktreeListAuxiliaryRows.tsx
  • src/renderer/src/components/sidebar/WorktreeListFolderWorkspaceRow.tsx
  • src/renderer/src/components/sidebar/WorktreeListHostSectionHeader.tsx
  • src/renderer/src/components/sidebar/WorktreeListProjectGroupHeaderActions.tsx
  • src/renderer/src/components/sidebar/WorktreeListRepoHeaderActions.tsx
  • src/renderer/src/components/sidebar/WorktreeListSectionHeader.tsx
  • src/renderer/src/components/sidebar/WorktreeListWorktreeRow.tsx
  • src/renderer/src/components/sidebar/project-group-header-dom.test.ts
  • src/renderer/src/components/sidebar/use-reused-array-identity.ts
  • src/renderer/src/components/sidebar/use-worktree-active-row.ts
  • src/renderer/src/components/sidebar/use-worktree-folder-path-statuses.ts
  • src/renderer/src/components/sidebar/use-worktree-list-keyboard.ts
  • src/renderer/src/components/sidebar/use-worktree-list-reveal-highlight.ts
  • src/renderer/src/components/sidebar/use-worktree-list-sidebar-row-reveal.ts
  • src/renderer/src/components/sidebar/use-worktree-list-smart-order.ts
  • src/renderer/src/components/sidebar/use-worktree-list-virtualizer.ts
  • src/renderer/src/components/sidebar/use-worktree-list-workspace-reveal.ts
  • src/renderer/src/components/sidebar/use-worktree-native-drag-handlers.ts
  • src/renderer/src/components/sidebar/use-worktree-native-drag-start.ts
  • src/renderer/src/components/sidebar/use-worktree-native-drop-effects.ts
  • src/renderer/src/components/sidebar/use-worktree-pointer-drag-commit.ts
  • src/renderer/src/components/sidebar/use-worktree-pointer-drag-controller.ts
  • src/renderer/src/components/sidebar/use-worktree-pointer-drag-flush.ts
  • src/renderer/src/components/sidebar/use-worktree-status-drop-handlers.ts
  • src/renderer/src/components/sidebar/use-worktree-virtual-row-measurement.ts
  • src/renderer/src/components/sidebar/use-worktree-visible-review-refresh.ts
  • src/renderer/src/components/sidebar/worktree-keyboard-cycle.test.ts
  • src/renderer/src/components/sidebar/worktree-list-behavior.ts
  • src/renderer/src/components/sidebar/worktree-list-dom-activation.ts
  • src/renderer/src/components/sidebar/worktree-list-drag-indexes.ts
  • src/renderer/src/components/sidebar/worktree-list-drag-model.ts
  • src/renderer/src/components/sidebar/worktree-list-groups.test.ts
  • src/renderer/src/components/sidebar/worktree-list-project-header-events.ts
  • src/renderer/src/components/sidebar/worktree-list-render-row-model.ts
  • src/renderer/src/components/sidebar/worktree-list-row-ancestry.ts
  • src/renderer/src/components/sidebar/worktree-list-section-header-model.test.ts
  • src/renderer/src/components/sidebar/worktree-list-section-header-model.ts
  • src/renderer/src/components/sidebar/worktree-list-viewport-types.ts

@coderabbitai

coderabbitai Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f99e86dd-cd86-42c9-a944-6511452d47eb

📥 Commits

Reviewing files that changed from the base of the PR and between 4221f8d and 820bf7d.

📒 Files selected for processing (40)
  • src/renderer/src/components/sidebar/FolderWorkspacePathStatusIndicator.tsx
  • src/renderer/src/components/sidebar/WorktreeList.tsx
  • src/renderer/src/components/sidebar/WorktreeListAuxiliaryRows.tsx
  • src/renderer/src/components/sidebar/WorktreeListFolderWorkspaceRow.tsx
  • src/renderer/src/components/sidebar/WorktreeListHostSectionHeader.tsx
  • src/renderer/src/components/sidebar/WorktreeListProjectGroupHeaderActions.tsx
  • src/renderer/src/components/sidebar/WorktreeListRepoHeaderActions.tsx
  • src/renderer/src/components/sidebar/WorktreeListSectionHeader.tsx
  • src/renderer/src/components/sidebar/WorktreeListWorktreeRow.tsx
  • src/renderer/src/components/sidebar/project-group-header-dom.test.ts
  • src/renderer/src/components/sidebar/use-reused-array-identity.ts
  • src/renderer/src/components/sidebar/use-worktree-active-row.ts
  • src/renderer/src/components/sidebar/use-worktree-folder-path-statuses.ts
  • src/renderer/src/components/sidebar/use-worktree-list-keyboard.ts
  • src/renderer/src/components/sidebar/use-worktree-list-reveal-highlight.ts
  • src/renderer/src/components/sidebar/use-worktree-list-sidebar-row-reveal.ts
  • src/renderer/src/components/sidebar/use-worktree-list-smart-order.ts
  • src/renderer/src/components/sidebar/use-worktree-list-virtualizer.ts
  • src/renderer/src/components/sidebar/use-worktree-list-workspace-reveal.ts
  • src/renderer/src/components/sidebar/use-worktree-native-drag-handlers.ts
  • src/renderer/src/components/sidebar/use-worktree-native-drag-start.ts
  • src/renderer/src/components/sidebar/use-worktree-native-drop-effects.ts
  • src/renderer/src/components/sidebar/use-worktree-pointer-drag-commit.ts
  • src/renderer/src/components/sidebar/use-worktree-pointer-drag-controller.ts
  • src/renderer/src/components/sidebar/use-worktree-pointer-drag-flush.ts
  • src/renderer/src/components/sidebar/use-worktree-status-drop-handlers.ts
  • src/renderer/src/components/sidebar/use-worktree-virtual-row-measurement.ts
  • src/renderer/src/components/sidebar/use-worktree-visible-review-refresh.ts
  • src/renderer/src/components/sidebar/worktree-keyboard-cycle.test.ts
  • src/renderer/src/components/sidebar/worktree-list-behavior.ts
  • src/renderer/src/components/sidebar/worktree-list-dom-activation.ts
  • src/renderer/src/components/sidebar/worktree-list-drag-indexes.ts
  • src/renderer/src/components/sidebar/worktree-list-drag-model.ts
  • src/renderer/src/components/sidebar/worktree-list-groups.test.ts
  • src/renderer/src/components/sidebar/worktree-list-project-header-events.ts
  • src/renderer/src/components/sidebar/worktree-list-render-row-model.ts
  • src/renderer/src/components/sidebar/worktree-list-row-ancestry.ts
  • src/renderer/src/components/sidebar/worktree-list-section-header-model.test.ts
  • src/renderer/src/components/sidebar/worktree-list-section-header-model.ts
  • src/renderer/src/components/sidebar/worktree-list-viewport-types.ts
🚧 Files skipped from review as they are similar to previous changes (34)
  • src/renderer/src/components/sidebar/worktree-list-groups.test.ts
  • src/renderer/src/components/sidebar/project-group-header-dom.test.ts
  • src/renderer/src/components/sidebar/use-worktree-list-reveal-highlight.ts
  • src/renderer/src/components/sidebar/worktree-list-behavior.ts
  • src/renderer/src/components/sidebar/worktree-list-row-ancestry.ts
  • src/renderer/src/components/sidebar/FolderWorkspacePathStatusIndicator.tsx
  • src/renderer/src/components/sidebar/WorktreeListSectionHeader.tsx
  • src/renderer/src/components/sidebar/WorktreeListAuxiliaryRows.tsx
  • src/renderer/src/components/sidebar/WorktreeListWorktreeRow.tsx
  • src/renderer/src/components/sidebar/use-worktree-folder-path-statuses.ts
  • src/renderer/src/components/sidebar/worktree-list-project-header-events.ts
  • src/renderer/src/components/sidebar/use-reused-array-identity.ts
  • src/renderer/src/components/sidebar/use-worktree-list-workspace-reveal.ts
  • src/renderer/src/components/sidebar/WorktreeListFolderWorkspaceRow.tsx
  • src/renderer/src/components/sidebar/use-worktree-native-drop-effects.ts
  • src/renderer/src/components/sidebar/use-worktree-list-sidebar-row-reveal.ts
  • src/renderer/src/components/sidebar/use-worktree-visible-review-refresh.ts
  • src/renderer/src/components/sidebar/use-worktree-pointer-drag-controller.ts
  • src/renderer/src/components/sidebar/worktree-list-section-header-model.test.ts
  • src/renderer/src/components/sidebar/worktree-list-drag-model.ts
  • src/renderer/src/components/sidebar/use-worktree-list-keyboard.ts
  • src/renderer/src/components/sidebar/worktree-list-section-header-model.ts
  • src/renderer/src/components/sidebar/worktree-list-dom-activation.ts
  • src/renderer/src/components/sidebar/use-worktree-pointer-drag-commit.ts
  • src/renderer/src/components/sidebar/use-worktree-native-drag-start.ts
  • src/renderer/src/components/sidebar/WorktreeListRepoHeaderActions.tsx
  • src/renderer/src/components/sidebar/worktree-keyboard-cycle.test.ts
  • src/renderer/src/components/sidebar/worktree-list-viewport-types.ts
  • src/renderer/src/components/sidebar/worktree-list-render-row-model.ts
  • src/renderer/src/components/sidebar/worktree-list-drag-indexes.ts
  • src/renderer/src/components/sidebar/use-worktree-native-drag-handlers.ts
  • src/renderer/src/components/sidebar/use-worktree-list-smart-order.ts
  • src/renderer/src/components/sidebar/use-worktree-pointer-drag-flush.ts
  • src/renderer/src/components/sidebar/use-worktree-list-virtualizer.ts

@AmethystLiang

Copy link
Copy Markdown
Contributor Author

@coderabbitai Fixing. Will be in the next commit

@coderabbitai

coderabbitai Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

@AmethystLiang Thanks for the update. I will verify the fix after the next commit.

You are interacting with an AI system.

@nwparker

Copy link
Copy Markdown
Contributor

Closing as superseded. The WorktreeList extraction and follow-up quality gates landed in #14465 and #14467, and the resulting module layout was organized by #14486. The #14633 review history confirms this PR is obsolete and should not merge.

@nwparker

Copy link
Copy Markdown
Contributor

Superseded by the merged extraction and follow-up work in #14465, #14467, and #14486.

@nwparker nwparker closed this Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants