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

75 lines
2.8 KiB
TypeScript
Raw Normal View History

2026-05-27 12:23:06 +08:00
import { ArrowDownToLine, LoaderCircle } from "lucide-react";
import { useState } from "react";
2026-05-26 12:07:13 +08:00
import { useI18n } from "../../../i18n";
import type { Post } from "../../../types/post";
import { useLightbox } from "../overlays/ImageLightbox";
import { autolink } from "../utils/autolink";
2026-05-27 12:23:06 +08:00
import { downloadAttachment } from "../utils/downloadFile";
import { formatBytes } from "../utils/formatBytes";
2026-05-26 12:07:13 +08:00
import { postDisplayText } from "../utils/postText";
export function ImageWithTextBubble({ post }: { post: Post }) {
const { openLightbox } = useLightbox();
2026-05-27 12:23:06 +08:00
const { lang, t } = useI18n();
const [isDownloading, setIsDownloading] = useState(false);
const att = post.attachments[0];
2026-05-26 12:07:13 +08:00
const text = postDisplayText(post, lang);
if (!att) return null;
const ratio =
att.width && att.height ? `${att.width} / ${att.height}` : "4 / 3";
return (
2026-05-26 14:07:10 +08:00
<div className="relative overflow-hidden rounded-xl bg-black/20">
<button
type="button"
2026-05-26 12:07:13 +08:00
onClick={() => openLightbox([att], 0, text, post.id)}
2026-05-26 14:07:10 +08:00
className="block w-full"
aria-label={att.filename}
>
<img
src={att.url}
alt={att.filename}
loading="lazy"
2026-05-26 14:07:10 +08:00
className="block h-auto w-full"
style={{ aspectRatio: ratio }}
/>
</button>
2026-05-27 12:23:06 +08:00
<button
type="button"
onClick={(e) => {
e.stopPropagation();
if (isDownloading) return;
setIsDownloading(true);
void downloadAttachment(post.id, att.id, att.filename)
.finally(() => setIsDownloading(false))
.catch(() => {});
}}
disabled={isDownloading}
className="group absolute left-3 top-3 z-10 inline-flex overflow-hidden rounded-full bg-black/45 text-xs text-white shadow-lg ring-1 ring-white/15 backdrop-blur-md transition hover:bg-black/60 disabled:cursor-wait"
aria-label={
isDownloading ? t("downloading") : `Download ${att.filename}`
}
aria-busy={isDownloading}
>
<span className="flex h-8 w-8 items-center justify-center bg-white/10 transition group-hover:bg-white/15">
{isDownloading ? (
<LoaderCircle className="h-4 w-4 animate-spin" strokeWidth={2.3} />
) : (
<ArrowDownToLine className="h-4 w-4" strokeWidth={2.3} />
)}
</span>
<span className="flex h-8 items-center gap-1 px-2.5">
{isDownloading ? t("downloading") : formatBytes(att.sizeBytes)}
</span>
</button>
2026-05-26 12:07:13 +08:00
{text ? (
<div className="bg-gradient-to-b from-ark-panel/90 to-ark-panel px-4 py-3 text-[14px] leading-snug text-neutral-100">
2026-05-26 18:03:07 +08:00
<div className="message-stream-copyable-text select-text whitespace-pre-wrap break-words">
2026-05-26 14:07:10 +08:00
{autolink(text)}
</div>
</div>
) : null}
</div>
);
}