tahamajs/IE / Projects /front /src /components /multimodal /FileAttachmentBubble.tsx
tahamajs's picture
download
raw
4.45 kB
// src/components/multimodal/FileAttachmentBubble.tsx
import { useState } from "react";
import type { ProcessedFile } from "@/types/multimodal";
import { ImagePreviewModal } from "./ImagePreviewModal";
import { CsvDataTable } from "./CsvDataTable";
import { PdfEmbedViewer } from "./PdfEmbedViewer";
import { CodeBlock } from "./CodeBlock";
import { ChemicalFormulaRenderer } from "./ChemicalFormulaRenderer";
import {
FileImage, FileSpreadsheet, FileText, FileCode, File, FileArchive,
FlaskConical, Download, ExternalLink, ChevronDown, ChevronUp,
} from "lucide-react";
interface FileAttachmentBubbleProps {
file: ProcessedFile;
isOwn: boolean;
}
function formatSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
export function FileAttachmentBubble({ file, isOwn }: FileAttachmentBubbleProps) {
const [expanded, setExpanded] = useState(false);
const [lightboxOpen, setLightboxOpen] = useState(false);
const renderPreview = () => {
if (!expanded) return null;
switch (file.category) {
case "image":
return file.thumbnailUrl ? (
<img
src={file.serverUrl ?? file.thumbnailUrl}
alt={file.name}
className="w-full max-h-64 object-contain rounded-lg cursor-pointer"
onClick={() => setLightboxOpen(true)}
/>
) : null;
case "csv":
return file.csvHeaders ? (
<CsvDataTable headers={file.csvHeaders} rows={file.csvRows ?? []} />
) : null;
case "pdf":
return file.serverUrl ? (
<PdfEmbedViewer url={file.serverUrl} />
) : (
<p className="text-sm text-gray-500">Upload to preview PDF</p>
);
case "code":
return file.previewData ? (
<CodeBlock code={file.previewData} language={file.language ?? "text"} />
) : null;
case "chemical":
return (
<ChemicalFormulaRenderer
text={file.previewData ?? ""}
smiles={file.moleculeSmiles}
/>
);
default:
return (
<p className="text-sm text-gray-500">
{file.category} file – {formatSize(file.size)}
</p>
);
}
};
const downloadFile = () => {
const url = file.serverUrl ?? file.thumbnailUrl ?? "";
if (!url) return;
const a = document.createElement("a");
a.href = url;
a.download = file.name;
a.click();
};
const IconComponent = {
image: FileImage,
csv: FileSpreadsheet,
pdf: FileText,
code: FileCode,
latex: FileCode,
chemical: FlaskConical,
document: FileText,
audio: File,
video: FileImage,
archive: FileArchive,
unknown: File,
}[file.category] ?? File;
return (
<div
className={`mt-2 rounded-xl overflow-hidden border ${
isOwn
? "border-white/20 bg-white/10"
: "border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-700/50"
}`}
>
{/* Header bar */}
<button
onClick={() => setExpanded(!expanded)}
className="w-full flex items-center gap-3 p-3 hover:bg-black/5 dark:hover:bg-white/5 transition text-left"
>
<IconComponent className="w-5 h-5 shrink-0" />
<div className="flex-1 min-w-0">
<p className="text-sm font-medium truncate">{file.name}</p>
<p className="text-xs opacity-60">{formatSize(file.size)}</p>
</div>
<div className="flex items-center gap-1">
<button
onClick={(e) => {
e.stopPropagation();
downloadFile();
}}
className="p-1 rounded hover:bg-black/10 dark:hover:bg-white/10"
>
<Download className="w-4 h-4" />
</button>
{expanded ? (
<ChevronUp className="w-4 h-4" />
) : (
<ChevronDown className="w-4 h-4" />
)}
</div>
</button>
{/* Expandable preview */}
{expanded && <div className="p-3 border-t border-inherit">{renderPreview()}</div>}
{/* Lightbox for images */}
{lightboxOpen && (
<ImagePreviewModal
src={file.serverUrl ?? file.thumbnailUrl ?? ""}
alt={file.name}
onClose={() => setLightboxOpen(false)}
/>
)}
</div>
);
}

Xet Storage Details

Size:
4.45 kB
·
Xet hash:
c792f7304aee0a23b2dd4d6514967c0f0f8b6a6493c401d4b723b4032522ba38

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.