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

151 lines
5.7 KiB
TypeScript
Raw Normal View History

2025-01-23 09:33:34 +01:00
"use client";
import React, { useEffect, useState } from "react";
2025-01-23 12:01:39 +01:00
import { useRouter } from "next/navigation";
2025-01-23 11:31:46 +01:00
import { Card, CardContent } from "@/components/ui/card";
2025-01-23 09:33:34 +01:00
import { Button } from "@/components/ui/button";
2025-01-23 10:35:07 +01:00
import { Category } from "@/lib/types";
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-23 09:27:58 +01:00
const CategoryView = () => {
2025-01-23 09:33:34 +01:00
const [categories, setCategories] = useState<Category[]>([]);
const [selectedCategory, setSelectedCategory] = useState<Category | null>(null);
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();
console.log("Fetched categories:", data); // Debugging
setCategories(data);
} 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-23 09:33:34 +01:00
const handleCategoryClick = (category: Category) => {
2025-01-23 09:27:58 +01:00
setSelectedCategory(category);
};
const handleBackClick = () => {
setSelectedCategory(null);
};
2025-01-23 12:01:39 +01:00
const handleScriptClick = (scriptSlug: string) => {
router.push(`/scripts?id=${scriptSlug}`);
};
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-27 13:31:47 +01:00
const getRandomScripts = (scripts: any[]) => {
if (!scripts || scripts.length <= 5) return scripts;
return scripts.sort(() => 0.5 - Math.random()).slice(0, 5);
2025-01-27 13:16:26 +01:00
};
2025-01-23 09:27:58 +01:00
return (
2025-01-23 11:40:58 +01:00
<div className="p-4 mt-20">
{categories.length === 0 && (
<p className="text-center text-gray-500">No categories available. Please check the API endpoint.</p>
)}
{selectedCategory ? (
2025-01-23 09:27:58 +01:00
<div>
2025-01-23 09:42:50 +01:00
<Button variant="default" onClick={handleBackClick} className="mb-4">
2025-01-23 09:27:58 +01:00
Back to Categories
</Button>
2025-01-27 13:31:47 +01:00
<h2 className="text-3xl font-semibold mb-4">{selectedCategory.name}</h2>
2025-01-23 09:35:24 +01:00
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">
2025-01-23 11:40:58 +01:00
{selectedCategory.scripts
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}
className="p-4 cursor-pointer"
onClick={() => handleScriptClick(script.slug)}
>
2025-01-23 11:51:08 +01:00
<CardContent className="flex flex-col gap-4">
2025-01-27 13:48:19 +01:00
<div>
<h3 className="text-lg font-bold">{script.name}</h3>
<p className="text-sm text-gray-500">
<b>Created at:</b> {script.date_created || "No date available"}
</p>
</div>
<div className="flex flex-wrap justify-center gap-2">
2025-01-23 11:51:08 +01:00
<img
src={script.logo || defaultLogo}
alt={script.name}
className="h-12 w-12 object-contain"
/>
</div>
2025-01-27 13:48:19 +01:00
<div>
<p className="text-sm text-gray-700">
{truncateDescription(script.description || "No description available.")}
</p>
<div className="text-sm text-gray-600 mt-2">
<b>CPU:</b> {script.install_methods[0]?.resources.cpu || "N/A"}vCPU |{" "}
<b>RAM:</b> {script.install_methods[0]?.resources.ram || "N/A"}MB |{" "}
<b>HDD:</b> {script.install_methods[0]?.resources.hdd || "N/A"}GB
</div>
2025-01-23 11:51:08 +01:00
</div>
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-23 09:27:58 +01:00
</div>
) : (
<div>
2025-01-23 11:40:58 +01:00
<div className="flex justify-between items-center mb-6">
2025-01-27 13:31:47 +01:00
<h1 className="text-4xl font-bold mb-2">Categories</h1>
2025-01-23 11:40:58 +01:00
<p className="text-sm text-gray-500">
{categories.reduce((acc, cat) => acc + (cat.scripts?.length || 0), 0)} Total scripts
</p>
</div>
2025-01-27 13:31:47 +01:00
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-6">
2025-01-23 09:27:58 +01:00
{categories.map((category) => (
2025-01-23 09:35:24 +01:00
<Card
key={category.name}
onClick={() => handleCategoryClick(category)}
2025-01-23 11:20:10 +01:00
className="cursor-pointer hover:shadow-lg flex flex-col items-center justify-center"
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 13:48:19 +01:00
<h3 className="text-xl font-bold mb-2">{category.name}</h3>
2025-01-27 13:31:47 +01:00
<div className="flex flex-wrap justify-center gap-2 mb-4">
{category.scripts &&
getRandomScripts(category.scripts).map((script, index) => (
<img
key={index}
src={script.logo || defaultLogo}
alt={script.name || "Script logo"}
className="h-8 w-8 object-contain"
/>
))}
2025-01-23 11:20:10 +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;