2024-11-04 22:55:08 +00:00
|
|
|
import {
|
|
|
|
CommandDialog,
|
|
|
|
CommandEmpty,
|
|
|
|
CommandGroup,
|
|
|
|
CommandInput,
|
|
|
|
CommandItem,
|
|
|
|
CommandList,
|
|
|
|
} from "@/components/ui/command";
|
2024-11-06 22:47:04 +00:00
|
|
|
import { fetchCategories } from "@/lib/data";
|
2024-11-04 22:55:08 +00:00
|
|
|
import { Category } from "@/lib/types";
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
import Image from "next/image";
|
|
|
|
import { useRouter } from "next/navigation";
|
2024-11-06 22:47:04 +00:00
|
|
|
import React from "react";
|
|
|
|
import { Badge } from "./ui/badge";
|
2024-11-04 22:55:08 +00:00
|
|
|
import { Button } from "./ui/button";
|
|
|
|
import { DialogTitle } from "./ui/dialog";
|
|
|
|
|
2024-11-06 22:47:04 +00:00
|
|
|
export const formattedBadge = (type: string) => {
|
|
|
|
switch (type) {
|
|
|
|
case "vm":
|
|
|
|
return <Badge className="text-blue-500/75 border-blue-500/75">VM</Badge>;
|
|
|
|
case "ct":
|
|
|
|
return (
|
|
|
|
<Badge className="text-yellow-500/75 border-yellow-500/75">LXC</Badge>
|
|
|
|
);
|
|
|
|
case "misc":
|
|
|
|
return <Badge className="text-red-500/75 border-red-500/75">MISC</Badge>;
|
|
|
|
}
|
|
|
|
return null;
|
2024-11-04 22:55:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function CommandMenu() {
|
|
|
|
const [open, setOpen] = React.useState(false);
|
|
|
|
const [links, setLinks] = React.useState<Category[]>([]);
|
|
|
|
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);
|
|
|
|
}, []);
|
|
|
|
|
2024-11-06 22:47:04 +00:00
|
|
|
const fetchSortedCategories = () => {
|
2024-11-04 22:55:08 +00:00
|
|
|
setIsLoading(true);
|
2024-11-06 22:47:04 +00:00
|
|
|
fetchCategories()
|
|
|
|
.then((categories) => {
|
|
|
|
setLinks(categories);
|
|
|
|
setIsLoading(false);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
setIsLoading(false);
|
|
|
|
console.error(error);
|
|
|
|
});
|
2024-11-04 22:55:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
variant="outline"
|
|
|
|
className={cn(
|
|
|
|
"relative h-9 w-full justify-start rounded-[0.5rem] bg-muted/50 text-sm font-normal text-muted-foreground shadow-none sm:pr-12 md:w-40 lg:w-64",
|
|
|
|
)}
|
|
|
|
onClick={() => {
|
2024-11-06 22:47:04 +00:00
|
|
|
fetchSortedCategories();
|
|
|
|
setOpen(true);
|
2024-11-04 22:55:08 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<span className="inline-flex">Search scripts...</span>
|
|
|
|
<kbd className="pointer-events-none absolute right-[0.3rem] top-[0.45rem] hidden h-5 select-none items-center gap-1 rounded border bg-muted px-1.5 font-mono text-[10px] font-medium opacity-100 sm:flex">
|
|
|
|
<span className="text-xs">⌘</span>K
|
|
|
|
</kbd>
|
|
|
|
</Button>
|
|
|
|
<CommandDialog open={open} onOpenChange={setOpen}>
|
2024-11-06 22:47:04 +00:00
|
|
|
<DialogTitle className="sr-only">Search scripts</DialogTitle>
|
|
|
|
<CommandInput placeholder="Search for a script..." />
|
2024-11-04 22:55:08 +00:00
|
|
|
<CommandList>
|
2024-11-06 22:47:04 +00:00
|
|
|
<CommandEmpty>
|
|
|
|
{isLoading ? "Loading..." : "No scripts found."}
|
|
|
|
</CommandEmpty>
|
2024-11-04 22:55:08 +00:00
|
|
|
{links.map((category) => (
|
|
|
|
<CommandGroup
|
2024-11-06 22:47:04 +00:00
|
|
|
key={`category:${category.name}`}
|
|
|
|
heading={category.name}
|
2024-11-04 22:55:08 +00:00
|
|
|
>
|
2024-11-06 22:47:04 +00:00
|
|
|
{category.scripts.map((script) => (
|
2024-11-04 22:55:08 +00:00
|
|
|
<CommandItem
|
2024-11-08 21:27:01 +00:00
|
|
|
key={`script:${script.slug}`}
|
|
|
|
value={script.slug}
|
2024-11-04 22:55:08 +00:00
|
|
|
onSelect={() => {
|
|
|
|
setOpen(false);
|
2024-11-08 21:27:01 +00:00
|
|
|
router.push(`/scripts?id=${script.slug}`);
|
2024-11-04 22:55:08 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className="flex gap-2" onClick={() => setOpen(false)}>
|
|
|
|
<Image
|
2024-11-06 22:47:04 +00:00
|
|
|
src={script.logo || "/logo.png"}
|
2024-11-04 22:55:08 +00:00
|
|
|
onError={(e) =>
|
|
|
|
((e.currentTarget as HTMLImageElement).src =
|
|
|
|
"/logo.png")
|
|
|
|
}
|
2024-11-09 22:36:20 +00:00
|
|
|
unoptimized
|
2024-11-04 22:55:08 +00:00
|
|
|
width={16}
|
2024-11-06 22:47:04 +00:00
|
|
|
height={16}
|
2024-11-04 22:55:08 +00:00
|
|
|
alt=""
|
|
|
|
className="h-5 w-5"
|
|
|
|
/>
|
2024-11-06 22:47:04 +00:00
|
|
|
<span>{script.name}</span>
|
|
|
|
<span>{formattedBadge(script.type)}</span>
|
2024-11-04 22:55:08 +00:00
|
|
|
</div>
|
|
|
|
</CommandItem>
|
|
|
|
))}
|
|
|
|
</CommandGroup>
|
|
|
|
))}
|
|
|
|
</CommandList>
|
|
|
|
</CommandDialog>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|