import React, { useEffect, useState } from "react"; import useLinkStore from "@/store/links"; import { ArchivedFormat, LinkIncludingShortenedCollectionAndTags, } from "@/types/global"; import toast from "react-hot-toast"; import Link from "next/link"; import Modal from "../Modal"; import { useRouter } from "next/router"; import { useSession } from "next-auth/react"; import { pdfAvailable, readabilityAvailable, screenshotAvailable, } from "@/lib/shared/getArchiveValidity"; import PreservedFormatRow from "@/components/PreserverdFormatRow"; type Props = { onClose: Function; activeLink: LinkIncludingShortenedCollectionAndTags; }; export default function PreservedFormatsModal({ onClose, activeLink }: Props) { const session = useSession(); const { 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); }; return (

Preserved Formats

{screenshotAvailable(link) || pdfAvailable(link) || readabilityAvailable(link) ? (

The following formats are available for this link:

) : (

No preserved formats available.

)}
{readabilityAvailable(link) ? ( ) : undefined} {screenshotAvailable(link) ? ( ) : undefined} {pdfAvailable(link) ? ( ) : undefined}
{link?.collection.ownerId === session.data?.user.id ? (
updateArchive()} >

Update Preserved Formats

(Refresh Link)

) : undefined}

View latest snapshot on archive.org

); }