import { CommandDialog, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command"; import { basePath } from "@/config/siteConfig"; import { fetchCategories } from "@/lib/data"; import { Category, Script } 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"; import { Sparkles } from "lucide-react"; // <- Hinzugefügt export const formattedBadge = (type: string) => { switch (type) { case "vm": return VM; case "ct": return LXC; case "pve": return PVE; case "addon": return ADDON; } return null; }; // random Script function getRandomScript(categories: Category[]): Script | null { const allScripts = categories.flatMap((cat) => cat.scripts || []); if (allScripts.length === 0) return null; const idx = Math.floor(Math.random() * allScripts.length); return allScripts[idx]; } export default function CommandMenu() { const [open, setOpen] = React.useState(false); const [links, setLinks] = React.useState([]); const [isLoading, setIsLoading] = React.useState(false); const router = useRouter(); React.useEffect(() => { const down = (e: KeyboardEvent) => { if (e.key === "k" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); fetchSortedCategories(); 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); }); }; const openRandomScript = async () => { if (links.length === 0) { setIsLoading(true); try { const categories = await fetchCategories(); setLinks(categories); const randomScript = getRandomScript(categories); if (randomScript) { router.push(`/scripts?id=${randomScript.slug}`); } } finally { setIsLoading(false); } } else { const randomScript = getRandomScript(links); if (randomScript) { router.push(`/scripts?id=${randomScript.slug}`); } } }; return ( <>
Search scripts {isLoading ? "Loading..." : "No scripts found."} {links.map((category) => ( {category.scripts.map((script) => ( { setOpen(false); router.push(`/scripts?id=${script.slug}`); }} >
setOpen(false)}> ((e.currentTarget as HTMLImageElement).src = `/${basePath}/logo.png`)} unoptimized width={16} height={16} alt="" className="h-5 w-5" /> {script.name} {formattedBadge(script.type)}
))}
))}
); }