Skip to content

The agent that grows with you

32.6 KB
Raw Blame
import { useStore } from '@nanostores/react'
import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'

import { RemoteSetupFields } from '@/components/remote-setup/fields'
import { useRemoteSetup } from '@/components/remote-setup/use-remote-setup'
import { Button } from '@/components/ui/button'
import { ConfirmDialog } from '@/components/ui/confirm-dialog'
import { Input } from '@/components/ui/input'
import { Loader } from '@/components/ui/loader'
import type {
  DesktopConnectionKind,
  DesktopConnectionsRegistry,
  DesktopRegistryConnection,
  DesktopRegistryConnectionInput
} from '@/global'
import { useI18n } from '@/i18n'
import {
  CONNECTION_SEARCH_THRESHOLD,
  connectionMatchesQuery,
  sortConnectionsForDisplay
} from '@/lib/connection-display'
import { triggerHaptic } from '@/lib/haptics'
import { Cloud, Globe, Loader2, Monitor, Pencil, Plus, RefreshCw, SearchIcon, Terminal, Trash2 } from '@/lib/icons'
import { $activeConnectionId, setConnectionsRegistry } from '@/store/connections'
import { refreshFleetRoster } from '@/store/fleet-roster'
import { notify, notifyError } from '@/store/notifications'

import { EmptyState, ListRow, Pill, SectionHeading, SettingsBreadcrumbContext, ToggleRow } from './primitives'

const KIND_ICONS: Record<DesktopConnectionKind, typeof Globe> = {
  cloud: Cloud,
  local: Monitor,
  remote: Globe,
  ssh: Terminal
}

interface EditorState {
  // null id → creating a new connection.
  id: null | string
  kind: DesktopConnectionKind
  label: string
  host: string
  keyPath: string
  remoteHermesPath: string
  // ssh remote profile, hydrated on edit so the duplicate key matches the
  // main-process one (user@host:port + profile); the editor doesn't expose it.
  remoteProfile: string
  // Extra gateway headers (access proxies such as Cloudflare Access).
  // `value: ''` on a row that came from storage means "keep the saved
  // secret" (sent as null); a typed value replaces it; a removed row clears
  // it. `stored` marks rows hydrated from headerNames so the placeholder can
  // say "saved" instead of demanding a value.
  headers: { name: string; stored: boolean; value: string }[]
}

function editorFromConnection(conn: DesktopRegistryConnection): EditorState {
  return {
    id: conn.id,
    kind: conn.kind,
    label: conn.label,
    // Reconstruct the composite the single ssh host field displays. The save
    // payload sends ONLY this string (never separate user/port), because
    // normalizeSshConfig gives explicit user/port fields precedence over the
    // parsed host string — sending stored user/port alongside a retyped host
    // would silently resurrect the old values.
    host: conn.host ? `${conn.user ? `${conn.user}@` : ''}${conn.host}${conn.port ? `:${conn.port}` : ''}` : '',
    keyPath: conn.keyPath || '',
    remoteHermesPath: conn.remoteHermesPath || '',
    remoteProfile: conn.remoteProfile || '',
    headers: (conn.headerNames || []).map(name => ({ name, stored: true, value: '' }))
  }
}

function emptyEditor(kind: DesktopConnectionKind): EditorState {
  return {
    id: null,
    kind,
    label: '',
    host: '',
    keyPath: '',
    remoteHermesPath: '',
    remoteProfile: '',
    headers: []
  }
}

/** Dedupe key for a remote/cloud gateway URL: trim, drop trailing slashes, lowercase. */
export function normalizeGatewayUrl(url: string): string {
  return url.trim().replace(/\/+$/, '').toLowerCase()
}

/**
 * Dedupe key for the composite ssh host string the editor shows
 * (`user@host:port`, user/port optional): normalized to `user@host:port` with
 * the default port made explicit so `box` and `box:22` collide.
 */
export function sshCompositeKey(composite: string): string {
  const raw = composite.trim().toLowerCase()

  if (!raw) {
    return ''
  }

  const at = raw.lastIndexOf('@')
  const user = at > 0 ? raw.slice(0, at) : ''
  const rest = at >= 0 ? raw.slice(at + 1) : raw
  const portMatch = rest.match(/^(.*?):(\d+)$/)
  const host = portMatch ? portMatch[1] : rest
  const port = portMatch ? portMatch[2] : '22'

  if (!host) {
    return ''
  }

  return `${user}@${host}:${port}`
}

/**
 * The renderer-side duplicate rule, mirrored by the main process
 * (normalizeConnectionInput enforces the same keys in the save path):
 *  - at most ONE local entry, ever;
 *  - remote/cloud entries are duplicates when their normalized URLs match;
 *  - ssh entries are duplicates on user@host:port + remote profile.
 * Returns the existing entry the candidate collides with, or null.
 */
export function findDuplicateConnection(
  editor: Pick<EditorState, 'host' | 'id' | 'kind' | 'remoteProfile'> & { url: string },
  connections: DesktopRegistryConnection[]
): DesktopRegistryConnection | null {
  if (editor.kind === 'local') {
    return connections.find(c => c.kind === 'local' && c.id !== editor.id) ?? null
  }

  if (editor.kind === 'remote' || editor.kind === 'cloud') {
    const key = normalizeGatewayUrl(editor.url)

    if (!key) {
      return null
    }

    return (
      connections.find(
        c =>
          (c.kind === 'remote' || c.kind === 'cloud') && c.id !== editor.id && normalizeGatewayUrl(c.url || '') === key
      ) ?? null
    )
  }

  const key = sshCompositeKey(editor.host)

  if (!key) {
    return null
  }

  const profile = editor.remoteProfile.trim()

  return (
    connections.find(
      c =>
        c.kind === 'ssh' &&
        c.id !== editor.id &&
        sshCompositeKey(`${c.user ? `${c.user}@` : ''}${c.host ?? ''}${c.port ? `:${c.port}` : ''}`) === key &&
        (c.remoteProfile || '').trim() === profile
    ) ?? null
  )
}

/**
 * Display-only same-backend hint: when two registered connections report the
 * same /api/status install_id, they are one physical backend registered under
 * two addresses (hostname + Tailscale IP). Returns the label of the FIRST
 * earlier sibling sharing this connection's id, or null. Never blocks or
 * deletes — dual routes are legitimate failover config; the roster already
 * collapses the duplicate bot rows.
 */
export function sameBackendPeerLabel(
  conn: Pick<DesktopRegistryConnection, 'id' | 'installId'>,
  connections: Pick<DesktopRegistryConnection, 'id' | 'installId' | 'label'>[]
): null | string {
  if (!conn.installId) {
    return null
  }

  for (const other of connections) {
    if (other.id === conn.id) {
      // Only rows AFTER the first occurrence carry the hint, so exactly one
      // of a same-backend pair is annotated (the later-listed one).
      return null
    }

    if (other.installId === conn.installId) {
      return other.label
    }
  }

  return null
}

function scrollableAncestor(element: HTMLElement): HTMLElement | null {
  let parent = element.parentElement

  while (parent) {
    if (/(auto|scroll)/.test(window.getComputedStyle(parent).overflowY)) {
      return parent
    }

    parent = parent.parentElement
  }

  return null
}

/**
 * The connections registry section of Settings → Gateways: manage the named
 * agent sources (local runtime + any number of remote gateways / Hermes Cloud
 * instances / SSH hosts). Storage-level management — the active/primary
 * switchover UX is the connection-mode controls above this section.
 */
export function ConnectionsRegistrySection() {
  const hasBreadcrumb = useContext(SettingsBreadcrumbContext)
  const { t } = useI18n()
  const s = t.settings.connections
  const activeConnectionId = useStore($activeConnectionId)
  const [registry, setRegistry] = useState<DesktopConnectionsRegistry | null>(null)
  const [loading, setLoading] = useState(true)
  const [editor, setEditor] = useState<EditorState | null>(null)
  const [saving, setSaving] = useState(false)
  const [busyId, setBusyId] = useState<null | string>(null)
  const [testingId, setTestingId] = useState<null | string>(null)
  const [removeTarget, setRemoveTarget] = useState<DesktopRegistryConnection | null>(null)
  const [plainTextConfirm, setPlainTextConfirm] = useState(false)
  const [launchModeBusy, setLaunchModeBusy] = useState(false)
  const [updatingAll, setUpdatingAll] = useState(false)
  const [searchQuery, setSearchQuery] = useState('')
  const searchInputRef = useRef<HTMLInputElement>(null)
  const pendingSearchTopRef = useRef<null | number>(null)
  // Inline duplicate rejection from the save path (dedupe is also enforced in
  // the main process, so a crafted payload can't slip past the UI check).
  const [dupeError, setDupeError] = useState<null | string>(null)

  // The draft's auth mode lives inside the remote-setup state below; the
  // sign-in identity callback runs at login time (after that state exists),
  // so it reads the mode through this ref rather than the render-time closure.
  const remoteAuthModeRef = useRef<'oauth' | 'token'>('token')

  const remote = useRemoteSetup({
    host: 'registry',
    enabled: editor?.kind === 'remote' || editor?.kind === 'cloud',
    onNotice: notify,
    // The registry draft can sign in BEFORE it is saved: the login carries
    // the draft's identity so the main process settles the id the save will
    // reuse and writes the session into the jar the saved connection reads.
    // The kind/authMode gate in oauth-partition.ts decides WHICH jar that is:
    // a cookie-auth remote draft gets its own; cloud and token drafts share
    // the legacy jar, exactly what they resolve to after the save. Pin the
    // settled id into the draft so Save reuses it.
    oauthLoginIdentity: () => ({
      connectionId: editor?.id ?? null,
      label: editor?.label ?? '',
      kind: editor?.kind,
      authMode: editor?.kind === 'cloud' ? 'oauth' : remoteAuthModeRef.current
    }),
    onOAuthLoginSettled: settledId => {
      setEditor(prev => (prev && !prev.id ? { ...prev, id: settledId } : prev))
    }
  })

  remoteAuthModeRef.current = remote.credentials.authMode

  const bridge = window.hermesDesktop?.connections

  const hasLocal = Boolean(registry?.connections.some(c => c.kind === 'local'))

  const publishRegistry = useCallback((next: DesktopConnectionsRegistry) => {
    setRegistry(next)
    setConnectionsRegistry(next)
  }, [])

  const load = useCallback(async () => {
    if (!bridge) {
      setLoading(false)

      return
    }

    setLoading(true)

    try {
      publishRegistry(await bridge.list())
    } catch (err) {
      notifyError(err, s.loadFailed)
    } finally {
      setLoading(false)
    }
  }, [bridge, publishRegistry, s.loadFailed])

  useEffect(() => {
    void load()
  }, [load])

  const openEditor = (next: EditorState | null, saved?: DesktopRegistryConnection): void => {
    setDupeError(null)
    remote.reset({
      url: saved?.url || '',
      authMode: next?.kind === 'cloud' ? 'oauth' : saved?.authMode || 'token',
      tokenSet: saved?.tokenSet ?? false,
      tokenPreview: saved?.tokenPreview ?? null
    })
    setEditor(next)
  }

  const save = useCallback(
    async (allowPlainTextToken: boolean = false): Promise<void> => {
      if (!bridge || !editor) {
        return
      }

      // Duplicate prevention lives in the save path (not just a disabled
      // button): reject a candidate that collides with an existing entry with
      // an inline error before anything crosses the IPC boundary.
      const dupe = findDuplicateConnection({ ...editor, url: remote.credentials.url }, registry?.connections ?? [])

      if (dupe) {
        setDupeError(
          editor.kind === 'local'
            ? s.duplicateLocal
            : editor.kind === 'ssh'
              ? s.duplicateSsh(dupe.label)
              : s.duplicateUrl(dupe.label)
        )

        return
      }

      setDupeError(null)
      setSaving(true)

      try {
        const payload: DesktopRegistryConnectionInput = {
          kind: editor.kind,
          label: editor.label
        }

        if (editor.id) {
          payload.id = editor.id
        }

        if (editor.kind === 'remote' || editor.kind === 'cloud') {
          payload.url = remote.payload.remoteUrl
          payload.authMode = editor.kind === 'cloud' ? 'oauth' : remote.credentials.authMode

          if (editor.kind === 'remote' && remote.payload.remoteToken) {
            payload.token = remote.payload.remoteToken
          }

          if (allowPlainTextToken) {
            payload.allowPlainTextToken = true
          }

          // Authoritative header map: typed value → new secret; empty value on
          // a stored row → null (keep saved secret); a removed row is simply
          // absent, which clears it server-side.
          const headerEntries = editor.headers
            .map(row => ({ name: row.name.trim(), stored: row.stored, value: row.value.trim() }))
            .filter(row => row.name)

          if (headerEntries.length > 0 || editor.headers.length === 0) {
            payload.headers = Object.fromEntries(
              headerEntries.map(row => [row.name, row.value ? row.value : row.stored ? null : ''])
            )
          }
        } else if (editor.kind === 'ssh') {
          // The composite host string (user@host:port) is the single source
          // of truth — never send separate user/port (see editorFromConnection).
          payload.host = editor.host
          payload.keyPath = editor.keyPath || undefined
          payload.remoteHermesPath = editor.remoteHermesPath.trim()
        }

        const result = await bridge.save(payload)
        publishRegistry(result.registry)
        setEditor(null)
        setPlainTextConfirm(false)
      } catch (err) {
        // Keyring-less machine and the user hasn't consented to plain-text
        // storage yet: raise the same opt-in dialog the connection-mode form
        // uses instead of dead-ending the save.
        if (
          !allowPlainTextToken &&
          registry?.secureTokenStorage === false &&
          editor.kind === 'remote' &&
          remote.credentials.authMode === 'token' &&
          remote.credentials.token.trim()
        ) {
          setPlainTextConfirm(true)

          return
        }

        notifyError(err, s.saveFailed)
      } finally {
        setSaving(false)
      }
    },
    [
      bridge,
      editor,
      remote.credentials,
      remote.payload,
      publishRegistry,
      registry?.connections,
      registry?.secureTokenStorage,
      s
    ]
  )

  const remove = useCallback(async () => {
    if (!bridge || !removeTarget) {
      return
    }

    setBusyId(removeTarget.id)

    try {
      const result = await bridge.remove(removeTarget.id)
      publishRegistry(result.registry)
    } catch (err) {
      notifyError(err, s.removeFailed)
    } finally {
      setBusyId(null)
      setRemoveTarget(null)
    }
  }, [bridge, publishRegistry, removeTarget, s.removeFailed])

  const makePrimary = useCallback(
    async (id: string) => {
      if (!bridge) {
        return
      }

      setBusyId(id)

      try {
        const result = await bridge.setPrimary(id)
        publishRegistry(result.registry)
      } catch (err) {
        notifyError(err, s.saveFailed)
      } finally {
        setBusyId(null)
      }
    },
    [bridge, publishRegistry, s.saveFailed]
  )

  const setLaunchMode = useCallback(
    async (mode: 'last-used' | 'primary') => {
      if (!bridge?.setLaunchMode) {
        return
      }

      setLaunchModeBusy(true)

      try {
        const result = await bridge.setLaunchMode(mode)
        publishRegistry(result.registry)
      } catch (err) {
        notifyError(err, s.saveFailed)
      } finally {
        setLaunchModeBusy(false)
      }
    },
    [bridge, publishRegistry, s.saveFailed]
  )

  const test = useCallback(
    async (conn: DesktopRegistryConnection) => {
      if (!bridge) {
        return
      }

      setTestingId(conn.id)

      try {
        const result = await bridge.test(conn.id)
        const reachable = result.ok === true || result.reachable === true

        if (reachable) {
          notify({ title: conn.label, message: s.testOk })
          // A successful Test may have warmed a cold OAuth session that the
          // roster missed; explicit recovery should bypass its cache window.
          void refreshFleetRoster({ force: true })
        } else {
          notifyError(new Error(result.error || conn.label), s.testFailed)
        }
      } catch (err) {
        notifyError(err, s.testFailed)
      } finally {
        setTestingId(null)
      }
    },
    [bridge, s.testFailed, s.testOk]
  )

  // Fan out `hermes update` to every eligible source; per-connection results
  // land as individual toasts so one dead box doesn't hide the others.
  const updateAll = useCallback(async () => {
    if (!bridge?.updateAll) {
      return
    }

    setUpdatingAll(true)

    try {
      const { results } = await bridge.updateAll()

      for (const row of results) {
        if (row.ok) {
          notify({ title: row.label, message: row.detail || s.updateAllDone })
        } else if (row.skipped && row.reason === 'cloud-managed') {
          notify({ title: row.label, message: s.updateSkippedCloud })
        } else if (row.skipped && row.reason === 'darwin-drain-unsupported' && row.detail) {
          // A deliberate per-row skip (e.g. a macOS SSH remote whose running
          // serve Desktop cannot safely stop) — informational, not a failure.
          notify({ title: row.label, message: row.detail })
        } else {
          notifyError(new Error(row.error || row.detail || row.reason || row.label), s.updateAllFailed)
        }
      }
    } catch (err) {
      notifyError(err, s.updateAllFailed)
    } finally {
      setUpdatingAll(false)
    }
  }, [bridge, s.updateAllDone, s.updateAllFailed, s.updateSkippedCloud])

  const kindMeta: Record<DesktopConnectionKind, { label: string; desc: string }> = {
    cloud: { desc: s.kindCloudDesc, label: s.kindCloud },
    local: { desc: s.kindLocalDesc, label: s.kindLocal },
    remote: { desc: s.kindRemoteDesc, label: s.kindRemote },
    ssh: { desc: s.kindSshDesc, label: s.kindSsh }
  }

  const sortedConnections = useMemo(
    () => sortConnectionsForDisplay(registry?.connections ?? []),
    [registry?.connections]
  )

  const showSearch = sortedConnections.length >= CONNECTION_SEARCH_THRESHOLD
  const effectiveSearchQuery = showSearch ? searchQuery : ''

  const displayedConnections = sortedConnections.filter(connection =>
    connectionMatchesQuery(connection, effectiveSearchQuery, [kindMeta[connection.kind].label])
  )

  useLayoutEffect(() => {
    const previousTop = pendingSearchTopRef.current
    const input = searchInputRef.current

    pendingSearchTopRef.current = null

    if (previousTop == null || !input) {
      return
    }

    const scroller = scrollableAncestor(input)

    if (!scroller) {
      return
    }

    const delta = input.getBoundingClientRect().top - previousTop

    if (Math.abs(delta) > 0.5) {
      scroller.scrollTop += delta
    }
  }, [displayedConnections.length, effectiveSearchQuery])

  const updateSearchQuery = (nextQuery: string) => {
    pendingSearchTopRef.current = searchInputRef.current?.getBoundingClientRect().top ?? null
    setSearchQuery(nextQuery)
  }

  if (!bridge) {
    return null
  }

  return (
    <div className={hasBreadcrumb ? undefined : 'mt-8 border-t border-border/60 pt-6'}>
      <SectionHeading icon={Globe} page title={s.title} />
      <p className="mb-1 text-[length:var(--conversation-caption-font-size)] text-(--ui-text-tertiary)">{s.intro}</p>
      {/* Source selection lives in Sessions. Primary is the registry fallback,
          not an immediate workspace switch. */}
      <p className="mb-4 text-[length:var(--conversation-caption-font-size)] text-(--ui-text-tertiary)">
        {s.stagedNote}
      </p>

      {!loading && showSearch && (
        <Input
          aria-label={s.searchPlaceholder}
          containerClassName="mt-3 mb-0 w-full max-w-sm"
          onChange={event => updateSearchQuery(event.target.value)}
          placeholder={s.searchPlaceholder}
          prefix={<SearchIcon className="size-3.5" />}
          ref={searchInputRef}
          size="sm"
          type="search"
          value={searchQuery}
        />
      )}

      {loading ? (
        <Loader className="mx-auto my-2 size-6 text-(--ui-text-tertiary)" label={t.common.loading} type="rose-curve" />
      ) : !registry || registry.connections.length === 0 ? (
        <EmptyState title={s.empty} />
      ) : displayedConnections.length === 0 ? (
        <EmptyState title={s.noSearchResults} />
      ) : (
        displayedConnections.map(conn => {
          const Icon = KIND_ICONS[conn.kind]
          const isCurrent = activeConnectionId === conn.id
          const isPrimary = registry.primary === conn.id
          const busy = busyId === conn.id
          // Display-only: this connection is a second address for a backend
          // already registered under another entry (same install_id).
          const sameBackendPeer = sameBackendPeerLabel(conn, sortedConnections)

          const baseDescription =
            conn.kind === 'ssh'
              ? `${kindMeta[conn.kind].label} · ${conn.user ? `${conn.user}@` : ''}${conn.host}${conn.port ? `:${conn.port}` : ''}`
              : conn.url
                ? `${kindMeta[conn.kind].label} · ${conn.url}`
                : kindMeta[conn.kind].desc

          return (
            <ListRow
              action={
                <div className="flex items-center gap-2">
                  <Button
                    disabled={testingId === conn.id}
                    onClick={() => {
                      triggerHaptic('selection')
                      void test(conn)
                    }}
                    size="sm"
                    variant="outline"
                  >
                    {testingId === conn.id ? <Loader2 className="animate-spin" /> : s.testConnection}
                  </Button>
                  {!isPrimary && (
                    <Button disabled={busy} onClick={() => void makePrimary(conn.id)} size="sm" variant="outline">
                      {s.makePrimary}
                    </Button>
                  )}
                  {conn.kind !== 'local' && (
                    <>
                      <Button
                        aria-label={s.editConnection}
                        onClick={() => openEditor(editorFromConnection(conn), conn)}
                        size="icon-sm"
                        variant="ghost"
                      >
                        <Pencil className="size-3.5" />
                      </Button>
                      <Button
                        aria-label={s.removeConnection}
                        disabled={busy}
                        onClick={() => setRemoveTarget(conn)}
                        size="icon-sm"
                        variant="ghost"
                      >
                        {busy ? <Loader2 className="animate-spin" /> : <Trash2 />}
                      </Button>
                    </>
                  )}
                </div>
              }
              description={
                sameBackendPeer ? `${baseDescription} · ${s.sameBackendHint(sameBackendPeer)}` : baseDescription
              }
              key={conn.id}
              title={
                <span className="flex items-center gap-2">
                  <Icon className="size-4 shrink-0 text-muted-foreground" />
                  <span className="truncate">{conn.label}</span>
                  {isCurrent && <Pill tone="primary">{s.currentPill}</Pill>}
                  {isPrimary && <Pill>{s.primaryPill}</Pill>}
                  {conn.kind === 'local' && <Pill>{s.managedPill}</Pill>}
                </span>
              }
            />
          )
        })
      )}

      {editor ? (
        <div className="mt-4 space-y-3 rounded-lg border border-border/60 p-4">
          <div className="grid grid-cols-2 gap-2 @2xl:grid-cols-4">
            {/* Kind is fixed once created (buttons disable on edit). On create
                every kind is offered; Local is disabled while the managed
                local entry exists (the registry holds at most one). */}
            {(editor.id ? ([editor.kind] as const) : (['local', 'cloud', 'remote', 'ssh'] as const)).map(kind => (
              <Button
                disabled={Boolean(editor.id) || (kind === 'local' && hasLocal)}
                key={kind}
                onClick={() => {
                  setDupeError(null)

                  // Cloud uses browser sign-in even after a token-auth remote edit.
                  if (kind === 'cloud') {
                    remote.setAuthMode('oauth')
                  }

                  setEditor({ ...editor, kind })
                }}
                size="sm"
                variant={editor.kind === kind ? 'default' : 'outline'}
              >
                {kindMeta[kind].label}
              </Button>
            ))}
          </div>
          <p className="text-xs text-muted-foreground">{kindMeta[editor.kind].desc}</p>
          {!editor.id && hasLocal ? <p className="text-xs text-muted-foreground">{s.localAddHint}</p> : null}
          {!editor.id && editor.kind === 'cloud' ? (
            <p className="text-xs text-muted-foreground">{s.cloudAddHint}</p>
          ) : null}

          <ListRow
            action={
              <Input
                onChange={e => setEditor({ ...editor, label: e.target.value })}
                placeholder={s.labelPlaceholder}
                value={editor.label}
              />
            }
            description={s.labelDesc}
            title={s.labelTitle}
          />

          {(editor.kind === 'remote' || editor.kind === 'cloud') && (
            <RemoteSetupFields
              disabled={saving}
              onUrlChange={() => setDupeError(null)}
              setup={remote}
              urlOnly={editor.kind === 'cloud'}
            />
          )}

          {editor.kind === 'cloud' && (
            <ListRow
              action={
                remote.credentials.oauthConnected ? (
                  <Pill tone="primary">{t.settings.gateway.signedIn}</Pill>
                ) : (
                  <Button
                    disabled={saving || remote.signingIn || !remote.payload.remoteUrl}
                    onClick={() => void remote.signIn()}
                    size="sm"
                  >
                    {remote.signingIn ? <Loader2 className="animate-spin" /> : null}
                    {remote.isPassword
                      ? t.settings.gateway.signIn
                      : t.settings.gateway.signInWith(remote.providerLabel)}
                  </Button>
                )
              }
              description={
                remote.credentials.oauthConnected
                  ? remote.isPassword
                    ? t.settings.gateway.authSignedInPassword
                    : t.settings.gateway.authSignedInOauth
                  : remote.isPassword
                    ? t.settings.gateway.authNeedsPassword
                    : t.settings.gateway.authNeedsOauth(remote.providerLabel)
              }
              title={t.settings.gateway.authTitle}
            />
          )}

          {(editor.kind === 'remote' || editor.kind === 'cloud') && (
            <div className="grid gap-2">
              <div>
                <div className="text-sm font-medium">{s.headersTitle}</div>
                <p className="mt-1 text-xs text-muted-foreground">{s.headersDesc}</p>
              </div>
              {editor.headers.map((row, index) => (
                <div className="flex items-center gap-2" key={index}>
                  <Input
                    className="flex-1"
                    onChange={e =>
                      setEditor({
                        ...editor,
                        headers: editor.headers.map((h, i) => (i === index ? { ...h, name: e.target.value } : h))
                      })
                    }
                    placeholder="CF-Access-Client-Id"
                    value={row.name}
                  />
                  <Input
                    className="flex-1"
                    onChange={e =>
                      setEditor({
                        ...editor,
                        headers: editor.headers.map((h, i) => (i === index ? { ...h, value: e.target.value } : h))
                      })
                    }
                    placeholder={row.stored ? s.headerValueSaved : s.headerValuePlaceholder}
                    type="password"
                    value={row.value}
                  />
                  <Button
                    onClick={() => setEditor({ ...editor, headers: editor.headers.filter((_, i) => i !== index) })}
                    size="sm"
                    variant="ghost"
                  >
                    {s.headerRemove}
                  </Button>
                </div>
              ))}
              <div>
                <Button
                  onClick={() =>
                    setEditor({ ...editor, headers: [...editor.headers, { name: '', stored: false, value: '' }] })
                  }
                  size="sm"
                  variant="outline"
                >
                  {s.headerAdd}
                </Button>
              </div>
            </div>
          )}

          {editor.kind === 'ssh' && (
            <>
              <ListRow
                action={
                  <Input
                    onChange={e => {
                      setDupeError(null)
                      setEditor({ ...editor, host: e.target.value })
                    }}
                    placeholder="user@host:22"
                    value={editor.host}
                  />
                }
                title={s.sshHostTitle}
              />
              <ListRow
                action={
                  <Input
                    onChange={e => setEditor({ ...editor, remoteHermesPath: e.target.value })}
                    placeholder={t.settings.gateway.sshHermesPathPlaceholder}
                    value={editor.remoteHermesPath}
                  />
                }
                description={t.settings.gateway.sshHermesPathDesc}
                title={t.settings.gateway.sshHermesPathTitle}
              />
            </>
          )}

          {dupeError ? <p className="text-xs text-destructive">{dupeError}</p> : null}

          <div className="flex justify-end gap-2">
            <Button disabled={saving} onClick={() => openEditor(null)} size="sm" variant="ghost">
              {s.cancel}
            </Button>
            <Button disabled={saving || !editor.label.trim()} onClick={() => void save()} size="sm">
              {saving ? s.saving : s.save}
            </Button>
          </div>
        </div>
      ) : (
        <div className="mt-4 flex items-center gap-2">
          <Button
            onClick={() => {
              triggerHaptic('selection')
              openEditor(emptyEditor('remote'))
            }}
            size="sm"
            variant="outline"
          >
            <Plus className="size-3.5" /> {s.addConnection}
          </Button>
          {bridge?.updateAll && (registry?.connections.length ?? 0) > 1 && (
            <Button
              disabled={updatingAll}
              onClick={() => {
                triggerHaptic('selection')
                void updateAll()
              }}
              size="sm"
              variant="outline"
            >
              {updatingAll ? (
                <>
                  <Loader2 className="animate-spin" /> {s.updateAllRunning}
                </>
              ) : (
                <>
                  <RefreshCw /> {s.updateAll}
                </>
              )}
            </Button>
          )}
        </div>
      )}

      {/* Not gated on connections.length > 1: a registry that drifted down to
          local-only is exactly the state where a user needs to see and change
          the launch behavior, and hiding the control there left hand-editing
          connections.json as the only recourse (#90174). */}
      {!loading && registry && (
        <div className="mt-6 border-t border-border/60 pt-4">
          <ToggleRow
            checked={registry.launchMode === 'last-used'}
            description={s.launchModeDesc}
            disabled={launchModeBusy || !bridge?.setLaunchMode}
            label={s.launchModeTitle}
            onChange={enabled => void setLaunchMode(enabled ? 'last-used' : 'primary')}
          />
        </div>
      )}

      <ConfirmDialog
        confirmLabel={s.removeConnection}
        description={removeTarget ? s.removeConfirmDesc(removeTarget.label) : ''}
        destructive
        onClose={() => setRemoveTarget(null)}
        onConfirm={() => remove()}
        open={Boolean(removeTarget)}
        title={s.removeConfirmTitle}
      />

      {/* Keyring-less opt-in: same consent flow as the connection-mode form. */}
      <ConfirmDialog
        confirmLabel={t.settings.gateway.plainTextConfirmAction}
        description={t.settings.gateway.plainTextConfirmDesc}
        destructive
        onClose={() => setPlainTextConfirm(false)}
        onConfirm={() => save(true)}
        open={plainTextConfirm}
        title={t.settings.gateway.plainTextConfirmTitle}
      />
    </div>
  )
}