import { LoaderCircle } from "lucide-react"; import { DownloadCloudIcon } from "../icons/DownloadCloudIcon"; 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"; import { useToast } from "../Toast"; type AttachmentDownloadPillProps = { postId: string; attachment: Attachment; leadingLabel?: string; className?: string; /** * 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; }; export function AttachmentDownloadPill({ postId, attachment, leadingLabel, className = "absolute left-2 top-2", adaptive = false, }: AttachmentDownloadPillProps) { const { t } = useI18n(); const { showToast } = useToast(); const [isDownloading, setIsDownloading] = useState(false); const handleDownload = async (e: MouseEvent) => { e.stopPropagation(); if (isDownloading) return; setIsDownloading(true); try { await downloadAttachment(postId, attachment.id, attachment.filename); } catch { showToast(t("downloadFail"), "error"); } finally { setIsDownloading(false); } }; return ( ); }