Fix file content search reset per worktree (#294)
* fix: scope file search state to each worktree * refactor: extract search filters component
This commit is contained in:
@@ -1,25 +1,18 @@
|
||||
import React, { useCallback, useDeferredValue, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { useVirtualizer } from '@tanstack/react-virtual'
|
||||
import {
|
||||
Search as SearchIcon,
|
||||
CaseSensitive,
|
||||
WholeWord,
|
||||
Regex,
|
||||
ChevronRight,
|
||||
X,
|
||||
ChevronDown,
|
||||
Loader2
|
||||
} from 'lucide-react'
|
||||
import { Search as SearchIcon, CaseSensitive, WholeWord, Regex, X, Loader2 } from 'lucide-react'
|
||||
import { useAppStore } from '@/store'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import type { SearchFileResult, SearchMatch } from '../../../../shared/types'
|
||||
import { buildSearchRows } from './search-rows'
|
||||
import { cancelRevealFrame, openMatchResult } from './search-match-open'
|
||||
import { SearchFilters } from './SearchFilters'
|
||||
import { ToggleButton, FileResultRow, MatchResultRow } from './SearchResultItems'
|
||||
|
||||
const SEARCH_DEBOUNCE_MS = 300
|
||||
const SEARCH_MAX_RESULTS = 2000
|
||||
const SEARCH_VIRTUAL_OVERSCAN = 12
|
||||
const EMPTY_COLLAPSED_FILES = new Set<string>()
|
||||
|
||||
export default function Search(): React.JSX.Element {
|
||||
const activeWorktreeId = useAppStore((s) => s.activeWorktreeId)
|
||||
@@ -27,24 +20,20 @@ export default function Search(): React.JSX.Element {
|
||||
const openFile = useAppStore((s) => s.openFile)
|
||||
const setPendingEditorReveal = useAppStore((s) => s.setPendingEditorReveal)
|
||||
|
||||
const fileSearchQuery = useAppStore((s) => s.fileSearchQuery)
|
||||
const fileSearchCaseSensitive = useAppStore((s) => s.fileSearchCaseSensitive)
|
||||
const fileSearchWholeWord = useAppStore((s) => s.fileSearchWholeWord)
|
||||
const fileSearchUseRegex = useAppStore((s) => s.fileSearchUseRegex)
|
||||
const fileSearchIncludePattern = useAppStore((s) => s.fileSearchIncludePattern)
|
||||
const fileSearchExcludePattern = useAppStore((s) => s.fileSearchExcludePattern)
|
||||
const fileSearchResults = useAppStore((s) => s.fileSearchResults)
|
||||
const fileSearchLoading = useAppStore((s) => s.fileSearchLoading)
|
||||
const fileSearchCollapsedFiles = useAppStore((s) => s.fileSearchCollapsedFiles)
|
||||
const searchState = useAppStore((s) =>
|
||||
activeWorktreeId ? s.fileSearchStateByWorktree[activeWorktreeId] : null
|
||||
)
|
||||
const fileSearchQuery = searchState?.query ?? ''
|
||||
const fileSearchCaseSensitive = searchState?.caseSensitive ?? false
|
||||
const fileSearchWholeWord = searchState?.wholeWord ?? false
|
||||
const fileSearchUseRegex = searchState?.useRegex ?? false
|
||||
const fileSearchIncludePattern = searchState?.includePattern ?? ''
|
||||
const fileSearchExcludePattern = searchState?.excludePattern ?? ''
|
||||
const fileSearchResults = searchState?.results ?? null
|
||||
const fileSearchLoading = searchState?.loading ?? false
|
||||
const fileSearchCollapsedFiles = searchState?.collapsedFiles ?? EMPTY_COLLAPSED_FILES
|
||||
|
||||
const setFileSearchQuery = useAppStore((s) => s.setFileSearchQuery)
|
||||
const setFileSearchCaseSensitive = useAppStore((s) => s.setFileSearchCaseSensitive)
|
||||
const setFileSearchWholeWord = useAppStore((s) => s.setFileSearchWholeWord)
|
||||
const setFileSearchUseRegex = useAppStore((s) => s.setFileSearchUseRegex)
|
||||
const setFileSearchIncludePattern = useAppStore((s) => s.setFileSearchIncludePattern)
|
||||
const setFileSearchExcludePattern = useAppStore((s) => s.setFileSearchExcludePattern)
|
||||
const setFileSearchResults = useAppStore((s) => s.setFileSearchResults)
|
||||
const setFileSearchLoading = useAppStore((s) => s.setFileSearchLoading)
|
||||
const updateFileSearchState = useAppStore((s) => s.updateFileSearchState)
|
||||
const toggleFileSearchCollapsedFile = useAppStore((s) => s.toggleFileSearchCollapsedFile)
|
||||
const clearFileSearch = useAppStore((s) => s.clearFileSearch)
|
||||
|
||||
@@ -56,14 +45,41 @@ export default function Search(): React.JSX.Element {
|
||||
const revealRafRef = useRef<number | null>(null)
|
||||
const revealInnerRafRef = useRef<number | null>(null)
|
||||
|
||||
const updateActiveSearchState = useCallback(
|
||||
(updates: Partial<NonNullable<typeof searchState>>) => {
|
||||
if (!activeWorktreeId) {
|
||||
return
|
||||
}
|
||||
updateFileSearchState(activeWorktreeId, updates)
|
||||
},
|
||||
[activeWorktreeId, updateFileSearchState]
|
||||
)
|
||||
|
||||
const clearActiveSearch = useCallback(() => {
|
||||
if (!activeWorktreeId) {
|
||||
return
|
||||
}
|
||||
clearFileSearch(activeWorktreeId)
|
||||
}, [activeWorktreeId, clearFileSearch])
|
||||
|
||||
const toggleActiveCollapsedFile = useCallback(
|
||||
(filePath: string) => {
|
||||
if (!activeWorktreeId) {
|
||||
return
|
||||
}
|
||||
toggleFileSearchCollapsedFile(activeWorktreeId, filePath)
|
||||
},
|
||||
[activeWorktreeId, toggleFileSearchCollapsedFile]
|
||||
)
|
||||
|
||||
const cancelPendingSearch = useCallback(() => {
|
||||
latestSearchIdRef.current += 1
|
||||
if (searchTimerRef.current) {
|
||||
clearTimeout(searchTimerRef.current)
|
||||
searchTimerRef.current = null
|
||||
}
|
||||
setFileSearchLoading(false)
|
||||
}, [setFileSearchLoading])
|
||||
updateActiveSearchState({ loading: false })
|
||||
}, [updateActiveSearchState])
|
||||
|
||||
// Find active worktree path
|
||||
const worktreePath = useMemo(() => {
|
||||
@@ -96,9 +112,9 @@ export default function Search(): React.JSX.Element {
|
||||
useEffect(() => {
|
||||
if (!worktreePath) {
|
||||
cancelPendingSearch()
|
||||
setFileSearchResults(null)
|
||||
updateActiveSearchState({ results: null })
|
||||
}
|
||||
}, [worktreePath, cancelPendingSearch, setFileSearchResults])
|
||||
}, [worktreePath, cancelPendingSearch, updateActiveSearchState])
|
||||
|
||||
// Why: large search result sets can update while the user is still typing.
|
||||
// Deferring the heavy row-model update keeps the input responsive instead of
|
||||
@@ -158,12 +174,11 @@ export default function Search(): React.JSX.Element {
|
||||
}
|
||||
|
||||
if (!query.trim() || !worktreePath) {
|
||||
setFileSearchResults(null)
|
||||
setFileSearchLoading(false)
|
||||
updateActiveSearchState({ results: null, loading: false })
|
||||
return
|
||||
}
|
||||
|
||||
setFileSearchLoading(true)
|
||||
updateActiveSearchState({ loading: true })
|
||||
searchTimerRef.current = setTimeout(async () => {
|
||||
searchTimerRef.current = null
|
||||
try {
|
||||
@@ -171,51 +186,56 @@ export default function Search(): React.JSX.Element {
|
||||
const results = await window.api.fs.search({
|
||||
query: query.trim(),
|
||||
rootPath: worktreePath,
|
||||
caseSensitive: state.fileSearchCaseSensitive,
|
||||
wholeWord: state.fileSearchWholeWord,
|
||||
useRegex: state.fileSearchUseRegex,
|
||||
includePattern: state.fileSearchIncludePattern || undefined,
|
||||
excludePattern: state.fileSearchExcludePattern || undefined,
|
||||
caseSensitive:
|
||||
state.fileSearchStateByWorktree[activeWorktreeId!]?.caseSensitive ?? false,
|
||||
wholeWord: state.fileSearchStateByWorktree[activeWorktreeId!]?.wholeWord ?? false,
|
||||
useRegex: state.fileSearchStateByWorktree[activeWorktreeId!]?.useRegex ?? false,
|
||||
includePattern:
|
||||
state.fileSearchStateByWorktree[activeWorktreeId!]?.includePattern || undefined,
|
||||
excludePattern:
|
||||
state.fileSearchStateByWorktree[activeWorktreeId!]?.excludePattern || undefined,
|
||||
maxResults: SEARCH_MAX_RESULTS
|
||||
})
|
||||
if (latestSearchIdRef.current === searchId) {
|
||||
setFileSearchResults(results)
|
||||
updateActiveSearchState({ results })
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Search failed:', err)
|
||||
if (latestSearchIdRef.current === searchId) {
|
||||
setFileSearchResults({ files: [], totalMatches: 0, truncated: false })
|
||||
updateActiveSearchState({
|
||||
results: { files: [], totalMatches: 0, truncated: false }
|
||||
})
|
||||
}
|
||||
} finally {
|
||||
if (latestSearchIdRef.current === searchId) {
|
||||
setFileSearchLoading(false)
|
||||
updateActiveSearchState({ loading: false })
|
||||
}
|
||||
}
|
||||
}, SEARCH_DEBOUNCE_MS)
|
||||
},
|
||||
[worktreePath, setFileSearchResults, setFileSearchLoading]
|
||||
[worktreePath, updateActiveSearchState, activeWorktreeId]
|
||||
)
|
||||
|
||||
const handleClearSearch = useCallback(() => {
|
||||
cancelPendingSearch()
|
||||
clearFileSearch()
|
||||
}, [cancelPendingSearch, clearFileSearch])
|
||||
clearActiveSearch()
|
||||
}, [cancelPendingSearch, clearActiveSearch])
|
||||
|
||||
// Re-execute search from event handlers when options change
|
||||
const rerunSearch = useCallback(() => {
|
||||
const q = useAppStore.getState().fileSearchQuery
|
||||
const q = useAppStore.getState().fileSearchStateByWorktree[activeWorktreeId!]?.query ?? ''
|
||||
if (q.trim()) {
|
||||
executeSearch(q)
|
||||
}
|
||||
}, [executeSearch])
|
||||
}, [executeSearch, activeWorktreeId])
|
||||
|
||||
const handleQueryChange = useCallback(
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const val = e.target.value
|
||||
setFileSearchQuery(val)
|
||||
updateActiveSearchState({ query: val })
|
||||
executeSearch(val)
|
||||
},
|
||||
[setFileSearchQuery, executeSearch]
|
||||
[updateActiveSearchState, executeSearch]
|
||||
)
|
||||
|
||||
const handleKeyDown = useCallback(
|
||||
@@ -260,9 +280,7 @@ export default function Search(): React.JSX.Element {
|
||||
|
||||
return (
|
||||
<div className="flex flex-col h-full">
|
||||
{/* Search input area */}
|
||||
<div className="flex flex-col gap-1.5 p-2 border-b border-border">
|
||||
{/* Main search row */}
|
||||
<div className="flex items-center gap-1 bg-input/50 border border-border rounded-sm px-1.5 focus-within:border-ring">
|
||||
<SearchIcon size={14} className="text-muted-foreground flex-shrink-0" />
|
||||
<input
|
||||
@@ -289,11 +307,10 @@ export default function Search(): React.JSX.Element {
|
||||
<X size={12} />
|
||||
</Button>
|
||||
)}
|
||||
{/* Toggle buttons */}
|
||||
<ToggleButton
|
||||
active={fileSearchCaseSensitive}
|
||||
onClick={() => {
|
||||
setFileSearchCaseSensitive(!fileSearchCaseSensitive)
|
||||
updateActiveSearchState({ caseSensitive: !fileSearchCaseSensitive })
|
||||
rerunSearch()
|
||||
}}
|
||||
title="Match Case"
|
||||
@@ -303,7 +320,7 @@ export default function Search(): React.JSX.Element {
|
||||
<ToggleButton
|
||||
active={fileSearchWholeWord}
|
||||
onClick={() => {
|
||||
setFileSearchWholeWord(!fileSearchWholeWord)
|
||||
updateActiveSearchState({ wholeWord: !fileSearchWholeWord })
|
||||
rerunSearch()
|
||||
}}
|
||||
title="Match Whole Word"
|
||||
@@ -313,7 +330,7 @@ export default function Search(): React.JSX.Element {
|
||||
<ToggleButton
|
||||
active={fileSearchUseRegex}
|
||||
onClick={() => {
|
||||
setFileSearchUseRegex(!fileSearchUseRegex)
|
||||
updateActiveSearchState({ useRegex: !fileSearchUseRegex })
|
||||
rerunSearch()
|
||||
}}
|
||||
title="Use Regular Expression"
|
||||
@@ -322,46 +339,22 @@ export default function Search(): React.JSX.Element {
|
||||
</ToggleButton>
|
||||
</div>
|
||||
|
||||
{/* Files to include/exclude toggle */}
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
className="h-auto justify-start gap-1 self-start px-0 text-[10px] text-muted-foreground hover:text-foreground"
|
||||
onClick={() => setShowFilters(!showFilters)}
|
||||
>
|
||||
{showFilters ? <ChevronDown size={10} /> : <ChevronRight size={10} />}
|
||||
<span>files to include/exclude</span>
|
||||
</Button>
|
||||
|
||||
{showFilters && (
|
||||
<div className="flex flex-col gap-1">
|
||||
<input
|
||||
type="text"
|
||||
className="bg-input/50 border border-border rounded-sm px-2 py-1 text-xs outline-none focus:border-ring text-foreground placeholder:text-muted-foreground"
|
||||
placeholder="files to include (e.g. *.ts, src/**)"
|
||||
value={fileSearchIncludePattern}
|
||||
onChange={(e) => {
|
||||
setFileSearchIncludePattern(e.target.value)
|
||||
rerunSearch()
|
||||
}}
|
||||
spellCheck={false}
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
className="bg-input/50 border border-border rounded-sm px-2 py-1 text-xs outline-none focus:border-ring text-foreground placeholder:text-muted-foreground"
|
||||
placeholder="files to exclude (e.g. *.min.js, dist/**)"
|
||||
value={fileSearchExcludePattern}
|
||||
onChange={(e) => {
|
||||
setFileSearchExcludePattern(e.target.value)
|
||||
rerunSearch()
|
||||
}}
|
||||
spellCheck={false}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<SearchFilters
|
||||
showFilters={showFilters}
|
||||
includePattern={fileSearchIncludePattern}
|
||||
excludePattern={fileSearchExcludePattern}
|
||||
onToggleFilters={() => setShowFilters(!showFilters)}
|
||||
onIncludeChange={(value) => {
|
||||
updateActiveSearchState({ includePattern: value })
|
||||
rerunSearch()
|
||||
}}
|
||||
onExcludeChange={(value) => {
|
||||
updateActiveSearchState({ excludePattern: value })
|
||||
rerunSearch()
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Results area */}
|
||||
<div ref={resultsScrollRef} className="flex-1 min-h-0 overflow-y-auto scrollbar-sleek">
|
||||
{searchRows.length > 0 && (
|
||||
<div
|
||||
@@ -395,9 +388,7 @@ export default function Search(): React.JSX.Element {
|
||||
<FileResultRow
|
||||
fileResult={row.fileResult}
|
||||
collapsed={row.collapsed}
|
||||
onToggleCollapse={() =>
|
||||
toggleFileSearchCollapsedFile(row.fileResult.filePath)
|
||||
}
|
||||
onToggleCollapse={() => toggleActiveCollapsedFile(row.fileResult.filePath)}
|
||||
/>
|
||||
)}
|
||||
{row.type === 'match' && (
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import React from 'react'
|
||||
import { ChevronDown, ChevronRight } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
|
||||
type SearchFiltersProps = {
|
||||
showFilters: boolean
|
||||
includePattern: string
|
||||
excludePattern: string
|
||||
onToggleFilters: () => void
|
||||
onIncludeChange: (value: string) => void
|
||||
onExcludeChange: (value: string) => void
|
||||
}
|
||||
|
||||
export function SearchFilters({
|
||||
showFilters,
|
||||
includePattern,
|
||||
excludePattern,
|
||||
onToggleFilters,
|
||||
onIncludeChange,
|
||||
onExcludeChange
|
||||
}: SearchFiltersProps): React.JSX.Element {
|
||||
return (
|
||||
<>
|
||||
<Button
|
||||
type="button"
|
||||
variant="ghost"
|
||||
className="h-auto justify-start gap-1 self-start px-0 text-[10px] text-muted-foreground hover:text-foreground"
|
||||
onClick={onToggleFilters}
|
||||
>
|
||||
{showFilters ? <ChevronDown size={10} /> : <ChevronRight size={10} />}
|
||||
<span>files to include/exclude</span>
|
||||
</Button>
|
||||
|
||||
{showFilters && (
|
||||
<div className="flex flex-col gap-1">
|
||||
<input
|
||||
type="text"
|
||||
className="bg-input/50 border border-border rounded-sm px-2 py-1 text-xs outline-none focus:border-ring text-foreground placeholder:text-muted-foreground"
|
||||
placeholder="files to include (e.g. *.ts, src/**)"
|
||||
value={includePattern}
|
||||
onChange={(e) => onIncludeChange(e.target.value)}
|
||||
spellCheck={false}
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
className="bg-input/50 border border-border rounded-sm px-2 py-1 text-xs outline-none focus:border-ring text-foreground placeholder:text-muted-foreground"
|
||||
placeholder="files to exclude (e.g. *.min.js, dist/**)"
|
||||
value={excludePattern}
|
||||
onChange={(e) => onExcludeChange(e.target.value)}
|
||||
spellCheck={false}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -68,9 +68,8 @@ export function FileResultRow({
|
||||
onClick={onToggleCollapse}
|
||||
>
|
||||
<ChevronRight
|
||||
size={12}
|
||||
className={cn(
|
||||
'flex-shrink-0 text-muted-foreground transition-transform',
|
||||
'size-3 flex-shrink-0 text-muted-foreground transition-transform',
|
||||
!collapsed && 'rotate-90'
|
||||
)}
|
||||
/>
|
||||
|
||||
@@ -196,25 +196,26 @@ export type EditorSlice = {
|
||||
) => void
|
||||
|
||||
// File search state
|
||||
fileSearchQuery: string
|
||||
fileSearchCaseSensitive: boolean
|
||||
fileSearchWholeWord: boolean
|
||||
fileSearchUseRegex: boolean
|
||||
fileSearchIncludePattern: string
|
||||
fileSearchExcludePattern: string
|
||||
fileSearchResults: SearchResult | null
|
||||
fileSearchLoading: boolean
|
||||
fileSearchCollapsedFiles: Set<string>
|
||||
setFileSearchQuery: (query: string) => void
|
||||
setFileSearchCaseSensitive: (v: boolean) => void
|
||||
setFileSearchWholeWord: (v: boolean) => void
|
||||
setFileSearchUseRegex: (v: boolean) => void
|
||||
setFileSearchIncludePattern: (v: string) => void
|
||||
setFileSearchExcludePattern: (v: string) => void
|
||||
setFileSearchResults: (results: SearchResult | null) => void
|
||||
setFileSearchLoading: (loading: boolean) => void
|
||||
toggleFileSearchCollapsedFile: (filePath: string) => void
|
||||
clearFileSearch: () => void
|
||||
fileSearchStateByWorktree: Record<
|
||||
string,
|
||||
{
|
||||
query: string
|
||||
caseSensitive: boolean
|
||||
wholeWord: boolean
|
||||
useRegex: boolean
|
||||
includePattern: string
|
||||
excludePattern: string
|
||||
results: SearchResult | null
|
||||
loading: boolean
|
||||
collapsedFiles: Set<string>
|
||||
}
|
||||
>
|
||||
updateFileSearchState: (
|
||||
worktreeId: string,
|
||||
updates: Partial<EditorSlice['fileSearchStateByWorktree'][string]>
|
||||
) => void
|
||||
toggleFileSearchCollapsedFile: (worktreeId: string, filePath: string) => void
|
||||
clearFileSearch: (worktreeId: string) => void
|
||||
|
||||
// Editor navigation (for search result → go-to-line)
|
||||
pendingEditorReveal: {
|
||||
@@ -1070,39 +1071,64 @@ export const createEditorSlice: StateCreator<AppState, [], [], EditorSlice> = (s
|
||||
}),
|
||||
|
||||
// File search
|
||||
fileSearchQuery: '',
|
||||
fileSearchCaseSensitive: false,
|
||||
fileSearchWholeWord: false,
|
||||
fileSearchUseRegex: false,
|
||||
fileSearchIncludePattern: '',
|
||||
fileSearchExcludePattern: '',
|
||||
fileSearchResults: null,
|
||||
fileSearchLoading: false,
|
||||
fileSearchCollapsedFiles: new Set<string>(),
|
||||
setFileSearchQuery: (query) => set({ fileSearchQuery: query }),
|
||||
setFileSearchCaseSensitive: (v) => set({ fileSearchCaseSensitive: v }),
|
||||
setFileSearchWholeWord: (v) => set({ fileSearchWholeWord: v }),
|
||||
setFileSearchUseRegex: (v) => set({ fileSearchUseRegex: v }),
|
||||
setFileSearchIncludePattern: (v) => set({ fileSearchIncludePattern: v }),
|
||||
setFileSearchExcludePattern: (v) => set({ fileSearchExcludePattern: v }),
|
||||
setFileSearchResults: (results) => set({ fileSearchResults: results }),
|
||||
setFileSearchLoading: (loading) => set({ fileSearchLoading: loading }),
|
||||
toggleFileSearchCollapsedFile: (filePath) =>
|
||||
fileSearchStateByWorktree: {},
|
||||
updateFileSearchState: (worktreeId, updates) =>
|
||||
set((s) => {
|
||||
const next = new Set(s.fileSearchCollapsedFiles)
|
||||
if (next.has(filePath)) {
|
||||
next.delete(filePath)
|
||||
} else {
|
||||
next.add(filePath)
|
||||
const current = s.fileSearchStateByWorktree[worktreeId] || {
|
||||
query: '',
|
||||
caseSensitive: false,
|
||||
wholeWord: false,
|
||||
useRegex: false,
|
||||
includePattern: '',
|
||||
excludePattern: '',
|
||||
results: null,
|
||||
loading: false,
|
||||
collapsedFiles: new Set()
|
||||
}
|
||||
return {
|
||||
fileSearchStateByWorktree: {
|
||||
...s.fileSearchStateByWorktree,
|
||||
[worktreeId]: { ...current, ...updates }
|
||||
}
|
||||
}
|
||||
return { fileSearchCollapsedFiles: next }
|
||||
}),
|
||||
clearFileSearch: () =>
|
||||
set({
|
||||
fileSearchQuery: '',
|
||||
fileSearchResults: null,
|
||||
fileSearchLoading: false,
|
||||
fileSearchCollapsedFiles: new Set<string>()
|
||||
toggleFileSearchCollapsedFile: (worktreeId, filePath) =>
|
||||
set((s) => {
|
||||
const current = s.fileSearchStateByWorktree[worktreeId]
|
||||
if (!current) {
|
||||
return s
|
||||
}
|
||||
const nextCollapsed = new Set(current.collapsedFiles)
|
||||
if (nextCollapsed.has(filePath)) {
|
||||
nextCollapsed.delete(filePath)
|
||||
} else {
|
||||
nextCollapsed.add(filePath)
|
||||
}
|
||||
return {
|
||||
fileSearchStateByWorktree: {
|
||||
...s.fileSearchStateByWorktree,
|
||||
[worktreeId]: { ...current, collapsedFiles: nextCollapsed }
|
||||
}
|
||||
}
|
||||
}),
|
||||
clearFileSearch: (worktreeId) =>
|
||||
set((s) => {
|
||||
const current = s.fileSearchStateByWorktree[worktreeId]
|
||||
if (!current) {
|
||||
return s
|
||||
}
|
||||
return {
|
||||
fileSearchStateByWorktree: {
|
||||
...s.fileSearchStateByWorktree,
|
||||
[worktreeId]: {
|
||||
...current,
|
||||
query: '',
|
||||
results: null,
|
||||
loading: false,
|
||||
collapsedFiles: new Set()
|
||||
}
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
// Editor navigation
|
||||
|
||||
@@ -85,6 +85,19 @@ describe('removeWorktree cascade', () => {
|
||||
deleteStateByWorktreeId: {
|
||||
[worktreeId]: { isDeleting: false, error: null, canForceDelete: false }
|
||||
},
|
||||
fileSearchStateByWorktree: {
|
||||
[worktreeId]: {
|
||||
query: 'needle',
|
||||
caseSensitive: true,
|
||||
wholeWord: false,
|
||||
useRegex: false,
|
||||
includePattern: '*.ts',
|
||||
excludePattern: 'dist/**',
|
||||
results: { files: [], totalMatches: 0, truncated: false },
|
||||
loading: false,
|
||||
collapsedFiles: new Set(['/path/wt1/file.ts'])
|
||||
}
|
||||
},
|
||||
activeWorktreeId: worktreeId,
|
||||
activeTabId: 'tab1',
|
||||
openFiles: [makeOpenFile({ id: '/path/wt1/file.ts', worktreeId })],
|
||||
@@ -105,6 +118,7 @@ describe('removeWorktree cascade', () => {
|
||||
expect(s.terminalLayoutsByTabId['tab1']).toBeUndefined()
|
||||
expect(s.terminalLayoutsByTabId['tab2']).toBeUndefined()
|
||||
expect(s.deleteStateByWorktreeId[worktreeId]).toBeUndefined()
|
||||
expect(s.fileSearchStateByWorktree[worktreeId]).toBeUndefined()
|
||||
expect(s.activeWorktreeId).toBeNull()
|
||||
expect(s.activeTabId).toBeNull()
|
||||
expect(s.openFiles).toEqual([])
|
||||
@@ -196,6 +210,30 @@ describe('removeWorktree cascade', () => {
|
||||
tab1: makeLayout(),
|
||||
tab2: makeLayout()
|
||||
},
|
||||
fileSearchStateByWorktree: {
|
||||
[wt1]: {
|
||||
query: 'old',
|
||||
caseSensitive: false,
|
||||
wholeWord: false,
|
||||
useRegex: false,
|
||||
includePattern: '',
|
||||
excludePattern: '',
|
||||
results: { files: [], totalMatches: 0, truncated: false },
|
||||
loading: false,
|
||||
collapsedFiles: new Set()
|
||||
},
|
||||
[wt2]: {
|
||||
query: 'keep',
|
||||
caseSensitive: true,
|
||||
wholeWord: true,
|
||||
useRegex: false,
|
||||
includePattern: '*.md',
|
||||
excludePattern: '',
|
||||
results: { files: [], totalMatches: 1, truncated: false },
|
||||
loading: false,
|
||||
collapsedFiles: new Set(['/path/wt2/notes.md'])
|
||||
}
|
||||
},
|
||||
activeWorktreeId: wt2,
|
||||
activeTabId: 'tab2'
|
||||
})
|
||||
@@ -208,6 +246,7 @@ describe('removeWorktree cascade', () => {
|
||||
expect(s.tabsByWorktree[wt2][0].id).toBe('tab2')
|
||||
expect(s.ptyIdsByTabId['tab2']).toEqual(['pty2'])
|
||||
expect(s.terminalLayoutsByTabId['tab2']).toEqual(makeLayout())
|
||||
expect(s.fileSearchStateByWorktree[wt2]?.query).toBe('keep')
|
||||
expect(s.activeWorktreeId).toBe(wt2)
|
||||
expect(s.activeTabId).toBe('tab2')
|
||||
|
||||
@@ -216,6 +255,7 @@ describe('removeWorktree cascade', () => {
|
||||
expect(s.tabsByWorktree[wt1]).toBeUndefined()
|
||||
expect(s.ptyIdsByTabId['tab1']).toBeUndefined()
|
||||
expect(s.terminalLayoutsByTabId['tab1']).toBeUndefined()
|
||||
expect(s.fileSearchStateByWorktree[wt1]).toBeUndefined()
|
||||
})
|
||||
|
||||
it('shuts down terminals before asking the backend to remove the worktree', async () => {
|
||||
|
||||
@@ -137,6 +137,14 @@ export const createWorktreeSlice: StateCreator<AppState, [], [], WorktreeSlice>
|
||||
ptyIdsByTabId: nextPtyIdsByTabId,
|
||||
terminalLayoutsByTabId: nextLayouts,
|
||||
deleteStateByWorktreeId: nextDeleteState,
|
||||
fileSearchStateByWorktree: (() => {
|
||||
const nextSearch = { ...s.fileSearchStateByWorktree }
|
||||
// Why: file search UI state is worktree-scoped. Removing the worktree
|
||||
// must also remove its cached query/results so another worktree never
|
||||
// inherits stale matches from a path that no longer exists.
|
||||
delete nextSearch[worktreeId]
|
||||
return nextSearch
|
||||
})(),
|
||||
activeWorktreeId: s.activeWorktreeId === worktreeId ? null : s.activeWorktreeId,
|
||||
activeTabId: s.activeTabId && tabIds.has(s.activeTabId) ? null : s.activeTabId,
|
||||
openFiles: newOpenFiles,
|
||||
|
||||
Reference in New Issue
Block a user