import { api } from '../api.js'; class PhotoGallery extends HTMLElement { constructor() { super(); this.photos = []; this.boundHandleClick = this.handleClick.bind(this); } connectedCallback() { this.addEventListener('click', this.boundHandleClick); this.render(); this.loadPhotos(); } disconnectedCallback() { this.removeEventListener('click', this.boundHandleClick); } async loadPhotos() { try { this.photos = await api.getPhotos(); this.renderPhotos(); } catch (error) { console.error('Failed to load photos:', error); this.querySelector('.gallery-grid').innerHTML = '

Failed to load photos

'; } } async renderPhotos() { const grid = this.querySelector('.gallery-grid'); if (this.photos.length === 0) { grid.innerHTML = ''; const header = this.querySelector('.gallery-header'); const empty = document.createElement('p'); empty.className = 'empty-state'; empty.textContent = 'No photos found.'; header.insertAdjacentElement('afterend', empty); return; } // Remove any existing empty-state const existingEmpty = this.querySelector('.empty-state'); if (existingEmpty) existingEmpty.remove(); grid.innerHTML = this.photos.map(photo => `
${photo.name}
${photo.name} ${new Date(photo.created_at).toLocaleDateString()}
`).join(''); grid.querySelectorAll('img[data-photo-id]').forEach(async (img) => { const photoId = img.dataset.photoId; try { const response = await fetch(`/files/thumbnail/${photoId}`, { headers: { 'Authorization': `Bearer ${api.getToken()}` } }); if (response.ok) { const blob = await response.blob(); img.src = URL.createObjectURL(blob); } else { img.style.display = 'none'; } } catch (error) { img.style.display = 'none'; } }); this.attachListeners(); } handleClick(e) { const photoItem = e.target.closest('.photo-item'); if (photoItem) { const fileId = photoItem.dataset.fileId; const photo = this.photos.find(p => p.id === parseInt(fileId)); this.dispatchEvent(new CustomEvent('photo-click', { detail: { photo }, bubbles: true })); } } attachListeners() { } render() { this.innerHTML = ` `; } } customElements.define('photo-gallery', PhotoGallery); export { PhotoGallery };