import React, { useEffect, useState } from "react"; import { Toaster } from "react-hot-toast"; import CollectionSelection from "@/components/InputSelect/CollectionSelection"; import TagSelection from "@/components/InputSelect/TagSelection"; import TextInput from "@/components/TextInput"; import unescapeString from "@/lib/client/unescapeString"; import useLinkStore from "@/store/links"; import { ArchivedFormat, LinkIncludingShortenedCollectionAndTags, } from "@/types/global"; import toast from "react-hot-toast"; import Link from "next/link"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faArrowUpRightFromSquare, faCloudArrowDown, faLink, faTrashCan, faUpRightFromSquare, } from "@fortawesome/free-solid-svg-icons"; import Modal from "../Modal"; import { faFileImage, faFilePdf } from "@fortawesome/free-regular-svg-icons"; import { useRouter } from "next/router"; import { useSession } from "next-auth/react"; type Props = { onClose: Function; activeLink: LinkIncludingShortenedCollectionAndTags; }; export default function PreservedFormatsModal({ onClose, activeLink }: Props) { const session = useSession(); const { links, getLink } = useLinkStore(); const [link, setLink] = useState(activeLink); const router = useRouter(); useEffect(() => { let isPublicRoute = router.pathname.startsWith("/public") ? true : undefined; (async () => { const data = await getLink(link.id as number, isPublicRoute); setLink( (data as any).response as LinkIncludingShortenedCollectionAndTags ); })(); let interval: any; if (link?.screenshotPath === "pending" || link?.pdfPath === "pending") { interval = setInterval(async () => { const data = await getLink(link.id as number, isPublicRoute); setLink( (data as any).response as LinkIncludingShortenedCollectionAndTags ); }, 5000); } else { if (interval) { clearInterval(interval); } } return () => { if (interval) { clearInterval(interval); } }; }, [link?.screenshotPath, link?.pdfPath, link?.readabilityPath]); const updateArchive = async () => { const load = toast.loading("Sending request..."); const response = await fetch(`/api/v1/links/${link?.id}/archive`, { method: "PUT", }); const data = await response.json(); toast.dismiss(load); if (response.ok) { toast.success(`Link is being archived...`); getLink(link?.id as number); } else toast.error(data.response); }; const handleDownload = (format: ArchivedFormat) => { 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 link = document.createElement("a"); link.href = path; link.download = format === ArchivedFormat.png ? "Screenshot" : "PDF"; link.click(); } else { console.error("Failed to download file"); } }) .catch((error) => { console.error("Error:", error); }); }; return (

Preserved Formats

{link?.screenshotPath && link?.screenshotPath !== "pending" ? (

Screenshot

handleDownload(ArchivedFormat.png)} className="cursor-pointer hover:opacity-60 duration-100 p-2 rounded-md" >
) : undefined} {link?.pdfPath && link.pdfPath !== "pending" ? (

PDF

handleDownload(ArchivedFormat.pdf)} className="cursor-pointer hover:opacity-60 duration-100 p-2 rounded-md" >
) : undefined}
{link?.collection.ownerId === session.data?.user.id ? (
updateArchive()} >

Update Preserved Formats

(Refresh Link)

) : undefined}

View Latest Snapshot on archive.org

); }