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-30 15:42:13 +08:00
|
|
|
import { filenameWithExtension, splitFilename } from "../utils/filenameDisplay";
|
2026-05-25 05:25:57 +08:00
|
|
|
import { formatBytes } from "../utils/formatBytes";
|
2026-06-03 08:18:05 +08:00
|
|
|
import { formatDateTime } from "../utils/formatTime";
|
2026-05-26 12:07:13 +08:00
|
|
|
import { postDisplayText } from "../utils/postText";
|
2026-05-29 23:49:59 +08:00
|
|
|
import { CollapsibleText } from "../CollapsibleText";
|
2026-06-01 23:00:28 +08:00
|
|
|
import {
|
|
|
|
|
mediaSaveKindFromAttachment,
|
|
|
|
|
useSaveToAlbumGuide,
|
|
|
|
|
} from "../../SaveToAlbumGuide";
|
2026-05-29 11:50:27 +08:00
|
|
|
import { useToast } from "../../Toast";
|
2026-06-03 08:18:05 +08:00
|
|
|
import { FavoriteButton } from "../../../favorites/FavoriteButton";
|
|
|
|
|
import type { MessageBubbleVariant } from "../MessageBubble";
|
2026-05-25 05:25:57 +08:00
|
|
|
|
2026-06-03 21:20:53 +08:00
|
|
|
function AttachmentRow({ att }: { postId: string; att: Attachment }) {
|
2026-05-25 05:25:57 +08:00
|
|
|
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-29 13:08:16 +08:00
|
|
|
const [previewFailed, setPreviewFailed] = useState(false);
|
2026-05-26 18:04:36 +08:00
|
|
|
|
2026-05-29 12:38:50 +08:00
|
|
|
const isImage = att.kind === "image" || att.mime.startsWith("image/");
|
|
|
|
|
const previewUrl =
|
|
|
|
|
att.thumbnailUrl ?? att.posterUrl ?? (isImage ? att.url : undefined);
|
|
|
|
|
|
2026-05-25 05:25:57 +08:00
|
|
|
return (
|
2026-05-30 00:43:27 +08:00
|
|
|
<div className="group flex min-h-[64px] items-center gap-3">
|
2026-05-29 13:08:16 +08:00
|
|
|
{previewUrl && !previewFailed ? (
|
2026-05-29 12:38:50 +08:00
|
|
|
<img
|
|
|
|
|
src={previewUrl}
|
2026-05-29 13:08:16 +08:00
|
|
|
alt=""
|
2026-05-29 12:38:50 +08:00
|
|
|
loading="lazy"
|
2026-05-30 01:54:28 +08:00
|
|
|
decoding="async"
|
2026-05-29 13:08:16 +08:00
|
|
|
onError={() => setPreviewFailed(true)}
|
2026-05-30 02:51:09 +08:00
|
|
|
className="h-16 w-16 shrink-0 rounded-lg object-fill"
|
2026-05-29 12:38:50 +08:00
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<div
|
2026-05-30 00:43:27 +08:00
|
|
|
className="flex h-16 w-16 shrink-0 items-center justify-center rounded-lg"
|
2026-05-29 12:38:50 +08:00
|
|
|
style={{ backgroundColor: color }}
|
|
|
|
|
aria-hidden="true"
|
|
|
|
|
>
|
2026-05-30 00:43:27 +08:00
|
|
|
<Icon className="h-9 w-9 text-white" strokeWidth={2.1} />
|
2026-05-29 12:38:50 +08:00
|
|
|
</div>
|
|
|
|
|
)}
|
2026-05-28 15:11:13 +08:00
|
|
|
<div className="min-w-0 flex-1">
|
|
|
|
|
<div
|
2026-05-30 15:42:13 +08:00
|
|
|
className="flex min-w-0 items-baseline text-[15px] font-medium leading-6 text-ark-gold group-hover:text-ark-gold2"
|
2026-05-28 15:11:13 +08:00
|
|
|
title={displayFilename}
|
|
|
|
|
>
|
2026-05-30 15:42:13 +08:00
|
|
|
{(() => {
|
|
|
|
|
const { base, ext } = splitFilename(displayFilename);
|
|
|
|
|
const tailChars = Math.min(4, base.length);
|
|
|
|
|
const head = base.slice(0, base.length - tailChars);
|
|
|
|
|
const tail = base.slice(base.length - tailChars) + ext;
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<span className="min-w-0 truncate">{head}</span>
|
|
|
|
|
<span className="shrink-0 whitespace-pre">{tail}</span>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
})()}
|
2026-05-28 15:11:13 +08:00
|
|
|
</div>
|
|
|
|
|
<div className="text-[12px] font-medium leading-[19.2px] text-[#A8A9AE]">
|
2026-06-03 21:20:53 +08:00
|
|
|
{formatBytes(att.sizeBytes)}
|
2026-05-28 15:11:13 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-05-26 18:04:36 +08:00
|
|
|
</div>
|
2026-05-25 05:25:57 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-03 08:18:05 +08:00
|
|
|
function LatestFileCard({ post }: { post: Post }) {
|
|
|
|
|
const { t, lang } = useI18n();
|
|
|
|
|
const { showToast } = useToast();
|
|
|
|
|
const { showSaveToAlbumGuide } = useSaveToAlbumGuide();
|
|
|
|
|
const [isDownloading, setIsDownloading] = useState(false);
|
|
|
|
|
const att = post.attachments[0];
|
|
|
|
|
const text = postDisplayText(post, lang);
|
|
|
|
|
|
|
|
|
|
if (!att) return null;
|
|
|
|
|
|
|
|
|
|
const { Icon, color } = fileIcon({ mime: att.mime, filename: att.filename });
|
|
|
|
|
const displayFilename = filenameWithExtension(att.filename, att.mime);
|
|
|
|
|
|
|
|
|
|
const handleDownload = async () => {
|
|
|
|
|
if (isDownloading) return;
|
|
|
|
|
setIsDownloading(true);
|
|
|
|
|
try {
|
|
|
|
|
await downloadAttachment(post.id, att.id, displayFilename);
|
|
|
|
|
const mediaKind = mediaSaveKindFromAttachment(att);
|
|
|
|
|
if (mediaKind) showSaveToAlbumGuide(mediaKind);
|
|
|
|
|
} catch {
|
|
|
|
|
showToast(t("downloadFail"), "error");
|
|
|
|
|
} finally {
|
|
|
|
|
setIsDownloading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex h-[188px] flex-col gap-6 px-4 py-4">
|
|
|
|
|
<div className="flex h-[52px] min-w-0 items-center gap-4">
|
|
|
|
|
<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="flex min-w-0 items-baseline text-[15px] font-medium leading-6 text-ark-gold"
|
|
|
|
|
title={displayFilename}
|
|
|
|
|
>
|
|
|
|
|
{(() => {
|
|
|
|
|
const { base, ext } = splitFilename(displayFilename);
|
|
|
|
|
const tailChars = Math.min(8, base.length);
|
|
|
|
|
const head = base.slice(0, base.length - tailChars);
|
|
|
|
|
const tail = base.slice(base.length - tailChars) + ext;
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<span className="min-w-0 truncate">{head}</span>
|
|
|
|
|
<span className="shrink-0 whitespace-pre">{tail}</span>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
})()}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="text-[12px] font-medium leading-[19.2px] text-[#A8A9AE]">
|
|
|
|
|
{isDownloading ? t("downloading") : formatBytes(att.sizeBytes)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{text ? (
|
|
|
|
|
<div className="message-stream-copyable-text line-clamp-2 min-h-[48px] select-text whitespace-pre-wrap break-words text-[15px] font-medium leading-6 text-white">
|
|
|
|
|
{text}
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="min-h-[48px]" />
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div className="mt-auto flex h-10 items-end justify-between gap-3">
|
|
|
|
|
<time
|
|
|
|
|
dateTime={post.publishedAt}
|
|
|
|
|
className="text-[12px] font-medium leading-[19.2px] text-[#A8A9AE]"
|
|
|
|
|
>
|
|
|
|
|
{formatDateTime(post.publishedAt)}
|
|
|
|
|
</time>
|
|
|
|
|
<div className="flex shrink-0 items-center gap-2">
|
|
|
|
|
<FavoriteButton resourceId={post.id} size="sm" />
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={handleDownload}
|
|
|
|
|
disabled={isDownloading}
|
|
|
|
|
className="flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-[#191921] transition hover:bg-[#22232D] disabled:cursor-wait"
|
|
|
|
|
aria-label={
|
|
|
|
|
isDownloading ? t("downloading") : `Download ${att.filename}`
|
|
|
|
|
}
|
|
|
|
|
aria-busy={isDownloading}
|
|
|
|
|
>
|
|
|
|
|
{isDownloading ? (
|
|
|
|
|
<LoaderCircle
|
|
|
|
|
className="h-5 w-5 animate-spin text-[#A8A9AE]"
|
|
|
|
|
strokeWidth={2.3}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<DownloadCloudIcon />
|
|
|
|
|
)}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function FileDocBubble({
|
|
|
|
|
post,
|
|
|
|
|
variant = "default",
|
|
|
|
|
}: {
|
|
|
|
|
post: Post;
|
|
|
|
|
variant?: MessageBubbleVariant;
|
|
|
|
|
}) {
|
2026-05-26 12:07:13 +08:00
|
|
|
const { lang } = useI18n();
|
|
|
|
|
const text = postDisplayText(post, lang);
|
2026-06-03 08:18:05 +08:00
|
|
|
|
|
|
|
|
if (variant === "latest") {
|
|
|
|
|
return <LatestFileCard post={post} />;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 05:25:57 +08:00
|
|
|
return (
|
2026-05-28 15:11:13 +08:00
|
|
|
<div className="flex flex-col gap-3">
|
2026-05-25 05:25:57 +08:00
|
|
|
{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-29 23:49:59 +08:00
|
|
|
<CollapsibleText 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}
|
2026-05-29 23:49:59 +08:00
|
|
|
</CollapsibleText>
|
2026-05-25 05:25:57 +08:00
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|