diff --git a/src/renderer/src/components/sidebar/WorktreeList.tsx b/src/renderer/src/components/sidebar/WorktreeList.tsx index 803a987ba..90ab6bfeb 100644 --- a/src/renderer/src/components/sidebar/WorktreeList.tsx +++ b/src/renderer/src/components/sidebar/WorktreeList.tsx @@ -8,7 +8,7 @@ import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip import { cn } from '@/lib/utils' import type { Worktree, Repo } from '../../../../shared/types' import { buildWorktreeComparator } from './smart-sort' -import { branchName, type Row, buildRows, getGroupKeyForWorktree } from './worktree-list-groups' +import { matchesSearch, type Row, buildRows, getGroupKeyForWorktree } from './worktree-list-groups' const WorktreeList = React.memo(function WorktreeList() { // ── Granular selectors (each is a primitive or shallow-stable ref) ── @@ -32,8 +32,10 @@ const WorktreeList = React.memo(function WorktreeList() { // PR cache is needed for PR-status grouping and for recent sorting, which // incorporates whether the current branch has a live PR attached. const prCache = useAppStore((s) => - groupBy === 'pr-status' || sortBy === 'recent' ? s.prCache : null + groupBy === 'pr-status' || sortBy === 'recent' || searchQuery ? s.prCache : null ) + // Subscribe to issue cache only during active search to avoid unnecessary re-renders. + const issueCache = useAppStore((s) => (searchQuery ? s.issueCache : null)) const sortEpoch = useAppStore((s) => s.sortEpoch) const scrollRef = useRef(null) @@ -86,15 +88,11 @@ const WorktreeList = React.memo(function WorktreeList() { all = all.filter((w) => selectedRepoIds.has(w.repoId)) } - // Filter by search + // Filter by search — matches against displayName, branch, repo, comment, + // PR number/title, and issue number/title (see matchesSearch). if (searchQuery) { const q = searchQuery.toLowerCase() - all = all.filter( - (w) => - w.displayName.toLowerCase().includes(q) || - branchName(w.branch).toLowerCase().includes(q) || - (repoMap.get(w.repoId)?.displayName ?? '').toLowerCase().includes(q) - ) + all = all.filter((w) => matchesSearch(w, q, repoMap, prCache, issueCache)) } // Filter active only @@ -122,7 +120,9 @@ const WorktreeList = React.memo(function WorktreeList() { showActiveOnly, repoMap, tabsByWorktree, - sortedIds + sortedIds, + prCache, + issueCache ]) const worktrees = visibleWorktrees diff --git a/src/renderer/src/components/sidebar/worktree-list-groups.test.ts b/src/renderer/src/components/sidebar/worktree-list-groups.test.ts index 1bb83d90d..1103c02fa 100644 --- a/src/renderer/src/components/sidebar/worktree-list-groups.test.ts +++ b/src/renderer/src/components/sidebar/worktree-list-groups.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { getPRGroupKey } from './worktree-list-groups' +import { getPRGroupKey, matchesSearch } from './worktree-list-groups' import type { Repo, Worktree } from '../../../../shared/types' const repo: Repo = { @@ -28,9 +28,10 @@ const worktree: Worktree = { lastActivityAt: 0 } +const repoMap = new Map([[repo.id, repo]]) + describe('getPRGroupKey', () => { it('puts merged PRs in the done group', () => { - const repoMap = new Map([[repo.id, repo]]) const prCache = { '/tmp/orca::feature/super-critical': { data: { state: 'merged' } @@ -40,3 +41,109 @@ describe('getPRGroupKey', () => { expect(getPRGroupKey(worktree, repoMap, prCache)).toBe('done') }) }) + +describe('matchesSearch', () => { + it('matches on displayName', () => { + expect(matchesSearch(worktree, 'super-critical', repoMap, null, null)).toBe(true) + }) + + it('matches on branch name', () => { + expect(matchesSearch(worktree, 'feature/super', repoMap, null, null)).toBe(true) + }) + + it('matches on repo displayName', () => { + expect(matchesSearch(worktree, 'orca', repoMap, null, null)).toBe(true) + }) + + it('matches on comment', () => { + const w = { ...worktree, comment: 'Fix the login bug' } + expect(matchesSearch(w, 'login', repoMap, null, null)).toBe(true) + }) + + it('does not match empty comment', () => { + expect(matchesSearch(worktree, 'login', repoMap, null, null)).toBe(false) + }) + + it('matches on PR number from cache', () => { + const prCache = { + '/tmp/orca::feature/super-critical': { + data: { number: 304, title: 'Add search enhancements' } + } + } + expect(matchesSearch(worktree, '304', repoMap, prCache, null)).toBe(true) + }) + + it('matches on PR title from cache', () => { + const prCache = { + '/tmp/orca::feature/super-critical': { + data: { number: 304, title: 'Add search enhancements' } + } + } + expect(matchesSearch(worktree, 'search enhance', repoMap, prCache, null)).toBe(true) + }) + + it('matches on linkedPR when cache is empty', () => { + const w = { ...worktree, linkedPR: 42 } + expect(matchesSearch(w, '42', repoMap, null, null)).toBe(true) + }) + + it('matches on issue number from linkedIssue', () => { + const w = { ...worktree, linkedIssue: 99 } + expect(matchesSearch(w, '99', repoMap, null, null)).toBe(true) + }) + + it('matches on issue title from cache', () => { + const w = { ...worktree, linkedIssue: 99 } + const issueCache = { + '/tmp/orca::99': { + data: { number: 99, title: 'Sidebar performance regression' } + } + } + expect(matchesSearch(w, 'sidebar perf', repoMap, null, issueCache)).toBe(true) + }) + + it('strips # prefix for number matching', () => { + const prCache = { + '/tmp/orca::feature/super-critical': { + data: { number: 304, title: 'Enhancement' } + } + } + expect(matchesSearch(worktree, '#304', repoMap, prCache, null)).toBe(true) + }) + + it('strips # prefix for issue number matching', () => { + const w = { ...worktree, linkedIssue: 55 } + expect(matchesSearch(w, '#55', repoMap, null, null)).toBe(true) + }) + + it('returns false when nothing matches', () => { + expect(matchesSearch(worktree, 'nonexistent-query-xyz', repoMap, null, null)).toBe(false) + }) + + it('handles null PR cache entry data gracefully', () => { + const prCache = { + '/tmp/orca::feature/super-critical': { data: null } + } + // Should fall through to other checks without error + expect(matchesSearch(worktree, 'anything', repoMap, prCache, null)).toBe(false) + }) + + it('handles null issue cache entry data gracefully', () => { + const w = { ...worktree, linkedIssue: 99 } + const issueCache = { + '/tmp/orca::99': { data: null } + } + // Should match on issue number but not crash on null title + expect(matchesSearch(w, '99', repoMap, null, issueCache)).toBe(true) + }) + + it('does not treat bare # as a wildcard that matches all numbers', () => { + const prCache = { + '/tmp/orca::feature/super-critical': { + data: { number: 304, title: 'Enhancement' } + } + } + const w = { ...worktree, linkedIssue: 99 } + expect(matchesSearch(w, '#', repoMap, prCache, null)).toBe(false) + }) +}) diff --git a/src/renderer/src/components/sidebar/worktree-list-groups.ts b/src/renderer/src/components/sidebar/worktree-list-groups.ts index afcea0de8..c3f99e7a6 100644 --- a/src/renderer/src/components/sidebar/worktree-list-groups.ts +++ b/src/renderer/src/components/sidebar/worktree-list-groups.ts @@ -178,6 +178,73 @@ export function buildRows( return result } +/** + * Returns true when the worktree matches the search query against any of: + * displayName, branch, repo name, comment, PR number/title, issue number/title. + * `q` must already be lowercased by the caller. + */ +export function matchesSearch( + w: Worktree, + q: string, + repoMap: Map, + prCache: Record | null, + issueCache: Record | null +): boolean { + // Cheap field checks first + if (w.displayName.toLowerCase().includes(q)) { + return true + } + if (branchName(w.branch).toLowerCase().includes(q)) { + return true + } + if ((repoMap.get(w.repoId)?.displayName ?? '').toLowerCase().includes(q)) { + return true + } + if (w.comment && w.comment.toLowerCase().includes(q)) { + return true + } + + // Strip leading '#' so that searching "#304" matches number 304. + // Guard against bare '#' which would produce an empty string and match everything. + const numQuery = q.startsWith('#') ? q.slice(1) : q + if (!numQuery) { + return false + } + + // PR: check auto-detected PR from cache, then manual linkedPR as fallback + const repo = repoMap.get(w.repoId) + const branch = branchName(w.branch) + const prKey = repo && branch ? `${repo.path}::${branch}` : '' + const pr = prKey && prCache ? prCache[prKey]?.data : undefined + + if (pr) { + if (String(pr.number).includes(numQuery)) { + return true + } + if (pr.title.toLowerCase().includes(q)) { + return true + } + } else if (w.linkedPR != null) { + if (String(w.linkedPR).includes(numQuery)) { + return true + } + } + + // Issue: check linkedIssue number and cached title + if (w.linkedIssue != null) { + if (String(w.linkedIssue).includes(numQuery)) { + return true + } + const issueKey = repo ? `${repo.path}::${w.linkedIssue}` : '' + const issue = issueKey && issueCache ? issueCache[issueKey]?.data : undefined + if (issue?.title.toLowerCase().includes(q)) { + return true + } + } + + return false +} + export function getGroupKeyForWorktree( groupBy: 'none' | 'repo' | 'pr-status', worktree: Worktree,