import { FaFile, FaDownload, FaTrash } from "react-icons/fa"; import { MdOutlineRemoveRedEye } from "react-icons/md"; import styles from "./DateienItem.module.css"; import { useState } from "react"; import { useFileOperations } from "../../hooks/useFiles"; import FilePreviewPopup from "../Dashboard/DashboardChat/DashboardChatAreaFilePreview"; import { Document } from "../Dashboard/DashboardChat/dashboardChatAreaTypes"; import { useLanguage } from "../../contexts/LanguageContext"; type DateienItemProps = { file: { id: number; file_name: string; action: string; created_at: string; size?: number; source?: string; // 'user_uploaded', 'agent_created', or 'shared_with_me' }; onDelete?: () => void; onOptimisticDelete?: () => void; // New prop for immediate UI update }; const DateienItem = ({ file, onDelete, onOptimisticDelete }: DateienItemProps) => { const { t } = useLanguage(); const { downloadingFiles, deletingFiles, handleFileDownload, handleFileDelete } = useFileOperations(); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [previewDocument, setPreviewDocument] = useState(null); const [isPreviewOpen, setIsPreviewOpen] = useState(false); const isDownloading = downloadingFiles.has(file.id); const isDeleting = deletingFiles.has(file.id); /** * Formats a file size in bytes to a human-readable string (KB, MB, etc.) */ const formatFileSize = (bytes?: number): string => { if (bytes === undefined || bytes === null) return t('files.unknown_size', 'Unknown Size'); if (bytes === 0) return '0 Bytes'; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; const i = Math.floor(Math.log(bytes) / Math.log(1024)); if (i === 0) return `${bytes} ${sizes[i]}`; return `${(bytes / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`; }; /** * Formats the file source for display */ const formatFileSource = (source?: string): string => { switch (source) { case 'user_uploaded': return t('files.source.uploaded', 'Uploaded'); case 'agent_created': return t('files.source.ai_created', 'AI-created'); case 'shared_with_me': return t('files.source.shared', 'Shared'); default: return t('files.source.unknown', 'Unknown'); } }; // Format the date properly const formatDate = (dateString: string) => { try { const date = new Date(dateString); // Check if date is valid if (isNaN(date.getTime())) { return t('files.unknown_date', 'Unknown Date'); } return date.toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric' }); } catch (e) { console.error('Error formatting date:', e); return t('files.unknown_date', 'Unknown Date'); } }; const handleDeleteClick = async () => { if (showDeleteConfirm) { const success = await handleFileDelete(file.id, onOptimisticDelete); if (!success) { // If deletion failed, refresh the file list to restore the file if (onDelete) { onDelete(); } } setShowDeleteConfirm(false); } else { setShowDeleteConfirm(true); } }; const handleCancelDelete = () => { setShowDeleteConfirm(false); }; const handlePreview = () => { // Split filename to get name and extension const nameParts = file.file_name.split('.'); const extension = nameParts.length > 1 ? nameParts.pop() : undefined; const fileName = nameParts.join('.'); // Create a Document object compatible with FilePreviewPopup const document: Document = { id: String(file.id), fileId: file.id, name: fileName, ext: extension, size: file.size }; setPreviewDocument(document); setIsPreviewOpen(true); }; const handleClosePreview = () => { setIsPreviewOpen(false); setPreviewDocument(null); }; return ( <>
  • {/* 1st column: Name with icon */}
    {file.file_name}
    {/* 2nd column: Type with source */}
    {file.action}
    {formatFileSource(file.source)}
    {/* 3rd column: Size */}
    {formatFileSize(file.size)}
    {/* 4th column: Date and action buttons */}
    {formatDate(file.created_at)}
  • {/* File Preview Popup */} {previewDocument && ( )} ); }; export default DateienItem;