2024-11-04 23:55:08 +01:00
|
|
|
import { CalendarPlus } from "lucide-react";
|
2025-06-28 00:38:09 +02:00
|
|
|
import { useMemo, useState } from "react";
|
2024-11-04 23:55:08 +01:00
|
|
|
import Image from "next/image";
|
|
|
|
|
import Link from "next/link";
|
2025-06-28 00:38:09 +02:00
|
|
|
|
|
|
|
|
import type { Category, Script } from "@/lib/types";
|
|
|
|
|
|
|
|
|
|
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
|
|
|
|
|
import { basePath, mostPopularScripts } from "@/config/site-config";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { extractDate } from "@/lib/time";
|
2024-11-04 23:55:08 +01:00
|
|
|
|
|
|
|
|
const ITEMS_PER_PAGE = 3;
|
|
|
|
|
|
2025-06-28 00:38:09 +02:00
|
|
|
export function getDisplayValueFromType(type: string) {
|
2024-11-06 23:47:04 +01:00
|
|
|
switch (type) {
|
|
|
|
|
case "ct":
|
|
|
|
|
return "LXC";
|
|
|
|
|
case "vm":
|
|
|
|
|
return "VM";
|
2025-04-09 13:10:02 +02:00
|
|
|
case "pve":
|
|
|
|
|
case "addon":
|
2024-11-06 23:47:04 +01:00
|
|
|
return "";
|
|
|
|
|
default:
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2025-06-28 00:38:09 +02:00
|
|
|
}
|
2024-11-06 23:47:04 +01:00
|
|
|
|
2024-11-04 23:55:08 +01:00
|
|
|
export function LatestScripts({ items }: { items: Category[] }) {
|
|
|
|
|
const [page, setPage] = useState(1);
|
|
|
|
|
|
|
|
|
|
const latestScripts = useMemo(() => {
|
2025-06-28 00:38:09 +02:00
|
|
|
if (!items)
|
|
|
|
|
return [];
|
2025-04-09 13:10:02 +02:00
|
|
|
|
2025-06-28 00:38:09 +02:00
|
|
|
const scripts = items.flatMap(category => category.scripts || []);
|
2025-01-28 19:33:22 +01:00
|
|
|
|
|
|
|
|
// Filter out duplicates by slug
|
|
|
|
|
const uniqueScriptsMap = new Map<string, Script>();
|
|
|
|
|
scripts.forEach((script) => {
|
|
|
|
|
if (!uniqueScriptsMap.has(script.slug)) {
|
|
|
|
|
uniqueScriptsMap.set(script.slug, script);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Array.from(uniqueScriptsMap.values()).sort(
|
2025-04-09 13:10:02 +02:00
|
|
|
(a, b) => new Date(b.date_created).getTime() - new Date(a.date_created).getTime(),
|
2024-11-04 23:55:08 +01:00
|
|
|
);
|
|
|
|
|
}, [items]);
|
|
|
|
|
|
|
|
|
|
const goToNextPage = () => {
|
2025-06-28 00:38:09 +02:00
|
|
|
setPage(prevPage => prevPage + 1);
|
2024-11-04 23:55:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const goToPreviousPage = () => {
|
2025-06-28 00:38:09 +02:00
|
|
|
setPage(prevPage => prevPage - 1);
|
2024-11-04 23:55:08 +01:00
|
|
|
};
|
2025-04-09 13:10:02 +02:00
|
|
|
|
2024-11-04 23:55:08 +01:00
|
|
|
const startIndex = (page - 1) * ITEMS_PER_PAGE;
|
|
|
|
|
const endIndex = page * ITEMS_PER_PAGE;
|
|
|
|
|
|
|
|
|
|
if (!items) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="">
|
|
|
|
|
{latestScripts.length > 0 && (
|
|
|
|
|
<div className="flex w-full items-center justify-between">
|
|
|
|
|
<h2 className="text-lg font-semibold">Newest Scripts</h2>
|
|
|
|
|
<div className="flex items-center justify-end gap-1">
|
|
|
|
|
{page > 1 && (
|
2025-04-09 13:10:02 +02:00
|
|
|
<div className="cursor-pointer select-none p-2 text-sm font-semibold" onClick={goToPreviousPage}>
|
2024-11-04 23:55:08 +01:00
|
|
|
Previous
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{endIndex < latestScripts.length && (
|
2025-04-09 13:10:02 +02:00
|
|
|
<div onClick={goToNextPage} className="cursor-pointer select-none p-2 text-sm font-semibold">
|
2024-11-04 23:55:08 +01:00
|
|
|
{page === 1 ? "More.." : "Next"}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<div className="min-w flex w-full flex-row flex-wrap gap-4">
|
2025-06-28 00:38:09 +02:00
|
|
|
{latestScripts.slice(startIndex, endIndex).map(script => (
|
2025-04-09 13:10:02 +02:00
|
|
|
<Card key={script.slug} className="min-w-[250px] flex-1 flex-grow bg-accent/30">
|
2024-11-04 23:55:08 +01:00
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle className="flex items-center gap-3">
|
2025-01-23 17:36:57 +01:00
|
|
|
<div className="flex h-16 w-16 min-w-16 items-center justify-center rounded-lg bg-accent p-1">
|
2024-11-04 23:55:08 +01:00
|
|
|
<Image
|
2024-11-13 15:45:39 +01:00
|
|
|
src={script.logo || `/${basePath}/logo.png`}
|
2024-11-04 23:55:08 +01:00
|
|
|
unoptimized
|
|
|
|
|
height={64}
|
|
|
|
|
width={64}
|
|
|
|
|
alt=""
|
2025-06-28 00:38:09 +02:00
|
|
|
onError={e => ((e.currentTarget as HTMLImageElement).src = `/${basePath}/logo.png`)}
|
2024-11-04 23:55:08 +01:00
|
|
|
className="h-11 w-11 object-contain"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-col">
|
|
|
|
|
<p className="text-lg line-clamp-1">
|
2025-06-28 00:38:09 +02:00
|
|
|
{script.name}
|
|
|
|
|
{" "}
|
|
|
|
|
{getDisplayValueFromType(script.type)}
|
2024-11-04 23:55:08 +01:00
|
|
|
</p>
|
|
|
|
|
<p className="text-sm text-muted-foreground flex items-center gap-1">
|
|
|
|
|
<CalendarPlus className="h-4 w-4" />
|
2024-11-06 23:47:04 +01:00
|
|
|
{extractDate(script.date_created)}
|
2024-11-04 23:55:08 +01:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</CardTitle>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
2025-04-09 13:10:02 +02:00
|
|
|
<CardDescription className="line-clamp-3 text-card-foreground">{script.description}</CardDescription>
|
2024-11-04 23:55:08 +01:00
|
|
|
</CardContent>
|
|
|
|
|
<CardFooter className="">
|
|
|
|
|
<Button asChild variant="outline">
|
|
|
|
|
<Link
|
|
|
|
|
href={{
|
|
|
|
|
pathname: "/scripts",
|
2024-11-08 22:27:01 +01:00
|
|
|
query: { id: script.slug },
|
2024-11-04 23:55:08 +01:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
View Script
|
|
|
|
|
</Link>
|
|
|
|
|
</Button>
|
|
|
|
|
</CardFooter>
|
|
|
|
|
</Card>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function MostViewedScripts({ items }: { items: Category[] }) {
|
2024-11-06 23:47:04 +01:00
|
|
|
const mostViewedScripts = items.reduce((acc: Script[], category) => {
|
2025-06-28 00:38:09 +02:00
|
|
|
const foundScripts = category.scripts.filter(script => mostPopularScripts.includes(script.slug));
|
2024-11-06 23:47:04 +01:00
|
|
|
return acc.concat(foundScripts);
|
|
|
|
|
}, []);
|
2024-11-04 23:55:08 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="">
|
|
|
|
|
{mostViewedScripts.length > 0 && (
|
|
|
|
|
<>
|
2025-09-11 14:01:14 +02:00
|
|
|
<h2 className="text-lg font-semibold mb-1">Most Viewed Scripts</h2>
|
2024-11-04 23:55:08 +01:00
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
<div className="min-w flex w-full flex-row flex-wrap gap-4">
|
2025-06-28 00:38:09 +02:00
|
|
|
{mostViewedScripts.map(script => (
|
2025-04-09 13:10:02 +02:00
|
|
|
<Card key={script.slug} className="min-w-[250px] flex-1 flex-grow bg-accent/30">
|
2024-11-04 23:55:08 +01:00
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle className="flex items-center gap-3">
|
2025-01-23 17:36:57 +01:00
|
|
|
<div className="flex size-16 min-w-16 items-center justify-center rounded-lg bg-accent p-1">
|
2024-11-04 23:55:08 +01:00
|
|
|
<Image
|
|
|
|
|
unoptimized
|
2024-11-13 15:45:39 +01:00
|
|
|
src={script.logo || `/${basePath}/logo.png`}
|
2024-11-04 23:55:08 +01:00
|
|
|
height={64}
|
|
|
|
|
width={64}
|
|
|
|
|
alt=""
|
2025-06-28 00:38:09 +02:00
|
|
|
onError={e => ((e.currentTarget as HTMLImageElement).src = `/${basePath}/logo.png`)}
|
2024-11-04 23:55:08 +01:00
|
|
|
className="h-11 w-11 object-contain"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-col">
|
|
|
|
|
<p className="line-clamp-1 text-lg">
|
2025-06-28 00:38:09 +02:00
|
|
|
{script.name}
|
|
|
|
|
{" "}
|
|
|
|
|
{getDisplayValueFromType(script.type)}
|
2024-11-04 23:55:08 +01:00
|
|
|
</p>
|
|
|
|
|
<p className="flex items-center gap-1 text-sm text-muted-foreground">
|
|
|
|
|
<CalendarPlus className="h-4 w-4" />
|
2024-11-06 23:47:04 +01:00
|
|
|
{extractDate(script.date_created)}
|
2024-11-04 23:55:08 +01:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</CardTitle>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
|
|
|
|
<CardDescription className="line-clamp-3 text-card-foreground break-words">
|
2024-11-06 23:47:04 +01:00
|
|
|
{script.description}
|
2024-11-04 23:55:08 +01:00
|
|
|
</CardDescription>
|
|
|
|
|
</CardContent>
|
|
|
|
|
<CardFooter className="">
|
|
|
|
|
<Button asChild variant="outline">
|
|
|
|
|
<Link
|
|
|
|
|
href={{
|
|
|
|
|
pathname: "/scripts",
|
2024-11-08 22:27:01 +01:00
|
|
|
query: { id: script.slug },
|
2024-11-04 23:55:08 +01:00
|
|
|
}}
|
|
|
|
|
prefetch={false}
|
|
|
|
|
>
|
|
|
|
|
View Script
|
|
|
|
|
</Link>
|
|
|
|
|
</Button>
|
|
|
|
|
</CardFooter>
|
|
|
|
|
</Card>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|