svrjs-nextjs-website/app/(root)/downloads/page.tsx

114 lines
3 KiB
TypeScript
Raw Normal View History

2024-06-20 20:46:05 +02:00
"use client";
import { useEffect, useState } from "react";
2024-06-16 13:13:09 +02:00
import { Button } from "@/components/ui/button";
import {
2024-07-30 21:34:39 +02:00
Table,
TableBody,
TableCaption,
TableCell,
TableHead,
TableHeader,
TableRow,
2024-06-16 13:13:09 +02:00
} from "@/components/ui/table";
import { Download } from "lucide-react";
import Link from "next/link";
2024-07-30 21:34:39 +02:00
import { Skeleton } from "@/components/ui/skeleton";
2024-06-16 13:13:09 +02:00
2024-06-20 20:46:05 +02:00
interface Download {
2024-07-30 21:34:39 +02:00
_id: string;
date: string;
fileName: string;
version: string;
fileSize: string;
2024-08-26 13:11:47 +02:00
downloadLink?: string; // Optional
2024-06-20 20:46:05 +02:00
}
const DownloadPage: React.FC = () => {
2024-07-30 21:34:39 +02:00
const [downloads, setDownloads] = useState<Download[]>([]);
const [error, setError] = useState("");
2024-06-20 20:46:05 +02:00
2024-07-30 21:34:39 +02:00
const fetchDownloads = async () => {
try {
const response = await fetch("/api/downloads", {
method: "GET",
});
if (response.ok) {
const data: Download[] = await response.json();
setDownloads(data);
} else {
throw new Error(`HTTP error! status: ${response.status}`);
}
} catch (error: any) {
2024-08-26 13:11:47 +02:00
setError(error.message);
2024-07-30 21:34:39 +02:00
}
};
2024-06-20 20:46:05 +02:00
2024-07-30 21:34:39 +02:00
useEffect(() => {
fetchDownloads();
2024-06-20 20:46:05 +02:00
2024-07-30 21:34:39 +02:00
const interval = setInterval(() => {
fetchDownloads();
}, 10000);
2024-06-20 20:46:05 +02:00
2024-07-30 21:34:39 +02:00
return () => clearInterval(interval);
}, []);
2024-06-16 13:13:09 +02:00
2024-07-30 21:34:39 +02:00
return (
<section
id="download"
className="wrapper container py-24 md:py-28 gap-2 flex flex-col"
>
<h1 className="text-3xl md:text-5xl pb-1 md:pb-2 font-bold text-black dark:bg-clip-text dark:text-transparent dark:bg-gradient-to-b dark:from-white dark:to-neutral-400">
Downloads
</h1>
<p className="md:text-lg text-muted-foreground text-start mb-6">
Get all the latest version of SVRJS download and compiled Files here!
</p>
{error && <p className="text-red-500">{error}</p>}
<Table>
<TableCaption>A list of all available downloads.</TableCaption>
<TableHeader>
<TableRow>
<TableHead className="w-[150px]">Date</TableHead>
<TableHead>File Name</TableHead>
<TableHead>Version</TableHead>
<TableHead>File Size</TableHead>
<TableHead className="text-right">Download Link</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{downloads
.slice(0, 10)
.reverse()
.map((download) => (
<TableRow key={download._id}>
<TableCell className="font-medium">{download.date}</TableCell>
<TableCell>{download.fileName}</TableCell>
<TableCell>{download.version}</TableCell>
<TableCell className="text-left">{download.fileSize}</TableCell>
<TableCell className="flex items-center justify-end">
2024-08-26 13:11:47 +02:00
{download.downloadLink ? (
<Link href={download.downloadLink}>
<Button variant={"ghost"} className="">
<Download className="w-4 h-4 mr-2" />
Download
</Button>
</Link>
) : (
<Button variant={"ghost"} disabled>
2024-07-30 21:34:39 +02:00
<Download className="w-4 h-4 mr-2" />
2024-08-26 13:11:47 +02:00
Unavailable
2024-07-30 21:34:39 +02:00
</Button>
2024-08-26 13:11:47 +02:00
)}
2024-07-30 21:34:39 +02:00
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</section>
);
2024-06-16 13:13:09 +02:00
};
export default DownloadPage;