"use client"; import { KeyboardIcon, MessageSquarePlusIcon, SettingsIcon, } from "lucide-react"; import { useRouter } from "next/navigation"; import { useCallback, useMemo, useState } from "react"; import { CommandDialog, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandShortcut, } from "@/components/ui/command"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { useI18n } from "@/core/i18n/hooks"; import { useGlobalShortcuts } from "@/hooks/use-global-shortcuts"; export function CommandPalette() { const { t } = useI18n(); const router = useRouter(); const [open, setOpen] = useState(false); const [shortcutsOpen, setShortcutsOpen] = useState(false); const handleNewChat = useCallback(() => { router.push("/workspace/chats/new"); setOpen(false); }, [router]); const handleOpenSettings = useCallback(() => { router.push("/workspace/settings"); setOpen(false); }, [router]); const handleShowShortcuts = useCallback(() => { setOpen(false); setShortcutsOpen(true); }, []); const shortcuts = useMemo( () => [ { key: "k", meta: true, action: () => setOpen((o) => !o) }, { key: "n", meta: true, shift: true, action: handleNewChat }, { key: ",", meta: true, action: handleOpenSettings }, { key: "/", meta: true, action: handleShowShortcuts }, ], [handleNewChat, handleOpenSettings, handleShowShortcuts], ); useGlobalShortcuts(shortcuts); const isMac = typeof navigator !== "undefined" && navigator.userAgent.includes("Mac"); const metaKey = isMac ? "⌘" : "Ctrl+"; const shiftKey = isMac ? "⇧" : "Shift+"; return ( <> {t.shortcuts.noResults} {t.sidebar.newChat} {metaKey}{shiftKey}N {t.common.settings} {metaKey}, {t.shortcuts.keyboardShortcuts} {metaKey}/ {t.shortcuts.keyboardShortcuts} {t.shortcuts.keyboardShortcutsDescription}
{[ { keys: `${metaKey}K`, label: t.shortcuts.openCommandPalette }, { keys: `${metaKey}${shiftKey}N`, label: t.sidebar.newChat }, { keys: `${metaKey}B`, label: t.shortcuts.toggleSidebar }, { keys: `${metaKey},`, label: t.common.settings }, { keys: `${metaKey}/`, label: t.shortcuts.keyboardShortcuts, }, ].map(({ keys, label }) => (
{label} {keys}
))}
); }