import { LoaderCircle } from "lucide-react"; import { DownloadCloudIcon } from "../../icons/DownloadCloudIcon"; import { useState } from "react"; import { useI18n } from "../../../i18n"; import type { Attachment, Post } from "../../../types/post"; import { downloadAttachment } from "../utils/downloadFile"; import { fileIcon } from "../utils/fileIcon"; import { filenameWithExtension, middleEllipsisFilename, } from "../utils/filenameDisplay"; import { formatBytes } from "../utils/formatBytes"; import { postDisplayText } from "../utils/postText"; import { CollapsibleText } from "../CollapsibleText"; import { useToast } from "../../Toast"; function AttachmentRow({ postId, att }: { postId: string; att: Attachment }) { const { t } = useI18n(); const { showToast } = useToast(); const { Icon, color } = fileIcon({ mime: att.mime, filename: att.filename }); const displayFilename = filenameWithExtension(att.filename, att.mime); const [isDownloading, setIsDownloading] = useState(false); const [previewFailed, setPreviewFailed] = useState(false); const handleDownload = async () => { if (isDownloading) return; setIsDownloading(true); try { await downloadAttachment(postId, att.id, displayFilename); } catch { showToast(t("downloadFail"), "error"); } finally { setIsDownloading(false); } }; const isImage = att.kind === "image" || att.mime.startsWith("image/"); const previewUrl = att.thumbnailUrl ?? att.posterUrl ?? (isImage ? att.url : undefined); return (
{previewUrl && !previewFailed ? ( setPreviewFailed(true)} className="h-16 w-16 shrink-0 rounded-lg object-fill" /> ) : ( )}
{middleEllipsisFilename(displayFilename)}
{isDownloading ? t("downloading") : formatBytes(att.sizeBytes)}
); } export function FileDocBubble({ post }: { post: Post }) { const { lang } = useI18n(); const text = postDisplayText(post, lang); return (
{post.attachments.map((att) => ( ))} {text ? ( {text} ) : null}
); }