2026-05-28 10:36:38 +08:00
|
|
|
import { LoaderCircle } from "lucide-react";
|
|
|
|
|
import { DownloadCloudIcon } from "../icons/DownloadCloudIcon";
|
2026-05-27 12:33:26 +08:00
|
|
|
import { useState, type MouseEvent } from "react";
|
|
|
|
|
import { useI18n } from "../../i18n";
|
|
|
|
|
import type { Attachment } from "../../types/post";
|
|
|
|
|
import { downloadAttachment } from "./utils/downloadFile";
|
|
|
|
|
import { formatBytes } from "./utils/formatBytes";
|
2026-05-29 11:50:27 +08:00
|
|
|
import { useToast } from "../Toast";
|
2026-05-27 12:33:26 +08:00
|
|
|
|
|
|
|
|
type AttachmentDownloadPillProps = {
|
|
|
|
|
postId: string;
|
|
|
|
|
attachment: Attachment;
|
|
|
|
|
leadingLabel?: string;
|
|
|
|
|
className?: string;
|
2026-05-30 01:27:02 +08:00
|
|
|
/**
|
|
|
|
|
* When true, the pill scales with its host container's width via container-
|
|
|
|
|
* query units (clamped 22-30px). The host element must establish a query
|
|
|
|
|
* container with `style={{ containerType: "inline-size" }}`. Use this on
|
|
|
|
|
* album tiles so the pill shrinks on small thumbnails in mixed layouts.
|
|
|
|
|
* Defaults to false (fixed 30px) for standalone images/videos.
|
|
|
|
|
*/
|
|
|
|
|
adaptive?: boolean;
|
2026-05-27 12:33:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function AttachmentDownloadPill({
|
|
|
|
|
postId,
|
|
|
|
|
attachment,
|
|
|
|
|
leadingLabel,
|
|
|
|
|
className = "absolute left-2 top-2",
|
2026-05-30 01:27:02 +08:00
|
|
|
adaptive = false,
|
2026-05-27 12:33:26 +08:00
|
|
|
}: AttachmentDownloadPillProps) {
|
|
|
|
|
const { t } = useI18n();
|
2026-05-29 11:50:27 +08:00
|
|
|
const { showToast } = useToast();
|
2026-05-27 12:33:26 +08:00
|
|
|
const [isDownloading, setIsDownloading] = useState(false);
|
|
|
|
|
|
2026-05-29 11:50:27 +08:00
|
|
|
const handleDownload = async (e: MouseEvent<HTMLButtonElement>) => {
|
2026-05-27 12:33:26 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
|
if (isDownloading) return;
|
|
|
|
|
setIsDownloading(true);
|
2026-05-29 11:50:27 +08:00
|
|
|
try {
|
|
|
|
|
await downloadAttachment(postId, attachment.id, attachment.filename);
|
|
|
|
|
} catch {
|
|
|
|
|
showToast(t("downloadFail"), "error");
|
|
|
|
|
} finally {
|
|
|
|
|
setIsDownloading(false);
|
|
|
|
|
}
|
2026-05-27 12:33:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={handleDownload}
|
|
|
|
|
disabled={isDownloading}
|
2026-05-30 01:27:02 +08:00
|
|
|
className={`group z-10 inline-flex overflow-hidden rounded-full bg-black/80 ${
|
|
|
|
|
adaptive ? "text-[clamp(10px,7cqw,12px)]" : "text-[12px]"
|
|
|
|
|
} text-white shadow-lg ring-1 ring-inset ring-white/20 backdrop-blur-md transition hover:bg-black/90 disabled:cursor-wait ${className}`}
|
2026-05-27 12:33:26 +08:00
|
|
|
aria-label={
|
|
|
|
|
isDownloading ? t("downloading") : `Download ${attachment.filename}`
|
|
|
|
|
}
|
|
|
|
|
aria-busy={isDownloading}
|
|
|
|
|
>
|
2026-05-30 01:27:02 +08:00
|
|
|
<span
|
|
|
|
|
className={`flex items-center justify-center bg-[#545454]/50 transition group-hover:bg-[#545454]/70 ${
|
|
|
|
|
adaptive
|
|
|
|
|
? "h-[clamp(22px,18cqw,30px)] w-[clamp(22px,18cqw,30px)]"
|
|
|
|
|
: "h-[30px] w-[30px]"
|
|
|
|
|
}`}
|
|
|
|
|
>
|
2026-05-27 12:33:26 +08:00
|
|
|
{isDownloading ? (
|
2026-05-28 10:36:38 +08:00
|
|
|
<LoaderCircle
|
2026-05-30 01:27:02 +08:00
|
|
|
className={`${
|
|
|
|
|
adaptive
|
|
|
|
|
? "h-[clamp(13px,11cqw,18px)] w-[clamp(13px,11cqw,18px)]"
|
|
|
|
|
: "h-[18px] w-[18px]"
|
|
|
|
|
} animate-spin`}
|
2026-05-28 10:36:38 +08:00
|
|
|
strokeWidth={2.3}
|
|
|
|
|
/>
|
2026-05-27 12:33:26 +08:00
|
|
|
) : (
|
2026-05-30 01:27:02 +08:00
|
|
|
<DownloadCloudIcon
|
|
|
|
|
className={
|
|
|
|
|
adaptive
|
|
|
|
|
? "h-[clamp(13px,11cqw,18px)] w-[clamp(13px,11cqw,18px)]"
|
|
|
|
|
: "h-[18px] w-[18px]"
|
|
|
|
|
}
|
|
|
|
|
/>
|
2026-05-27 12:33:26 +08:00
|
|
|
)}
|
|
|
|
|
</span>
|
2026-05-30 01:27:02 +08:00
|
|
|
<span
|
|
|
|
|
className={`flex items-center gap-0.5 ${
|
|
|
|
|
adaptive
|
|
|
|
|
? "h-[clamp(22px,18cqw,30px)] px-[clamp(6px,6cqw,10px)]"
|
|
|
|
|
: "h-[30px] px-2.5"
|
|
|
|
|
}`}
|
|
|
|
|
>
|
2026-05-27 12:33:26 +08:00
|
|
|
{isDownloading ? (
|
|
|
|
|
t("downloading")
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{leadingLabel ? (
|
|
|
|
|
<>
|
|
|
|
|
<span>{leadingLabel}</span>
|
|
|
|
|
<span className="opacity-60">·</span>
|
|
|
|
|
</>
|
|
|
|
|
) : null}
|
|
|
|
|
<span>{formatBytes(attachment.sizeBytes)}</span>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</span>
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
}
|