import { memo, useState } from "react";
import { cn } from "@/lib/utils";
import { Attachment } from "@/types/proto/api/v1/attachment_service";
import { getAttachmentThumbnailUrl, getAttachmentType, getAttachmentUrl } from "@/utils/attachment";
import MemoAttachment from "./MemoAttachment";
import PreviewImageDialog from "./PreviewImageDialog";
const MemoAttachmentListView = ({ attachments = [] }: { attachments: Attachment[] }) => {
const [previewImage, setPreviewImage] = useState<{ open: boolean; urls: string[]; index: number }>({
open: false,
urls: [],
index: 0,
});
const mediaAttachments: Attachment[] = [];
const otherAttachments: Attachment[] = [];
attachments.forEach((attachment) => {
const type = getAttachmentType(attachment);
if (type === "image/*" || type === "video/*") {
mediaAttachments.push(attachment);
return;
}
otherAttachments.push(attachment);
});
const handleImageClick = (imgUrl: string) => {
const imgUrls = mediaAttachments
.filter((attachment) => getAttachmentType(attachment) === "image/*")
.map((attachment) => getAttachmentUrl(attachment));
const index = imgUrls.findIndex((url) => url === imgUrl);
setPreviewImage({ open: true, urls: imgUrls, index });
};
const MediaCard = ({ attachment, className }: { attachment: Attachment; className?: string }) => {
const type = getAttachmentType(attachment);
const attachmentUrl = getAttachmentUrl(attachment);
const attachmentThumbnailUrl = getAttachmentThumbnailUrl(attachment);
if (type === "image/*") {
return (
{
// Fallback to original image if thumbnail fails
const target = e.target as HTMLImageElement;
if (target.src.includes("?thumbnail=true")) {
console.warn("Thumbnail failed, falling back to original image:", attachmentUrl);
target.src = attachmentUrl;
}
}}
onClick={() => handleImageClick(attachmentUrl)}
decoding="async"
loading="lazy"
/>
);
} else if (type === "video/*") {
return (
);
} else {
return <>>;
}
};
const MediaList = ({ attachments = [] }: { attachments: Attachment[] }) => {
const cards = attachments.map((attachment) => (