Repository navigation
Optimize worktree parent picker: conditional mount, virtualization, lazy status loading - #12225
Conversation
- Mount WorktreeParentPickerPopover only when open to avoid hundreds of unmounted instances subscribing to lineage and worktree store updates. - Virtualize the candidate list and resolve activity statuses only for visible rows, eliminating redundant status subscriptions. - Extract filtering, placement calculations, and row rendering into separate modules for testability and clarity.
Wrap search state update in useCallback to stabilize the handler across re-renders. Reduces unnecessary effect runs and enables better memoization of child components.
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThe worktree parent picker now filters candidates by display name, branch, and path. It renders candidates in a virtualized list with viewport-aware placement. Keyboard navigation supports arrows, Home, End, Enter, and IME composition handling. Rows display worktree status and repository metadata. Activity status selection uses shallow Zustand state. The context menu keeps the picker mounted during its exit animation. Localization adds parent-selection labels in five locales. 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
📝 Generate docstrings
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
src/renderer/src/components/sidebar/worktree-parent-picker-filtering.ts (1)
12-19: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsider avoiding the readonly-to-mutable cast.
Line 18 returns
candidates as Worktree[], casting awayreadonlyand returning the original array reference. The matched-search branch (Lines 20-27) always returns a new array via.map(). Returning the original reference here breaks that consistency and lets a caller mutate the memoizedcandidatesarray by mistake.Use a shallow copy instead of the cast, for example
[...candidates], so both branches return an independent array.src/renderer/src/components/sidebar/WorktreeParentPickerPopover.tsx (1)
245-248: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick winMemoize
worktreeIdsbefore passing it touseWorktreeActivityStatuses.
virtualRows.map((row) => filtered[row.index]?.id).filter(...)creates a new array on every render.useWorktreeActivityStatuseswraps its selector inuseCallback(..., [worktreeIds]), so a new array reference each render means thatuseCallbacknever memoizes and the Zustand selector re-runs on every render, not only when the visible row set changes.Wrap the id list in
useMemokeyed on the values that actually determine the visible set (for example, the virtual row indices andfiltered) so the selector stabilizes between renders where the visible rows are unchanged.♻️ Proposed fix
- const virtualRows = virtualizer.getVirtualItems() - const statuses = useWorktreeActivityStatuses( - virtualRows.map((row) => filtered[row.index]?.id).filter((id): id is string => id !== undefined) - ) + const virtualRows = virtualizer.getVirtualItems() + const visibleWorktreeIds = useMemo( + () => + virtualRows.map((row) => filtered[row.index]?.id).filter((id): id is string => id !== undefined), + [virtualRows, filtered] + ) + const statuses = useWorktreeActivityStatuses(visibleWorktreeIds)src/renderer/src/components/sidebar/use-worktree-activity-statuses.ts (1)
61-69: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick winAvoid recomputing
worktreeIdson every picker render.
WorktreeParentPickerPopover.tsxcreates a newworktreeIdsarray inside theuseWorktreeActivityStatusescall. AdduseMemoaroundvirtualRows.map((row) => filtered[row.index]?.id).filter(...)in the caller, or key the hook memoization to a stable derived value, so the status selector only rebuilds when the visible ids change.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: e5d138c5-fd02-43d6-8d37-95b019c34f67
📒 Files selected for processing (13)
src/renderer/src/components/sidebar/WorktreeContextMenu.tsxsrc/renderer/src/components/sidebar/WorktreeParentPickerPopover.test.tssrc/renderer/src/components/sidebar/WorktreeParentPickerPopover.tsxsrc/renderer/src/components/sidebar/WorktreeParentPickerRow.tsxsrc/renderer/src/components/sidebar/use-worktree-activity-statuses.test.tssrc/renderer/src/components/sidebar/use-worktree-activity-statuses.tssrc/renderer/src/components/sidebar/worktree-parent-picker-filtering.tssrc/renderer/src/components/sidebar/worktree-parent-picker-placement.tssrc/renderer/src/i18n/locales/en.jsonsrc/renderer/src/i18n/locales/es.jsonsrc/renderer/src/i18n/locales/ja.jsonsrc/renderer/src/i18n/locales/ko.jsonsrc/renderer/src/i18n/locales/zh.json
- Defer unmount until exit animation completes (200ms) to prevent premature teardown - Memoize visibleWorktreeIds to prevent status hook from rebuilding its selector on every render
…azy status loading (stablyai#12225) * Optimize worktree parent picker: conditional mount, virtualization, lazy - Mount WorktreeParentPickerPopover only when open to avoid hundreds of unmounted instances subscribing to lineage and worktree store updates. - Virtualize the candidate list and resolve activity statuses only for visible rows, eliminating redundant status subscriptions. - Extract filtering, placement calculations, and row rendering into separate modules for testability and clarity. * Memoize worktree parent picker search handler Wrap search state update in useCallback to stabilize the handler across re-renders. Reduces unnecessary effect runs and enables better memoization of child components. * Optimize worktree parent picker: defer unmount, memoize IDs - Defer unmount until exit animation completes (200ms) to prevent premature teardown - Memoize visibleWorktreeIds to prevent status hook from rebuilding its selector on every render
Summary
Optimizes the worktree parent picker to address slow load times when opening the popover. Three key improvements:
Conditional mount: Picker only mounts when open, eliminating unnecessary subscriptions to worktree and lineage maps for every worktree card in the sidebar.
Virtualization: Large candidate lists now use
@tanstack/react-virtualto render only visible rows, with status data fetched only for displayed items viauseWorktreeActivityStatuses.Lazy filtering and ranking: Search filtering uses cmdk's scoring algorithm and happens on input, not upfront. Anchor position is calculated to avoid viewport overflow without DOM layout thrashing.
Architecture Changes
WorktreeParentPickerRowcomponent to prevent unnecessary re-rendersworktree-parent-picker-filtering.tswith cmdk-based rankingworktree-parent-picker-placement.tsuse-worktree-activity-statuses.tshook returns a shallow-stable status map for only the displayed candidates, preventing unrelated worktree updates from re-rendering the pickeraria-activedescendantupdatesUI Improvements
StatusIndicatorcomponentTesting