"use client";
import React from "react";
import { useRouter } from "next/navigation";
import { SidebarNavItem } from "@/types";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import {
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command";
import { Icons } from "@/components/shared/icons";
export function SearchCommand({ links }: { links: SidebarNavItem[] }) {
const [open, setOpen] = React.useState(false);
const router = useRouter();
React.useEffect(() => {
const down = (e: KeyboardEvent) => {
if (e.key === "k" && (e.metaKey || e.ctrlKey)) {
e.preventDefault();
setOpen((open) => !open);
}
};
document.addEventListener("keydown", down);
return () => document.removeEventListener("keydown", down);
}, []);
const runCommand = React.useCallback((command: () => unknown) => {
setOpen(false);
command();
}, []);
return (
<>
No results found.
{links.map((section) => (
{section.items.map((item) => {
const Icon = Icons[item.icon || "arrowRight"];
return (
{
runCommand(() => router.push(item.href as string));
}}
>
{item.title}
);
})}
))}
>
);
}