tahamajs/IE / Projects /front /src /components /command-palette /CommandPaletteProvider.tsx
tahamajs's picture
download
raw
5.74 kB
// src/components/command-palette/CommandPaletteProvider.tsx
import { createContext, useContext, useState, useCallback, useMemo, ReactNode } from "react";
import { KBarProvider, KBarPortal, KBarPositioner, KBarAnimator, KBarSearch, KBarResults, useMatches, useKBar } from "kbar";
import { Search } from "lucide-react"; // Add this import
import { useCommandActions } from "./hooks/useCommandActions";
import { useCommandHistory } from "./hooks/useCommandHistory";
import { CommandAction, CommandPaletteContextValue } from "./types";
const CommandPaletteContext = createContext<CommandPaletteContextValue | null>(null);
export const useCommandPalette = () => {
const ctx = useContext(CommandPaletteContext);
if (!ctx) throw new Error("useCommandPalette must be used within CommandPaletteProvider");
return ctx;
};
// Internal component that renders the KBar UI
function KBarInternal() {
const { query } = useKBar();
const { results } = useMatches();
const items = results as any[];
if (!results) return null;
return (
<KBarPortal>
<KBarPositioner className="fixed inset-0 z-50 flex items-start justify-center bg-black/50 p-4">
<KBarAnimator className="w-full max-w-2xl overflow-hidden rounded-2xl bg-white dark:bg-gray-800 shadow-2xl">
<div className="flex items-center border-b border-gray-200 dark:border-gray-700 px-4">
<Search className="h-5 w-5 text-gray-400" />
<KBarSearch
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"
placeholder="Search commands, articles, projects..."
/>
<kbd className="hidden text-xs text-gray-400 sm:inline-flex gap-0.5">
<span className="rounded bg-gray-100 dark:bg-gray-700 px-1.5 py-0.5">ESC</span>
</kbd>
</div>
<KBarResults
items={items}
onRender={({ item, active }) => {
const commandItem = item as any;
return (
<button
onClick={() => commandItem.perform?.()}
className={`w-full flex items-center gap-3 px-4 py-2.5 text-left transition-all duration-150 ${
active
? "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"
}`}
>
{commandItem.icon && <span className="flex-shrink-0 text-gray-400">{commandItem.icon}</span>}
<div className="flex-1 min-w-0">
<div className={`font-medium truncate ${active ? "text-primary" : "text-gray-900 dark:text-white"}`}>
{commandItem.name}
</div>
{commandItem.subtitle && (
<div className="text-xs text-gray-500 truncate">{commandItem.subtitle}</div>
)}
</div>
{commandItem.shortcut && (
<kbd className="text-xs text-gray-400 bg-gray-100 dark:bg-gray-700 px-1.5 py-0.5 rounded">
{commandItem.shortcut.map((key: string) => key.toUpperCase()).join("+")}
</kbd>
)}
</button>
);
}}
/>
<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">
<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>
{results.length > 0 && (
<span className="text-[10px]">{results.length} result{results.length !== 1 ? "s" : ""}</span>
)}
</div>
</KBarAnimator>
</KBarPositioner>
</KBarPortal>
);
}
interface CommandPaletteProviderProps {
children: ReactNode;
navigate: (path: string) => void;
toggleTheme: () => void;
isDark: boolean;
user: any;
recentPapers: { id: string; title: string }[];
recentContacts: { username: string; fullName?: string }[];
}
export function CommandPaletteProvider({
children,
navigate,
toggleTheme,
isDark,
user,
recentPapers,
recentContacts,
}: CommandPaletteProviderProps) {
const { addToHistory } = useCommandHistory();
const baseActions = useCommandActions();
// Build actions for KBar
const actions = useMemo(() => {
return baseActions.map(action => ({
id: action.id,
name: action.name,
shortcut: action.shortcut,
keywords: action.keywords?.join(" "),
section: action.section,
subtitle: action.subtitle,
icon: action.icon,
perform: () => {
action.perform();
addToHistory(action.id);
},
}));
}, [baseActions, addToHistory]) as any[];
return (
<KBarProvider actions={actions as any}>
{children}
<KBarInternal />
</KBarProvider>
);
}

Xet Storage Details

Size:
5.74 kB
·
Xet hash:
0f8251b7b4e5242fdc6c9608b34bce852088c80f1fc31cf831646df349a7ec1d

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