"use client"; import * as React from "react"; import { cn } from "../../lib/utils"; import { Dialog, DialogContent } from "./dialog"; import { Search, Loader2, } from "lucide-react"; interface CommandContextType { value: string; onValueChange: (value: string) => void; filter: (items: CommandItem[]) => CommandItem[]; isLoading?: boolean; setIsLoading?: (isLoading: boolean) => void; emptyMessage?: string; } interface CommandItem { id: string; value: string; label: React.ReactNode; disabled?: boolean; onSelect?: () => void; } const CommandContext = React.createContext( undefined ); function useCommand() { const context = React.useContext(CommandContext); if (!context) { throw new Error("useCommand must be used within a Command component"); } return context; } interface CommandProps extends React.HTMLAttributes { isLoading?: boolean; emptyMessage?: string; } const Command = React.forwardRef( ( { className, isLoading: controlledLoading, emptyMessage = "No results found.", ...props }, ref ) => { const [value, setValue] = React.useState(""); const [internalLoading, setInternalLoading] = React.useState(false); const isLoading = controlledLoading !== undefined ? controlledLoading : internalLoading; const filter = React.useCallback( (items: CommandItem[]) => { if (!value) return items; return items.filter((item) => typeof item.label === "string" ? item.label.toLowerCase().includes(value.toLowerCase()) : item.value.toLowerCase().includes(value.toLowerCase()) ); }, [value] ); return (
); } ); Command.displayName = "Command"; interface CommandDialogProps { children: React.ReactNode; open?: boolean; onOpenChange?: (open: boolean) => void; className?: string; } const CommandDialog: React.FC = ({ children, open, onOpenChange, className, }) => { // Prevent form submission causing page refresh const handleDialogClick = (e: React.MouseEvent) => { // Prevent any click events from bubbling up to a form e.stopPropagation(); }; // Handle ESC key to close dialog React.useEffect(() => { if (!open) return; const handleKeyDown = (e: KeyboardEvent) => { if (e.key === "Escape") { e.preventDefault(); if (onOpenChange) { onOpenChange(false); } } }; document.addEventListener("keydown", handleKeyDown); return () => { document.removeEventListener("keydown", handleKeyDown); }; }, [open, onOpenChange]); // Add body class to enable blur effect on the entire page React.useEffect(() => { if (open) { document.body.classList.add("command-dialog-open"); } else { document.body.classList.remove("command-dialog-open"); } return () => { document.body.classList.remove("command-dialog-open"); }; }, [open]); return ( <> {open && (