import { api } from '../api.js'; class FilePreview extends HTMLElement { constructor() { super(); this.file = null; this.handleEscape = this.handleEscape.bind(this); } connectedCallback() { this.render(); this.setupEventListeners(); } setupEventListeners() { const closeBtn = this.querySelector('.close-preview'); const modal = this.querySelector('.preview-modal'); const downloadBtn = this.querySelector('.download-btn'); const shareBtn = this.querySelector('.share-btn'); closeBtn.addEventListener('click', () => this.close()); modal.addEventListener('click', (e) => { if (e.target === modal) this.close(); }); downloadBtn.addEventListener('click', () => this.downloadFile()); shareBtn.addEventListener('click', () => this.shareFile()); } handleEscape(e) { if (e.key === 'Escape' && this.style.display === 'block') { this.close(); } } async show(file) { this.file = file; this.style.display = 'block'; document.addEventListener('keydown', this.handleEscape); this.renderPreview(); } close() { this.style.display = 'none'; this.file = null; document.removeEventListener('keydown', this.handleEscape); } async renderPreview() { const previewContent = this.querySelector('.preview-content'); const fileName = this.querySelector('.preview-file-name'); const fileInfo = this.querySelector('.preview-file-info'); fileName.textContent = this.file.name; fileInfo.textContent = `${this.formatFileSize(this.file.size)} • ${new Date(this.file.created_at).toLocaleDateString()}`; if (this.file.mime_type.startsWith('image/')) { previewContent.innerHTML = `${this.file.name}`; this.loadAuthenticatedMedia(previewContent.querySelector('img')); } else if (this.file.mime_type.startsWith('video/')) { previewContent.innerHTML = ` `; this.loadAuthenticatedMedia(previewContent.querySelector('video')); } else if (this.file.mime_type.startsWith('audio/')) { previewContent.innerHTML = ` `; this.loadAuthenticatedMedia(previewContent.querySelector('audio')); } else if (this.file.mime_type === 'application/pdf') { previewContent.innerHTML = ``; this.loadAuthenticatedMedia(previewContent.querySelector('iframe')); } else if (this.file.mime_type.startsWith('text/')) { this.loadTextPreview(previewContent); } else { previewContent.innerHTML = `

Preview not available for this file type

`; } } async loadAuthenticatedMedia(element) { try { const blob = await api.downloadFile(this.file.id); const url = URL.createObjectURL(blob); element.src = url; } catch (error) { console.error('Failed to load media:', error); element.parentElement.innerHTML = '

Failed to load preview

'; } } async loadTextPreview(container) { try { const blob = await api.downloadFile(this.file.id); const text = await blob.text(); container.innerHTML = `
${this.escapeHtml(text)}
`; } catch (error) { container.innerHTML = '

Failed to load preview

'; } } async downloadFile() { try { const blob = await api.downloadFile(this.file.id); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = this.file.name; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(url); document.body.removeChild(a); } catch (error) { console.error('Failed to download file:', error); } } shareFile() { this.dispatchEvent(new CustomEvent('share-file', { detail: { file: this.file }, bubbles: true })); } formatFileSize(bytes) { const units = ['B', 'KB', 'MB', 'GB']; let size = bytes; let unitIndex = 0; while (size >= 1024 && unitIndex < units.length - 1) { size /= 1024; unitIndex++; } return `${size.toFixed(1)} ${units[unitIndex]}`; } escapeHtml(text) { const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } render() { this.innerHTML = `

`; this.style.display = 'none'; } } customElements.define('file-preview', FilePreview); export { FilePreview };