el.xwx.moe/components/PreserverdFormatRow.tsx

86 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-12-17 02:22:08 -06:00
import {
ArchivedFormat,
LinkIncludingShortenedCollectionAndTags,
} from "@/types/global";
import Link from "next/link";
import { useRouter } from "next/router";
import { useGetLink } from "@/hooks/store/links";
2023-12-17 02:22:08 -06:00
type Props = {
name: string;
icon: string;
format: ArchivedFormat;
2024-08-13 02:01:02 -05:00
link: LinkIncludingShortenedCollectionAndTags;
downloadable?: boolean;
2023-12-17 02:22:08 -06:00
};
export default function PreservedFormatRow({
name,
icon,
format,
2024-08-13 02:01:02 -05:00
link,
downloadable,
}: Props) {
const getLink = useGetLink();
2023-12-17 02:22:08 -06:00
const router = useRouter();
2023-12-19 16:20:09 -06:00
let isPublic = router.pathname.startsWith("/public") ? true : undefined;
2023-12-17 02:22:08 -06:00
const handleDownload = () => {
const path = `/api/v1/archives/${link?.id}?format=${format}`;
fetch(path)
.then((response) => {
if (response.ok) {
// Create a temporary link and click it to trigger the download
2024-03-15 13:41:41 -05:00
const anchorElement = document.createElement("a");
anchorElement.href = path;
anchorElement.download =
2024-06-27 20:58:07 -05:00
format === ArchivedFormat.monolith
2024-06-27 11:39:03 -05:00
? "Webpage"
: format === ArchivedFormat.pdf
? "PDF"
: "Screenshot";
2024-03-15 13:41:41 -05:00
anchorElement.click();
2023-12-17 02:22:08 -06:00
} else {
console.error("Failed to download file");
}
})
.catch((error) => {
console.error("Error:", error);
});
};
return (
<div className="flex justify-between items-center pr-1 border border-neutral-content rounded-md">
<div className="flex gap-2 items-center">
<div className="bg-primary text-primary-content p-2 rounded-l-md">
<i className={`${icon} text-2xl`} />
2023-12-17 02:22:08 -06:00
</div>
<p>{name}</p>
</div>
<div className="flex gap-1">
{downloadable || false ? (
<div
onClick={() => handleDownload()}
className="btn btn-sm btn-square"
2023-12-17 02:22:08 -06:00
>
<i className="bi-cloud-arrow-down text-xl text-neutral" />
2023-12-17 02:22:08 -06:00
</div>
) : undefined}
<Link
2023-12-29 11:21:22 -06:00
href={`${
isPublic ? "/public" : ""
}/preserved/${link?.id}?format=${format}`}
2023-12-17 02:22:08 -06:00
target="_blank"
className="btn btn-sm btn-square"
2023-12-17 02:22:08 -06:00
>
<i className="bi-box-arrow-up-right text-xl text-neutral" />
2023-12-17 02:22:08 -06:00
</Link>
</div>
</div>
);
}