Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(node-icon): let the picker own Escape when it is the top dialog

albaneselorenzo51e3548b40af
diff --git a/src/renderer/components/NodeIconPicker.test.tsx b/src/renderer/components/NodeIconPicker.test.tsx
index 33cb76f67..57ae2ce4b 100644
--- a/src/renderer/components/NodeIconPicker.test.tsx
+++ b/src/renderer/components/NodeIconPicker.test.tsx
@@ -18,6 +18,7 @@ import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { NodeIconDialogHost, nodeIconDialog, type NodeIconChoice } from './NodeIconPicker'
import { useProjects } from '../state/projects'
+import { popDialog, pushDialog, resetDialogStack } from './dialog-stack'
// React refuses act() outside a configured test environment without this flag.
;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
@@ -129,3 +130,81 @@ describe('NodeIconPicker image pick', () => {
})
})
})
+
+// Escape, which was reachable only while the text input had focus — `useDialogStack()` was called
+// and its answer thrown away. The gate matches `confirmKeyAction`: top-of-stack only, with no
+// focus requirement, because Escape is the safe direction and Enter is the affirmative one.
+describe('NodeIconPicker escape', () => {
+ let root: Root | undefined
+ let host: HTMLElement
+
+ // The pending choice is returned WRAPPED. An async function returning a promise flattens it, so
+ // `await open()` would wait for the dialog's own promise — which nothing has dismissed yet.
+ const open = async (): Promise<{ choice: Promise<NodeIconChoice> }> => {
+ host = document.createElement('div')
+ document.body.appendChild(host)
+ useProjects.setState({ activeProjectId: 'p1', getProject: () => PROJECT } as never)
+ const choice = nodeIconDialog({ nodeId: 'n1', title: 'Build' })
+ root = createRoot(host)
+ await act(async () => {
+ root!.render(<NodeIconDialogHost />)
+ })
+ return { choice }
+ }
+
+ const pressEscape = async (target: EventTarget): Promise<void> => {
+ await act(async () => {
+ target.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))
+ })
+ }
+
+ afterEach(async () => {
+ await act(async () => root?.unmount())
+ root = undefined
+ host?.remove()
+ resetDialogStack()
+ })
+
+ it('closes when the press lands outside the input — the reported bug', async () => {
+ const { choice } = await open()
+ // Focus is NOT in the text input: a swatch is the realistic case, since clicking one is how
+ // most people arrive here.
+ const swatch = document.querySelector('.node-icon-dialog__swatch')
+ expect(swatch).toBeTruthy()
+ await pressEscape(swatch!)
+ // `undefined` is CANCEL, distinct from `null` (remove the icon). Collapsing the two is how a
+ // dismissed dialog silently clears an icon the user wanted to keep.
+ await expect(choice).resolves.toBeUndefined()
+ })
+
+ it('still closes from the input, and from the document body', async () => {
+ for (const pick of [
+ () => document.querySelector('.node-icon-dialog__input')!,
+ () => document.body
+ ]) {
+ const { choice } = await open()
+ await pressEscape(pick())
+ await expect(choice).resolves.toBeUndefined()
+ await act(async () => root?.unmount())
+ host.remove()
+ }
+ })
+
+ it('ignores Escape while another dialog is stacked on top of it', async () => {
+ const { choice } = await open()
+ let settled = false
+ void choice.then(() => {
+ settled = true
+ })
+
+ pushDialog('dialog-above')
+ await pressEscape(document.body)
+ await act(async () => {})
+ expect(settled).toBe(false)
+
+ // ...and takes it back once that dialog closes.
+ popDialog('dialog-above')
+ await pressEscape(document.body)
+ await expect(choice).resolves.toBeUndefined()
+ })
+})
diff --git a/src/renderer/components/NodeIconPicker.tsx b/src/renderer/components/NodeIconPicker.tsx
index 79fcfacca..eb08018d4 100644
--- a/src/renderer/components/NodeIconPicker.tsx
+++ b/src/renderer/components/NodeIconPicker.tsx
@@ -109,12 +109,37 @@ function NodeIconPicker({
const [busy, setBusy] = useState(false)
const [error, setError] = useState('')
const inputRef = useRef<HTMLInputElement>(null)
- useDialogStack()
+ const isTop = useDialogStack()
useEffect(() => {
inputRef.current?.focus()
}, [])
+ /**
+ * Escape belongs to the dialog, not to its text input.
+ *
+ * It was handled only in the input's `onKeyDown`, so the moment focus moved anywhere else — the
+ * emoji grid, "Choose image…", a swatch the user had just clicked — Escape did nothing and the
+ * dialog could only be dismissed with the mouse. `useDialogStack()` was already called; its
+ * answer was simply discarded.
+ *
+ * `isTop()` is the ONLY gate, deliberately matching `confirmKeyAction`: there, `inDialog` guards
+ * Enter and never Escape. The asymmetry is the point — Enter is the affirmative key and must be
+ * aimed at the dialog, while Escape is the safe direction, and requiring focus to be inside the
+ * box would reproduce exactly the bug this fixes for a user whose focus sits on the body. Being
+ * top is what keeps it honest: with a dialog stacked above, the key is not ours, and we must not
+ * `preventDefault` it either or we would swallow it from the dialog the user can see.
+ */
+ useEffect(() => {
+ const onKey = (e: KeyboardEvent): void => {
+ if (e.key !== 'Escape' || !isTop()) return
+ e.preventDefault()
+ onDone(undefined)
+ }
+ window.addEventListener('keydown', onKey)
+ return () => window.removeEventListener('keydown', onKey)
+ }, [isTop, onDone])
+
const commitTyped = (raw: string): void => {
// The same validator the persisted file goes through, so what the picker accepts and what a
// reload accepts cannot drift: type three characters, keep the first grapheme.
@@ -212,12 +237,12 @@ function NodeIconPicker({
spellCheck={false}
onChange={(e) => setTyped(e.target.value)}
onKeyDown={(e) => {
+ // Escape is deliberately NOT handled here: the window listener above owns it for
+ // the whole dialog, and a second handler on one press is the double-answer the
+ // dialog stack exists to prevent.
if (e.key === 'Enter') {
e.preventDefault()
commitTyped(typed)
- } else if (e.key === 'Escape') {
- e.preventDefault()
- onDone(undefined)
}
}}
/>