"use client"; import { Suspense } from "react"; import { useEffect, useState } from "react"; import { useSearchParams, useRouter } from "next/navigation"; import toast, { Toaster } from "react-hot-toast"; import Head from "next/head"; import { number } from "zod"; // import { SharePage } from "~/components/SharePage"; interface FileDetails { name: string; size: number; owner: string; uploadDate: string; id: string; isOwner: boolean; type: string; url: string; } function UploadsPage() { const searchParams = useSearchParams(); const router = useRouter(); const fileId = searchParams.get("id"); const [fileDetails, setFileDetails] = useState(null); const [error, setError] = useState(null); // const mediasrc = SharePage() as string; // Replace with a valid string URL or logic to generate the URL useEffect(() => { if (!fileId) { setError("File name is required."); return; } const fetchFileDetails = async () => { try { const response = await fetch(`/api/share?id=${encodeURIComponent(fileId)}`); if (!response.ok) { throw new Error("Failed to fetch file details"); } const data: FileDetails = await response.json(); // Explicitly type the response setFileDetails(data); } catch (err) { console.error(err); setError("Failed to load file details."); } }; void fetchFileDetails(); // Use `void` to mark the promise as intentionally ignored }, [fileId]); // set page og meta tags useEffect(() => { if (fileDetails) { const ogTitle = `File Details: ${fileDetails.name}`; // proper size conversion const sizeInKB = fileDetails.size / 1024; const sizeInMB = sizeInKB / 1024; const sizeInGB = sizeInMB / 1024; let sizeDescription: string = `${sizeInKB.toFixed(2)} KB`; if (sizeInMB >= 1) { sizeDescription = `${sizeInMB.toFixed(2)} MB`; } else if (sizeInGB >= 1) { sizeDescription = `${sizeInGB.toFixed(2)} GB`; } const ogDescription = `File Name: ${fileDetails.name}, Size: ${sizeDescription}, Owner: ${fileDetails.owner}, Upload Date: ${new Date(fileDetails.uploadDate).toLocaleString()}`; // document.title = ogTitle; // if meta og tags are not present, create them if (!document.querySelector('meta[name="description"]')) { const metaDescription = document.createElement("meta"); metaDescription.name = "description"; document.head.appendChild(metaDescription); } if (!document.querySelector('meta[property="og:title"]')) { const metaOgTitle = document.createElement("meta"); metaOgTitle.setAttribute("property", "og:title"); document.head.appendChild(metaOgTitle); } if (!document.querySelector('meta[property="og:description"]')) { const metaOgDescription = document.createElement("meta"); metaOgDescription.setAttribute("property", "og:description"); document.head.appendChild(metaOgDescription); } document.querySelector('meta[name="description"]')?.setAttribute("content", ogDescription); document.querySelector('meta[property="og:title"]')?.setAttribute("content", ogTitle); document.querySelector('meta[property="og:description"]')?.setAttribute("content", ogDescription); } }, [fileDetails]); const handleDownload = async () => { try { if (!fileDetails) { toast.error("File details not available."); return; } const response = await fetch(`/api/files/download?fileId=${encodeURIComponent(fileDetails.id)}&fileName=${encodeURIComponent(fileDetails.name)}`); if (!response.ok) { throw new Error("Failed to download file"); } // Download the file with the correct filename const blob = await response.blob(); const url = window.URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = fileDetails.name; document.body.appendChild(a); a.click(); a.remove(); window.URL.revokeObjectURL(url); toast.success(`File "${fileDetails.name}" downloaded successfully!`); } catch (err) { console.error(err); toast.error("Failed to download file."); } }; const handleShare = () => { if (fileDetails) { const shareableLink = `${window.location.origin}/share?id=${fileDetails.id}`; navigator.clipboard .writeText(shareableLink) .then(() => toast.success("Shareable link copied to clipboard!")) .catch(() => toast.error("Failed to copy link.")); } }; const handleRemove = async () => { try { const response = await fetch(`/api/remove`, { method: "DELETE", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ id: fileDetails?.id }), // Use optional chaining }); if (!response.ok) { throw new Error("Failed to remove file"); } toast.success("File removed successfully!"); router.push("/"); } catch (err) { console.error(err); toast.error("Failed to remove file."); } }; if (error) { return
{error}
; } if (!fileDetails) { return (

File Details

); } return (

File Details

{/* {(fileDetails.type.startsWith(".png") || fileDetails.type.startsWith(".jpg") || fileDetails.type.startsWith(".jpeg") || fileDetails.type.startsWith(".gif")) && (mediasrc && Media preview)} {(fileDetails.type.startsWith(".mp4") || fileDetails.type.startsWith(".webm") || fileDetails.type.startsWith(".ogg")) && (mediasrc && )} */}

Name: {fileDetails.name}

Size: {(fileDetails.size / 1024).toFixed(2)} KB

Owner: {fileDetails.owner}

Upload Date: {new Date(fileDetails.uploadDate).toLocaleString()}

{fileDetails.isOwner && ( <> )}
); } export default function Page() { return ( Loading...}> ); }