el.xwx.moe/lib/client/generateLinkHref.ts

29 lines
1.2 KiB
TypeScript
Raw Normal View History

import useAccountStore from "@/store/account";
import { ArchivedFormat, LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import { LinksRouteTo } from "@prisma/client";
2024-02-08 00:59:17 -06:00
import { pdfAvailable, readabilityAvailable, screenshotAvailable } from "../shared/getArchiveValidity";
2024-02-08 00:44:41 -06:00
export const generateLinkHref = (link: LinkIncludingShortenedCollectionAndTags): string => {
const { account } = useAccountStore();
2024-02-08 00:59:17 -06:00
// Return the links href based on the account's preference
// If the user's preference is not available, return the original link
switch (account.linksRouteTo) {
case LinksRouteTo.ORIGINAL:
return link.url || '';
case LinksRouteTo.PDF:
2024-02-08 00:59:17 -06:00
if (!pdfAvailable(link)) return link.url || '';
return `/preserved/${link?.id}?format=${ArchivedFormat.pdf}`;
case LinksRouteTo.READABLE:
2024-02-08 00:59:17 -06:00
if (!readabilityAvailable(link)) return link.url || '';
return `/preserved/${link?.id}?format=${ArchivedFormat.readability}`;
case LinksRouteTo.SCREENSHOT:
2024-02-08 00:59:17 -06:00
if (!screenshotAvailable(link)) return link.url || '';
return `/preserved/${link?.id}?format=${link?.image?.endsWith("png") ? ArchivedFormat.png : ArchivedFormat.jpeg}`;
default:
return link.url || '';
}
};