import React, { useEffect, useRef } from "react"; import { CommandDialog, CommandInput, CommandList, CommandItem, CommandGroup, CommandSeparator, CommandEmpty, } from "@/components/ui/command"; import { LayoutDashboard, ReceiptText, Maximize2, Terminal } from "lucide-react"; import { useNavigate } from "react-router-dom"; import { useCommandStore } from "@/lib/useCommandStore"; import { __ } from "@/lib/i18n"; type Action = { label: string; icon: React.ComponentType<{ className?: string }>; run: () => void; shortcut?: string; // e.g. "D", "O", "⌘⇧F" group: "Navigation" | "Actions"; }; export function CommandPalette({ toggleFullscreen }: { toggleFullscreen?: () => void }) { const { open, setOpen } = useCommandStore(); const navigate = useNavigate(); const inputRef = useRef(null); useEffect(() => { if (open) { // Focus the input shortly after opening to avoid dialog focus race const id = setTimeout(() => inputRef.current?.focus(), 0); return () => clearTimeout(id); } }, [open]); const actions: Action[] = [ { label: __("Dashboard"), icon: LayoutDashboard, run: () => navigate("/"), shortcut: "D", group: "Navigation" }, { label: __("Orders"), icon: ReceiptText, run: () => navigate("/orders"), shortcut: "O", group: "Navigation" }, { label: __("Toggle Fullscreen"), icon: Maximize2, run: () => toggleFullscreen?.(), shortcut: "⌘⇧F / Ctrl+Shift+F", group: "Actions" }, { label: __("Keyboard Shortcuts"), icon: Terminal, run: () => alert(__("Shortcut reference coming soon")), shortcut: "⌘K / Ctrl+K", group: "Actions" }, ]; // Helper: run action then close palette (close first to avoid focus glitches) const select = (fn: () => void) => { setOpen(false); // Allow dialog to close before navigation/action to keep focus clean setTimeout(fn, 0); }; return ( {__("No results found.")} {actions.filter(a => a.group === "Navigation").map((a) => ( select(a.run)}> {a.label} {a.shortcut ? ( {a.shortcut} ) : null} ))} {actions.filter(a => a.group === "Actions").map((a) => ( select(a.run)}> {a.label} {a.shortcut ? ( {a.shortcut} ) : null} ))} ); }