import { FaFile, FaDownload, FaTrash } from "react-icons/fa"; import styles from "./DateienItem.module.css"; import { useState } from "react"; import { useFileOperations } from "../../hooks/useFiles"; type DateienItemProps = { file: { id: number; file_name: string; action: string; created_at: string; size?: number; }; onDelete?: () => void; }; /** * 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 'Unbekannte Größe'; 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]}`; }; const DateienItem = ({ file, onDelete }: DateienItemProps) => { const { downloadingFiles, deletingFiles, handleFileDownload, handleFileDelete } = useFileOperations(); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const isDownloading = downloadingFiles.has(file.id); const isDeleting = deletingFiles.has(file.id); // Format the date properly const formatDate = (dateString: string) => { try { const date = new Date(dateString); // Check if date is valid if (isNaN(date.getTime())) { return 'Unbekanntes Datum'; } return date.toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric' }); } catch (e) { console.error('Error formatting date:', e); return 'Unbekanntes Datum'; } }; const handleDeleteClick = async () => { if (showDeleteConfirm) { const success = await handleFileDelete(file.id); if (success && onDelete) { onDelete(); } setShowDeleteConfirm(false); } else { setShowDeleteConfirm(true); } }; const handleCancelDelete = () => { setShowDeleteConfirm(false); }; return (