From 2b5ac7239f34b20b50964c599b7de21e8a9ec28a Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Mon, 13 Apr 2026 22:51:12 -0700 Subject: [PATCH] fix: improve cmd+j palette search stability and selection behavior (#622) - Snap selection to first result when search query changes instead of keeping a stale mid-list selection - Add 150ms debounce on search to reduce result list reshuffling - Scroll list to top on query change - Remove green highlight on current worktree row (Current badge remains) - Remove primary-branch promotion sort that caused jumpy reordering --- .../src/components/WorktreeJumpPalette.tsx | 45 ++++++++++++++++--- .../src/lib/worktree-palette-search.test.ts | 10 ++--- .../src/lib/worktree-palette-search.ts | 23 ---------- 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/src/renderer/src/components/WorktreeJumpPalette.tsx b/src/renderer/src/components/WorktreeJumpPalette.tsx index 9566d62b8..0bc079cb3 100644 --- a/src/renderer/src/components/WorktreeJumpPalette.tsx +++ b/src/renderer/src/components/WorktreeJumpPalette.tsx @@ -77,10 +77,23 @@ export default function WorktreeJumpPalette(): React.JSX.Element | null { const browserTabsByWorktree = useAppStore((s) => s.browserTabsByWorktree) const [query, setQuery] = useState('') + const [debouncedQuery, setDebouncedQuery] = useState('') const [selectedWorktreeId, setSelectedWorktreeId] = useState('') const previousWorktreeIdRef = useRef(null) const wasVisibleRef = useRef(false) const skipRestoreFocusRef = useRef(false) + const prevQueryRef = useRef('') + const listRef = useRef(null) + + // Why: debounce the search query so the result list doesn't reshuffle on + // every keystroke while the user is still typing. The input stays responsive + // (controlled by `query`), but the heavier search + re-render is gated by + // `debouncedQuery`. 150ms is fast enough to feel instant on a pause, slow + // enough to skip intermediate keystrokes. + useEffect(() => { + const id = setTimeout(() => setDebouncedQuery(query), 150) + return () => clearTimeout(id) + }, [query]) const repoMap = useMemo(() => new Map(repos.map((r) => [r.id, r])), [repos]) const canCreateWorktree = useMemo(() => repos.some((repo) => isGitRepoKind(repo)), [repos]) @@ -95,10 +108,10 @@ export default function WorktreeJumpPalette(): React.JSX.Element | null { // Search results const matches = useMemo( - () => searchWorktrees(sortedWorktrees, query.trim(), repoMap, prCache, issueCache), - [sortedWorktrees, query, repoMap, prCache, issueCache] + () => searchWorktrees(sortedWorktrees, debouncedQuery.trim(), repoMap, prCache, issueCache), + [sortedWorktrees, debouncedQuery, repoMap, prCache, issueCache] ) - const createWorktreeName = query.trim() + const createWorktreeName = debouncedQuery.trim() // Why: only surface the create-worktree action when the query yields no matches, // so it doesn't clutter the list when existing worktrees already satisfy the search. const showCreateAction = @@ -124,6 +137,7 @@ export default function WorktreeJumpPalette(): React.JSX.Element | null { previousWorktreeIdRef.current = activeWorktreeId skipRestoreFocusRef.current = false setQuery('') + setDebouncedQuery('') setSelectedWorktreeId('') } @@ -134,7 +148,25 @@ export default function WorktreeJumpPalette(): React.JSX.Element | null { if (!visible) { return } + const queryChanged = debouncedQuery !== prevQueryRef.current + prevQueryRef.current = debouncedQuery + const firstSelectableId = showCreateAction ? '__create_worktree__' : null + + // Why: when the search query changes, the results reorder to reflect new + // relevance ranking. Always snap the selection to the top result so the + // user sees the best match highlighted, and scroll the list to the top so + // the selected item is visible without the user having to scroll up. + if (queryChanged) { + if (matches.length > 0) { + setSelectedWorktreeId(matches[0].worktreeId) + } else { + setSelectedWorktreeId(firstSelectableId ?? '') + } + listRef.current?.scrollTo(0, 0) + return + } + if (matches.length === 0) { setSelectedWorktreeId(firstSelectableId ?? '') return @@ -151,7 +183,7 @@ export default function WorktreeJumpPalette(): React.JSX.Element | null { // logical worktree selected instead of drifting to a new visual index. setSelectedWorktreeId(firstSelectableId ?? matches[0].worktreeId) } - }, [visible, matches, selectedWorktreeId, showCreateAction]) + }, [visible, matches, selectedWorktreeId, showCreateAction, debouncedQuery]) const focusActiveSurface = useCallback(() => { // Why: double rAF — first waits for React to commit state (palette closes), @@ -251,7 +283,7 @@ export default function WorktreeJumpPalette(): React.JSX.Element | null { iconClassName="mr-2.5 h-4 w-4 text-muted-foreground/60" className="h-12 text-[14px] placeholder:text-muted-foreground/75" /> - + {isLoading ? (
diff --git a/src/renderer/src/lib/worktree-palette-search.test.ts b/src/renderer/src/lib/worktree-palette-search.test.ts index 3a00fe6e3..5de8af258 100644 --- a/src/renderer/src/lib/worktree-palette-search.test.ts +++ b/src/renderer/src/lib/worktree-palette-search.test.ts @@ -101,7 +101,7 @@ describe('worktree-palette-search', () => { }) }) - it('promotes the primary worktree to the top when query matches a repo name', () => { + it('preserves input order when query matches a repo name', () => { const worktrees = [ makeWorktree({ id: 'wt-feature', @@ -125,11 +125,11 @@ describe('worktree-palette-search', () => { const results = searchWorktrees(worktrees, 'orca', repoMap, null, null) - // All three match on the repo name + // All three match on the repo name, order preserved from input expect(results).toHaveLength(3) - // The primary worktree should be first despite being last in input order - expect(results[0].worktreeId).toBe('wt-main') - expect(results[0].matchedField).toBe('repo') + expect(results[0].worktreeId).toBe('wt-feature') + expect(results[1].worktreeId).toBe('wt-bugfix') + expect(results[2].worktreeId).toBe('wt-main') }) it('matches issue numbers with a leading hash and returns issue render context', () => { diff --git a/src/renderer/src/lib/worktree-palette-search.ts b/src/renderer/src/lib/worktree-palette-search.ts index ad77cbbcc..82d33fee5 100644 --- a/src/renderer/src/lib/worktree-palette-search.ts +++ b/src/renderer/src/lib/worktree-palette-search.ts @@ -242,28 +242,5 @@ export function searchWorktrees( } } - // Promote primary worktrees when the query matches their repo name. - // When a user types a repo name, the primary branch is the most likely jump - // target — floating it to the top reduces selection friction. We only promote - // when matchedField is 'repo' so that stronger signals (displayName, branch) - // are not overridden by the promotion. - const worktreeById = new Map(worktrees.map((w) => [w.id, w])) - const promote = new Set() - for (const r of results) { - if (r.matchedField === 'repo') { - const w = worktreeById.get(r.worktreeId) - if (w?.isMainWorktree) { - promote.add(r.worktreeId) - } - } - } - if (promote.size > 0) { - results.sort((a, b) => { - const ap = promote.has(a.worktreeId) ? 1 : 0 - const bp = promote.has(b.worktreeId) ? 1 : 0 - return bp - ap - }) - } - return results }