peppa/apps/web/components/Attachments.tsx

44 lines
2.1 KiB
TypeScript

import { FileSpreadsheet, FileText, Presentation } from "lucide-react";
import type { AttachmentView } from "@peppa/core";
import { humanSize } from "@peppa/shared/format";
/**
* PEP-159: files of a message, a comment or a story — pictures as thumbnails that open large,
* everything else as a row with the name and the size (a PDF opens, the rest downloads).
*/
export function Attachments({ files }: { files?: AttachmentView[] }) {
if (!files?.length) return null;
const images = files.filter((f) => f.kind === "image");
const rest = files.filter((f) => f.kind !== "image");
return (
<div className="flex min-w-0 flex-col gap-1.5" data-attachments>
{images.length > 0 && (
<div className="flex flex-wrap gap-2">
{images.map((f) => (
<a key={f.id} href={`/api/attachments/${f.id}`} target="_blank" rel="noreferrer" className="block overflow-hidden rounded-md border bg-background" title={f.name}>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={`/api/attachments/${f.id}`} alt={f.name} loading="lazy" className="block h-24 max-w-[12rem] object-cover" />
</a>
))}
</div>
)}
{rest.length > 0 && (
<ul className="m-0 flex list-none flex-col gap-1 p-0">
{rest.map((f) => {
const Icon = f.mime.includes("spreadsheet") ? FileSpreadsheet : f.mime.includes("presentation") ? Presentation : FileText;
return (
<li key={f.id}>
<a href={`/api/attachments/${f.id}${f.kind === "pdf" ? "" : "?download=1"}`} target={f.kind === "pdf" ? "_blank" : undefined} rel="noreferrer" className="inline-flex max-w-full items-center gap-2 rounded-md border bg-background px-2 py-1 text-xs hover:bg-accent hover:no-underline">
<Icon className="size-4 shrink-0 text-muted-foreground" />
<span className="min-w-0 truncate">{f.name}</span>
<span className="shrink-0 text-muted-foreground">{humanSize(f.size)}</span>
</a>
</li>
);
})}
</ul>
)}
</div>
);
}