Files
ProxmoxVE/frontend/src/app/category-view/page.tsx

290 lines
11 KiB
TypeScript
Raw Normal View History

2025-01-23 09:33:34 +01:00
"use client";
2025-01-27 14:21:10 +01:00
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
2025-01-23 10:35:07 +01:00
import { Category } from "@/lib/types";
import { ChevronLeft, ChevronRight } from "lucide-react";
import { useRouter } from "next/navigation";
import React, { useEffect, useState } from "react";
2025-01-23 09:27:58 +01:00
2025-01-23 11:24:12 +01:00
const defaultLogo = "/default-logo.png"; // Fallback logo path
2025-01-23 11:54:50 +01:00
const MAX_DESCRIPTION_LENGTH = 100; // Set max length for description
2025-01-27 13:56:09 +01:00
const MAX_LOGOS = 5; // Max logos to display at once
2025-01-23 11:54:50 +01:00
2025-01-27 14:21:10 +01:00
const formattedBadge = (type: string) => {
switch (type) {
case "vm":
2025-01-27 17:44:49 +01:00
return <Badge className="text-blue-500/75 border-blue-500/75 badge">VM</Badge>;
2025-01-27 14:21:10 +01:00
case "ct":
return <Badge className="text-yellow-500/75 border-yellow-500/75 badge">LXC</Badge>;
case "pve":
return <Badge className="text-orange-500/75 border-orange-500/75 badge">PVE</Badge>;
case "addon":
return <Badge className="text-green-500/75 border-green-500/75 badge">ADDON</Badge>;
2025-01-27 14:21:10 +01:00
}
return null;
};
2025-01-23 09:27:58 +01:00
const CategoryView = () => {
2025-01-23 09:33:34 +01:00
const [categories, setCategories] = useState<Category[]>([]);
2025-01-27 14:03:35 +01:00
const [selectedCategoryIndex, setSelectedCategoryIndex] = useState<number | null>(null);
2025-01-27 17:48:22 +01:00
const [currentScripts, setCurrentScripts] = useState<any[]>([]);
2025-01-27 14:21:10 +01:00
const [logoIndices, setLogoIndices] = useState<{ [key: string]: number }>({});
2025-01-23 12:01:39 +01:00
const router = useRouter();
2025-01-23 09:33:34 +01:00
useEffect(() => {
2025-01-23 11:31:46 +01:00
const fetchCategories = async () => {
try {
const basePath = process.env.NODE_ENV === "production" ? "/ProxmoxVE" : "";
2025-01-23 11:40:58 +01:00
const response = await fetch(`${basePath}/api/categories`);
2025-01-23 11:31:46 +01:00
if (!response.ok) {
throw new Error("Failed to fetch categories");
}
const data = await response.json();
setCategories(data);
2025-01-27 14:21:10 +01:00
// Initialize logo indices
const initialLogoIndices: { [key: string]: number } = {};
data.forEach((category: any) => {
initialLogoIndices[category.name] = 0;
});
setLogoIndices(initialLogoIndices);
2025-01-23 11:31:46 +01:00
} catch (error) {
console.error("Error fetching categories:", error);
}
};
2025-01-23 09:49:47 +01:00
2025-01-23 10:35:07 +01:00
fetchCategories();
2025-01-23 09:33:34 +01:00
}, []);
2025-01-23 09:27:58 +01:00
2025-01-27 14:03:35 +01:00
const handleCategoryClick = (index: number) => {
setSelectedCategoryIndex(index);
2025-01-27 17:48:22 +01:00
setCurrentScripts(categories[index]?.scripts || []); // Update scripts for the selected category
2025-01-23 09:27:58 +01:00
};
const handleBackClick = () => {
2025-01-27 14:03:35 +01:00
setSelectedCategoryIndex(null);
2025-01-27 17:48:22 +01:00
setCurrentScripts([]); // Clear scripts when going back
2025-01-23 09:27:58 +01:00
};
2025-01-23 12:01:39 +01:00
const handleScriptClick = (scriptSlug: string) => {
router.push(`/scripts?id=${scriptSlug}`);
};
2025-01-27 14:03:35 +01:00
const navigateCategory = (direction: "prev" | "next") => {
if (selectedCategoryIndex !== null) {
const newIndex =
direction === "prev"
? (selectedCategoryIndex - 1 + categories.length) % categories.length
: (selectedCategoryIndex + 1) % categories.length;
setSelectedCategoryIndex(newIndex);
2025-01-27 17:48:22 +01:00
setCurrentScripts(categories[newIndex]?.scripts || []); // Update scripts for the new category
2025-01-27 14:03:35 +01:00
}
};
2025-01-27 14:21:10 +01:00
const switchLogos = (categoryName: string, direction: "prev" | "next") => {
setLogoIndices((prev) => {
const currentIndex = prev[categoryName] || 0;
const category = categories.find((cat) => cat.name === categoryName);
if (!category || !category.scripts) return prev;
const totalLogos = category.scripts.length;
const newIndex =
direction === "prev"
? (currentIndex - MAX_LOGOS + totalLogos) % totalLogos
: (currentIndex + MAX_LOGOS) % totalLogos;
return { ...prev, [categoryName]: newIndex };
});
};
2025-01-23 11:54:50 +01:00
const truncateDescription = (text: string) => {
return text.length > MAX_DESCRIPTION_LENGTH ? `${text.slice(0, MAX_DESCRIPTION_LENGTH)}...` : text;
2025-01-23 11:54:50 +01:00
};
2025-01-27 14:03:35 +01:00
const renderResources = (script: any) => {
const cpu = script.install_methods[0]?.resources.cpu;
const ram = script.install_methods[0]?.resources.ram;
const hdd = script.install_methods[0]?.resources.hdd;
2025-01-27 14:15:42 +01:00
2025-01-27 14:03:35 +01:00
const resourceParts = [];
if (cpu)
resourceParts.push(
<span key="cpu">
<b>CPU:</b> {cpu}vCPU
</span>,
);
if (ram)
resourceParts.push(
<span key="ram">
<b>RAM:</b> {ram}MB
</span>,
);
if (hdd)
resourceParts.push(
<span key="hdd">
<b>HDD:</b> {hdd}GB
</span>,
);
2025-01-27 14:15:42 +01:00
2025-01-27 14:03:35 +01:00
return resourceParts.length > 0 ? (
2025-01-27 14:07:56 +01:00
<div className="text-sm text-gray-400">
{resourceParts.map((part, index) => (
<React.Fragment key={index}>
{part}
{index < resourceParts.length - 1 && " | "}
</React.Fragment>
))}
</div>
2025-01-27 14:03:35 +01:00
) : null;
2025-01-27 13:16:26 +01:00
};
2025-01-23 09:27:58 +01:00
return (
2025-01-27 13:53:00 +01:00
<div className="p-6 mt-20">
2025-01-23 11:40:58 +01:00
{categories.length === 0 && (
<p className="text-center text-gray-500">No categories available. Please check the API endpoint.</p>
)}
2025-01-27 14:03:35 +01:00
{selectedCategoryIndex !== null ? (
2025-01-23 09:27:58 +01:00
<div>
2025-01-27 14:03:35 +01:00
{/* Header with Navigation */}
<div className="flex items-center justify-between mb-6">
<Button
variant="ghost"
onClick={() => navigateCategory("prev")}
2025-01-27 17:51:25 +01:00
className="p-2 transition-transform duration-300 hover:scale-105"
2025-01-27 14:03:35 +01:00
>
<ChevronLeft className="h-6 w-6" />
</Button>
2025-01-27 17:51:25 +01:00
<h2 className="text-3xl font-semibold transition-opacity duration-300 hover:opacity-90">
{categories[selectedCategoryIndex].name}
</h2>
2025-01-27 14:03:35 +01:00
<Button
variant="ghost"
onClick={() => navigateCategory("next")}
2025-01-27 17:51:25 +01:00
className="p-2 transition-transform duration-300 hover:scale-105"
2025-01-27 14:03:35 +01:00
>
<ChevronRight className="h-6 w-6" />
</Button>
</div>
{/* Scripts Grid */}
2025-01-27 13:53:00 +01:00
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-6">
2025-01-27 17:48:22 +01:00
{currentScripts
2025-01-23 09:27:58 +01:00
.sort((a, b) => a.name.localeCompare(b.name))
2025-01-23 10:35:07 +01:00
.map((script) => (
2025-01-27 13:31:47 +01:00
<Card
key={script.name}
2025-01-27 17:51:25 +01:00
className="p-4 cursor-pointer hover:shadow-md transition-shadow duration-300"
2025-01-27 13:31:47 +01:00
onClick={() => handleScriptClick(script.slug)}
>
2025-01-23 11:51:08 +01:00
<CardContent className="flex flex-col gap-4">
2025-01-27 17:51:25 +01:00
<h3 className="text-lg font-bold script-text text-center hover:text-blue-600 transition-colors duration-300">
{script.name}
</h3>
2025-01-27 17:49:14 +01:00
<img
src={script.logo || defaultLogo}
alt={script.name || "Script logo"}
className="h-12 w-12 object-contain mx-auto"
/>
<p className="text-sm text-gray-500 text-center">
2025-01-27 14:15:42 +01:00
<b>Created at:</b> {script.date_created || "No date available"}
</p>
<p
2025-01-27 17:51:25 +01:00
className="text-sm text-gray-700 hover:text-gray-900 text-center transition-colors duration-300"
2025-01-27 14:15:42 +01:00
title={script.description || "No description available."}
>
{truncateDescription(script.description || "No description available.")}
</p>
2025-01-27 14:03:35 +01:00
{renderResources(script)}
2025-01-23 09:35:24 +01:00
</CardContent>
</Card>
2025-01-23 09:27:58 +01:00
))}
2025-01-23 09:35:24 +01:00
</div>
2025-01-27 14:03:35 +01:00
{/* Back to Categories Button */}
<div className="mt-8 text-center">
<Button
variant="default"
onClick={handleBackClick}
2025-01-27 17:51:25 +01:00
className="px-6 py-2 text-white bg-blue-600 hover:bg-blue-700 rounded-lg shadow-md transition-transform duration-300 hover:scale-105"
2025-01-27 14:03:35 +01:00
>
Back to Categories
</Button>
</div>
2025-01-23 09:27:58 +01:00
</div>
) : (
<div>
2025-01-27 14:03:35 +01:00
{/* Categories Grid */}
2025-01-27 13:53:00 +01:00
<div className="flex justify-between items-center mb-8">
2025-01-27 14:15:42 +01:00
<h1 className="text-3xl font-semibold mb-4">Categories</h1>
2025-01-23 11:40:58 +01:00
<p className="text-sm text-gray-500">
2025-01-27 17:32:51 +01:00
{categories.reduce((total, category) => total + (category.scripts?.length || 0), 0)} Total scripts
2025-01-23 11:40:58 +01:00
</p>
</div>
2025-01-27 13:53:00 +01:00
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-8">
2025-01-27 14:03:35 +01:00
{categories.map((category, index) => (
2025-01-23 09:35:24 +01:00
<Card
key={category.name}
2025-01-27 14:03:35 +01:00
onClick={() => handleCategoryClick(index)}
2025-01-27 17:51:25 +01:00
className="cursor-pointer hover:shadow-lg flex flex-col items-center justify-center py-6 transition-shadow duration-300"
2025-01-23 09:35:24 +01:00
>
2025-01-23 11:20:10 +01:00
<CardContent className="flex flex-col items-center">
2025-01-27 17:51:25 +01:00
<h3 className="text-xl font-bold mb-4 category-title transition-colors duration-300 hover:text-blue-600">
{category.name}
</h3>
2025-01-27 14:21:10 +01:00
<div className="flex justify-center items-center gap-2 mb-4">
<Button
variant="ghost"
2025-01-27 17:32:51 +01:00
onClick={(e) => {
e.stopPropagation();
switchLogos(category.name, "prev");
}}
2025-01-27 17:51:25 +01:00
className="p-1 transition-transform duration-300 hover:scale-110"
2025-01-27 14:21:10 +01:00
>
<ChevronLeft className="h-4 w-4" />
</Button>
2025-01-27 14:13:16 +01:00
{category.scripts &&
2025-01-27 14:21:10 +01:00
category.scripts
.slice(logoIndices[category.name] || 0, (logoIndices[category.name] || 0) + MAX_LOGOS)
.map((script, i) => (
2025-01-27 17:51:25 +01:00
<div key={i} className="flex flex-col items-center">
<img
src={script.logo || defaultLogo}
alt={script.name || "Script logo"}
title={script.name}
className="h-8 w-8 object-contain cursor-pointer"
onClick={(e) => {
e.stopPropagation();
handleScriptClick(script.slug);
}}
/>
{formattedBadge(script.type)}
</div>
2025-01-27 14:21:10 +01:00
))}
<Button
variant="ghost"
2025-01-27 17:32:51 +01:00
onClick={(e) => {
e.stopPropagation();
switchLogos(category.name, "next");
}}
2025-01-27 17:51:25 +01:00
className="p-1 transition-transform duration-300 hover:scale-110"
2025-01-27 14:21:10 +01:00
>
<ChevronRight className="h-4 w-4" />
</Button>
2025-01-27 14:13:16 +01:00
</div>
2025-01-27 13:16:26 +01:00
<p className="text-sm text-gray-400 text-center">
2025-01-27 13:33:58 +01:00
{(category as any).description || "No description available."}
2025-01-23 11:24:12 +01:00
</p>
2025-01-23 11:20:10 +01:00
</CardContent>
2025-01-23 09:35:24 +01:00
</Card>
2025-01-23 09:27:58 +01:00
))}
2025-01-23 09:35:24 +01:00
</div>
2025-01-23 09:27:58 +01:00
</div>
)}
</div>
);
};
2025-01-27 13:16:26 +01:00
export default CategoryView;