el.xwx.moe/components/Modal/Link/PreservedFormats.tsx

206 lines
6.4 KiB
TypeScript
Raw Normal View History

2023-11-19 15:22:27 -06:00
import {
ArchivedFormat,
LinkIncludingShortenedCollectionAndTags,
} from "@/types/global";
2023-10-30 14:20:15 -05:00
import { useEffect, useState } from "react";
import Link from "next/link";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
faArrowUpRightFromSquare,
faCloudArrowDown,
} from "@fortawesome/free-solid-svg-icons";
import { faFileImage, faFilePdf } from "@fortawesome/free-regular-svg-icons";
import useLinkStore from "@/store/links";
import { toast } from "react-hot-toast";
import { useRouter } from "next/router";
import { useSession } from "next-auth/react";
2023-12-13 05:59:36 -06:00
import {
pdfAvailable,
screenshotAvailable,
} from "@/lib/shared/getArchiveValidity";
2023-10-30 14:20:15 -05:00
export default function PreservedFormats() {
const session = useSession();
const { links, getLink } = useLinkStore();
const [link, setLink] = useState<LinkIncludingShortenedCollectionAndTags>();
const router = useRouter();
useEffect(() => {
if (links) setLink(links.find((e) => e.id === Number(router.query.id)));
}, [links]);
useEffect(() => {
2023-12-11 02:05:47 -06:00
let interval: any;
2023-12-13 05:59:36 -06:00
if (screenshotAvailable(link) && pdfAvailable(link)) {
2023-11-19 07:12:37 -06:00
let isPublicRoute = router.pathname.startsWith("/public")
? true
: undefined;
interval = setInterval(
2023-12-13 05:59:36 -06:00
() => getLink(link?.id as number, isPublicRoute),
2023-11-19 07:12:37 -06:00
5000
);
2023-10-30 14:20:15 -05:00
} 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);
2023-10-31 14:44:58 -05:00
} else toast.error(data.response);
2023-10-30 14:20:15 -05:00
};
2023-11-19 15:22:27 -06:00
const handleDownload = (format: ArchivedFormat) => {
const path = `/api/v1/archives/${link?.id}?format=${format}`;
2023-10-30 14:20:15 -05:00
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;
2023-11-25 02:19:02 -06:00
link.download = format === ArchivedFormat.png ? "Screenshot" : "PDF";
2023-10-30 14:20:15 -05:00
link.click();
} else {
console.error("Failed to download file");
}
})
.catch((error) => {
console.error("Error:", error);
});
};
return (
<div className={`flex flex-col gap-3 sm:w-[35rem] w-80 pt-3`}>
2023-12-13 05:59:36 -06:00
{screenshotAvailable(link) ? (
<div className="flex justify-between items-center pr-1 border border-neutral-content rounded-md">
2023-10-30 14:20:15 -05:00
<div className="flex gap-2 items-center">
2023-12-04 09:24:45 -06:00
<div className="bg-primary text-primary-content p-2 rounded-l-md">
2023-10-30 14:20:15 -05:00
<FontAwesomeIcon icon={faFileImage} className="w-6 h-6" />
</div>
2023-11-24 07:39:55 -06:00
<p>Screenshot</p>
2023-10-30 14:20:15 -05:00
</div>
2023-11-24 07:39:55 -06:00
<div className="flex gap-1">
2023-10-30 14:20:15 -05:00
<div
2023-11-25 02:19:02 -06:00
onClick={() => handleDownload(ArchivedFormat.png)}
2023-10-31 04:39:05 -05:00
className="cursor-pointer hover:opacity-60 duration-100 p-2 rounded-md"
2023-10-30 14:20:15 -05:00
>
<FontAwesomeIcon
icon={faCloudArrowDown}
2023-11-25 04:39:56 -06:00
className="w-5 h-5 cursor-pointer text-neutral"
2023-10-30 14:20:15 -05:00
/>
</div>
<Link
2023-11-25 02:19:02 -06:00
href={`/api/v1/archives/${link?.id}?format=${
2023-12-13 05:59:36 -06:00
link?.screenshotPath?.endsWith("png")
2023-11-25 02:19:02 -06:00
? ArchivedFormat.png
: ArchivedFormat.jpeg
}`}
2023-10-30 14:20:15 -05:00
target="_blank"
2023-10-31 04:39:05 -05:00
className="cursor-pointer hover:opacity-60 duration-100 p-2 rounded-md"
2023-10-30 14:20:15 -05:00
>
<FontAwesomeIcon
icon={faArrowUpRightFromSquare}
2023-11-25 04:39:56 -06:00
className="w-5 h-5 text-neutral"
2023-10-30 14:20:15 -05:00
/>
</Link>
</div>
</div>
) : undefined}
2023-12-13 05:59:36 -06:00
{pdfAvailable(link) ? (
<div className="flex justify-between items-center pr-1 border border-neutral-content rounded-md">
2023-10-30 14:20:15 -05:00
<div className="flex gap-2 items-center">
2023-12-04 09:24:45 -06:00
<div className="bg-primary text-primary-content p-2 rounded-l-md">
2023-10-30 14:20:15 -05:00
<FontAwesomeIcon icon={faFilePdf} className="w-6 h-6" />
</div>
2023-11-24 07:39:55 -06:00
<p>PDF</p>
2023-10-30 14:20:15 -05:00
</div>
2023-11-24 07:39:55 -06:00
<div className="flex gap-1">
2023-10-30 14:20:15 -05:00
<div
2023-11-19 15:22:27 -06:00
onClick={() => handleDownload(ArchivedFormat.pdf)}
2023-10-31 04:39:05 -05:00
className="cursor-pointer hover:opacity-60 duration-100 p-2 rounded-md"
2023-10-30 14:20:15 -05:00
>
<FontAwesomeIcon
icon={faCloudArrowDown}
2023-11-25 04:39:56 -06:00
className="w-5 h-5 cursor-pointer text-neutral"
2023-10-30 14:20:15 -05:00
/>
</div>
<Link
2023-11-19 15:22:27 -06:00
href={`/api/v1/archives/${link?.id}?format=${ArchivedFormat.pdf}`}
2023-10-30 14:20:15 -05:00
target="_blank"
2023-10-31 04:39:05 -05:00
className="cursor-pointer hover:opacity-60 duration-100 p-2 rounded-md"
2023-10-30 14:20:15 -05:00
>
<FontAwesomeIcon
icon={faArrowUpRightFromSquare}
2023-11-25 04:39:56 -06:00
className="w-5 h-5 text-neutral"
2023-10-30 14:20:15 -05:00
/>
</Link>
</div>
</div>
) : undefined}
<div className="flex flex-col-reverse sm:flex-row gap-5 items-center justify-center">
{link?.collection.ownerId === session.data?.user.id ? (
<div
2023-12-07 11:29:45 -06:00
className={`btn btn-accent dark:border-violet-400 text-white ${
2023-12-13 05:59:36 -06:00
screenshotAvailable(link) && pdfAvailable(link) ? "mt-3" : ""
2023-10-31 14:44:58 -05:00
}`}
2023-10-30 14:20:15 -05:00
onClick={() => updateArchive()}
>
2023-12-04 09:24:45 -06:00
<div>
<p>Update Preserved Formats</p>
<p className="text-xs">(Refresh Link)</p>
</div>
2023-10-30 14:20:15 -05:00
</div>
) : undefined}
<Link
2023-12-04 09:24:45 -06:00
href={`https://web.archive.org/web/${link?.url?.replace(
2023-10-30 14:20:15 -05:00
/(^\w+:|^)\/\//,
""
)}`}
target="_blank"
2023-11-25 04:39:56 -06:00
className={`text-neutral duration-100 hover:opacity-60 flex gap-2 w-fit items-center text-sm ${
2023-12-13 05:59:36 -06:00
screenshotAvailable(link) && pdfAvailable(link) ? "sm:mt-3" : ""
2023-10-31 14:44:58 -05:00
}`}
2023-10-30 14:20:15 -05:00
>
<FontAwesomeIcon
icon={faArrowUpRightFromSquare}
className="w-4 h-4"
/>
<p className="whitespace-nowrap">
View Latest Snapshot on archive.org
</p>
</Link>
</div>
</div>
);
}