You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
11 KiB

7 months ago
"use client";
import { useEffect, useState } from "react";
import { useAuth } from "@/lib/auth-context";
import { useRouter } from "next/navigation";
7 months ago
import {
Pagination,
PaginationContent,
PaginationItem,
PaginationLink,
PaginationPrevious,
PaginationNext,
} from "@/components/ui/pagination";
7 months ago
interface UploadedFile {
id: string;
original_name: string;
file_path: string;
upload_status: string;
created_at: string;
}
export default function RendersPage() {
const { isAuthenticated, user } = useAuth();
const router = useRouter();
const [files, setFiles] = useState<UploadedFile[]>([]);
const [loading, setLoading] = useState(true);
const [authInitialized, setAuthInitialized] = useState(false);
const [deletingId, setDeletingId] = useState<string | null>(null);
7 months ago
const [page, setPage] = useState(1);
const pageSize = 5;
const [totalPages, setTotalPages] = useState(1);
7 months ago
useEffect(() => {
const timer = setTimeout(() => setAuthInitialized(true), 100);
return () => clearTimeout(timer);
}, []);
useEffect(() => {
if (!authInitialized) return;
if (!isAuthenticated || !user) {
router.push("/login?redirect=/renders");
return;
}
7 months ago
fetchFiles(page);
7 months ago
// eslint-disable-next-line
7 months ago
}, [authInitialized, isAuthenticated, user, page]);
7 months ago
7 months ago
async function fetchFiles(pageNum: number) {
setLoading(true);
7 months ago
try {
7 months ago
const res = await fetch(
`/api/renders/in?page=${pageNum}&pageSize=${pageSize}`,
{
7 months ago
headers: { "user-id": user?.id || "" },
7 months ago
}
);
7 months ago
const data = await res.json();
setFiles(data.files || []);
7 months ago
setTotalPages(data.pagination?.totalPages || 1);
7 months ago
} catch {
setFiles([]);
7 months ago
setTotalPages(1);
7 months ago
} finally {
setLoading(false);
}
}
async function handleDelete(id: string) {
if (!user) return;
const ok = confirm("Delete this file? This cannot be undone.");
if (!ok) return;
setDeletingId(id);
try {
const res = await fetch("/api/renders/in", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
"user-id": user.id,
},
body: JSON.stringify({ id }),
});
const data = await res.json();
if (!res.ok) throw new Error(data?.error || "Delete failed");
7 months ago
await fetchFiles(page);
7 months ago
} catch (e) {
alert((e as any).message || "Delete failed");
} finally {
setDeletingId(null);
}
}
if (!authInitialized || !isAuthenticated) {
return (
<div className="min-h-screen flex items-center justify-center">
Loading...
</div>
);
}
7 months ago
const canPrev = page > 1;
const canNext = page < totalPages;
7 months ago
return (
7 months ago
<div className="min-h-screen bg-gray-50 py-6 sm:py-12">
<div className="max-w-5xl mx-auto px-4 sm:px-6">
<h1 className="text-2xl sm:text-3xl md:text-4xl font-bold text-gray-900 mb-6 sm:mb-8">
My Renders
</h1>
7 months ago
7 months ago
<section className="mb-8 sm:mb-12">
<h2 className="text-xl sm:text-2xl font-bold text-orange-600 mb-4">
7 months ago
Uploaded Files
</h2>
{loading ? (
<div>Loading...</div>
) : files.length === 0 ? (
<div className="text-gray-500">No files uploaded yet.</div>
) : (
7 months ago
<>
7 months ago
<div className="overflow-x-auto -mx-4 sm:mx-0">
<div className="inline-block min-w-full align-middle">
<table className="min-w-full bg-white rounded-lg shadow-md">
<thead>
<tr className="bg-orange-50">
<th className="p-2 sm:p-3 text-left font-semibold text-xs sm:text-sm">
Filename
</th>
<th className="p-2 sm:p-3 text-left font-semibold text-xs sm:text-sm">
Status
</th>
<th className="p-2 sm:p-3 text-left font-semibold text-xs sm:text-sm hidden sm:table-cell">
Uploaded At
</th>
<th className="p-2 sm:p-3 text-left font-semibold text-xs sm:text-sm">
7 months ago
Download
7 months ago
</th>
<th className="p-2 sm:p-3 text-left font-semibold text-xs sm:text-sm">
Actions
</th>
</tr>
</thead>
<tbody>
{files.map((f) => (
<tr key={f.id} className="border-t">
<td className="p-2 sm:p-3 text-xs sm:text-sm break-words max-w-[120px] sm:max-w-none">
{f.original_name}
</td>
<td className="p-2 sm:p-3 capitalize">
{/* Mobile: Green checkmark */}
{f.upload_status === "COMPLETE" && (
<span className="sm:hidden text-green-500 text-xl">
</span>
)}
{f.upload_status !== "COMPLETE" && (
<span className="sm:hidden text-gray-400 text-xl">
</span>
)}
{/* Desktop: Text badge */}
<span
className={`hidden sm:inline-block px-2 py-1 rounded-full text-xs font-bold ${
f.upload_status === "COMPLETE"
? "bg-green-100 text-green-800"
: "bg-gray-200 text-gray-600"
}`}
>
{f.upload_status}
</span>
</td>
<td className="p-2 sm:p-3 text-xs sm:text-sm hidden sm:table-cell">
{new Date(f.created_at).toLocaleString()}
</td>
<td className="p-2 sm:p-3">
<a
href={
f.file_path
? `/uploads/${f.file_path.split("/").pop()}`
: "#"
}
className="underline text-blue-700 text-xs sm:text-sm"
target="_blank"
rel="noopener noreferrer"
download={f.original_name}
>
Download
</a>
</td>
<td className="p-2 sm:p-3">
{/* Mobile: Red X icon - circular */}
<button
className="sm:hidden w-6 h-6 rounded-full bg-red-500 text-white hover:bg-red-600 disabled:bg-red-300 disabled:opacity-50 flex items-center justify-center"
disabled={deletingId === f.id}
onClick={() => handleDelete(f.id)}
aria-label="Delete"
>
{deletingId === f.id ? (
<span className="text-xs">...</span>
) : (
<span className="text-sm font-bold">×</span>
)}
</button>
{/* Desktop: Text button */}
<button
className="hidden sm:inline-block px-3 py-1 text-sm rounded-md bg-red-500 text-white hover:bg-red-600 disabled:bg-red-300"
disabled={deletingId === f.id}
onClick={() => handleDelete(f.id)}
>
{deletingId === f.id ? "Deleting..." : "Delete"}
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
7 months ago
<div className="mt-4 flex justify-center">
<Pagination>
<PaginationContent>
<PaginationItem>
<PaginationPrevious
href="#"
onClick={(e) => {
e.preventDefault();
if (canPrev) setPage(page - 1);
}}
aria-disabled={!canPrev}
className={
!canPrev ? "pointer-events-none opacity-50" : ""
}
/>
</PaginationItem>
{Array.from({ length: totalPages }, (_, i) => i + 1).map(
(p) => (
<PaginationItem key={p}>
<PaginationLink
href="#"
isActive={p === page}
onClick={(e) => {
e.preventDefault();
setPage(p);
}}
>
{p}
</PaginationLink>
</PaginationItem>
)
)}
<PaginationItem>
<PaginationNext
href="#"
onClick={(e) => {
e.preventDefault();
if (canNext) setPage(page + 1);
}}
aria-disabled={!canNext}
className={
!canNext ? "pointer-events-none opacity-50" : ""
}
/>
</PaginationItem>
</PaginationContent>
</Pagination>
</div>
</>
7 months ago
)}
</section>
<section>
7 months ago
<h2 className="text-xl sm:text-2xl font-bold text-gray-600 mb-4">
7 months ago
Rendered Files (outputs)
</h2>
7 months ago
<div className="border-2 border-dashed border-gray-300 rounded-lg p-4 sm:p-8 text-center text-gray-400 text-sm sm:text-base">
7 months ago
Placeholder: Your rendered products will appear here in the future!
</div>
</section>
</div>
</div>
);
}