import { CommandDialog, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { Category } from "@/lib/types"; import { cn } from "@/lib/utils"; import Image from "next/image"; import { useRouter } from "next/navigation"; import React, { useEffect } from "react"; import { Button } from "./ui/button"; import { DialogTitle } from "./ui/dialog"; const sortCategories = (categories: Category[]): Category[] => { return categories.sort((a: Category, b: Category) => { if ( a.catagoryName === "Proxmox VE Tools" && b.catagoryName !== "Proxmox VE Tools" ) { return -1; } else if ( a.catagoryName !== "Proxmox VE Tools" && b.catagoryName === "Proxmox VE Tools" ) { return 1; } else { return a.catagoryName.localeCompare(b.catagoryName); } }); }; export default function CommandMenu() { const [open, setOpen] = React.useState(false); const [links, setLinks] = React.useState([]); const router = useRouter(); const [isLoading, setIsLoading] = React.useState(false); 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 fetchCategories = async () => { setIsLoading(true); fetch( `api/categories?_=${process.env.NEXT_PUBLIC_BUILD_TIME || Date.now()}`, ) .then((response) => response.json()) .then((categories) => { const sortedCategories = sortCategories(categories); setLinks(sortedCategories); setIsLoading(false); }) .catch((error) => { setIsLoading(false); console.error(error); }); }; return ( <> Search scripts {isLoading ? "Loading..." : "No scripts found."} {links.map((category) => ( {category.expand.items.map((script) => ( { setOpen(false); router.push(`/scripts?id=${script.title}`); }} >
setOpen(false)}> ((e.currentTarget as HTMLImageElement).src = "/logo.png") } width={16} alt="" className="h-5 w-5" /> {script.title} {script.item_type}
))}
))}
); }