feat(desktop): close preview tabs on preview.close
Omit url to drop the whole rail; a url/path/label match closes that tab. Background sessions still cannot yank the user's preview shut.
This commit is contained in:
@@ -35,17 +35,27 @@ function Harness() {
|
||||
return null
|
||||
}
|
||||
|
||||
async function emitPreviewOpen(url = '/tmp/artifact-test.html') {
|
||||
async function emitPreviewOpen(url = '/tmp/artifact-test.html', sessionId = RUNTIME_SESSION_ID) {
|
||||
await act(async () => {
|
||||
handleEvent({
|
||||
payload: { label: 'hi bestie', url },
|
||||
session_id: RUNTIME_SESSION_ID,
|
||||
session_id: sessionId,
|
||||
type: 'preview.open'
|
||||
} as unknown as RpcEvent)
|
||||
})
|
||||
}
|
||||
|
||||
describe('open_preview', () => {
|
||||
async function emitPreviewClose(url?: string, sessionId = RUNTIME_SESSION_ID) {
|
||||
await act(async () => {
|
||||
handleEvent({
|
||||
payload: url === undefined ? {} : { url },
|
||||
session_id: sessionId,
|
||||
type: 'preview.close'
|
||||
} as unknown as RpcEvent)
|
||||
})
|
||||
}
|
||||
|
||||
describe('preview routing', () => {
|
||||
beforeEach(() => {
|
||||
// A live session always has a runtime id; only the STORED id lags.
|
||||
$activeSessionId.set(RUNTIME_SESSION_ID)
|
||||
@@ -70,6 +80,8 @@ describe('open_preview', () => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('open_preview', () => {
|
||||
|
||||
// The rail used to hold a session-keyed singleton alongside its tabs, written
|
||||
// under one session-id rule and reconciled under another. A live session with
|
||||
// no stored id yet resolved to '' on the write side, so the target was set and
|
||||
@@ -123,13 +135,7 @@ describe('open_preview', () => {
|
||||
render(<Harness />)
|
||||
|
||||
try {
|
||||
await act(async () => {
|
||||
handleEvent({
|
||||
payload: { url: '/tmp/from-tile.html' },
|
||||
session_id: 'tile-runtime',
|
||||
type: 'preview.open'
|
||||
} as unknown as RpcEvent)
|
||||
})
|
||||
await emitPreviewOpen('/tmp/from-tile.html', 'tile-runtime')
|
||||
|
||||
await waitFor(() => expect($previewTarget.get()?.path).toBe('/tmp/from-tile.html'))
|
||||
} finally {
|
||||
@@ -199,4 +205,71 @@ describe('open_preview', () => {
|
||||
|
||||
expect($previewTabs.get()).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('close_preview', () => {
|
||||
it('closes the whole pane when no url is given', async () => {
|
||||
render(<Harness />)
|
||||
|
||||
await emitPreviewOpen('/tmp/one.html')
|
||||
await emitPreviewOpen('/tmp/two.html')
|
||||
await waitFor(() => expect($previewTabs.get()).toHaveLength(2))
|
||||
|
||||
await emitPreviewClose('')
|
||||
|
||||
expect($previewTabs.get()).toHaveLength(0)
|
||||
expect($previewTarget.get()).toBeNull()
|
||||
})
|
||||
|
||||
it('closes only the matching tab when a url is given', async () => {
|
||||
render(<Harness />)
|
||||
|
||||
await emitPreviewOpen('/tmp/keep.html')
|
||||
await emitPreviewOpen('/tmp/drop.html')
|
||||
await waitFor(() => expect($previewTabs.get()).toHaveLength(2))
|
||||
|
||||
await emitPreviewClose('/tmp/drop.html')
|
||||
|
||||
await waitFor(() => expect($previewTabs.get()).toHaveLength(1))
|
||||
expect($previewTarget.get()?.path).toBe('/tmp/keep.html')
|
||||
})
|
||||
|
||||
it('ignores a close from a session that is not the one on screen', async () => {
|
||||
render(<Harness />)
|
||||
|
||||
await emitPreviewOpen('/tmp/stay.html')
|
||||
await waitFor(() => expect($previewTabs.get()).toHaveLength(1))
|
||||
|
||||
await emitPreviewClose('/tmp/stay.html', 'some-other-session')
|
||||
|
||||
expect($previewTabs.get()).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('honors a close from an open tile session even when main holds focus', async () => {
|
||||
const { $sessionTiles } = await import('@/store/session-states')
|
||||
const tiles = $sessionTiles.get()
|
||||
|
||||
$sessionTiles.set([{ dir: 'right', runtimeId: 'tile-runtime', storedSessionId: 'stored-tile' }])
|
||||
render(<Harness />)
|
||||
|
||||
try {
|
||||
await emitPreviewOpen('/tmp/from-tile.html', 'tile-runtime')
|
||||
await waitFor(() => expect($previewTabs.get()).toHaveLength(1))
|
||||
|
||||
await emitPreviewClose('/tmp/from-tile.html', 'tile-runtime')
|
||||
|
||||
await waitFor(() => expect($previewTabs.get()).toHaveLength(0))
|
||||
} finally {
|
||||
$sessionTiles.set(tiles)
|
||||
}
|
||||
})
|
||||
|
||||
it('is a no-op when nothing is open', async () => {
|
||||
render(<Harness />)
|
||||
|
||||
await emitPreviewClose()
|
||||
|
||||
expect($previewTabs.get()).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,6 +6,8 @@ import { reachablePreviewUrl } from '@/lib/preview-reach'
|
||||
import {
|
||||
$previewTabs,
|
||||
beginPreviewServerRestart,
|
||||
closePreviewMatching,
|
||||
closeRightRail,
|
||||
completePreviewServerRestart,
|
||||
openPreview,
|
||||
progressPreviewServerRestart,
|
||||
@@ -27,6 +29,14 @@ function asRecord(payload: unknown): Record<string, unknown> {
|
||||
return payload && typeof payload === 'object' ? (payload as Record<string, unknown>) : {}
|
||||
}
|
||||
|
||||
function sessionIsOnScreen(sessionId: string): boolean {
|
||||
return (
|
||||
sessionId === $focusedRuntimeId.get() ||
|
||||
sessionId === $activeSessionId.get() ||
|
||||
$sessionTiles.get().some(tile => tile.runtimeId === sessionId)
|
||||
)
|
||||
}
|
||||
|
||||
export function usePreviewRouting({ baseHandleGatewayEvent, currentCwd, requestGateway }: PreviewRoutingOptions) {
|
||||
const restartPreviewServer = useCallback(
|
||||
async (url: string, context?: string) => {
|
||||
@@ -76,12 +86,7 @@ export function usePreviewRouting({ baseHandleGatewayEvent, currentCwd, requestG
|
||||
const { url, label } = asRecord(event.payload)
|
||||
const target = typeof url === 'string' ? url.trim() : ''
|
||||
|
||||
const onScreen = (sid: string) =>
|
||||
sid === $focusedRuntimeId.get() ||
|
||||
sid === $activeSessionId.get() ||
|
||||
$sessionTiles.get().some(tile => tile.runtimeId === sid)
|
||||
|
||||
if (target && (!event.session_id || onScreen(event.session_id))) {
|
||||
if (target && (!event.session_id || sessionIsOnScreen(event.session_id))) {
|
||||
void normalizeOrLocalPreviewTarget(target, $currentCwd.get() || currentCwd || undefined).then(
|
||||
async resolved => {
|
||||
if (!resolved) {
|
||||
@@ -103,6 +108,45 @@ export function usePreviewRouting({ baseHandleGatewayEvent, currentCwd, requestG
|
||||
return
|
||||
}
|
||||
|
||||
if (event.type === 'preview.close') {
|
||||
// Agent-driven close via close_preview. Same on-screen gate as open:
|
||||
// a session the user can see may tidy the pane it opened; a hidden
|
||||
// background turn must not dismiss the user's preview.
|
||||
const { url } = asRecord(event.payload)
|
||||
const target = typeof url === 'string' ? url.trim() : ''
|
||||
|
||||
if (event.session_id && !sessionIsOnScreen(event.session_id)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!target) {
|
||||
closeRightRail()
|
||||
return
|
||||
}
|
||||
|
||||
if (closePreviewMatching(target)) {
|
||||
return
|
||||
}
|
||||
|
||||
void normalizeOrLocalPreviewTarget(target, $currentCwd.get() || currentCwd || undefined).then(
|
||||
async resolved => {
|
||||
const candidates = [target]
|
||||
|
||||
if (resolved) {
|
||||
candidates.push(resolved.source, resolved.url)
|
||||
|
||||
if (resolved.kind === 'url') {
|
||||
candidates.push(await reachablePreviewUrl(resolved.url))
|
||||
}
|
||||
}
|
||||
|
||||
closePreviewMatching(...candidates)
|
||||
}
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (event.type === 'preview.restart.complete') {
|
||||
const { task_id, text } = asRecord(event.payload)
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
$previewTarget,
|
||||
beginPreviewServerRestart,
|
||||
closePreviewForSource,
|
||||
closePreviewMatching,
|
||||
closeRightRail,
|
||||
closeRightRailTab,
|
||||
openPreview,
|
||||
@@ -127,6 +128,30 @@ describe('preview store', () => {
|
||||
expect(closePreviewForSource('http://localhost:5174')).toBe(false)
|
||||
})
|
||||
|
||||
it('closes a tab whose url or label matches even when source differs', () => {
|
||||
openPreview(
|
||||
{ kind: 'url', label: 'HN', source: 'https://news.ycombinator.com', url: 'https://news.ycombinator.com/' },
|
||||
'tool-result'
|
||||
)
|
||||
|
||||
expect(closePreviewMatching('https://news.ycombinator.com/')).toBe(true)
|
||||
expect($previewTabs.get()).toHaveLength(0)
|
||||
|
||||
openPreview({ ...fileTarget('/work/demo.html'), label: 'Demo' }, 'tool-result')
|
||||
|
||||
expect(closePreviewMatching('Demo')).toBe(true)
|
||||
expect($previewTabs.get()).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('does not wipe the rail on an empty or unknown close query', () => {
|
||||
openPreview(fileTarget('/work/keep.html'), 'file-browser')
|
||||
|
||||
expect(closePreviewMatching()).toBe(false)
|
||||
expect(closePreviewMatching(' ')).toBe(false)
|
||||
expect(closePreviewMatching('https://missing.example')).toBe(false)
|
||||
expect($previewTabs.get()).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('persists file and url tabs but never artifacts, whose content is memory-only', () => {
|
||||
openPreview(fileTarget('/work/demo.html'), 'file-browser')
|
||||
openPreview(urlTarget('http://localhost:5174'), 'tool-result')
|
||||
|
||||
@@ -258,7 +258,24 @@ export function closeRightRailTab(tabId: string) {
|
||||
|
||||
/** Close the tab showing `source`, if one is open. Returns whether it closed. */
|
||||
export function closePreviewForSource(source: string): boolean {
|
||||
const tab = $previewTabs.get().find(item => item.target.source === source)
|
||||
return closePreviewMatching(source)
|
||||
}
|
||||
|
||||
/** Close the first tab whose source, url, or label matches any candidate.
|
||||
* Empty candidates are a no-op so a missed match cannot wipe the rail —
|
||||
* closing the whole pane is `closeRightRail`. */
|
||||
export function closePreviewMatching(...candidates: string[]): boolean {
|
||||
const queries = [...new Set(candidates.map(value => value.trim()).filter(Boolean))]
|
||||
|
||||
if (queries.length === 0) {
|
||||
return false
|
||||
}
|
||||
|
||||
const tab = $previewTabs.get().find(item => {
|
||||
const fields = [item.target.source, item.target.url, item.target.label]
|
||||
|
||||
return queries.some(query => fields.includes(query))
|
||||
})
|
||||
|
||||
if (!tab) {
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user