import { api } from '../api.js'; export class DeletedFiles extends HTMLElement { constructor() { super(); this.deletedFiles = []; } async connectedCallback() { await this.loadDeletedFiles(); } async loadDeletedFiles() { try { this.deletedFiles = await api.listDeletedFiles(); this.render(); } catch (error) { console.error('Failed to load deleted files:', error); this.innerHTML = '

Failed to load deleted files.

'; } } render() { if (this.deletedFiles.length === 0) { this.innerHTML = `

Deleted Files

No deleted files found.

`; return; } this.innerHTML = `

Deleted Files

${this.deletedFiles.map(file => this.renderDeletedFile(file)).join('')}
`; this.attachListeners(); } renderDeletedFile(file) { const icon = this.getFileIcon(file.mime_type); const size = this.formatFileSize(file.size); const deletedDate = new Date(file.deleted_at).toLocaleDateString(); return `
${icon}
${file.name}
${size}
Deleted: ${deletedDate}
`; } getFileIcon(mimeType) { if (mimeType.startsWith('image/')) return '📷'; if (mimeType.startsWith('video/')) return '🎥'; if (mimeType.startsWith('audio/')) return '🎵'; if (mimeType.includes('pdf')) return '📄'; if (mimeType.includes('text')) return '📄'; return '📄'; } formatFileSize(bytes) { if (bytes < 1024) return bytes + ' B'; if (bytes < 1048576) return (bytes / 1024).toFixed(1) + ' KB'; if (bytes < 1073741824) return (bytes / 1048576).toFixed(1) + ' MB'; return (bytes / 1073741824).toFixed(1) + ' GB'; } attachListeners() { this.querySelectorAll('.action-btn').forEach(btn => { btn.addEventListener('click', async (e) => { e.stopPropagation(); const action = btn.dataset.action; const id = parseInt(btn.dataset.id); if (action === 'restore') { await this.handleRestore(id); } }); }); } async handleRestore(fileId) { if (confirm('Are you sure you want to restore this file?')) { try { await api.restoreFile(fileId); document.dispatchEvent(new CustomEvent('show-toast', { detail: { message: 'File restored successfully!', type: 'success' } })); await this.loadDeletedFiles(); // Reload the list } catch (error) { document.dispatchEvent(new CustomEvent('show-toast', { detail: { message: 'Failed to restore file: ' + error.message, type: 'error' } })); } } } } customElements.define('deleted-files', DeletedFiles);