Files
Arkie-Library-Frontend/src/components/messageStream/bubbles/FileDocBubble.tsx

105 lines
3.5 KiB
TypeScript
Raw Normal View History

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";
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";
2026-05-26 12:07:13 +08:00
import { postDisplayText } from "../utils/postText";
import { useToast } from "../../Toast";
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();
const { showToast } = useToast();
const { Icon, color } = fileIcon({ mime: att.mime, filename: att.filename });
const displayFilename = filenameWithExtension(att.filename, att.mime);
2026-05-27 12:23:06 +08:00
const [isDownloading, setIsDownloading] = useState(false);
const handleDownload = async () => {
2026-05-27 12:23:06 +08:00
if (isDownloading) return;
setIsDownloading(true);
try {
await downloadAttachment(postId, att.id, displayFilename);
showToast(t("downloadOk"));
} 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 (
<div className="group flex h-[52px] items-center gap-3">
{previewUrl ? (
<img
src={previewUrl}
alt={displayFilename}
loading="lazy"
className="h-[52px] w-[52px] shrink-0 rounded-full object-cover"
/>
) : (
<div
className="flex h-[52px] w-[52px] shrink-0 items-center justify-center rounded-full"
style={{ backgroundColor: color }}
aria-hidden="true"
>
<Icon className="h-8 w-8 text-white" strokeWidth={2.1} />
</div>
)}
<div className="min-w-0 flex-1">
<div
className="truncate text-[15px] font-medium leading-6 text-ark-gold group-hover:text-ark-gold2"
title={displayFilename}
>
{middleEllipsisFilename(displayFilename)}
</div>
<div className="text-[12px] font-medium leading-[19.2px] text-[#A8A9AE]">
{isDownloading ? t("downloading") : formatBytes(att.sizeBytes)}
</div>
</div>
<button
type="button"
onClick={handleDownload}
2026-05-27 12:23:06 +08:00
disabled={isDownloading}
className="flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-[#191921] text-white transition hover:bg-[#22232D] disabled:cursor-wait"
2026-05-27 12:23:06 +08:00
aria-label={
isDownloading ? t("downloading") : `Download ${att.filename}`
}
aria-busy={isDownloading}
>
{isDownloading ? (
<LoaderCircle className="h-5 w-5 animate-spin" strokeWidth={2.3} />
) : (
<DownloadCloudIcon className="h-6 w-6" />
)}
</button>
</div>
);
}
export function FileDocBubble({ post }: { post: Post }) {
2026-05-26 12:07:13 +08:00
const { lang } = useI18n();
const text = postDisplayText(post, lang);
return (
<div className="flex flex-col gap-3">
{post.attachments.map((att) => (
2026-05-26 12:07:13 +08:00
<AttachmentRow key={att.id} postId={post.id} att={att} />
))}
2026-05-26 12:07:13 +08:00
{text ? (
<div className="message-stream-copyable-text select-text whitespace-pre-wrap break-words text-[15px] font-medium leading-6 text-neutral-100">
2026-05-26 12:07:13 +08:00
{text}
</div>
) : null}
</div>
);
}