fix(mobile): clear state after closing final tab

This commit is contained in:
Brennan Benson
2026-07-30 17:35:02 -07:00
parent 5fe3aaf2b7
commit b6aec5ad88
2 changed files with 26 additions and 2 deletions
@@ -4131,6 +4131,7 @@ export default function SessionScreen() {
reason: 'user'
})
if (response.ok) {
const remainingTabs = sessionTabsRef.current.filter((candidate) => candidate.id !== tab.id)
if (tab.type === 'browser' && tab.browserPageId === pendingBrowserFocusPageIdRef.current) {
pendingBrowserFocusPageIdRef.current = null
}
@@ -4141,14 +4142,16 @@ export default function SessionScreen() {
initializedHandlesRef.current.delete(terminalHandle)
clearTerminalLiveInputDefault(terminalHandle)
}
setSessionTabs((prev) => prev.filter((candidate) => candidate.id !== tab.id))
sessionTabsRef.current = remainingTabs
setSessionTabs(remainingTabs)
// Why: tombstone the closed tab and rely on the snapshot, not a blind refetch that often re-added the not-yet-closed tab.
closedTabTombstonesRef.current.set(tab.id, Date.now() + 10_000)
// Why: bulk close re-activates the anchor before awaiting each close;
// the render-synced ref sees that switch while this closure would not,
// so comparing against the ref keeps the anchor from being nulled out.
if (activeSessionTabIdRef.current === tab.id) {
if (activeSessionTabIdRef.current === tab.id || remainingTabs.length === 0) {
activeSessionTabTypeRef.current = null
activeSessionTabIdRef.current = null
setActiveSessionTabId(null)
activeHandleRef.current = null
setActiveHandle(null)
@@ -0,0 +1,21 @@
import { readFileSync } from 'node:fs'
import { describe, expect, it } from 'vitest'
const sessionRouteSource = readFileSync(
new URL('../../app/h/[hostId]/session/[worktreeId].tsx', import.meta.url),
'utf8'
)
describe('mobile session last-tab close', () => {
it('clears stale active identity when closing leaves no tabs', () => {
const start = sessionRouteSource.indexOf('async function handleCloseSessionTab')
const end = sessionRouteSource.indexOf('const bulkCloseActions', start)
const block = sessionRouteSource.slice(start, end)
expect(block).toContain(
'activeSessionTabIdRef.current === tab.id || remainingTabs.length === 0'
)
expect(block).toContain('activeSessionTabIdRef.current = null')
expect(block).toContain('activeHandleRef.current = null')
})
})