"use client" import { FileCode, FileText, Loader2, X } from "lucide-react" import Image from "next/image" import { useEffect, useRef, useState } from "react" import { useDictionary } from "@/hooks/use-dictionary" import { isPdfFile, isTextFile } from "@/lib/pdf-utils" function formatCharCount(count: number): string { if (count >= 1000) { return `${(count / 1000).toFixed(1)}k` } return String(count) } interface FilePreviewListProps { files: File[] onRemoveFile: (fileToRemove: File) => void pdfData?: Map< File, { text: string; charCount: number; isExtracting: boolean } > } export function FilePreviewList({ files, onRemoveFile, pdfData = new Map(), }: FilePreviewListProps) { const dict = useDictionary() const [selectedImage, setSelectedImage] = useState(null) const [imageUrls, setImageUrls] = useState>(new Map()) const imageUrlsRef = useRef>(new Map()) // Create and cleanup object URLs when files change useEffect(() => { const currentUrls = imageUrlsRef.current const newUrls = new Map() files.forEach((file) => { if (file.type.startsWith("image/")) { // Reuse existing URL if file is already tracked const existingUrl = currentUrls.get(file) if (existingUrl) { newUrls.set(file, existingUrl) } else { newUrls.set(file, URL.createObjectURL(file)) } } }) // Revoke URLs for files that are no longer in the list currentUrls.forEach((url, file) => { if (!newUrls.has(file)) { URL.revokeObjectURL(url) } }) imageUrlsRef.current = newUrls setImageUrls(newUrls) }, [files]) // Cleanup all URLs on unmount only useEffect(() => { return () => { imageUrlsRef.current.forEach((url) => { URL.revokeObjectURL(url) }) // Clear the ref so StrictMode remount creates fresh URLs imageUrlsRef.current = new Map() } }, []) // Clear selected image if its URL was revoked useEffect(() => { if ( selectedImage && !Array.from(imageUrls.values()).includes(selectedImage) ) { setSelectedImage(null) } }, [imageUrls, selectedImage]) if (files.length === 0) return null return ( <>
{files.map((file, index) => { const imageUrl = imageUrls.get(file) || null const pdfInfo = pdfData.get(file) return (
file.type.startsWith("image/") && imageUrl && setSelectedImage(imageUrl) } > {file.type.startsWith("image/") && imageUrl ? ( {file.name} ) : isPdfFile(file) || isTextFile(file) ? (
{pdfInfo?.isExtracting ? ( ) : isPdfFile(file) ? ( ) : ( )} {file.name.length > 10 ? `${file.name.slice(0, 7)}...` : file.name} {pdfInfo?.isExtracting ? ( {dict.file.reading} ) : pdfInfo?.charCount ? ( {formatCharCount( pdfInfo.charCount, )}{" "} {dict.file.chars} ) : null}
) : (
{file.name}
)}
) })}
{/* Image Modal/Lightbox */} {selectedImage && (
setSelectedImage(null)} >
Full size preview of uploaded diagram or image e.stopPropagation()} unoptimized />
)} ) }