110 lines
3.7 KiB
JavaScript
110 lines
3.7 KiB
JavaScript
|
|
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 = '<p class="error-message">Failed to load deleted files.</p>';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
render() {
|
||
|
|
if (this.deletedFiles.length === 0) {
|
||
|
|
this.innerHTML = `
|
||
|
|
<div class="deleted-files-container">
|
||
|
|
<h2>Deleted Files</h2>
|
||
|
|
<p class="empty-state">No deleted files found.</p>
|
||
|
|
</div>
|
||
|
|
`;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
this.innerHTML = `
|
||
|
|
<div class="deleted-files-container">
|
||
|
|
<h2>Deleted Files</h2>
|
||
|
|
<div class="file-grid">
|
||
|
|
${this.deletedFiles.map(file => this.renderDeletedFile(file)).join('')}
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
`;
|
||
|
|
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 `
|
||
|
|
<div class="file-item deleted-item" data-file-id="${file.id}">
|
||
|
|
<div class="file-icon">${icon}</div>
|
||
|
|
<div class="file-name">${file.name}</div>
|
||
|
|
<div class="file-size">${size}</div>
|
||
|
|
<div class="file-deleted-date">Deleted: ${deletedDate}</div>
|
||
|
|
<div class="file-actions-menu">
|
||
|
|
<button class="button button-danger action-btn" data-action="restore" data-id="${file.id}">Restore</button>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
`;
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|