import React, { useEffect, useState } from "react"; import { LinkIncludingShortenedCollectionAndTags, ArchivedFormat, } from "@/types/global"; import Link from "next/link"; import { useSession } from "next-auth/react"; import { pdfAvailable, readabilityAvailable, monolithAvailable, screenshotAvailable, previewAvailable, } from "@/lib/shared/getArchiveValidity"; import PreservedFormatRow from "@/components/PreserverdFormatRow"; import getPublicUserData from "@/lib/client/getPublicUserData"; import { useTranslation } from "next-i18next"; import { BeatLoader } from "react-spinners"; import { useUser } from "@/hooks/store/user"; import { useGetLink } from "@/hooks/store/links"; import LinkIcon from "./LinkViews/LinkComponents/LinkIcon"; import CopyButton from "./CopyButton"; import { useRouter } from "next/router"; import Icon from "./Icon"; import { IconWeight } from "@phosphor-icons/react"; import Image from "next/image"; import clsx from "clsx"; type Props = { className?: string; link: LinkIncludingShortenedCollectionAndTags; standalone?: boolean; }; export default function LinkDetails({ className, link, standalone }: Props) { const { t } = useTranslation(); const session = useSession(); const getLink = useGetLink(); const { data: user = {} } = useUser(); 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 !== user.id) { const owner = await getPublicUserData( link.collection.ownerId as number ); setCollectionOwner(owner); } else if (link.collection.ownerId === user.id) { setCollectionOwner({ id: user.id as number, name: user.name, username: user.username as string, image: user.image as string, archiveAsScreenshot: user.archiveAsScreenshot as boolean, archiveAsMonolith: user.archiveAsScreenshot as boolean, archiveAsPDF: user.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 () => { await getLink.mutateAsync({ id: link.id as number, }); })(); let interval: any; if (!isReady()) { interval = setInterval(async () => { await getLink.mutateAsync({ id: link.id as number, }); }, 5000); } else { if (interval) { clearInterval(interval); } } return () => { if (interval) { clearInterval(interval); } }; }, [link?.monolith]); const router = useRouter(); const isPublicRoute = router.pathname.startsWith("/public") ? true : false; return (
{previewAvailable(link) ? ( { const target = e.target as HTMLElement; target.style.display = "none"; }} /> ) : link.preview === "unavailable" ? (
) : (
)}
{link.name &&

{link.name}

} {link.url && ( <>

{t("link")}

{link.url}
)}

{t("collection")}

{link.collection.name}

{link.collection.icon ? ( ) : ( )}
{link.tags[0] && ( <>

{t("tags")}

{link.tags.map((tag) => isPublicRoute ? (
{tag.name}
) : ( {tag.name} ) )}
)} {link.description && ( <>

{t("notes")}

{link.description}

)}

{link.url ? t("preserved_formats") : t("file")}

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

{t("preservation_in_queue")}

{t("check_back_later")}

) : link.url && !isReady() && atLeastOneFormatAvailable() ? (

{t("there_are_more_formats")}

{t("check_back_later")}

) : undefined} {link.url && (

{t("view_latest_snapshot")}

)}
); }