"use client"; import React, { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { Card, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { ChevronLeft, ChevronRight } from "lucide-react"; import { Category } from "@/lib/types"; const defaultLogo = "/default-logo.png"; // Fallback logo path const MAX_DESCRIPTION_LENGTH = 100; // Set max length for description const MAX_LOGOS = 5; // Max logos to display at once const CategoryView = () => { const [categories, setCategories] = useState([]); const [selectedCategoryIndex, setSelectedCategoryIndex] = useState(null); const router = useRouter(); useEffect(() => { const fetchCategories = async () => { try { const basePath = process.env.NODE_ENV === "production" ? "/ProxmoxVE" : ""; const response = await fetch(`${basePath}/api/categories`); 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); } }; fetchCategories(); }, []); const handleCategoryClick = (index: number) => { setSelectedCategoryIndex(index); }; const handleBackClick = () => { setSelectedCategoryIndex(null); }; const handleScriptClick = (scriptSlug: string) => { router.push(`/scripts?id=${scriptSlug}`); }; const navigateCategory = (direction: "prev" | "next") => { if (selectedCategoryIndex !== null) { const newIndex = direction === "prev" ? (selectedCategoryIndex - 1 + categories.length) % categories.length : (selectedCategoryIndex + 1) % categories.length; setSelectedCategoryIndex(newIndex); } }; const truncateDescription = (text: string) => { return text.length > MAX_DESCRIPTION_LENGTH ? `${text.slice(0, MAX_DESCRIPTION_LENGTH)}...` : text; }; 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; const resourceParts = []; if (cpu) resourceParts.push(CPU: {cpu}vCPU); if (ram) resourceParts.push(RAM: {ram}MB); if (hdd) resourceParts.push(HDD: {hdd}GB); return resourceParts.length > 0 ? (
{resourceParts.reduce((prev, curr) => [prev, " | ", curr])}
) : null; }; return (
{categories.length === 0 && (

No categories available. Please check the API endpoint.

)} {selectedCategoryIndex !== null ? (
{/* Header with Navigation */}

{categories[selectedCategoryIndex].name}

{/* Scripts Grid */}
{categories[selectedCategoryIndex].scripts .sort((a, b) => a.name.localeCompare(b.name)) .map((script) => ( handleScriptClick(script.slug)} >
{script.name}

{script.name}

Created at: {script.date_created || "No date available"}

{truncateDescription(script.description || "No description available.")}

{renderResources(script)}
))}
{/* Back to Categories Button */}
) : (
{/* Categories Grid */}

Categories

{categories.reduce((acc, cat) => acc + (cat.scripts?.length || 0), 0)} Total scripts

{categories.map((category, index) => ( handleCategoryClick(index)} className="cursor-pointer hover:shadow-lg flex flex-col items-center justify-center py-6" >

{category.name}

{(category as any).description || "No description available."}

))}
)}
); }; export default CategoryView;