import LinkLayout from "@/layouts/LinkLayout"; import React, { useEffect, useState } from "react"; import Link from "next/link"; import useLinkStore from "@/store/links"; import { useRouter } from "next/router"; import { ArchivedFormat, LinkIncludingShortenedCollectionAndTags, } from "@/types/global"; import Image from "next/image"; import ColorThief, { RGBColor } from "colorthief"; import unescapeString from "@/lib/client/unescapeString"; import isValidUrl from "@/lib/client/isValidUrl"; import DOMPurify from "dompurify"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faBoxesStacked, faFolder } from "@fortawesome/free-solid-svg-icons"; import useModalStore from "@/store/modals"; import { useSession } from "next-auth/react"; import useLocalSettingsStore from "@/store/localSettings"; type LinkContent = { title: string; content: string; textContent: string; length: number; excerpt: string; byline: string; dir: string; siteName: string; lang: string; }; export default function Index() { const { links, getLink } = useLinkStore(); const { setModal } = useModalStore(); const { settings } = useLocalSettingsStore(); const session = useSession(); const userId = session.data?.user.id; const [link, setLink] = useState(); const [linkContent, setLinkContent] = useState(); const [imageError, setImageError] = useState(false); const [colorPalette, setColorPalette] = useState(); const router = useRouter(); useEffect(() => { const fetchLink = async () => { if (router.query.id) { await getLink(Number(router.query.id)); } }; fetchLink(); }, []); useEffect(() => { if (links[0]) setLink(links.find((e) => e.id === Number(router.query.id))); }, [links]); useEffect(() => { const fetchLinkContent = async () => { if ( router.query.id && link?.readabilityPath && link?.readabilityPath !== "pending" ) { const response = await fetch( `/api/v1/archives/${link?.id}?format=${ArchivedFormat.readability}` ); const data = await response?.json(); setLinkContent(data); } }; fetchLinkContent(); }, [link]); useEffect(() => { let interval: NodeJS.Timer | undefined; if ( link?.screenshotPath === "pending" || link?.pdfPath === "pending" || link?.readabilityPath === "pending" ) { interval = setInterval(() => getLink(link.id as number), 5000); } else { if (interval) { clearInterval(interval); } } return () => { if (interval) { clearInterval(interval); } }; }, [link?.screenshotPath, link?.pdfPath, link?.readabilityPath]); const colorThief = new ColorThief(); const rgbToHex = (r: number, g: number, b: number): string => "#" + [r, g, b] .map((x) => { const hex = x.toString(16); return hex.length === 1 ? "0" + hex : hex; }) .join(""); useEffect(() => { const banner = document.getElementById("link-banner"); const bannerInner = document.getElementById("link-banner-inner"); if (colorPalette && banner && bannerInner) { if (colorPalette[0] && colorPalette[1]) { banner.style.background = `linear-gradient(to right, ${rgbToHex( colorPalette[0][0], colorPalette[0][1], colorPalette[0][2] )}30, ${rgbToHex( colorPalette[1][0], colorPalette[1][1], colorPalette[1][2] )}30)`; } if (colorPalette[2] && colorPalette[3]) { bannerInner.style.background = `linear-gradient(to left, ${rgbToHex( colorPalette[2][0], colorPalette[2][1], colorPalette[2][2] )}30, ${rgbToHex( colorPalette[3][0], colorPalette[3][1], colorPalette[3][2] )})30`; } } }, [colorPalette]); return (
{link?.readabilityPath?.startsWith("archives") ? (
) : (
{link?.readabilityPath === "pending" ? (

Generating readable format, please wait...

) : ( <>

There is no reader view for this webpage

{link?.collection.ownerId === userId ? "You can update (refetch) the preserved formats by managing them below" : "The collections owners can refetch the preserved formats"}

{link?.collection.ownerId === userId ? (
link ? setModal({ modal: "LINK", state: true, active: link, method: "FORMATS", }) : undefined } className="mt-4 flex gap-2 w-fit mx-auto relative items-center font-semibold select-none cursor-pointer p-2 px-3 rounded-md dark:hover:bg-sky-600 text-white bg-sky-700 hover:bg-sky-600 duration-100" >

Manage preserved formats

) : undefined} )}
)}
); }