import React, { useEffect, useState } from "react"; import useLinkStore from "@/store/links"; import { ArchivedFormat, LinkIncludingShortenedCollectionAndTags, } from "@/types/global"; import Link from "next/link"; import { useRouter } from "next/router"; type Props = { name: string; icon: string; format: ArchivedFormat; activeLink: LinkIncludingShortenedCollectionAndTags; downloadable?: boolean; }; export default function PreservedFormatRow({ name, icon, format, activeLink, downloadable, }: Props) { const { getLink } = useLinkStore(); const [link, setLink] = useState(activeLink); const router = useRouter(); let isPublic = router.pathname.startsWith("/public") ? true : undefined; useEffect(() => { (async () => { const { data } = await getLink(link.id as number, isPublic); setLink(data as LinkIncludingShortenedCollectionAndTags); })(); let interval: NodeJS.Timeout | null = null; if (link?.image === "pending" || link?.pdf === "pending") { interval = setInterval(async () => { const { data } = await getLink(link.id as number, isPublic); setLink(data as LinkIncludingShortenedCollectionAndTags); }, 5000); } else { if (interval) { clearInterval(interval); } } return () => { if (interval) { clearInterval(interval); } }; }, [link?.image, link?.pdf, link?.readable, link?.monolith]); const handleDownload = () => { const path = `/api/v1/archives/${link?.id}?format=${format}`; fetch(path) .then((response) => { if (response.ok) { // Create a temporary link and click it to trigger the download const anchorElement = document.createElement("a"); anchorElement.href = path; anchorElement.download = format === ArchivedFormat.monolith ? "Webpage" : format === ArchivedFormat.pdf ? "PDF" : "Screenshot"; anchorElement.click(); } else { console.error("Failed to download file"); } }) .catch((error) => { console.error("Error:", error); }); }; return (

{name}

{downloadable || (false && (
handleDownload()} className="btn btn-sm btn-square" >
))}
); }