diff --git a/frontend/src/components/CommandMenu.tsx b/frontend/src/components/CommandMenu.tsx index 166b87a57..3b48f79d7 100644 --- a/frontend/src/components/CommandMenu.tsx +++ b/frontend/src/components/CommandMenu.tsx @@ -8,7 +8,7 @@ import { } from "@/components/ui/command"; import { basePath } from "@/config/siteConfig"; import { fetchCategories } from "@/lib/data"; -import { Category } from "@/lib/types"; +import { Category, Script } from "@/lib/types"; import { cn } from "@/lib/utils"; import Image from "next/image"; import { useRouter } from "next/navigation"; @@ -16,6 +16,7 @@ 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) { @@ -31,11 +32,19 @@ export const formattedBadge = (type: string) => { 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 router = useRouter(); const [isLoading, setIsLoading] = React.useState(false); + const router = useRouter(); React.useEffect(() => { const down = (e: KeyboardEvent) => { @@ -45,7 +54,6 @@ export default function CommandMenu() { setOpen((open) => !open); } }; - document.addEventListener("keydown", down); return () => document.removeEventListener("keydown", down); }, []); @@ -63,23 +71,58 @@ export default function CommandMenu() { }); }; + 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 diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 000000000..acd06abd6 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "ProxmoxVE", + "lockfileVersion": 3, + "requires": true, + "packages": {} +}