class CustomList extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = `
ID Type Status Actions
`; this.sortConfig = { key: 'id', direction: 'desc' }; } connectedCallback() { this.loadBatches(); feather.replace({ width: 16, height: 16 }); // Add event listeners for sorting this.shadowRoot.querySelectorAll('th[data-sort]').forEach(th => { th.addEventListener('click', () => this.handleSort(th.dataset.sort)); }); // Listen for new batch events document.addEventListener('batch-created', () => this.loadBatches()); } async loadBatches() { const tableBody = this.shadowRoot.getElementById('batchTableBody'); const loading = this.shadowRoot.getElementById('loading'); const emptyState = this.shadowRoot.getElementById('emptyState'); tableBody.innerHTML = ''; loading.classList.remove('hidden'); emptyState.classList.add('hidden'); try { const batches = await window.batchService.getAllBatches(); if (batches.length === 0) { loading.classList.add('hidden'); emptyState.classList.remove('hidden'); return; } // Apply sorting const sortedBatches = [...batches].sort((a, b) => { if (a[this.sortConfig.key] < b[this.sortConfig.key]) { return this.sortConfig.direction === 'asc' ? -1 : 1; } if (a[this.sortConfig.key] > b[this.sortConfig.key]) { return this.sortConfig.direction === 'asc' ? 1 : -1; } return 0; }); sortedBatches.forEach(batch => { const row = document.createElement('tr'); let statusBadgeClass = ''; switch (batch.status) { case 'PENDING': statusBadgeClass = 'processing-badge-pending'; break; case 'RUNNING': statusBadgeClass = 'processing-badge-running'; break; case 'COMPLETED': statusBadgeClass = 'processing-badge-completed'; break; } row.innerHTML = ` ${batch.id} ${batch.type} ${batch.status} `; tableBody.appendChild(row); }); // Add event listeners to download buttons this.shadowRoot.querySelectorAll('.download-btn').forEach(btn => { btn.addEventListener('click', () => this.handleDownload(btn.dataset.id)); }); // Update sort indicators this.updateSortIndicators(); } catch (error) { console.error('Error loading batches:', error); } finally { loading.classList.add('hidden'); feather.replace({ width: 16, height: 16 }); } } handleSort(key) { if (this.sortConfig.key === key) { this.sortConfig.direction = this.sortConfig.direction === 'asc' ? 'desc' : 'asc'; } else { this.sortConfig.key = key; this.sortConfig.direction = 'desc'; } this.loadBatches(); } updateSortIndicators() { this.shadowRoot.querySelectorAll('.sort-icon').forEach(icon => { icon.classList.remove('active'); icon.setAttribute('data-feather', 'chevron-down'); }); const activeTh = this.shadowRoot.querySelector(`th[data-sort="${this.sortConfig.key}"]`); if (activeTh) { const icon = activeTh.querySelector('.sort-icon'); icon.classList.add('active'); icon.setAttribute('data-feather', this.sortConfig.direction === 'asc' ? 'chevron-up' : 'chevron-down'); feather.replace({ width: 16, height: 16 }); } } async handleDownload(batchId) { try { await window.batchService.downloadResult(parseInt(batchId)); } catch (error) { console.error('Error downloading batch result:', error); } } } customElements.define('custom-list', CustomList);