Skip to content

The agent that grows with you

fix(desktop): focus SSH custom host input on first selection (#76825)

webtecnica49c444eccd43
diff --git a/apps/desktop/src/app/settings/gateway-settings.test.tsx b/apps/desktop/src/app/settings/gateway-settings.test.tsx
index f740a2e3336..e33cf269b48 100644
--- a/apps/desktop/src/app/settings/gateway-settings.test.tsx
+++ b/apps/desktop/src/app/settings/gateway-settings.test.tsx
@@ -1,6 +1,6 @@
import { GatewayReauthRequiredError } from '@hermes/shared'
import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
-import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { deferred } from '@/test/deferred'
@@ -25,6 +25,14 @@ vi.mock('./connections-registry', async importOriginal => ({
...(await importOriginal<any>()),
ConnectionsRegistrySection: () => null
}))
+
+// Radix Select calls scrollIntoView / pointer-capture APIs jsdom lacks.
+beforeAll(() => {
+ Element.prototype.scrollIntoView = vi.fn()
+ Element.prototype.hasPointerCapture = vi.fn(() => false)
+ Element.prototype.releasePointerCapture = vi.fn()
+})
+
const getConnectionConfig = vi.fn()
const saveConnectionConfig = vi.fn()
@@ -528,4 +536,34 @@ describe('GatewaySettings', () => {
registry.value = null
})
})
+
+ it('focuses the custom SSH host input on the first "Custom" selection', async () => {
+ getConnectionConfig.mockResolvedValue({
+ ...localConnection,
+ mode: 'ssh',
+ sshHost: '',
+ sshUser: '',
+ sshPort: 22,
+ sshKeyPath: '',
+ sshRemoteHermesPath: '',
+ sshRemoteProfile: ''
+ })
+ const sshConfigHosts = vi.fn().mockResolvedValue({ hosts: ['github.com'] })
+ Object.assign(window.hermesDesktop, { sshConfigHosts })
+
+ render(<GatewaySettings />)
+
+ // With ~/.ssh/config aliases available the host field is a dropdown.
+ const trigger = await screen.findByRole('combobox')
+ fireEvent.click(trigger)
+ fireEvent.click(screen.getByRole('option', { name: 'Custom (enter manually)…' }))
+
+ // The dropdown is swapped for a free-text input that must be focused and
+ // immediately typeable on the FIRST selection (no round-trip through
+ // another option) — Radix's deferred focus restoration must not steal it.
+ const hostRow = screen.getByText('Host').closest('.grid') as HTMLElement
+ const input = within(hostRow).getByRole('textbox') as HTMLInputElement
+
+ await waitFor(() => expect(document.activeElement).toBe(input))
+ })
})
diff --git a/apps/desktop/src/app/settings/gateway-settings.tsx b/apps/desktop/src/app/settings/gateway-settings.tsx
index da789224da7..a39358d6b97 100644
--- a/apps/desktop/src/app/settings/gateway-settings.tsx
+++ b/apps/desktop/src/app/settings/gateway-settings.tsx
@@ -271,6 +271,7 @@ function GatewayConnectionSettings({ embedded, standalone }: { embedded: boolean
const [lastTest, setLastTest] = useState<null | string>(null)
const [sshHostSuggestions, setSshHostSuggestions] = useState<string[]>([])
const [sshCustomHost, setSshCustomHost] = useState(false)
+ const sshHostInputRef = useRef<HTMLInputElement>(null)
const sshResolveSeq = useRef(0)
const sshTestSeq = useRef(0)
const saveSeq = useRef(0)
@@ -512,6 +513,23 @@ function GatewayConnectionSettings({ embedded, standalone }: { embedded: boolean
return () => void (cancelled = true)
}, [state.mode])
+ // Radix Select restores focus to its trigger when the popover closes. That
+ // restoration runs after `onValueChange` has swapped the Select out for the
+ // custom-host Input, so it races with the Input's `autoFocus` and steals
+ // focus from the freshly mounted field (the trigger is already unmounted, so
+ // focus lands on <body>). Re-focus on the next frame, once the Select's
+ // deferred focus restoration has settled, so the first "Custom" selection
+ // yields a field that immediately accepts typing.
+ useEffect(() => {
+ if (!sshCustomHost) {
+ return
+ }
+
+ const frame = requestAnimationFrame(() => sshHostInputRef.current?.focus())
+
+ return () => cancelAnimationFrame(frame)
+ }, [sshCustomHost])
+
// eslint-disable-next-line no-restricted-syntax -- monotonic request-sequence counters, not an atom mirror
useEffect(() => {
contextSeq.current += 1
@@ -1320,7 +1338,14 @@ function GatewayConnectionSettings({ embedded, standalone }: { embedded: boolean
action={
<Select
onValueChange={selectHost}
- value={sshHostSuggestions.includes(state.sshHost) ? state.sshHost : SSH_HOST_CUSTOM}
+ // Only report an actual host here. Leaving the value at the
+ // empty string shows the placeholder; using SSH_HOST_CUSTOM
+ // while the dropdown is still rendered would make the FIRST
+ // "Custom" click a no-op (Radix suppresses onValueChange when
+ // a controlled value doesn't change), so the custom-host
+ // input would never mount without a round-trip through
+ // another option.
+ value={sshHostSuggestions.includes(state.sshHost) ? state.sshHost : ''}
>
<SelectTrigger className={cn('h-8', CONTROL_TEXT)}>
<SelectValue placeholder={g.sshHostPick} />
@@ -1356,6 +1381,7 @@ function GatewayConnectionSettings({ embedded, standalone }: { embedded: boolean
void resolveSshHost(state.sshHost)
}}
onChange={event => setState(current => selectSshHost(current, event.target.value))}
+ ref={sshHostInputRef}
value={state.sshHost}
/>
}