import { Modal, useMantineColorScheme } from '@mantine/core'; import { IconBug, IconCalendarTime, IconFileReport, IconHistory, IconHome, IconKeyboard, IconListCheck, IconMessageCircle, IconMoon, IconPlayerPlay, IconPlus, IconSearch, IconServer, IconSettings, IconSun, IconUserCheck, } from '@/components/icons'; import { useEffect, useMemo, useRef, useState, type ReactNode } from 'react'; import { useNavigate } from 'react-router-dom'; import { useRuns } from '../api/hooks'; import { useAuth } from '../auth/AuthContext'; import { runAreas, runTitle } from '../domain/runName'; import { useWaitsOnMe } from '../domain/waiting'; import { isMac } from './shell/TopBar'; import { useShell } from './shell/prefs'; interface Item { id: string; group: string; label: string; hint?: string; keywords?: string; icon: ReactNode; run: () => void } const I = { size: 18, stroke: 1.7 }; /** The command palette (Ctrl or Command + K, or the search field): waiting items, go to, test runs, actions, and Ask. * It only navigates or opens something; nothing changes until the person confirms on the screen it opens. */ export function CommandPalette({ opened, onClose }: { opened: boolean; onClose: () => void }) { const nav = useNavigate(); const { can } = useAuth(); const mine = useWaitsOnMe(); const { chat, newRun, openShortcuts } = useShell(); const { colorScheme, setColorScheme } = useMantineColorScheme(); const runs = useRuns(40, 0); const [q, setQ] = useState(''); const [sel, setSel] = useState(0); const input = useRef(null); useEffect(() => { if (opened) { setQ(''); setSel(0); } }, [opened]); const items = useMemo(() => { const go = (to: string) => () => { onClose(); nav(to); }; const all = runs.data?.items ?? []; const waiting = all.filter(mine); const out: Item[] = []; for (const r of waiting) { out.push({ id: `w-${r.run_id}`, group: 'Waiting for you', icon: , keywords: `${runTitle(r)} approve decide review`, label: r.pending_gate === 'human_review' ? `Decide the failures in ${runAreas(r)}` : `Approve the scenarios for ${runAreas(r)}`, run: go(`/runs/${r.run_id}`), }); } out.push( { id: 'g-home', group: 'Go to', label: 'Home', hint: 'G H', keywords: 'patch readiness', icon: , run: go('/') }, { id: 'g-runs', group: 'Go to', label: 'Test runs', hint: 'G R', icon: , run: go('/runs') }, { id: 'g-scen', group: 'Go to', label: 'Scenarios', hint: 'G S', icon: , run: go('/scenarios') }, { id: 'g-def', group: 'Go to', label: 'Defects', hint: 'G D', keywords: 'tickets bugs', icon: , run: go('/tickets') }, { id: 'g-rep', group: 'Go to', label: 'Reports', hint: 'G P', keywords: 'sign off', icon: , run: go('/reports') }, { id: 'g-env', group: 'Go to', label: 'Test environments', hint: 'G E', keywords: 'pods fusion practice', icon: , run: go('/pods') }, ); if (can('admin')) { out.push( { id: 'g-sch', group: 'Go to', label: 'Scheduled runs', keywords: 'schedule monthly quarterly', icon: , run: go('/schedules') }, { id: 'g-set', group: 'Go to', label: 'Settings', keywords: 'ai models users automatic approval emergency stop kill switch', icon: , run: go('/admin') }, { id: 'g-aud', group: 'Go to', label: 'Audit log', keywords: 'verify chain', icon: , run: go('/audit') }, ); } for (const r of all.slice(0, 40)) { out.push({ id: `r-${r.run_id}`, group: 'Test runs', label: runTitle(r), keywords: `${r.run_id} ${r.status}`, icon: , run: go(`/runs/${r.run_id}`) }); } if (can('run.start')) out.push({ id: 'a-new', group: 'Actions', label: 'New test run', hint: 'N', keywords: 'start', icon: , run: () => { onClose(); newRun(); } }); out.push( { id: 'a-theme', group: 'Actions', label: colorScheme === 'dark' ? 'Use the light appearance' : 'Use the dark appearance', keywords: 'theme dark light appearance', icon: colorScheme === 'dark' ? : , run: () => { setColorScheme(colorScheme === 'dark' ? 'light' : 'dark'); onClose(); } }, { id: 'a-keys', group: 'Actions', label: 'Keyboard shortcuts', hint: '?', icon: , run: () => { onClose(); openShortcuts(); } }, ); return out; }, [runs.data, can, mine, nav, onClose, newRun, openShortcuts, colorScheme, setColorScheme]); const needle = q.trim().toLowerCase(); const shown = useMemo(() => { const words = needle.split(/\s+/).filter(Boolean); const hit = (it: Item) => words.every((w) => `${it.label} ${it.keywords ?? ''}`.toLowerCase().includes(w)); let list = needle ? items.filter(hit) : items.filter((it) => it.group !== 'Test runs'); list = list.slice(0, needle ? 12 : 14); if (chat) { list = [...list, { id: 'ask', group: 'Ask', label: needle ? `Ask: “${q.trim()}”` : 'Ask the co-worker a question', hint: 'Enter', icon: , run: () => { onClose(); chat.ask(needle ? q.trim() : undefined); } }]; } return list; }, [items, needle, q, chat, onClose]); useEffect(() => setSel(0), [needle]); const groups: string[] = []; for (const it of shown) if (!groups.includes(it.group)) groups.push(it.group); const flat = groups.flatMap((g) => shown.filter((it) => it.group === g)); const onKey = (e: React.KeyboardEvent) => { if (e.key === 'ArrowDown') { e.preventDefault(); setSel((s) => Math.min(flat.length - 1, s + 1)); } else if (e.key === 'ArrowUp') { e.preventDefault(); setSel((s) => Math.max(0, s - 1)); } else if (e.key === 'Enter') { e.preventDefault(); flat[sel]?.run(); } }; useEffect(() => { document.getElementById(`pal-${sel}`)?.scrollIntoView?.({ block: 'nearest' }); }, [sel]); return (
setQ(e.currentTarget.value)} placeholder="Search, go to a page, or ask a question" aria-label="Search or ask" role="combobox" aria-expanded aria-controls="pal-list" aria-activedescendant={flat[sel] ? `pal-${sel}` : undefined} autoComplete="off" spellCheck={false} /> Esc
{!flat.length &&
Nothing matches.
} {groups.map((g) => (
{g}
{shown.filter((it) => it.group === g).map((it) => { const idx = flat.indexOf(it); return (
setSel(idx)} onClick={it.run}> {it.icon}{it.label}{it.hint && {it.hint}}
); })}
))}
Up and Down to moveEnter to openNothing changes until you confirm.{isMac() ? '⌘K' : 'Ctrl K'}
); }