import classNames from 'classnames'; import type { ReactNode } from 'react'; import { useMemo, useCallback, useState } from 'react'; import { useActiveEnvironmentId } from '../hooks/useActiveEnvironmentId'; import { useAppRoutes } from '../hooks/useAppRoutes'; import { getRecentEnvironments } from '../hooks/useRecentEnvironments'; import { useRequests } from '../hooks/useRequests'; import { useWorkspaces } from '../hooks/useWorkspaces'; import { fallbackRequestName } from '../lib/fallbackRequestName'; import { Input } from './core/Input'; export function CommandPalette({ onClose }: { onClose: () => void }) { const [selectedIndex, setSelectedIndex] = useState(0); const routes = useAppRoutes(); const activeEnvironmentId = useActiveEnvironmentId(); const workspaces = useWorkspaces(); const requests = useRequests(); const items = useMemo<{ label: string; onSelect: () => void; key: string }[]>(() => { const items = []; for (const r of requests) { items.push({ key: `switch-request-${r.id}`, label: `Switch Request → ${fallbackRequestName(r)}`, onSelect: () => { return routes.navigate('request', { workspaceId: r.workspaceId, requestId: r.id, environmentId: activeEnvironmentId ?? undefined, }); }, }); } for (const w of workspaces) { items.push({ key: `switch-workspace-${w.id}`, label: `Switch Workspace → ${w.name}`, onSelect: async () => { const environmentId = (await getRecentEnvironments(w.id))[0]; return routes.navigate('workspace', { workspaceId: w.id, environmentId, }); }, }); } return items; }, [activeEnvironmentId, requests, routes, workspaces]); const handleSelectAndClose = (cb: () => void) => { onClose(); cb(); }; const handleKeyDown = useCallback( (e: KeyboardEvent) => { if (e.key === 'ArrowDown') { setSelectedIndex((prev) => prev + 1); } else if (e.key === 'ArrowUp') { setSelectedIndex((prev) => prev - 1); } else if (e.key === 'Enter') { const item = items[selectedIndex]; if (item) { handleSelectAndClose(item.onSelect); } } }, [items, onClose, selectedIndex], ); return (
{items.map((v, i) => ( handleSelectAndClose(v.onSelect)} > {v.label} ))}
); } function CommandPaletteItem({ children, active, onClick, }: { children: ReactNode; active: boolean; onClick: () => void; }) { return ( ); }