()
| 39 | } |
| 40 | |
| 41 | export function CommandPalette() { |
| 42 | const { |
| 43 | paletteOpen, |
| 44 | closePalette, |
| 45 | commands, |
| 46 | runCommand, |
| 47 | recentCommandIds, |
| 48 | openHelp, |
| 49 | } = useCommandRegistry(); |
| 50 | |
| 51 | const [query, setQuery] = useState(""); |
| 52 | const [activeIndex, setActiveIndex] = useState(0); |
| 53 | const inputRef = useRef<HTMLInputElement>(null); |
| 54 | const listRef = useRef<HTMLDivElement>(null); |
| 55 | |
| 56 | // Reset state when palette opens |
| 57 | useEffect(() => { |
| 58 | if (paletteOpen) { |
| 59 | setQuery(""); |
| 60 | setActiveIndex(0); |
| 61 | // Small delay to let the dialog animate in before focusing |
| 62 | setTimeout(() => inputRef.current?.focus(), 10); |
| 63 | } |
| 64 | }, [paletteOpen]); |
| 65 | |
| 66 | const filteredGroups = useMemo<GroupedResults[]>(() => { |
| 67 | if (!query.trim()) { |
| 68 | // Show recents first, then all categories |
| 69 | const recentCmds = recentCommandIds |
| 70 | .map((id) => commands.find((c) => c.id === id)) |
| 71 | .filter((c): c is Command => !!c); |
| 72 | |
| 73 | const groups: GroupedResults[] = []; |
| 74 | if (recentCmds.length > 0) { |
| 75 | groups.push({ label: "Recent", commands: recentCmds }); |
| 76 | } |
| 77 | for (const cat of SHORTCUT_CATEGORIES) { |
| 78 | const catCmds = commands.filter((c) => c.category === cat); |
| 79 | if (catCmds.length > 0) { |
| 80 | groups.push({ label: cat, commands: catCmds }); |
| 81 | } |
| 82 | } |
| 83 | return groups; |
| 84 | } |
| 85 | |
| 86 | // Search mode: flat scored list, re-grouped by category |
| 87 | const scored = commands |
| 88 | .map((cmd) => ({ cmd, s: score(cmd, query) })) |
| 89 | .filter(({ s }) => s > 0) |
| 90 | .sort((a, b) => b.s - a.s) |
| 91 | .map(({ cmd }) => cmd); |
| 92 | |
| 93 | if (scored.length === 0) return []; |
| 94 | |
| 95 | const byCategory: Partial<Record<ShortcutCategory, Command[]>> = {}; |
| 96 | for (const cmd of scored) { |
| 97 | if (!byCategory[cmd.category]) byCategory[cmd.category] = []; |
| 98 | byCategory[cmd.category]!.push(cmd); |
nothing calls this directly
no test coverage detected