File size: 8,084 Bytes
c6db453
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
class CustomList extends HTMLElement {
    constructor() {
        super();
        this.attachShadow({ mode: 'open' });
        this.shadowRoot.innerHTML = `
            <style>
                @import url('https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css');
                
                table {
                    width: 100%;
                    border-collapse: collapse;
                }
                
                th {
                    cursor: pointer;
                    user-select: none;
                }
                
                th:hover {
                    background-color: #f3f4f6;
                }
                
                .sort-icon {
                    margin-left: 4px;
                    opacity: 0.5;
                }
                
                .sort-icon.active {
                    opacity: 1;
                }
            </style>
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider" data-sort="id">
                                ID <i data-feather="chevron-down" class="sort-icon"></i>
                            </th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider" data-sort="type">
                                Type <i data-feather="chevron-down" class="sort-icon"></i>
                            </th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider" data-sort="status">
                                Status <i data-feather="chevron-down" class="sort-icon"></i>
                            </th>
                            <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                Actions
                            </th>
                        </tr>
                    </thead>
                    <tbody id="batchTableBody" class="bg-white divide-y divide-gray-200">
                        <!-- Batches will be loaded here -->
                    </tbody>
                </table>
            </div>
            <div id="loading" class="flex justify-center items-center py-8">
                <div class="animate-spin rounded-full h-8 w-8 border-b-2 border-indigo-600"></div>
            </div>
            <div id="emptyState" class="hidden text-center py-12">
                <i data-feather="inbox" class="mx-auto h-12 w-12 text-gray-400"></i>
                <h3 class="mt-2 text-sm font-medium text-gray-900">No batches yet</h3>
                <p class="mt-1 text-sm text-gray-500">Upload a file to get started</p>
            </div>
        `;
        
        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 = `
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">${batch.id}</td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">${batch.type}</td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        <span class="processing-badge ${statusBadgeClass}">${batch.status}</span>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        <button class="download-btn" ${batch.status !== 'COMPLETED' ? 'disabled' : ''} data-id="${batch.id}">
                            <i data-feather="download" class="w-4 h-4 mr-1"></i> Download
                        </button>
                    </td>
                `;
                
                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);