tahamajs's picture
download
raw
11.6 kB
// src/components/command-palette/CommandPalette.tsx
import { useEffect, useRef, useCallback } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { Search, Command, Loader2, Sparkles, TrendingUp, Users, FileText, Settings, Home, LayoutDashboard, BookOpen, MessageSquare, FolderOpen, Calendar, Bell, User, Heart, Star, Award, Zap, Activity, ChevronRight, Clock } from "lucide-react";
import { CommandAction } from "./types";
import { getKeyboardShortcutDisplay } from "./utils";
interface CommandPaletteProps {
isOpen: boolean;
onClose: () => void;
search: string;
onSearchChange: (value: string) => void;
actions: CommandAction[];
groupedActions: Map<string, CommandAction[]>;
selectedIndex: number;
onSelectIndex: (index: number) => void;
loading?: boolean;
recentSearches?: string[];
onRecentSearchClick?: (search: string) => void;
}
export function CommandPalette({
isOpen,
onClose,
search,
onSearchChange,
actions,
groupedActions,
selectedIndex,
onSelectIndex,
loading = false,
recentSearches = [],
onRecentSearchClick,
}: CommandPaletteProps) {
const inputRef = useRef<HTMLInputElement>(null);
const listRef = useRef<HTMLDivElement>(null);
const flatActions = actions;
// Focus input when opened
useEffect(() => {
if (isOpen) {
setTimeout(() => {
inputRef.current?.focus();
}, 50);
}
}, [isOpen]);
// Keyboard navigation
useEffect(() => {
if (!isOpen) return;
const handleKeyDown = (e: KeyboardEvent) => {
switch (e.key) {
case "ArrowDown":
e.preventDefault();
onSelectIndex(Math.min(selectedIndex + 1, flatActions.length - 1));
break;
case "ArrowUp":
e.preventDefault();
onSelectIndex(Math.max(selectedIndex - 1, 0));
break;
case "Enter":
e.preventDefault();
if (flatActions[selectedIndex]) {
flatActions[selectedIndex].perform();
onClose();
}
break;
case "Escape":
e.preventDefault();
onClose();
break;
}
};
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [isOpen, selectedIndex, flatActions, onSelectIndex, onClose]);
// Scroll selected item into view
useEffect(() => {
if (listRef.current) {
const selectedElement = listRef.current.querySelector(`[data-index="${selectedIndex}"]`);
selectedElement?.scrollIntoView({ block: "nearest" });
}
}, [selectedIndex]);
// Get icon for section
const getSectionIcon = (section: string) => {
const icons: Record<string, any> = {
"Navigation": Home,
"Content": FileText,
"Social": Users,
"Projects": FolderOpen,
"Research": BookOpen,
"Admin": Settings,
"Preferences": Settings,
"Actions": Activity,
};
const Icon = icons[section];
return Icon ? <Icon className="w-3 h-3 mr-1" /> : null;
};
// Clear search
const clearSearch = useCallback(() => {
onSearchChange("");
inputRef.current?.focus();
}, [onSearchChange]);
return (
<AnimatePresence mode="wait">
{isOpen && (
<>
{/* Backdrop */}
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.15 }}
className="fixed inset-0 z-50 bg-black/50 backdrop-blur-sm"
onClick={onClose}
/>
{/* Modal */}
<motion.div
initial={{ opacity: 0, scale: 0.95, y: -20 }}
animate={{ opacity: 1, scale: 1, y: 0 }}
exit={{ opacity: 0, scale: 0.95, y: -20 }}
transition={{ duration: 0.2, ease: "easeOut" }}
className="fixed left-1/2 top-[20vh] z-50 w-full max-w-2xl -translate-x-1/2 overflow-hidden rounded-2xl bg-white dark:bg-gray-800 shadow-2xl border border-gray-200 dark:border-gray-700"
>
{/* Search Input */}
<div className="flex items-center border-b border-gray-200 dark:border-gray-700 px-4 bg-white dark:bg-gray-800">
<Search className="h-5 w-5 text-gray-400 flex-shrink-0" />
<input
ref={inputRef}
type="text"
value={search}
onChange={(e) => onSearchChange(e.target.value)}
placeholder="Search commands, articles, projects..."
className="w-full border-none bg-transparent py-4 px-3 text-gray-900 dark:text-white outline-none placeholder:text-gray-400 text-base"
autoComplete="off"
spellCheck={false}
/>
{search && (
<button
onClick={clearSearch}
className="p-1 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700 transition"
>
<span className="text-xs text-gray-400">✕</span>
</button>
)}
<kbd className="hidden text-xs text-gray-400 sm:inline-flex gap-0.5 ml-2">
<span className="rounded bg-gray-100 dark:bg-gray-700 px-1.5 py-0.5">ESC</span>
</kbd>
</div>
{/* Loading State */}
{loading && (
<div className="flex items-center justify-center py-12">
<Loader2 className="h-6 w-6 animate-spin text-primary" />
<span className="ml-2 text-sm text-gray-500">Searching...</span>
</div>
)}
{/* Results */}
{!loading && (
<div ref={listRef} className="max-h-[60vh] overflow-y-auto">
{flatActions.length === 0 && search && (
<div className="py-12 text-center">
<Command className="mx-auto h-12 w-12 text-gray-300 dark:text-gray-600 opacity-50" />
<p className="mt-3 text-sm text-gray-500">No results found for "{search}"</p>
<p className="text-xs text-gray-400 mt-1">Try a different search term</p>
</div>
)}
{flatActions.length === 0 && !search && recentSearches.length > 0 && (
<div className="py-4">
<div className="px-4 py-2 text-xs font-medium text-gray-500 uppercase tracking-wider flex items-center gap-2">
<Clock className="w-3 h-3" />
Recent Searches
</div>
{recentSearches.map((term, idx) => (
<button
key={idx}
onClick={() => onRecentSearchClick?.(term)}
className="w-full flex items-center gap-3 px-4 py-2 text-left hover:bg-gray-50 dark:hover:bg-gray-700/50 transition"
>
<Search className="h-4 w-4 text-gray-400" />
<span className="text-sm text-gray-700 dark:text-gray-300">{term}</span>
<ChevronRight className="h-3 w-3 text-gray-400 ml-auto" />
</button>
))}
</div>
)}
{flatActions.length > 0 && Array.from(groupedActions.entries()).map(([section, sectionActions]) => (
<div key={section} className="pb-2">
<div className="sticky top-0 bg-gray-50 dark:bg-gray-900/95 backdrop-blur-sm px-4 py-2 text-xs font-medium text-gray-500 uppercase tracking-wider flex items-center border-b border-gray-100 dark:border-gray-800">
{getSectionIcon(section)}
{section}
</div>
{sectionActions.map((action) => {
const globalIndex = flatActions.findIndex(a => a.id === action.id);
const isSelected = globalIndex === selectedIndex;
return (
<motion.button
key={action.id}
data-index={globalIndex}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.1 }}
onClick={() => {
action.perform();
onClose();
}}
onMouseEnter={() => onSelectIndex(globalIndex)}
className={`w-full flex items-center gap-3 px-4 py-3 text-left transition-all duration-150 ${
isSelected
? "bg-primary/10 dark:bg-primary/20 border-l-4 border-l-primary"
: "hover:bg-gray-50 dark:hover:bg-gray-700/50 border-l-4 border-l-transparent"
}`}
>
{action.icon && (
<span className={`flex-shrink-0 ${isSelected ? "text-primary" : "text-gray-400"}`}>
{action.icon}
</span>
)}
<div className="flex-1 min-w-0">
<div className={`font-medium truncate ${isSelected ? "text-primary" : "text-gray-900 dark:text-white"}`}>
{action.name}
</div>
{action.subtitle && (
<div className="text-xs text-gray-500 truncate">{action.subtitle}</div>
)}
</div>
{action.shortcut && (
<kbd className="text-xs text-gray-400 bg-gray-100 dark:bg-gray-700 px-1.5 py-0.5 rounded">
{getKeyboardShortcutDisplay(action.shortcut)}
</kbd>
)}
</motion.button>
);
})}
</div>
))}
</div>
)}
{/* Footer */}
<div className="border-t border-gray-200 dark:border-gray-700 px-4 py-2 text-xs text-gray-400 flex justify-between items-center bg-gray-50 dark:bg-gray-900/50">
<div className="flex items-center gap-3">
<span className="flex items-center gap-1">
<kbd className="rounded bg-gray-200 dark:bg-gray-700 px-1.5 py-0.5 text-[10px]">↑</kbd>
<kbd className="rounded bg-gray-200 dark:bg-gray-700 px-1.5 py-0.5 text-[10px]">↓</kbd>
<span>to navigate</span>
</span>
<span className="flex items-center gap-1">
<kbd className="rounded bg-gray-200 dark:bg-gray-700 px-1.5 py-0.5 text-[10px]">↵</kbd>
<span>to select</span>
</span>
<span className="flex items-center gap-1">
<kbd className="rounded bg-gray-200 dark:bg-gray-700 px-1.5 py-0.5 text-[10px]">ESC</kbd>
<span>to close</span>
</span>
</div>
{flatActions.length > 0 && (
<span className="text-[10px]">{flatActions.length} result{flatActions.length !== 1 ? "s" : ""}</span>
)}
</div>
</motion.div>
</>
)}
</AnimatePresence>
);
}

Xet Storage Details

Size:
11.6 kB
·
Xet hash:
d238799d88238f981e6d957bdcbd0fd26941d5b91490428210b0592ac815ee86

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.