el.xwx.moe/components/ModalContent/PreservedFormatsModal.tsx

252 lines
7.5 KiB
TypeScript
Raw Normal View History

2023-12-06 15:13:11 -06:00
import React, { useEffect, useState } from "react";
import useLinkStore from "@/store/links";
import {
LinkIncludingShortenedCollectionAndTags,
2024-06-09 08:27:16 -05:00
ArchivedFormat,
} from "@/types/global";
2023-12-06 15:13:11 -06:00
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,
2024-03-15 13:41:41 -05:00
singlefileAvailable,
screenshotAvailable,
} from "@/lib/shared/getArchiveValidity";
2023-12-17 02:22:08 -06:00
import PreservedFormatRow from "@/components/PreserverdFormatRow";
import useAccountStore from "@/store/account";
import getPublicUserData from "@/lib/client/getPublicUserData";
2024-06-09 08:27:16 -05:00
import { useTranslation } from "next-i18next";
2024-03-26 00:38:08 -05:00
import { BeatLoader } from "react-spinners";
2023-12-06 15:13:11 -06:00
type Props = {
onClose: Function;
activeLink: LinkIncludingShortenedCollectionAndTags;
};
export default function PreservedFormatsModal({ onClose, activeLink }: Props) {
2024-06-09 08:27:16 -05:00
const { t } = useTranslation();
2023-12-06 15:13:11 -06:00
const session = useSession();
2023-12-15 14:47:08 -06:00
const { getLink } = useLinkStore();
const { account } = useAccountStore();
2023-12-06 15:13:11 -06:00
const [link, setLink] =
useState<LinkIncludingShortenedCollectionAndTags>(activeLink);
const router = useRouter();
2023-12-19 16:20:09 -06:00
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,
2024-03-15 13:41:41 -05:00
archiveAsSinglefile: 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,
2024-03-15 13:41:41 -05:00
archiveAsSinglefile: account.archiveAsScreenshot as boolean,
archiveAsPDF: account.archiveAsPDF as boolean,
});
}
};
fetchOwner();
}, [link.collection.ownerId]);
const isReady = () => {
return (
link &&
2024-01-02 14:11:38 -06:00
(collectionOwner.archiveAsScreenshot === true
? link.pdf && link.pdf !== "pending"
: true) &&
2024-03-15 13:41:41 -05:00
(collectionOwner.archiveAsSinglefile === true
? link.singlefile && link.singlefile !== "pending"
: true) &&
2024-01-02 14:11:38 -06:00
(collectionOwner.archiveAsPDF === true
? link.pdf && link.pdf !== "pending"
: true) &&
link.readable &&
link.readable !== "pending"
);
};
2024-03-26 00:38:08 -05:00
const atLeastOneFormatAvailable = () => {
return (
screenshotAvailable(link) ||
pdfAvailable(link) ||
readabilityAvailable(link) ||
singlefileAvailable(link)
);
};
2023-12-06 15:13:11 -06:00
useEffect(() => {
(async () => {
2023-12-19 16:20:09 -06:00
const data = await getLink(link.id as number, isPublic);
2023-12-06 15:13:11 -06:00
setLink(
(data as any).response as LinkIncludingShortenedCollectionAndTags
);
})();
let interval: any;
if (!isReady()) {
2023-12-06 15:13:11 -06:00
interval = setInterval(async () => {
2023-12-19 16:20:09 -06:00
const data = await getLink(link.id as number, isPublic);
2023-12-06 15:13:11 -06:00
setLink(
(data as any).response as LinkIncludingShortenedCollectionAndTags
);
}, 5000);
} else {
if (interval) {
clearInterval(interval);
}
}
return () => {
if (interval) {
clearInterval(interval);
}
};
}, [link, getLink, link?.singlefile]);
2023-12-06 15:13:11 -06:00
const updateArchive = async () => {
2024-06-09 08:27:16 -05:00
const load = toast.loading(t("sending_request"));
2023-12-06 15:13:11 -06:00
const response = await fetch(`/api/v1/links/${link?.id}/archive`, {
method: "PUT",
});
const data = await response.json();
toast.dismiss(load);
if (response.ok) {
2023-12-19 16:20:09 -06:00
const newLink = await getLink(link?.id as number);
setLink(
(newLink as any).response as LinkIncludingShortenedCollectionAndTags
);
2024-06-09 08:27:16 -05:00
toast.success(t("link_being_archived"));
2023-12-06 15:13:11 -06:00
} else toast.error(data.response);
};
return (
<Modal toggleModal={onClose}>
2024-06-09 08:27:16 -05:00
<p className="text-xl font-thin">{t("preserved_formats")}</p>
2023-12-15 14:47:08 -06:00
<div className="divider mb-2 mt-1"></div>
2024-03-26 00:38:08 -05:00
{screenshotAvailable(link) ||
pdfAvailable(link) ||
readabilityAvailable(link) ||
singlefileAvailable(link) ? (
2024-06-09 08:27:16 -05:00
<p className="mb-3">{t("available_formats")}</p>
2023-12-15 14:47:08 -06:00
) : (
""
2023-12-15 14:47:08 -06:00
)}
2023-12-06 15:13:11 -06:00
<div className={`flex flex-col gap-3`}>
2024-03-26 00:38:08 -05:00
{screenshotAvailable(link) ? (
<PreservedFormatRow
name={t("screenshot")}
2024-03-26 00:38:08 -05:00
icon={"bi-file-earmark-image"}
format={
link?.image?.endsWith("png")
? ArchivedFormat.png
: ArchivedFormat.jpeg
}
activeLink={link}
downloadable={true}
/>
) : undefined}
{pdfAvailable(link) ? (
<PreservedFormatRow
name={t("pdf")}
2024-03-26 00:38:08 -05:00
icon={"bi-file-earmark-pdf"}
format={ArchivedFormat.pdf}
activeLink={link}
downloadable={true}
/>
) : undefined}
{readabilityAvailable(link) ? (
<PreservedFormatRow
name={t("readable")}
2024-03-26 00:38:08 -05:00
icon={"bi-file-earmark-text"}
format={ArchivedFormat.readability}
activeLink={link}
/>
) : undefined}
{!isReady() && !atLeastOneFormatAvailable() ? (
<div className={`w-full h-full flex flex-col justify-center p-10`}>
<BeatLoader
color="oklch(var(--p))"
className="mx-auto mb-3"
size={30}
/>
2024-06-09 08:27:16 -05:00
<p className="text-center text-2xl">{t("preservation_in_queue")}</p>
<p className="text-center text-lg">{t("check_back_later")}</p>
</div>
2024-03-26 00:38:08 -05:00
) : !isReady() && atLeastOneFormatAvailable() ? (
<div className={`w-full h-full flex flex-col justify-center p-5`}>
<BeatLoader
color="oklch(var(--p))"
className="mx-auto mb-3"
size={20}
/>
<p className="text-center">
There are more preserved formats in the queue
</p>
<p className="text-center text-sm">{t("check_back_later")}</p>
2024-03-26 00:38:08 -05:00
</div>
) : undefined}
<div
className={`flex flex-col sm:flex-row gap-3 items-center justify-center ${
isReady() ? "sm:mt " : ""
}`}
>
2023-12-06 15:13:11 -06:00
<Link
href={`https://web.archive.org/web/${link?.url?.replace(
/(^\w+:|^)\/\//,
""
)}`}
target="_blank"
2024-06-09 08:27:16 -05:00
className="text-neutral duration-100 hover:opacity-60 flex gap-2 w-1/2 justify-center items-center text-sm"
2023-12-06 15:13:11 -06:00
>
2024-06-09 08:27:16 -05:00
<p className="whitespace-nowrap">{t("view_latest_snapshot")}</p>
<i className="bi-box-arrow-up-right" />
2023-12-06 15:13:11 -06:00
</Link>
2024-06-09 08:27:16 -05:00
{link?.collection.ownerId === session.data?.user.id && (
<div className="btn btn-outline" onClick={updateArchive}>
<div>
2024-06-09 08:27:16 -05:00
<p>{t("refresh_preserved_formats")}</p>
<p className="text-xs">
2024-06-09 08:27:16 -05:00
{t("this_deletes_current_preservations")}
</p>
</div>
</div>
2024-06-09 08:27:16 -05:00
)}
2023-12-06 15:13:11 -06:00
</div>
</div>
</Modal>
);
}