Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(posthog-ai): make esc cancel the plan approval card (#118229)

posthog[bot]452b8656d8b6
diff --git a/products/posthog_ai/frontend/components/PlanApprovalActions.tsx b/products/posthog_ai/frontend/components/PlanApprovalActions.tsx
index 41135c40e7dc..66b08d00f952 100644
--- a/products/posthog_ai/frontend/components/PlanApprovalActions.tsx
+++ b/products/posthog_ai/frontend/components/PlanApprovalActions.tsx
@@ -232,6 +232,16 @@ export function PlanApprovalSelector({
onReject(rejectOption.optionId, text)
}
+ // The composer hides while an approval shows, so its focus falls to the page body. The run's Escape
+ // boundary ignores body focus in the side panel and after a click elsewhere in the main chat, so the
+ // card takes focus to keep "Esc to cancel" working. Focus that rests on a visible element stays.
+ useEffect(() => {
+ const active = document.activeElement
+ if (!active || active === document.body || active.closest('[hidden]')) {
+ containerRef.current?.focus({ preventScroll: true })
+ }
+ }, [])
+
const moveSelection = (delta: number): void => {
selectRow((selectedIndex + delta + rowCount) % rowCount)
}
diff --git a/products/posthog_ai/frontend/components/RunEscapeBoundary.test.tsx b/products/posthog_ai/frontend/components/RunEscapeBoundary.test.tsx
index 31a814de5369..44578130e0d7 100644
--- a/products/posthog_ai/frontend/components/RunEscapeBoundary.test.tsx
+++ b/products/posthog_ai/frontend/components/RunEscapeBoundary.test.tsx
@@ -131,11 +131,17 @@ describe('RunEscapeBoundary', () => {
}
)
- it.each(['chat', 'composer'] as const)(
- 'scopes plan Escape and lets feedback editing finish first in %s',
- (scope) => {
+ it.each([
+ ['chat', 'body'],
+ ['chat', 'hidden composer'],
+ ['composer', 'body'],
+ ['composer', 'hidden composer'],
+ ] as const)(
+ 'hands plan Escape to the %s boundary from %s focus and lets feedback editing finish first',
+ (scope, focus) => {
const onEscape = jest.fn()
const legacyCancel = jest.fn()
+ const textAreaRef = createRef<HTMLTextAreaElement>()
const options = mapPermissionOptions(
[
{ optionId: 'auto', name: 'Allow', kind: 'allow_once' },
@@ -143,30 +149,41 @@ describe('RunEscapeBoundary', () => {
],
true
)
- render(
- <RunEscapeBoundary scope={scope} onEscape={onEscape}>
- <div data-attr="run-approval">
- <PlanApprovalSelector
- approveOptions={[options[0]]}
- rejectOption={options[1]}
- responding={false}
- onApprove={jest.fn()}
- onReject={jest.fn()}
- onCancel={legacyCancel}
- />
- </div>
+ const surface = (showApproval: boolean): JSX.Element => (
+ <RunEscapeBoundary scope={scope} textAreaRef={textAreaRef} onEscape={onEscape}>
+ {showApproval && (
+ <div data-attr="run-approval">
+ <PlanApprovalSelector
+ approveOptions={[options[0]]}
+ rejectOption={options[1]}
+ responding={false}
+ onApprove={jest.fn()}
+ onReject={jest.fn()}
+ onCancel={legacyCancel}
+ />
+ </div>
+ )}
+ <form hidden={showApproval}>
+ <textarea ref={textAreaRef} data-attr="composer" />
+ </form>
</RunEscapeBoundary>
)
- fireEvent.keyDown(document.body, { key: 'Escape' })
- expect(onEscape).toHaveBeenCalledTimes(scope === 'chat' ? 1 : 0)
- fireEvent.keyDown(document.body, { key: '2' })
+ const { rerender } = render(surface(false))
+ if (focus === 'hidden composer') {
+ screen.getByTestId('composer').focus()
+ }
+ rerender(surface(true))
+ const approval = screen.getByTestId('run-approval')
+ expect(approval).toContainElement(document.activeElement as HTMLElement)
+ fireEvent.keyDown(document.activeElement!, { key: 'Escape' })
+ expect(onEscape).toHaveBeenCalledTimes(1)
+ fireEvent.keyDown(document.activeElement!, { key: '2' })
const input = screen.getByPlaceholderText(/tell the agent what to do differently/i)
fireEvent.keyDown(input, { key: 'Escape' })
- expect(onEscape).toHaveBeenCalledTimes(scope === 'chat' ? 1 : 0)
- expect(legacyCancel).not.toHaveBeenCalled()
- expect(screen.getByTestId('run-approval')).toContainElement(document.activeElement as HTMLElement)
+ expect(onEscape).toHaveBeenCalledTimes(1)
+ expect(approval).toContainElement(document.activeElement as HTMLElement)
fireEvent.keyDown(document.activeElement!, { key: 'Escape' })
- expect(onEscape).toHaveBeenCalledTimes(scope === 'chat' ? 2 : 1)
+ expect(onEscape).toHaveBeenCalledTimes(2)
expect(legacyCancel).not.toHaveBeenCalled()
}
)