"use client"; import { useEffect, useState, useCallback, useRef } from "react"; import { motion } from "framer-motion"; import { Search, ArrowRight } from "lucide-react"; import { useAppStore } from "@/store/editor"; interface Command { id: string; label: string; description?: string; kbd?: string; icon?: string; action: () => void; } interface CommandPaletteProps { onClose: () => void; onOpenFile: (path: string) => void; } export default function CommandPalette({ onClose, onOpenFile }: CommandPaletteProps) { const [query, setQuery] = useState(""); const [focused, setFocused] = useState(0); const inputRef = useRef(null); const openFiles = useAppStore(s => s.openFiles); useEffect(() => { inputRef.current?.focus(); }, []); const commands: Command[] = [ { id: "new-terminal", label: "New Terminal", description: "Open a new terminal session", kbd: "⌃`", icon: "⬚", action: () => { onClose(); } }, { id: "toggle-sidebar", label: "Toggle Sidebar", description: "Show or hide the sidebar", kbd: "⌘B", icon: "◧", action: () => { onClose(); } }, { id: "format-doc", label: "Format Document", description: "Format the current file with Prettier", kbd: "⌥⇧F", icon: "⚡", action: () => { onClose(); } }, { id: "go-to-file", label: "Go to File…", description: "Quickly open a file by name", kbd: "⌘P", icon: "📄", action: () => { onClose(); } }, { id: "find-in-files", label: "Find in Files", description: "Search across all files", kbd: "⌘⇧F", icon: "🔍", action: () => { onClose(); } }, { id: "git-commit", label: "Git: Commit", description: "Stage and commit changes", icon: "⎇", action: () => { onClose(); } }, { id: "git-push", label: "Git: Push", description: "Push commits to remote", icon: "⎇", action: () => { onClose(); } }, { id: "git-pull", label: "Git: Pull", description: "Pull from remote", icon: "⎇", action: () => { onClose(); } }, { id: "toggle-theme", label: "Toggle Color Theme", description: "Switch between dark and light theme", icon: "🎨", action: () => { onClose(); } }, { id: "open-settings", label: "Open Settings", description: "Customize your IDE environment", kbd: "⌘,", icon: "⚙", action: () => { onClose(); } }, { id: "zen-mode", label: "Toggle Zen Mode", description: "Distraction-free coding", kbd: "⌘K Z", icon: "🧘", action: () => { onClose(); } }, ...openFiles.map(f => ({ id: `open-${f}`, label: f.split(/[\\/]/).pop() ?? f, description: f, icon: "📄", action: () => { onOpenFile(f); onClose(); } })), ]; const filtered = query.trim() ? commands.filter(c => c.label.toLowerCase().includes(query.toLowerCase()) || c.description?.toLowerCase().includes(query.toLowerCase()) ) : commands; const handleKeyDown = useCallback((e: React.KeyboardEvent) => { if (e.key === "Escape") { onClose(); return; } if (e.key === "ArrowDown") { e.preventDefault(); setFocused(f => Math.min(f + 1, filtered.length - 1)); } if (e.key === "ArrowUp") { e.preventDefault(); setFocused(f => Math.max(f - 1, 0)); } if (e.key === "Enter") { e.preventDefault(); filtered[focused]?.action(); } }, [filtered, focused, onClose]); return ( e.target === e.currentTarget && onClose()} >
{ setQuery(e.target.value); setFocused(0); }} onKeyDown={handleKeyDown} /> ESC
{filtered.length === 0 ? (
No commands found for "{query}"
) : ( filtered.map((cmd, i) => ( )) )}
); }