// Global state management const state = { isLoading: false, currentPdfUrl: null }; // Add state management for the current paper let currentPaperState = { pdfUrl: null, title: null }; // Add state management for search results let searchState = { lastResults: null, lastQuery: null }; // Utility Functions function getCsrfToken() { return document.querySelector('meta[name="csrf-token"]').getAttribute('content'); } function showLoading() { const overlay = document.getElementById('loadingOverlay'); overlay.classList.remove('hidden'); state.isLoading = true; } function hideLoading() { const overlay = document.getElementById('loadingOverlay'); overlay.classList.add('hidden'); state.isLoading = false; } // Search Functionality async function performSearch() { const searchQuery = document.getElementById('searchInput').value.trim(); const sortBy = document.getElementById('sortBy').value; const maxResults = parseInt(document.getElementById('maxResults').value); if (!searchQuery) { alert('Please enter a search term'); return; } // Show loading overlay document.getElementById('loadingOverlay').classList.remove('hidden'); try { const response = await fetch('/search', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRFToken': getCsrfToken() }, body: JSON.stringify({ paper_name: searchQuery, sort_by: sortBy, max_results: maxResults }) }); const data = await response.json(); // Save the results to the state searchState.lastResults = data; searchState.lastQuery = searchQuery; // Get results section and clear it const resultsSection = document.getElementById('resultsSection'); resultsSection.innerHTML = ''; if (!response.ok) { throw new Error(data.error || 'Search failed'); } // Show results section showResults(); if (data.length === 0) { resultsSection.innerHTML = `
No papers found matching your search.
Category: ${escapeHtml(paper.category)}
Published: ${escapeHtml(paper.published)}
${escapeHtml(paper.abstract.substring(0, 200))}...
`; resultsGrid.appendChild(paperCard); }); resultsSection.appendChild(resultsGrid); } catch (error) { console.error('Search error:', error); document.getElementById('resultsSection').innerHTML = ` `; } finally { // Hide loading overlay document.getElementById('loadingOverlay').classList.add('hidden'); } } // Helper function to escape HTML and prevent XSS function escapeHtml(unsafe) { return unsafe .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } // Display Functions function displayResults(results) { const resultsSection = document.getElementById('resultsSection'); resultsSection.innerHTML = ''; const resultsGrid = document.createElement('div'); resultsGrid.className = 'results-grid'; results.forEach((paper, index) => { // Create paper card const paperCard = document.createElement('div'); paperCard.className = 'paper-card'; // Build the card HTML - removed save button paperCard.innerHTML = `${paper.abstract}
No search history yet
'; return; } historyGrid.innerHTML = ''; history.forEach(item => { const historyCard = document.createElement('div'); historyCard.className = 'history-card'; historyCard.innerHTML = `Category: ${escapeHtml(paper.category)}
Published: ${escapeHtml(paper.published)}
${escapeHtml(paper.abstract.substring(0, 200))}...
`; resultsGrid.appendChild(paperCard); }); resultsSection.appendChild(resultsGrid); } else { // If no previous results, redirect to home showHome(); } } // Add function to handle search history function updateSearchHistory(searchTerm) { try { let searches = JSON.parse(localStorage.getItem('recentSearches') || '[]'); // Add new search with timestamp searches.unshift({ term: searchTerm, date: new Date().toISOString() }); // Keep only the most recent 9 searches searches = searches.slice(0, 9); localStorage.setItem('recentSearches', JSON.stringify(searches)); displaySearchHistory(); } catch (error) { console.error('Error updating search history:', error); } } // Add function to display search history function displaySearchHistory() { const historyContainer = document.getElementById('searchHistory'); if (!historyContainer) return; try { const searches = JSON.parse(localStorage.getItem('recentSearches') || '[]'); historyContainer.innerHTML = `