: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(desktop): make plan approval escape reliable (#77652)
Matt Pua0d68fac4b225
diff --git a/products/desktop/packages/ui/src/features/permissions/PlanApprovalSelector.test.tsx b/products/desktop/packages/ui/src/features/permissions/PlanApprovalSelector.test.tsxindex a8a2c0171778..a606ff03fb05 100644--- a/products/desktop/packages/ui/src/features/permissions/PlanApprovalSelector.test.tsx+++ b/products/desktop/packages/ui/src/features/permissions/PlanApprovalSelector.test.tsx@@ -1,7 +1,13 @@import type { PermissionOption } from "@agentclientprotocol/sdk";import { useSettingsStore } from "@posthog/ui/features/settings/settingsStore";import { Theme } from "@radix-ui/themes";-import { act, render, screen, waitFor } from "@testing-library/react";+import {+ act,+ fireEvent,+ render,+ screen,+ waitFor,+} from "@testing-library/react";import userEvent from "@testing-library/user-event";import { beforeEach, describe, expect, it, vi } from "vitest";import { PlanApprovalSelector } from "./PlanApprovalSelector";@@ -207,6 +213,16 @@ describe("PlanApprovalSelector", () => {expect(onSelect).not.toHaveBeenCalled();});+ it("cancels on Escape from the feedback input", async () => {+ const user = userEvent.setup();+ const { onCancel } = renderSelector([DEFAULT_MODE, REJECT]);++ await user.click(screen.getByText("2."));+ await user.keyboard("{Escape}");++ expect(onCancel).toHaveBeenCalledTimes(1);+ });+it("cancels on Escape", async () => {const user = userEvent.setup();const { onCancel } = renderSelector([DEFAULT_MODE, REJECT]);@@ -215,4 +231,83 @@ describe("PlanApprovalSelector", () => {expect(onCancel).toHaveBeenCalledTimes(1);});++ it("closes the mode menu before cancelling on Escape", async () => {+ const user = userEvent.setup();+ const { onCancel } = renderSelector([AUTO, DEFAULT_MODE, REJECT]);++ await user.click(screen.getByRole("button", { name: "Mode" }));+ expect(+ await screen.findByText("Manually approve edits"),+ ).toBeInTheDocument();++ await user.keyboard("{Escape}");++ expect(onCancel).not.toHaveBeenCalled();+ await waitFor(() =>+ expect(+ screen.queryByText("Manually approve edits"),+ ).not.toBeInTheDocument(),+ );++ await user.keyboard("{Escape}");++ expect(onCancel).toHaveBeenCalledTimes(1);+ });++ it("does not cancel while composing input", () => {+ const { onCancel } = renderSelector([DEFAULT_MODE, REJECT]);+ const selector = screen+ .getByText("Implementation Plan")+ .closest("[tabindex='0']") as HTMLElement;++ fireEvent.keyDown(selector, { key: "Escape", keyCode: 229 });++ expect(onCancel).not.toHaveBeenCalled();+ });++ it("only cancels the approval in the focused grid cell", () => {+ const firstOnCancel = vi.fn();+ const secondOnCancel = vi.fn();+ render(+ <Theme>+ <div data-grid-cell>+ <PlanApprovalSelector+ toolCall={toolCall}+ options={[DEFAULT_MODE, REJECT]}+ onSelect={vi.fn()}+ onCancel={firstOnCancel}+ />+ </div>+ <div data-grid-cell>+ <PlanApprovalSelector+ toolCall={{ ...toolCall, toolCallId: "plan-2" }}+ options={[DEFAULT_MODE, REJECT]}+ onSelect={vi.fn()}+ onCancel={secondOnCancel}+ />+ </div>+ </Theme>,+ );+ const firstSelector = screen+ .getAllByText("Implementation Plan")[0]+ .closest("[tabindex='0']") as HTMLElement;+ firstSelector.focus();++ fireEvent.keyDown(firstSelector, { key: "Escape" });++ expect(firstOnCancel).toHaveBeenCalledTimes(1);+ expect(secondOnCancel).not.toHaveBeenCalled();+ });++ it("does not steal focus from an external input", () => {+ const externalInput = document.createElement("input");+ document.body.appendChild(externalInput);+ externalInput.focus();++ renderSelector([DEFAULT_MODE, REJECT]);++ expect(document.activeElement).toBe(externalInput);+ externalInput.remove();+ });});diff --git a/products/desktop/packages/ui/src/features/permissions/PlanApprovalSelector.tsx b/products/desktop/packages/ui/src/features/permissions/PlanApprovalSelector.tsxindex 145add84aad5..35bacec515bf 100644--- a/products/desktop/packages/ui/src/features/permissions/PlanApprovalSelector.tsx+++ b/products/desktop/packages/ui/src/features/permissions/PlanApprovalSelector.tsx@@ -18,23 +18,21 @@ import { type BasePermissionProps, toSelectorOptions } from "./types";const TITLE = "Implementation Plan";const QUESTION = "Approve this plan to proceed?";-// Don't steal focus from an interactive element in a different grid cell-// (multi-task view). Mirrors the guard in useActionSelectorState.-function isInteractiveElementInDifferentCell(+// Don't steal focus from a different task in multi-task view.+function isElementInDifferentCell(containerRef: React.RefObject<HTMLDivElement | null>,): boolean {const el = document.activeElement;if (!(el instanceof HTMLElement)) return false;+ const activeCell = el.closest("[data-grid-cell]");+ const ownCell = containerRef.current?.closest("[data-grid-cell]");+ if (activeCell && ownCell && activeCell !== ownCell) return true;const isInteractive =el.tagName === "INPUT" ||el.tagName === "TEXTAREA" ||el.tagName === "SELECT" ||el.getAttribute("contenteditable") === "true";- if (!isInteractive) return false;- const activeCell = el.closest("[data-grid-cell]");- const ownCell = containerRef.current?.closest("[data-grid-cell]");- if (!activeCell || !ownCell) return true;- return activeCell !== ownCell;+ return isInteractive && (!activeCell || !ownCell);}/**@@ -124,7 +122,7 @@ export function PlanApprovalSelector({// different grid cell (multi-task view) already owns focus. Selection-driven// focus is handled inline in `selectRow`, not via a state-syncing effect.useEffect(() => {- if (!isInteractiveElementInDifferentCell(containerRef)) {+ if (!isElementInDifferentCell(containerRef)) {containerRef.current?.focus();}}, []);@@ -158,6 +156,7 @@ export function PlanApprovalSelector({};const handleContainerKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {+ if (e.nativeEvent.isComposing || e.keyCode === 229) return;// The reject textarea owns the keyboard while it's the selected row.if (rejectSelected) return;switch (e.key) {@@ -309,10 +308,7 @@ export function PlanApprovalSelector({onChange={setFeedback}onNavigateUp={() => selectRow(0)}onNavigateDown={() => selectRow(0)}- onEscape={() => {- setFeedback("");- selectRow(0);- }}+ onEscape={onCancel}onSubmit={submitReject}/></Box>