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 { 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 { useFleetRoster } from './use-fleet-roster' import { useProfilePrewarm } from './use-profile-prewarm' /** * 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 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) const { dialog: localDeviceDialog, request: requestLocalDevice } = useLocalDeviceSwitch() useFleetRoster(multipleConnections) // 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] ) 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, profile: agent.profile, replaceCenter: agent.profile === 'default' }).then(accepted => { setPendingRoute(current => (current === key ? null : current)) if (accepted) { commitRestSwitch(agent) } }) } const triggerLabel = showAll ? p.allProfiles : active ? profileLabel(active) : p.title return (
{ordered.map(profile => ( ))} {/* 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 && ( )} {restGroups.map(group => ( ))} setCreateOpen(true)}> void runImportProfileFlow()}> navigate(PROFILES_ROUTE)}> setCreateOpen(false)} onCreated={async name => { await refreshActiveProfile() selectProfile(name) }} open={createOpen} profiles={profiles} /> {localDeviceDialog}
) } function ProfileItem({ color, isDefault, label, name }: { color: null | string isDefault: boolean label: string name: string }) { const { cancelPrewarm, notePointerMove, startPrewarm } = useProfilePrewarm(name, prewarmProfilePick) return ( ) }