// Pagination state let allArticles = []; let filteredArticles = []; let displayedCount = 0; const ARTICLES_PER_PAGE = 5; let isLoading = false; let searchQuery = ''; // Load news on page load document.addEventListener('DOMContentLoaded', () => { loadNews(); loadStats(); setupInfiniteScroll(); loadCategories(); }); async function loadCategories() { try { const response = await fetch('/api/categories'); const data = await response.json(); const categories = data.categories || []; const container = document.getElementById('categoryCheckboxes'); container.innerHTML = ''; categories.forEach(category => { const label = document.createElement('label'); label.className = 'flex items-center space-x-3 cursor-pointer'; label.innerHTML = ` ${category.icon} ${category.name} `; container.appendChild(label); }); } catch (error) { console.error('Failed to load categories:', error); } } async function loadNews() { const newsGrid = document.getElementById('newsGrid'); newsGrid.innerHTML = '
Loading more...
'; newsGrid.appendChild(loader); } else if (filteredArticles.length > 0) { // Add end message const endMessage = document.createElement('div'); endMessage.className = 'text-center py-8 text-gray-400 text-sm'; endMessage.textContent = `✓ All ${filteredArticles.length} articles loaded`; newsGrid.appendChild(endMessage); } isLoading = false; } function setupInfiniteScroll() { window.addEventListener('scroll', () => { if (isLoading || displayedCount >= filteredArticles.length) return; const scrollPosition = window.innerHeight + window.scrollY; const threshold = document.documentElement.scrollHeight - 500; if (scrollPosition >= threshold) { loadMoreArticles(); } }); } // Search functionality let searchTimeout; async function handleSearch() { const searchInput = document.getElementById('searchInput'); const clearBtn = document.getElementById('clearSearch'); const searchStats = document.getElementById('searchStats'); const newsGrid = document.getElementById('newsGrid'); searchQuery = searchInput.value.trim(); // Show/hide clear button if (searchQuery) { clearBtn.classList.remove('hidden'); } else { clearBtn.classList.add('hidden'); } // Clear previous timeout if (searchTimeout) clearTimeout(searchTimeout); // If empty query, reset to all articles if (searchQuery === '') { filteredArticles = allArticles; displayedCount = 0; newsGrid.innerHTML = ''; updateSearchStats(); loadMoreArticles(); return; } // Debounce search API call searchTimeout = setTimeout(async () => { // Show searching state newsGrid.innerHTML = 'No relevant articles found
Try different keywords or concepts
${cleanSummary}