Skip to content

Commit 1c73e26

Browse files
Merge branch 'fix-terminals-getting-deleted-q7l3'
2 parents 39903e5 + 83da982 commit 1c73e26

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

src/routes/terminals/index.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function TerminalsView() {
7979
}
8080
}, [tabs, isValidTab, navigate])
8181

82-
const { data: tasks = [], isLoading: isTasksLoading } = useTasks()
82+
const { data: tasks = [], status: tasksStatus } = useTasks()
8383
const { data: repositories = [] } = useRepositories()
8484
const { data: worktreeBasePath } = useWorktreeBasePath()
8585
const [repoFilter, setRepoFilter] = useState<string | null>(null)
@@ -172,9 +172,9 @@ function TerminalsView() {
172172

173173
// Destroy orphaned worktree terminals (terminals in worktrees dir but no matching task)
174174
useEffect(() => {
175-
// Don't run cleanup until tasks are loaded - otherwise we'd destroy valid terminals
176-
// because allTaskWorktrees would be empty while tasks are still loading
177-
if (!worktreeBasePath || isTasksLoading) return
175+
// Only run cleanup when tasks have successfully loaded
176+
// status === 'success' ensures we have valid data, not stale/empty cache from React Query
177+
if (!worktreeBasePath || tasksStatus !== 'success') return
178178

179179
for (const terminal of terminals) {
180180
const isInWorktreesDir = terminal.cwd?.startsWith(worktreeBasePath)
@@ -184,7 +184,7 @@ function TerminalsView() {
184184
destroyTerminal(terminal.id)
185185
}
186186
}
187-
}, [terminals, allTaskWorktrees, worktreeBasePath, destroyTerminal, isTasksLoading])
187+
}, [terminals, allTaskWorktrees, worktreeBasePath, destroyTerminal, tasksStatus])
188188

189189
// Filter terminals for the active tab
190190
const visibleTerminals = useMemo(() => {
@@ -241,14 +241,17 @@ function TerminalsView() {
241241

242242
// Task-related terminals should not be in regular tabs - remove them if they are
243243
useEffect(() => {
244+
// Wait for tasks to load before determining which terminals are task-related
245+
if (tasksStatus !== 'success') return
246+
244247
for (const terminal of terminals) {
245248
const isTaskTerminal = terminal.cwd && allTaskWorktrees.has(terminal.cwd)
246249
if (isTaskTerminal && terminal.tabId) {
247250
// Remove task terminals from regular tabs - they should only appear in All Tasks
248251
assignTerminalToTab(terminal.id, null)
249252
}
250253
}
251-
}, [terminals, allTaskWorktrees, assignTerminalToTab])
254+
}, [terminals, allTaskWorktrees, assignTerminalToTab, tasksStatus])
252255

253256
// Auto-open newly created tabs and create a terminal inside
254257
useEffect(() => {

0 commit comments

Comments
 (0)