import React, { useState } from 'react'; import { DataItem } from '../types'; import { Trash2, RotateCcw, X, Folder, AlertTriangle, RefreshCw, Layers } from 'lucide-react'; import { restoreItem, restoreAllItems, permanentlyDeleteItem, emptyTrash } from '../services/firebase'; interface TrashModalProps { isOpen: boolean; onClose: () => void; deletedItems: DataItem[]; allFolders: DataItem[]; } export const TrashModal: React.FC = ({ isOpen, onClose, deletedItems, allFolders }) => { const [loadingAction, setLoadingAction] = useState(null); const [showConfirmEmpty, setShowConfirmEmpty] = useState(false); if (!isOpen) return null; // Filter items deleted in the last 30 days const THIRTY_DAYS_MS = 30 * 24 * 60 * 60 * 1000; const now = Date.now(); const validDeletedItems = deletedItems.filter(item => { if (!item.deletedAt) return true; return (now - item.deletedAt) <= THIRTY_DAYS_MS; }); const getFolderName = (parentId?: string | null) => { if (!parentId) return 'Raíz'; const folder = allFolders.find(f => f.id === parentId); if (!folder) return 'Carpeta anterior'; const raw = (folder.title || '').replace(/<[^>]*>?/gm, '').replace(/ /g, ' ').trim() || folder.title; return raw || 'Carpeta anterior'; }; const getTimeAgo = (timestamp?: number) => { if (!timestamp) return 'Recientemente'; const diff = now - timestamp; const minutes = Math.floor(diff / (1000 * 60)); const hours = Math.floor(diff / (1000 * 60 * 60)); const days = Math.floor(diff / (1000 * 60 * 60 * 24)); if (days > 0) return `Hace ${days} día${days > 1 ? 's' : ''}`; if (hours > 0) return `Hace ${hours} hora${hours > 1 ? 's' : ''}`; if (minutes > 0) return `Hace ${minutes} min`; return 'Hace un momento'; }; const getRemainingDays = (timestamp?: number) => { if (!timestamp) return 30; const diff = now - timestamp; const daysLeft = Math.max(0, 30 - Math.floor(diff / (1000 * 60 * 60 * 24))); return daysLeft; }; const handleRestoreOne = async (item: DataItem) => { try { setLoadingAction(item.id); await restoreItem(item.id); } catch (e) { alert('Error al restaurar el elemento.'); } finally { setLoadingAction(null); } }; const handleRestoreAll = async () => { if (validDeletedItems.length === 0) return; try { setLoadingAction('ALL'); await restoreAllItems(validDeletedItems.map(i => i.id)); } catch (e) { alert('Error al restaurar todos los elementos.'); } finally { setLoadingAction(null); } }; const handleDeletePermanent = async (item: DataItem) => { if (!confirm(`¿Eliminar permanentemente "${item.title.replace(/<[^>]*>?/gm, '')}"? Esta acción no se puede deshacer.`)) return; try { setLoadingAction(item.id); await permanentlyDeleteItem(item.id); } catch (e) { alert('Error al eliminar el elemento.'); } finally { setLoadingAction(null); } }; const handleEmptyTrash = async () => { try { setLoadingAction('EMPTY'); await emptyTrash(validDeletedItems.map(i => i.id)); setShowConfirmEmpty(false); } catch (e) { alert('Error al vaciar la papelera.'); } finally { setLoadingAction(null); } }; return (
{/* Header */}

Papelera de Reciclaje {validDeletedItems.length} {validDeletedItems.length === 1 ? 'elemento' : 'elementos'}

Los elementos eliminados se conservan por 30 días antes de ser purgados.

{/* Top Control Bar */} {validDeletedItems.length > 0 && (
)} {/* Content Body */}
{validDeletedItems.length === 0 ? (

La papelera está vacía

No hay elementos eliminados en los últimos 30 días. Todos tus archivos y voces se conservan en tus carpetas.

) : ( validDeletedItems.map(item => { const cleanTitle = (item.title || '').replace(/<[^>]*>?/gm, '').replace(/ /g, ' ') || 'Sin título'; const isFolder = item.isFolder; const folderName = getFolderName(item.parentId); const remainingDays = getRemainingDays(item.deletedAt); return (
{isFolder ? : }

{cleanTitle}

📁 {folderName} • {getTimeAgo(item.deletedAt)} • Expira en {remainingDays} día{remainingDays !== 1 ? 's' : ''}
{/* Actions for single item */}
); }) )}
{/* Footer */}
Sistema de Recuperación Automática (30 días)
{/* Confirmation Modal for Empty Trash */} {showConfirmEmpty && (

¿Vaciar la papelera?

Esta acción eliminará **permanentemente** todos los {validDeletedItems.length} elementos de la papelera. No podrás recuperarlos después.

)}
); };