import type { WebsocketConnection } from '@yaakapp-internal/models'; import { deleteModel, getModel } from '@yaakapp-internal/models'; import { formatDistanceToNowStrict } from 'date-fns'; import { deleteWebsocketConnections } from '../commands/deleteWebsocketConnections'; import { pluralizeCount } from '../lib/pluralize'; import { Dropdown } from './core/Dropdown'; import { Icon } from './core/Icon'; import { IconButton } from './core/IconButton'; import { HStack } from './core/Stacks'; interface Props { connections: WebsocketConnection[]; activeConnection: WebsocketConnection; onPinnedConnectionId: (id: string) => void; } export function RecentWebsocketConnectionsDropdown({ activeConnection, connections, onPinnedConnectionId, }: Props) { const latestConnectionId = connections[0]?.id ?? 'n/a'; return ( deleteModel(activeConnection), disabled: connections.length === 0, }, { label: `Clear ${pluralizeCount('Connection', connections.length)}`, onSelect: () => { const request = getModel('websocket_request', activeConnection.requestId); if (request != null) { deleteWebsocketConnections.mutate(request); } }, hidden: connections.length <= 1, disabled: connections.length === 0, }, { type: 'separator', label: 'History' }, ...connections.map((c) => ({ label: ( {formatDistanceToNowStrict(`${c.createdAt}Z`)} ago •{' '} {c.elapsed}ms ), leftSlot: activeConnection?.id === c.id ? : , onSelect: () => onPinnedConnectionId(c.id), })), ]} > ); }