import { X, FileIcon, Loader2 } from 'lucide-react' import type { UploadFile } from '../types' import { cn } from '@/utils/cn' import { ImagePreview } from '@/components/ui/image-preview' import { Image } from '@/components/ui/image' interface FilePreviewListProps { files: UploadFile[] onRemove: (uid: string) => void disabled?: boolean } export function FilePreviewList({ files, onRemove, disabled }: FilePreviewListProps) { if (files.length === 0) return null return (
{files.map(file => { const isImage = /\.(jpg|jpeg|png|gif|webp|svg|ico|bmp)$/i.test(file.name) const isUploading = file.uploadStatus === 'uploading' || file.uploadStatus === 'pending' const hasError = file.uploadStatus === 'error' const showImagePreview = isImage && file.url return (
{ if (file.url && !isUploading && !hasError) { if (!showImagePreview) { window.open(file.url, '_blank') } } }} > {/* Icon / Image Preview */}
{isUploading ? ( ) : showImagePreview ? ( {file.name} { e.currentTarget.style.display = 'none' e.currentTarget.parentElement?.classList.add('fallback-icon') }} /> ) : ( )}
{file.name} {isUploading ? '上传中...' : hasError ? '上传失败' : formatFileSize(file.byte_size)}
{/* Progress Bar */} {isUploading && (
)}
) })}
) } function formatFileSize(bytes?: number) { if (!bytes) return '' if (bytes < 1024) return bytes + ' B' const k = 1024 const sizes = ['KB', 'MB', 'GB'] const index = Math.min( Math.floor(Math.log(bytes) / Math.log(k)) - 1, sizes.length - 1, ) return parseFloat((bytes / Math.pow(k, index + 1)).toFixed(1)) + ' ' + sizes[index] }