|
| 1 | +/** |
| 2 | + * CommandPalette |
| 3 | + * |
| 4 | + * A global, keyboard-driven overlay (⌘K / Ctrl+K) to search for and run any |
| 5 | + * registered app action — the "run surface" that pairs with the read-only |
| 6 | + * Keyboard Shortcuts reference. |
| 7 | + * |
| 8 | + * Fully frontend-only: it lists the centralized action registry, filters with |
| 9 | + * cmdk's built-in fuzzy match, and dispatches the chosen action through the |
| 10 | + * registry's `execute()` — exactly as if the action's hotkey had been pressed. |
| 11 | + * |
| 12 | + * Self-contained: it registers its own open handler (`app.commandPalette`), |
| 13 | + * owns its open state, and integrates with the modal stack for layered close. |
| 14 | + */ |
| 15 | + |
| 16 | +import { useCallback, useMemo, useState } from 'react' |
| 17 | +import { useTranslation } from 'react-i18next' |
| 18 | +import { |
| 19 | + Dialog, |
| 20 | + DialogContent, |
| 21 | + DialogTitle, |
| 22 | + DialogDescription, |
| 23 | +} from '@/components/ui/dialog' |
| 24 | +import { |
| 25 | + Command, |
| 26 | + CommandInput, |
| 27 | + CommandList, |
| 28 | + CommandEmpty, |
| 29 | + CommandGroup, |
| 30 | + CommandItem, |
| 31 | + CommandShortcut, |
| 32 | +} from '@/components/ui/command' |
| 33 | +import { useRegisterModal } from '@/context/ModalContext' |
| 34 | +import { |
| 35 | + useAction, |
| 36 | + useActionRegistry, |
| 37 | + actionsByCategory, |
| 38 | + ACTION_LABEL_KEYS, |
| 39 | + categoryLabelKey, |
| 40 | + type ActionId, |
| 41 | +} from '@/actions' |
| 42 | + |
| 43 | +// Actions that should not appear as palette entries: |
| 44 | +// - the palette's own open action (running it from inside the palette is a no-op) |
| 45 | +// - the arrow-key alternates of Go Back / Go Forward (duplicate labels; the |
| 46 | +// primary bracket-key actions already represent them) |
| 47 | +const EXCLUDED_ACTIONS = new Set<ActionId>([ |
| 48 | + 'app.commandPalette', |
| 49 | + 'nav.goBackAlt', |
| 50 | + 'nav.goForwardAlt', |
| 51 | +]) |
| 52 | + |
| 53 | +export function CommandPalette() { |
| 54 | + const { t } = useTranslation() |
| 55 | + const { execute, canExecute, getHotkeyDisplay } = useActionRegistry() |
| 56 | + const [open, setOpen] = useState(false) |
| 57 | + |
| 58 | + // ⌘K / Ctrl+K toggles the palette. |
| 59 | + useAction('app.commandPalette', () => setOpen(prev => !prev)) |
| 60 | + |
| 61 | + // Participate in the layered modal stack (Cmd+W / X close the topmost modal). |
| 62 | + const handleClose = useCallback(() => setOpen(false), []) |
| 63 | + useRegisterModal(open, handleClose) |
| 64 | + |
| 65 | + // Build the grouped, display-ready action list once. |
| 66 | + const groups = useMemo(() => { |
| 67 | + return Object.entries(actionsByCategory) |
| 68 | + .map(([category, actions]) => ({ |
| 69 | + category, |
| 70 | + heading: t(categoryLabelKey(category)), |
| 71 | + items: actions |
| 72 | + .filter(action => !EXCLUDED_ACTIONS.has(action.id as ActionId)) |
| 73 | + // Only list actions that can actually run right now — hide |
| 74 | + // context-scoped ones (e.g. navigator/search actions) whose handler |
| 75 | + // is disabled, so the palette never shows a dead entry. Evaluated as |
| 76 | + // the palette opens, i.e. against the focus you're returning to. |
| 77 | + .filter(action => canExecute(action.id as ActionId)) |
| 78 | + .map(action => { |
| 79 | + const id = action.id as ActionId |
| 80 | + const labelKey = ACTION_LABEL_KEYS[id] |
| 81 | + return { |
| 82 | + id, |
| 83 | + label: labelKey ? t(labelKey) : action.label, |
| 84 | + hotkey: getHotkeyDisplay(id), |
| 85 | + } |
| 86 | + }), |
| 87 | + })) |
| 88 | + .filter(group => group.items.length > 0) |
| 89 | + // getHotkeyDisplay / t are stable enough for a menu; recompute on open. |
| 90 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 91 | + }, [open]) |
| 92 | + |
| 93 | + const runAction = useCallback( |
| 94 | + (id: ActionId) => { |
| 95 | + // Close first, then run on the next tick. Closing the dialog restores |
| 96 | + // focus to the element that was active before the palette opened, so the |
| 97 | + // action runs in the app's real focus context — actions that open a panel |
| 98 | + // or move focus (e.g. Search) would otherwise fire mid-teardown and get |
| 99 | + // clobbered by the dialog's focus restoration. |
| 100 | + setOpen(false) |
| 101 | + setTimeout(() => execute(id), 0) |
| 102 | + }, |
| 103 | + [execute], |
| 104 | + ) |
| 105 | + |
| 106 | + return ( |
| 107 | + <Dialog open={open} onOpenChange={setOpen}> |
| 108 | + <DialogContent |
| 109 | + showCloseButton={false} |
| 110 | + className="overflow-hidden p-0" |
| 111 | + data-testid="command-palette" |
| 112 | + aria-label={t('commands.title')} |
| 113 | + > |
| 114 | + <DialogTitle className="sr-only">{t('commands.title')}</DialogTitle> |
| 115 | + <DialogDescription className="sr-only"> |
| 116 | + {t('commands.searchCommands')} |
| 117 | + </DialogDescription> |
| 118 | + <Command |
| 119 | + className="[&_[cmdk-group-heading]]:px-3 [&_[cmdk-group-heading]]:py-1.5 [&_[cmdk-group-heading]]:text-xs [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground" |
| 120 | + > |
| 121 | + <CommandInput |
| 122 | + data-testid="command-palette-input" |
| 123 | + placeholder={t('commands.searchCommands')} |
| 124 | + /> |
| 125 | + <CommandList> |
| 126 | + <CommandEmpty data-testid="command-palette-empty"> |
| 127 | + {t('common.noResultsFound')} |
| 128 | + </CommandEmpty> |
| 129 | + {groups.map(group => ( |
| 130 | + <CommandGroup key={group.category} heading={group.heading}> |
| 131 | + {group.items.map(item => ( |
| 132 | + <CommandItem |
| 133 | + key={item.id} |
| 134 | + value={`${item.label} ${item.id}`} |
| 135 | + data-testid="command-palette-item" |
| 136 | + onSelect={() => runAction(item.id)} |
| 137 | + > |
| 138 | + <span>{item.label}</span> |
| 139 | + {item.hotkey && ( |
| 140 | + <CommandShortcut>{item.hotkey}</CommandShortcut> |
| 141 | + )} |
| 142 | + </CommandItem> |
| 143 | + ))} |
| 144 | + </CommandGroup> |
| 145 | + ))} |
| 146 | + </CommandList> |
| 147 | + </Command> |
| 148 | + </DialogContent> |
| 149 | + </Dialog> |
| 150 | + ) |
| 151 | +} |
0 commit comments