import { CollectionIncludingMembersAndLinkCount, LinkIncludingShortenedCollectionAndTags, } from "@/types/global"; import Image from "next/image"; import ColorThief, { RGBColor } from "colorthief"; import { useEffect, useState } from "react"; import Link from "next/link"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faArrowUpRightFromSquare, faBoxArchive, faCloudArrowDown, faFolder, } from "@fortawesome/free-solid-svg-icons"; import useCollectionStore from "@/store/collections"; import { faCalendarDays, faFileImage, faFilePdf, } from "@fortawesome/free-regular-svg-icons"; import isValidUrl from "@/lib/shared/isValidUrl"; import unescapeString from "@/lib/client/unescapeString"; import useLocalSettingsStore from "@/store/localSettings"; import Modal from "../Modal"; type Props = { link: LinkIncludingShortenedCollectionAndTags; onClose: Function; }; export default function LinkDetails({ link, onClose }: Props) { const { settings: { theme }, } = useLocalSettingsStore(); const [imageError, setImageError] = useState(false); const formattedDate = new Date(link.createdAt as string).toLocaleString( "en-US", { year: "numeric", month: "short", day: "numeric", } ); const { collections } = useCollectionStore(); const [collection, setCollection] = useState( collections.find( (e) => e.id === link.collection.id ) as CollectionIncludingMembersAndLinkCount ); useEffect(() => { setCollection( collections.find( (e) => e.id === link.collection.id ) as CollectionIncludingMembersAndLinkCount ); }, [collections]); const [colorPalette, setColorPalette] = useState(); const colorThief = new ColorThief(); const url = link.url && isValidUrl(link.url) ? new URL(link.url) : undefined; const handleDownload = (format: "png" | "pdf") => { const path = `/api/v1/archives/${link.collection.id}/${link.id}.${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 === "pdf" ? "PDF" : "Screenshot"; link.click(); } else { console.error("Failed to download file"); } }) .catch((error) => { console.error("Error:", error); }); }; return (
{!imageError && url && ( { try { const color = colorThief.getPalette( e.target as HTMLImageElement, 4 ); setColorPalette(color); } catch (err) { console.log(err); } }} onError={(e) => { setImageError(true); }} /> )}

{unescapeString(link.name)}

{url ? url.host : link.url}

{collection?.name}

{link.tags.map((e, i) => (

{e.name}

))}
{link.description && ( <>
{unescapeString(link.description)}
)}

Archived Formats:

{formattedDate}

Screenshot

handleDownload("png")} className="cursor-pointer hover:bg-slate-200 hover:dark:bg-neutral-700 duration-100 p-2 rounded-md" >

PDF

handleDownload("pdf")} className="cursor-pointer hover:bg-slate-200 hover:dark:bg-neutral-700 duration-100 p-2 rounded-md" >
); }