el.xwx.moe/components/PreserverdFormatRow.tsx

81 lines
2.1 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";
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) {
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 (
2024-08-18 01:55:59 -05:00
<div className="flex justify-between items-center rounded-lg p-2 bg-base-200">
2023-12-17 02:22:08 -06:00
<div className="flex gap-2 items-center">
2024-08-18 01:55:59 -05:00
<i className={`${icon} text-2xl text-primary`} />
2023-12-17 02:22:08 -06:00
<p>{name}</p>
</div>
<div className="flex gap-1">
{downloadable || false ? (
<div
onClick={() => handleDownload()}
2024-08-18 01:55:59 -05:00
className="btn btn-sm btn-square btn-ghost"
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
2024-08-18 13:06:36 -05:00
href={`${
isPublic ? "/public" : ""
}/preserved/${link?.id}?format=${format}`}
2023-12-17 02:22:08 -06:00
target="_blank"
2024-08-18 01:55:59 -05:00
className="btn btn-sm btn-square btn-ghost"
2023-12-17 02:22:08 -06:00
>
2024-08-18 01:55:59 -05:00
<i className="bi-box-arrow-up-right text-lg text-neutral" />
2023-12-17 02:22:08 -06:00
</Link>
</div>
</div>
);
2024-08-18 13:06:36 -05:00
}