import { CommandDialog, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { fetchCategories } from "@/lib/data"; import { Category } from "@/lib/types"; import { cn } from "@/lib/utils"; import Image from "next/image"; import { useRouter } from "next/navigation"; import React from "react"; import { Badge } from "./ui/badge"; import { Button } from "./ui/button"; import { DialogTitle } from "./ui/dialog"; export const formattedBadge = (type: string) => { switch (type) { case "vm": return VM; case "ct": return ( LXC ); case "misc": return MISC; } return null; }; 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 fetchSortedCategories = () => { setIsLoading(true); fetchCategories() .then((categories) => { setLinks(categories); setIsLoading(false); }) .catch((error) => { setIsLoading(false); console.error(error); }); }; return ( <> { fetchSortedCategories(); setOpen(true); }} > Search scripts... ⌘K Search scripts {isLoading ? "Loading..." : "No scripts found."} {links.map((category) => ( {category.scripts.map((script) => ( { setOpen(false); router.push(`/scripts?id=${script.name}`); }} > setOpen(false)}> ((e.currentTarget as HTMLImageElement).src = "/logo.png") } width={16} height={16} alt="" className="h-5 w-5" /> {script.name} {formattedBadge(script.type)} ))} ))} > ); }