Skip to content

The agent that grows with you

fix(desktop): Escape returns to the preview page when history exists

Brooklyn Nicholsona9a54245b231
diff --git a/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx b/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx
index 0c9d5aba597..006cd58021b 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx
+++ b/apps/desktop/src/app/chat/right-rail/preview-pane.test.tsx
@@ -232,6 +232,58 @@ describe('PreviewPane console state', () => {
expect(webview.getAttribute('src')).toBe('http://localhost:5174')
})
+ it('Escape goes back only when the webview has history', async () => {
+ let rendered!: ReturnType<typeof render>
+ await act(async () => {
+ rendered = render(
+ <PreviewPane
+ target={{ kind: 'url', label: 'Preview', source: 'http://localhost:5174', url: 'http://localhost:5174' }}
+ />
+ )
+ })
+
+ const webview = rendered.container.querySelector('webview') as HTMLElement & Record<string, unknown>
+ const goBack = vi.fn()
+
+ Object.assign(webview, { canGoBack: () => false, goBack, loadURL: vi.fn(async () => undefined) })
+
+ // No history yet: Escape is not claimed (the pane does not fake a back).
+ const pane = rendered.container.querySelector('aside') as HTMLElement
+
+ fireEvent.keyDown(pane, { key: 'Escape' })
+ expect(goBack).not.toHaveBeenCalled()
+
+ // After an in-page navigation there is history: Escape drives it.
+ Object.assign(webview, { canGoBack: () => true })
+
+ fireEvent.keyDown(pane, { key: 'Escape' })
+ expect(goBack).toHaveBeenCalledOnce()
+ })
+
+ it('Escape keeps its native meaning for editable surfaces inside the pane', async () => {
+ let rendered!: ReturnType<typeof render>
+ await act(async () => {
+ rendered = render(
+ <PreviewPane
+ target={{ kind: 'url', label: 'Preview', source: 'http://localhost:5174', url: 'http://localhost:5174' }}
+ />
+ )
+ })
+
+ const webview = rendered.container.querySelector('webview') as HTMLElement & Record<string, unknown>
+ const goBack = vi.fn()
+
+ Object.assign(webview, { canGoBack: () => true, goBack })
+
+ // The browser bar's address input: Escape resets the draft (its own
+ // handler), and must not ALSO navigate the webview back.
+ const address = rendered.getByRole('textbox', { name: 'Address' }) as HTMLInputElement
+
+ fireEvent.focus(address)
+ fireEvent.keyDown(address, { key: 'Escape' })
+ expect(goBack).not.toHaveBeenCalled()
+ })
+
it('continues comment numbering in one conversation and resets it when the conversation changes', async () => {
$selectedStoredSessionId.set('session-one')
const selectedCrop = 'data:image/png;base64,c2VsZWN0ZWQ='
diff --git a/apps/desktop/src/app/chat/right-rail/preview-pane.tsx b/apps/desktop/src/app/chat/right-rail/preview-pane.tsx
index 1e9fc71971f..83289e9999d 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-pane.tsx
+++ b/apps/desktop/src/app/chat/right-rail/preview-pane.tsx
@@ -767,6 +767,39 @@ export function PreviewPane({
}
}, [])
+ // Escape is the universal "leave where I am" key, and a preview webview that
+ // navigated away from the original page (a report's in-page links) is a
+ // dead end without it. Only the pane that holds DOM focus backs up, so two
+ // side-by-side panes never fight, and only when the webview actually has
+ // history — a remote-HTML report (data: document, no webview) or a
+ // first-page preview claims no back at all. While focus sits in an editable
+ // surface inside the pane (the browser bar's address input, an annotate
+ // note), Escape keeps its native meaning for that control and does not
+ // also navigate.
+ const onPaneKeyDown = useCallback(
+ (event: React.KeyboardEvent<HTMLElement>) => {
+ if (event.key !== 'Escape') {
+ return
+ }
+
+ const target = event.target as HTMLElement | null
+
+ if (target?.closest?.('input, textarea, [contenteditable="true"], [contenteditable=""]')) {
+ return
+ }
+
+ const webview = webviewRef.current
+
+ if (!webview?.canGoBack?.()) {
+ return
+ }
+
+ event.preventDefault()
+ webview.goBack?.()
+ },
+ []
+ )
+
// Gestures that land on the app's chrome (⌘R from the address bar, a mouse
// button over the frame). A gesture made INSIDE the page is answered by main
// against the focused guest — this renderer can't see into a webview.
@@ -1318,8 +1351,9 @@ export function PreviewPane({
return (
<aside
className="relative flex h-full w-full min-w-0 flex-col overflow-hidden bg-transparent text-muted-foreground"
- // Buttons 3/4 are a mouse's back/forward. Chromium delivers them to the
- // renderer as a normal mouse event inside the app's own chrome (the
+ onKeyDown={onPaneKeyDown}
+ // Buttons 3/4 are a mouse's back/forward. Chromium delivers them to
+ // the renderer as a normal mouse event inside the app's own chrome (the
// guest page gets its own via `app-command` in main), and unhandled they
// walk the HOST document's history.
onMouseDown={event => {