import React, { useEffect, useState } from "react"; import useLinkStore from "@/store/links"; import { LinkIncludingShortenedCollectionAndTags, ArchivedFormat, } 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, monolithAvailable, screenshotAvailable, } from "@/lib/shared/getArchiveValidity"; import PreservedFormatRow from "@/components/PreserverdFormatRow"; import useAccountStore from "@/store/account"; import getPublicUserData from "@/lib/client/getPublicUserData"; import { useTranslation } from "next-i18next"; import { BeatLoader } from "react-spinners"; type Props = { onClose: Function; activeLink: LinkIncludingShortenedCollectionAndTags; }; export default function PreservedFormatsModal({ onClose, activeLink }: Props) { const { t } = useTranslation(); const session = useSession(); const { getLink } = useLinkStore(); const { account } = useAccountStore(); const [link, setLink] = useState(activeLink); const router = useRouter(); let isPublic = router.pathname.startsWith("/public") ? true : undefined; const [collectionOwner, setCollectionOwner] = useState({ id: null as unknown as number, name: "", username: "", image: "", archiveAsScreenshot: undefined as unknown as boolean, archiveAsMonolith: undefined as unknown as boolean, archiveAsPDF: undefined as unknown as boolean, }); useEffect(() => { const fetchOwner = async () => { if (link.collection.ownerId !== account.id) { const owner = await getPublicUserData( link.collection.ownerId as number ); setCollectionOwner(owner); } else if (link.collection.ownerId === account.id) { setCollectionOwner({ id: account.id as number, name: account.name, username: account.username as string, image: account.image as string, archiveAsScreenshot: account.archiveAsScreenshot as boolean, archiveAsMonolith: account.archiveAsScreenshot as boolean, archiveAsPDF: account.archiveAsPDF as boolean, }); } }; fetchOwner(); }, [link.collection.ownerId]); const isReady = () => { return ( link && (collectionOwner.archiveAsScreenshot === true ? link.pdf && link.pdf !== "pending" : true) && (collectionOwner.archiveAsMonolith === true ? link.monolith && link.monolith !== "pending" : true) && (collectionOwner.archiveAsPDF === true ? link.pdf && link.pdf !== "pending" : true) && link.readable && link.readable !== "pending" ); }; const atLeastOneFormatAvailable = () => { return ( screenshotAvailable(link) || pdfAvailable(link) || readabilityAvailable(link) || monolithAvailable(link) ); }; useEffect(() => { (async () => { const data = await getLink(link.id as number, isPublic); setLink( (data as any).response as LinkIncludingShortenedCollectionAndTags ); })(); let interval: any; if (!isReady()) { interval = setInterval(async () => { const data = await getLink(link.id as number, isPublic); setLink( (data as any).response as LinkIncludingShortenedCollectionAndTags ); }, 5000); } else { if (interval) { clearInterval(interval); } } return () => { if (interval) { clearInterval(interval); } }; }, [link?.monolith]); const updateArchive = async () => { const load = toast.loading(t("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) { const newLink = await getLink(link?.id as number); setLink( (newLink as any).response as LinkIncludingShortenedCollectionAndTags ); toast.success(t("link_being_archived")); } else toast.error(data.response); }; return (

{t("preserved_formats")}

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

{t("available_formats")}

) : ( "" )}
{monolithAvailable(link) ? ( ) : undefined} {screenshotAvailable(link) ? ( ) : undefined} {pdfAvailable(link) ? ( ) : undefined} {readabilityAvailable(link) ? ( ) : undefined} {!isReady() && !atLeastOneFormatAvailable() ? (

{t("preservation_in_queue")}

{t("check_back_later")}

) : !isReady() && atLeastOneFormatAvailable() ? (

{t("there_are_more_formats")}

{t("check_back_later")}

) : undefined}

{t("view_latest_snapshot")}

{link?.collection.ownerId === session.data?.user.id && (

{t("refresh_preserved_formats")}

{t("this_deletes_current_preservations")}

)}
); }