| 39 | } |
| 40 | |
| 41 | export function CommandMenu({ |
| 42 | icon, |
| 43 | open, |
| 44 | onOpenChange, |
| 45 | commandSearchTerm, |
| 46 | onCommandSearchTermChange, |
| 47 | loading, |
| 48 | searchedVideos, |
| 49 | onCardClick, |
| 50 | onClose, |
| 51 | }: CommandMenuProps) { |
| 52 | const router = useRouter(); |
| 53 | |
| 54 | const handleShortcut = useCallback( |
| 55 | (route: string) => { |
| 56 | if (route.startsWith('http')) { |
| 57 | window.location.href = route; |
| 58 | } else { |
| 59 | router.push(route); |
| 60 | } |
| 61 | onClose(); |
| 62 | }, |
| 63 | [router, onClose], |
| 64 | ); |
| 65 | |
| 66 | // Shortcut Handlers |
| 67 | const handleKeyDown = useCallback( |
| 68 | (e: KeyboardEvent) => { |
| 69 | if (open && e.ctrlKey) { |
| 70 | const shortcuts = { |
| 71 | c: '/home', |
| 72 | h: '/watch-history', |
| 73 | b: '/bookmark', |
| 74 | d: '/question', |
| 75 | s: 'https://projects.100xdevs.com/', |
| 76 | g: 'https://github.com/code100x/', |
| 77 | }; |
| 78 | |
| 79 | const key = e.key.toLowerCase() as keyof typeof shortcuts; |
| 80 | if (shortcuts[key]) { |
| 81 | e.preventDefault(); |
| 82 | handleShortcut(shortcuts[key]); |
| 83 | } |
| 84 | } |
| 85 | }, |
| 86 | [open, handleShortcut], |
| 87 | ); |
| 88 | |
| 89 | useEffect(() => { |
| 90 | document.addEventListener('keydown', handleKeyDown); |
| 91 | return () => document.removeEventListener('keydown', handleKeyDown); |
| 92 | }, [handleKeyDown]); |
| 93 | |
| 94 | return ( |
| 95 | <CommandDialog open={open} onOpenChange={onOpenChange}> |
| 96 | <CommandInput |
| 97 | placeholder="Type a command or search..." |
| 98 | value={commandSearchTerm} |