"use client"; import { useTranslation } from "react-i18next"; import { GeneratedOutputFileActions, supportsGeneratedOutputActions, } from "@/components/chat/GeneratedOutputFileActions"; import { ChatFileLink } from "@/components/common/ChatFileLink"; import type { MessageAttachment } from "@/features/chat/ChatStateAdapter"; import { docIconFor } from "@/lib/doc-attachments"; import { imageSrcForAttachment } from "./attachment-media"; /** Format a byte count for a file card subtitle (e.g. "14 KB"). */ function formatFileSize(bytes?: number): string { if (!bytes || bytes <= 0) return ""; const units = ["B", "KB", "MB", "GB"]; let value = bytes; let unit = 0; while (value >= 1024 && unit < units.length - 1) { value /= 1024; unit += 1; } return `${unit === 0 ? value : value.toFixed(1)} ${units[unit]}`; } /** "DeepTutor_Introduction.pdf" → "DeepTutor Introduction" — the card title * reads like a document name; the extension already shows in the subtitle. */ function humanizeFilename(filename: string): string { const stem = filename.replace(/\.[A-Za-z0-9]{1,8}$/, ""); return ( stem .replace(/[_-]+/g, " ") .replace(/\s{2,}/g, " ") .trim() || filename ); } export default function GeneratedFileCardList({ files, onOpen, }: { files: MessageAttachment[]; onOpen?: (attachment: MessageAttachment) => void; }) { const { t } = useTranslation(); return (
{files.map((a, i) => { const filename = a.filename || t("File"); const key = a.workspace_item_id || a.id || a.url || `gen-${i}`; const mime = a.mime_type || ""; const mediaSrc = imageSrcForAttachment(a); const caption = a.caption?.trim() || ""; // Generated media renders inline; other MIME types use a file card. if (mime.startsWith("image/") && mediaSrc) { return (
{/* eslint-disable-next-line @next/next/no-img-element */} {filename} {a.title || humanizeFilename(filename)} {caption ? ( {caption} ) : null} {t("Open")} {supportsGeneratedOutputActions(a) ? ( ) : null}
); } if (mime.startsWith("video/") && mediaSrc) { return (
); } const spec = docIconFor(filename); const Icon = spec.Icon; const size = formatFileSize(a.size_bytes); return (
{a.title || humanizeFilename(filename)} {caption || spec.label} {size ? ` · ${size}` : ""} {t("Open")} {supportsGeneratedOutputActions(a) ? ( ) : null}
); })}
); }