2026-05-28 10:36:38 +08:00
|
|
|
import { LoaderCircle } from "lucide-react";
|
|
|
|
|
import { DownloadCloudIcon } from "../../icons/DownloadCloudIcon";
|
2026-05-27 12:23:06 +08:00
|
|
|
import { useState } from "react";
|
2026-05-26 12:07:13 +08:00
|
|
|
import { useI18n } from "../../../i18n";
|
2026-05-25 05:25:57 +08:00
|
|
|
import type { Attachment, Post } from "../../../types/post";
|
2026-05-27 12:17:47 +08:00
|
|
|
import { downloadAttachment } from "../utils/downloadFile";
|
2026-05-25 05:25:57 +08:00
|
|
|
import { fileIcon } from "../utils/fileIcon";
|
2026-05-26 18:04:36 +08:00
|
|
|
import {
|
|
|
|
|
filenameWithExtension,
|
|
|
|
|
middleEllipsisFilename,
|
|
|
|
|
} from "../utils/filenameDisplay";
|
2026-05-25 05:25:57 +08:00
|
|
|
import { formatBytes } from "../utils/formatBytes";
|
2026-05-26 12:07:13 +08:00
|
|
|
import { postDisplayText } from "../utils/postText";
|
2026-05-25 05:25:57 +08:00
|
|
|
|
2026-05-26 12:07:13 +08:00
|
|
|
function AttachmentRow({ postId, att }: { postId: string; att: Attachment }) {
|
2026-05-27 12:23:06 +08:00
|
|
|
const { t } = useI18n();
|
2026-05-25 05:25:57 +08:00
|
|
|
const isImageAsDoc = att.mime.startsWith("image/");
|
|
|
|
|
const { Icon, color } = fileIcon({ mime: att.mime, filename: att.filename });
|
2026-05-26 18:04:36 +08:00
|
|
|
const displayFilename = filenameWithExtension(att.filename, att.mime);
|
2026-05-27 12:23:06 +08:00
|
|
|
const [isDownloading, setIsDownloading] = useState(false);
|
2026-05-26 18:04:36 +08:00
|
|
|
|
|
|
|
|
const handleDownload = () => {
|
2026-05-27 12:23:06 +08:00
|
|
|
if (isDownloading) return;
|
|
|
|
|
setIsDownloading(true);
|
|
|
|
|
void downloadAttachment(postId, att.id, displayFilename)
|
|
|
|
|
.finally(() => setIsDownloading(false))
|
|
|
|
|
.catch(() => {});
|
2026-05-26 18:04:36 +08:00
|
|
|
};
|
2026-05-25 05:25:57 +08:00
|
|
|
|
|
|
|
|
return (
|
2026-05-26 18:04:36 +08:00
|
|
|
<div className="group flex items-center gap-2 rounded-xl px-1 py-0.5 transition hover:bg-white/5">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={handleDownload}
|
2026-05-27 12:23:06 +08:00
|
|
|
disabled={isDownloading}
|
|
|
|
|
className="relative h-11 w-11 shrink-0 overflow-hidden rounded-full disabled:cursor-wait md:h-12 md:w-12"
|
|
|
|
|
aria-label={
|
|
|
|
|
isDownloading ? t("downloading") : `Download ${att.filename}`
|
|
|
|
|
}
|
|
|
|
|
aria-busy={isDownloading}
|
2026-05-26 18:04:36 +08:00
|
|
|
>
|
2026-05-25 05:25:57 +08:00
|
|
|
{isImageAsDoc && att.thumbnailUrl ? (
|
2026-05-26 18:04:36 +08:00
|
|
|
<img
|
|
|
|
|
src={att.thumbnailUrl}
|
|
|
|
|
alt=""
|
|
|
|
|
className="absolute inset-0 h-full w-full object-cover"
|
|
|
|
|
/>
|
2026-05-25 05:25:57 +08:00
|
|
|
) : (
|
|
|
|
|
<div
|
|
|
|
|
className="flex h-full w-full items-center justify-center"
|
|
|
|
|
style={{ backgroundColor: color }}
|
|
|
|
|
>
|
|
|
|
|
<Icon className="h-5 w-5 text-white" strokeWidth={2.2} />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-05-26 18:04:36 +08:00
|
|
|
<div className="absolute inset-0 flex items-center justify-center bg-black/35 text-white opacity-100 transition group-hover:bg-black/45 group-focus-visible:bg-black/45">
|
2026-05-27 12:23:06 +08:00
|
|
|
{isDownloading ? (
|
|
|
|
|
<LoaderCircle className="h-4 w-4 animate-spin" strokeWidth={2.3} />
|
|
|
|
|
) : (
|
2026-05-28 10:36:38 +08:00
|
|
|
<DownloadCloudIcon className="h-4 w-4" />
|
2026-05-27 12:23:06 +08:00
|
|
|
)}
|
2026-05-26 18:04:36 +08:00
|
|
|
</div>
|
|
|
|
|
</button>
|
2026-05-25 05:25:57 +08:00
|
|
|
<div className="min-w-0 flex-1">
|
2026-05-26 18:04:36 +08:00
|
|
|
<div
|
|
|
|
|
className="truncate text-[14px] font-medium text-ark-gold2 group-hover:text-ark-gold"
|
|
|
|
|
title={displayFilename}
|
|
|
|
|
>
|
|
|
|
|
{middleEllipsisFilename(displayFilename)}
|
2026-05-25 05:25:57 +08:00
|
|
|
</div>
|
|
|
|
|
<div className="text-[11px] text-neutral-400">
|
2026-05-27 12:23:06 +08:00
|
|
|
{isDownloading ? t("downloading") : formatBytes(att.sizeBytes)}
|
2026-05-25 05:25:57 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-05-26 18:04:36 +08:00
|
|
|
</div>
|
2026-05-25 05:25:57 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function FileDocBubble({ post }: { post: Post }) {
|
2026-05-26 12:07:13 +08:00
|
|
|
const { lang } = useI18n();
|
|
|
|
|
const text = postDisplayText(post, lang);
|
2026-05-25 05:25:57 +08:00
|
|
|
return (
|
|
|
|
|
<div className="flex flex-col gap-1">
|
|
|
|
|
{post.attachments.map((att) => (
|
2026-05-26 12:07:13 +08:00
|
|
|
<AttachmentRow key={att.id} postId={post.id} att={att} />
|
2026-05-25 05:25:57 +08:00
|
|
|
))}
|
2026-05-26 12:07:13 +08:00
|
|
|
{text ? (
|
2026-05-26 18:03:07 +08:00
|
|
|
<div className="message-stream-copyable-text mt-1 select-text whitespace-pre-wrap break-words text-[14px] leading-snug text-neutral-100">
|
2026-05-26 12:07:13 +08:00
|
|
|
{text}
|
2026-05-25 05:25:57 +08:00
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|