Skip to content

The agent that grows with you

12.6 KB
Raw Blame
import { LOCAL_CONNECTION_ID } from '@hermes/shared'
import { useStore } from '@nanostores/react'
import { useEffect, useMemo, useRef, useState } from 'react'
import { useNavigate } from 'react-router'

import { Button } from '@/components/ui/button'
import { Codicon } from '@/components/ui/codicon'
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuRadioGroup,
  DropdownMenuRadioItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger
} from '@/components/ui/dropdown-menu'
import { ProfileGlyph } from '@/components/ui/profile-glyph'
import { useI18n } from '@/i18n'
import { triggerHaptic } from '@/lib/haptics'
import { hostHas } from '@/lib/host-capabilities'
import { Loader2 } from '@/lib/icons'
import { resolveProfileColor } from '@/lib/profile-color'
import { cn } from '@/lib/utils'
import {
  $activeConnectionId,
  $connectionsRegistry,
  $hasMultipleConnections,
  selectConnection
} from '@/store/connections'
import { $fleetRoster } from '@/store/fleet-roster'
import { notifyError } from '@/store/notifications'
import {
  $activeGatewayProfile,
  $profileColors,
  $profileCreateRequest,
  $profileOrder,
  $profiles,
  $showAllProfiles,
  ALL_PROFILES,
  normalizeProfileKey,
  prewarmProfilePick,
  profileLabel,
  refreshActiveProfile,
  selectProfile,
  setShowAllProfiles,
  sortByProfileOrder
} from '@/store/profile'
import { runImportProfileFlow } from '@/store/profile-share'

import { CreateProfileDialog } from '../../profiles/create-profile-dialog'
import { PROFILES_ROUTE } from '../../routes'

import { FleetGatewayMenuGroup } from './fleet-gateway-menu-group'
import { buildRestGroups, type FleetAgent, fleetRouteKey } from './fleet-rail'
import { useLocalDeviceSwitch } from './local-device-switch'
import { ProfileLaunchContextMenu } from './profile-launch-menu'
import { ProfileRowGlyph, UnservedProfileNote, useUnservedProfiles } from './unserved-profiles'
import { useFleetRoster } from './use-fleet-roster'
import { useProfilePrewarm } from './use-profile-prewarm'
import { useProfileRailRefreshOnActive } from './use-profile-rail-refresh-on-active'

/**
 * The profile picker that sits beside the gateway switcher in the statusbar
 * while the colored rail is hidden — the same choices the rail offers (this
 * gateway's profiles, every other gateway's agents in fleet mode, new / import
 * / manage; in the Webapp also right-click open-in-new-window / set-as-default)
 * in a dropdown that reads like its neighbour. The rail keeps the gestures that
 * need squares (drag-order, hold-to-recolor); this is the plain-dropdown door
 * for people who run profiles as bots and don't want a strip of them.
 */
export function ProfileSwitcher({ compact = false }: { compact?: boolean }) {
  const { t } = useI18n()
  const p = t.profiles
  const navigate = useNavigate()
  const profiles = useStore($profiles)
  const order = useStore($profileOrder)
  const colors = useStore($profileColors)
  const gatewayProfile = useStore($activeGatewayProfile)
  const showAll = useStore($showAllProfiles)
  const multipleConnections = useStore($hasMultipleConnections)
  const registry = useStore($connectionsRegistry)
  const activeConnectionId = useStore($activeConnectionId)
  const roster = useStore($fleetRoster)
  const [createOpen, setCreateOpen] = useState(false)
  const [pendingRoute, setPendingRoute] = useState<null | string>(null)
  const { dialog: localDeviceDialog, request: requestLocalDevice } = useLocalDeviceSwitch()

  useFleetRoster(multipleConnections)
  const unserved = useUnservedProfiles(profiles)
  // The Webapp always takes this dropdown in place of the rail's squares
  // (profile-switcher.tsx), so there it keeps the list fresh the same way and
  // carries the rail's right-click launch menu. Native Desktop's picker is main's.
  const browserHost = !hostHas('profileRailSquares')
  useProfileRailRefreshOnActive(browserHost)

  // The `profile.create` hotkey bumps this request atom; the rail answers it
  // while mounted, so with the rail hidden this picker owns the dialog instead.
  const createRequest = useStore($profileCreateRequest)
  const lastCreateRef = useRef(createRequest)

  // eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)
  useEffect(() => {
    if (createRequest === lastCreateRef.current) {
      return
    }

    lastCreateRef.current = createRequest
    setCreateOpen(true)
  }, [createRequest])

  const connections = registry?.connections

  const restGroups = useMemo(
    () =>
      multipleConnections ? buildRestGroups({ activeConnectionId, connections: connections ?? [], order, roster }) : [],
    [activeConnectionId, connections, multipleConnections, order, roster]
  )

  // Launch actions name the route a pick would dial, as the rail's squares do:
  // named profiles on This device keep the legacy door (per-profile overrides).
  const namedConnectionId = activeConnectionId === LOCAL_CONNECTION_ID ? null : activeConnectionId
  const activeKey = normalizeProfileKey(gatewayProfile)
  const defaultProfile = profiles.find(profile => profile.is_default)

  const named = sortByProfileOrder(
    profiles.filter(profile => !profile.is_default),
    order
  )

  const ordered = defaultProfile ? [defaultProfile, ...named] : named
  const active = showAll ? undefined : profiles.find(profile => normalizeProfileKey(profile.name) === activeKey)
  const value = showAll ? ALL_PROFILES : (active?.name ?? '')

  const choose = (name: string) => {
    triggerHaptic('selection')

    if (name === ALL_PROFILES) {
      setShowAllProfiles(true)
    } else {
      selectProfile(name)
    }
  }

  const commitRestSwitch = (agent: FleetAgent) => {
    const key = fleetRouteKey(agent.connectionId, agent.profile)
    triggerHaptic('selection')
    setPendingRoute(key)

    void selectConnection(agent.connectionId, { profile: agent.profile })
      .catch((error: unknown) => notifyError(error, p.switchConnectionFailed(agent.connectionLabel)))
      .finally(() => setPendingRoute(current => (current === key ? null : current)))
  }

  const switchToRest = (agent: FleetAgent) => {
    if (agent.connectionKind !== 'local') {
      commitRestSwitch(agent)

      return
    }

    const key = fleetRouteKey(agent.connectionId, agent.profile)
    setPendingRoute(key)

    void requestLocalDevice({
      connectionId: agent.connectionId,
      label: agent.connectionLabel
    }).then(accepted => {
      setPendingRoute(current => (current === key ? null : current))

      if (accepted) {
        commitRestSwitch(agent)
      }
    })
  }

  const triggerLabel = showAll ? p.allProfiles : active ? profileLabel(active) : p.title

  return (
    <div
      aria-busy={pendingRoute !== null}
      aria-label={p.title}
      className={cn('min-w-16 shrink overflow-hidden', compact ? 'h-full max-w-40' : 'max-w-72')}
      data-slot="profile-switcher"
      role="group"
    >
      <DropdownMenu>
        <DropdownMenuTrigger asChild>
          <Button
            aria-label={`${p.title}: ${triggerLabel}`}
            className={cn(
              'w-full min-w-0 justify-between overflow-hidden px-1 text-(--ui-text-secondary) data-[state=open]:bg-(--ui-control-active-background) data-[state=open]:text-foreground',
              compact && 'h-full min-h-0 rounded-none px-1.5 text-[0.6875rem] font-normal'
            )}
            size="xs"
            type="button"
            variant="ghost"
          >
            <span className="flex min-w-0 flex-1 items-center gap-1 overflow-hidden">
              {pendingRoute !== null ? (
                <Loader2 aria-hidden="true" className="size-3 shrink-0 animate-spin" />
              ) : showAll || !active ? (
                <Codicon aria-hidden="true" className="shrink-0" name="layers" size="0.75rem" />
              ) : (
                <ProfileGlyph
                  aria-hidden="true"
                  className="size-3 text-[0.4375rem]"
                  color={resolveProfileColor(active.name, colors)}
                  isDefault={active.is_default}
                  name={active.name}
                />
              )}
              <span className="truncate">{triggerLabel}</span>
            </span>
            <Codicon aria-hidden="true" className="shrink-0 opacity-60" name="chevron-down" size="0.875rem" />
          </Button>
        </DropdownMenuTrigger>
        <DropdownMenuContent align="start" className="min-w-48 max-w-72" collisionPadding={8} side="top">
          <DropdownMenuRadioGroup onValueChange={choose} value={value}>
            {ordered.map(profile => (
              <ProfileItem
                color={resolveProfileColor(profile.name, colors)}
                connectionId={profile.is_default ? activeConnectionId : namedConnectionId}
                isDefault={profile.is_default}
                key={profile.name}
                label={profileLabel(profile)}
                launchMenu={browserHost}
                name={profile.name}
                unserved={unserved.has(normalizeProfileKey(profile.name))}
              />
            ))}
            {/* Nothing to widen to with one profile: the ALL view only exists
                once a second profile does (the rail hides its toggle then too). */}
            {profiles.length > 1 && (
              <DropdownMenuRadioItem className="min-w-0" value={ALL_PROFILES}>
                <span className="flex min-w-0 items-center gap-1.5">
                  <Codicon aria-hidden="true" className="text-(--ui-text-tertiary)" name="layers" size="0.875rem" />
                  <span className="truncate">{p.allProfiles}</span>
                </span>
              </DropdownMenuRadioItem>
            )}
          </DropdownMenuRadioGroup>
          {restGroups.map(group => (
            <FleetGatewayMenuGroup
              group={group}
              key={group.connectionId}
              onSelect={switchToRest}
              slot="profile-switcher-gateway"
              wrapRow={
                browserHost
                  ? (row, agent, label) => (
                      <ProfileLaunchContextMenu
                        connectionId={agent.connectionId}
                        key={agent.profile}
                        label={label}
                        profile={agent.profile}
                      >
                        {row}
                      </ProfileLaunchContextMenu>
                    )
                  : undefined
              }
            />
          ))}
          <DropdownMenuSeparator />
          <DropdownMenuItem onSelect={() => setCreateOpen(true)}>
            <Codicon aria-hidden="true" name="add" size="0.875rem" />
            <span className="truncate">{p.newProfile}</span>
          </DropdownMenuItem>
          <DropdownMenuItem onSelect={() => void runImportProfileFlow()}>
            <Codicon aria-hidden="true" name="cloud-download" size="0.875rem" />
            <span className="truncate">{p.importProfile}</span>
          </DropdownMenuItem>
          <DropdownMenuItem onSelect={() => navigate(PROFILES_ROUTE)}>
            <Codicon aria-hidden="true" name="settings-gear" size="0.875rem" />
            <span className="truncate">{p.manageProfiles}</span>
          </DropdownMenuItem>
        </DropdownMenuContent>
      </DropdownMenu>

      <CreateProfileDialog
        onClose={() => setCreateOpen(false)}
        onCreated={async name => {
          await refreshActiveProfile()
          selectProfile(name)
        }}
        open={createOpen}
        profiles={profiles}
      />
      {localDeviceDialog}
    </div>
  )
}

interface ProfileItemProps {
  color: null | string
  connectionId: null | string
  isDefault: boolean
  label: string
  /** Wrap the row in the rail's right-click launch menu (the Webapp's picker). */
  launchMenu: boolean
  name: string
  /** Listed by its gateway but not served (the Webapp): shown, never picked, since the pick is refused. */
  unserved: boolean
}

function ProfileItem({ color, connectionId, isDefault, label, launchMenu, name, unserved }: ProfileItemProps) {
  const { cancelPrewarm, notePointerMove, startPrewarm } = useProfilePrewarm(name, prewarmProfilePick)

  const row = (
    <DropdownMenuRadioItem
      className="min-w-0"
      disabled={unserved}
      onPointerEnter={startPrewarm}
      onPointerLeave={cancelPrewarm}
      onPointerMove={notePointerMove}
      value={name}
    >
      <span className="flex min-w-0 items-center gap-1.5">
        <ProfileRowGlyph color={color} isDefault={isDefault} name={name} unserved={unserved} />
        <span className="truncate">{label}</span>
      </span>
      {unserved && <UnservedProfileNote />}
    </DropdownMenuRadioItem>
  )

  return launchMenu ? (
    <ProfileLaunchContextMenu connectionId={connectionId} label={label} profile={name}>
      {row}
    </ProfileLaunchContextMenu>
  ) : (
    row
  )
}