diff --git a/frontend/public/json/versions.json b/frontend/public/json/versions.json index 6c359b6..17bdb57 100644 --- a/frontend/public/json/versions.json +++ b/frontend/public/json/versions.json @@ -208,7 +208,7 @@ "version": "tc_v0.6.4" }, { - "name": "calibre-web-automatedcalibre-webautomated", + "name": "calibre-web-automated/calibre-webautomated", "version": "1.0" }, { diff --git a/frontend/src/app/api/versions/route.ts b/frontend/src/app/api/versions/route.ts index 2c6e526..6adfd7f 100644 --- a/frontend/src/app/api/versions/route.ts +++ b/frontend/src/app/api/versions/route.ts @@ -1,5 +1,7 @@ import { AppVersion } from "@/lib/types"; +import { error } from "console"; import { promises as fs } from "fs"; +// import Error from "next/error"; import { NextResponse } from "next/server"; import path from "path"; @@ -12,39 +14,33 @@ const encoding = "utf-8"; const getVersions = async () => { const filePath = path.resolve(jsonDir, versionsFileName); const fileContent = await fs.readFile(filePath, encoding); - const versions: AppVersion = JSON.parse(fileContent); - return versions; + const versions: AppVersion[] = JSON.parse(fileContent); + + const modifiedVersions = versions.map(version => { + const nameParts = version.name.split('/'); + let newName = nameParts[nameParts.length - 1]; + newName = newName.toLowerCase().replace(/[^a-z0-9]/g, ''); + return { ...version, name: newName }; + }); + + return modifiedVersions; }; - -export async function GET(request: Request) { +export async function GET() { try { - const { searchParams } = new URL(request.url); - const slug = searchParams.get("slug"); - if (!slug) { - return NextResponse.json({ error: "Missing slug parameter" }, { status: 400 }); - } - console.log("Slug: ", slug); const versions = await getVersions(); - - const cleanedSlug = slug.toLowerCase().replace(/[^a-z0-9]/g, ''); - - const matchedVersion = Object.values(versions).find( - (version: AppVersion) => { - const versionNameParts = version.name.split('/'); - const cleanedVersionName = versionNameParts[1]?.toLowerCase().replace(/[^a-z0-9]/g, ''); - return cleanedVersionName === cleanedSlug; - } - ); - - console.log("Matched Version: ", matchedVersion); - if (!matchedVersion) { - return NextResponse.json({name: "No version found", version: "No Version found"}); - } - return NextResponse.json(matchedVersion); + return NextResponse.json(versions); } catch (error) { - return NextResponse.json({name: error, version: "No version found - Error"}); + console.error(error); + const err = error as globalThis.Error; + return NextResponse.json({ + name: err.name, + message: err.message || "An unexpected error occurred", + version: "No version found - Error" + }, { + status: 500, + }); } } diff --git a/frontend/src/app/scripts/_components/ScriptItem.tsx b/frontend/src/app/scripts/_components/ScriptItem.tsx index 3414960..67e8b84 100644 --- a/frontend/src/app/scripts/_components/ScriptItem.tsx +++ b/frontend/src/app/scripts/_components/ScriptItem.tsx @@ -1,4 +1,4 @@ -"use client"; + import { Separator } from "@/components/ui/separator"; import { extractDate } from "@/lib/time"; import { Script, AppVersion } from "@/lib/types"; @@ -39,7 +39,7 @@ function ScriptItem({ useEffect(() => { - fetchVersions(item.slug) + fetchVersions() .then((fetchedVersions) => { console.log("Fetched Versions: ", fetchedVersions); if (Array.isArray(fetchedVersions)) { @@ -51,7 +51,7 @@ useEffect(() => { } }) .catch((error) => console.error("Error fetching versions:", error)); -}, [item.name]); +}, []); const defaultInstallMethod = item.install_methods?.[0]; const os = defaultInstallMethod?.resources?.os || "Proxmox Node"; @@ -103,7 +103,9 @@ const version = defaultInstallMethod?.resources?.version || ""; {versions.length === 0 ? (
Loading versions...
) : ( -Version: {versions[0].version}
+Version: { + versions.find((v) => v.name === item.slug.replace(/[^a-z0-9]/g, ''))?.version || "Not found" + }
)} diff --git a/frontend/src/lib/data.ts b/frontend/src/lib/data.ts index 48bae86..8546209 100644 --- a/frontend/src/lib/data.ts +++ b/frontend/src/lib/data.ts @@ -9,8 +9,8 @@ export const fetchCategories = async () => { return categories; }; -export const fetchVersions = async (slug: string) => { - const response = await fetch(`api/versions?slug=${slug}`); +export const fetchVersions = async () => { + const response = await fetch(`api/versions`); if (!response.ok) { throw new Error(`Failed to fetch versions: ${response.statusText}`); }