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

124 lines
4.2 KiB
TypeScript
Raw Normal View History

2025-01-23 09:33:34 +01:00
"use client";
import React, { useEffect, useState } from "react";
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
2025-01-23 09:44:53 +01:00
import { Category, Script } from "@/lib/types";
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);
useEffect(() => {
2025-01-23 10:25:19 +01:00
const fetchCategoriesAndScripts = async () => {
2025-01-23 09:49:47 +01:00
try {
2025-01-23 10:25:19 +01:00
const basePath = process.env.NODE_ENV === "production" ? "/ProxmoxVE" : ""; // Dynamischer Basis-Pfad
// Kategorien laden
const categoriesResponse = await fetch(`${basePath}/json/metadata.json`);
if (!categoriesResponse.ok) {
2025-01-23 09:49:47 +01:00
throw new Error("Failed to fetch categories");
}
2025-01-23 10:25:19 +01:00
const metadata = await categoriesResponse.json();
console.log("Raw metadata:", metadata); // Debugging
if (!metadata.categories) {
throw new Error("Invalid metadata structure: categories missing");
}
const categories = metadata.categories.map((category: Category) => ({
...category,
scripts: [],
}));
// Skripte laden
const scriptsResponse = await fetch(`${basePath}/json`);
if (!scriptsResponse.ok) {
throw new Error("Failed to fetch scripts");
}
const scriptsList = await scriptsResponse.json();
const scripts: Script[] = await Promise.all(
scriptsList
.filter((file: string) => file.endsWith(".json") && file !== "metadata.json")
.map(async (file: string) => {
const scriptResponse = await fetch(`${basePath}/json/${file}`);
if (scriptResponse.ok) {
return await scriptResponse.json();
}
return null;
})
).then((results) => results.filter((script) => script !== null));
// Kategorien und Skripte verknüpfen
categories.forEach((category) => {
category.scripts = scripts.filter((script: Script) =>
2025-01-23 09:49:47 +01:00
script.categories.includes(category.id)
);
});
2025-01-23 10:25:19 +01:00
console.log("Parsed categories with scripts:", categories); // Debugging
2025-01-23 09:49:47 +01:00
setCategories(categories);
} catch (error) {
2025-01-23 10:25:19 +01:00
console.error("Error fetching categories and scripts:", error);
2025-01-23 09:49:47 +01:00
}
};
2025-01-23 10:25:19 +01:00
fetchCategoriesAndScripts();
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);
};
return (
<div className="p-4">
2025-01-23 10:25:19 +01:00
{categories.length === 0 && (
<p className="text-center text-gray-500">No categories available. Please check the JSON file.</p>
)}
2025-01-23 09:27:58 +01:00
{selectedCategory ? (
<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>
<h2 className="text-xl 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 09:27:58 +01:00
{selectedCategory.scripts
.sort((a, b) => a.name.localeCompare(b.name))
2025-01-23 09:44:53 +01:00
.map((script: Script) => (
2025-01-23 09:35:24 +01:00
<Card key={script.name}>
<CardContent>
<h3 className="text-lg font-medium">{script.name}</h3>
2025-01-23 09:44:53 +01:00
<p className="text-sm text-gray-600">
{script.date_created || "No date available"}
</p>
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>
<h1 className="text-2xl font-bold mb-6">Categories</h1>
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 09:27:58 +01:00
{categories.map((category) => (
2025-01-23 09:35:24 +01:00
<Card
key={category.name}
onClick={() => handleCategoryClick(category)}
className="cursor-pointer hover:shadow-lg"
>
<CardHeader title={category.name} className="text-lg font-semibold" />
</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 09:44:53 +01:00
export default CategoryView;