diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..627d148 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-19 - Adding _searchStr derived field +**Learning:** We need to add `_searchStr` for performance so `renderPDFs` doesn't do multiple `toLowerCase` conversions for each record per keystroke. +**Action:** Add `prepareSearchIndex` and calculate runtime derived fields. diff --git a/script.js b/script.js index 22f399d..553d7e6 100644 --- a/script.js +++ b/script.js @@ -385,6 +385,35 @@ window.changeSemester = function (sem) { renderPDFs(); }; +function prepareSearchIndex(pdfs) { + const now = new Date(); + pdfs.forEach(pdf => { + const titleStr = typeof pdf.title === 'string' ? pdf.title : ''; + const descStr = typeof pdf.description === 'string' ? pdf.description : ''; + const catStr = typeof pdf.category === 'string' ? pdf.category : ''; + const authorStr = typeof pdf.author === 'string' ? pdf.author : ''; + + // Add pre-calculated derived fields for faster searching + pdf._searchStr = `${titleStr} ${descStr} ${catStr} ${authorStr}`.toLowerCase(); + + let uploadDateObj; + if (pdf.uploadDate && typeof pdf.uploadDate.toDate === 'function') { + uploadDateObj = pdf.uploadDate.toDate(); + } else if (pdf.uploadDate) { + uploadDateObj = new Date(pdf.uploadDate); + } else { + uploadDateObj = new Date(0); + } + + pdf._formattedDate = uploadDateObj.toLocaleDateString('en-US', { + year: 'numeric', month: 'short', day: 'numeric' + }); + + const timeDiff = now - uploadDateObj; + pdf._isNew = timeDiff < (7 * 24 * 60 * 60 * 1000); // 7 days + }); +} + async function syncClassSwitcher() { const classSelect = document.getElementById('classSelect'); if (!classSelect) return; @@ -454,6 +483,7 @@ async function loadPDFDatabase() { if (shouldUseCache) { pdfDatabase = cachedData; + prepareSearchIndex(pdfDatabase); // --- FIX: CALL THIS TO POPULATE UI --- syncClassSwitcher(); renderSemesterTabs(); @@ -477,8 +507,11 @@ async function loadPDFDatabase() { data: pdfDatabase })); + prepareSearchIndex(pdfDatabase); // --- FIX: CALL THIS TO POPULATE UI --- syncClassSwitcher(); + renderSemesterTabs(); + renderCategoryFilters(); renderPDFs(); hidePreloader(); @@ -905,26 +938,25 @@ function renderPDFs() { // Locate renderPDFs() in script.js and update the filter section const filteredPdfs = pdfDatabase.filter(pdf => { - const matchesSemester = pdf.semester === currentSemester; + if (pdf.semester !== currentSemester) return false; // NEW: Check if the PDF class matches the UI's current class selection // Note: If old documents don't have this field, they will be hidden. - const matchesClass = pdf.class === currentClass; + if (pdf.class !== currentClass) return false; - let matchesCategory = false; if (currentCategory === 'favorites') { - matchesCategory = favorites.includes(pdf.id); - } else { - matchesCategory = currentCategory === 'all' || pdf.category === currentCategory; + if (!favorites.includes(pdf.id)) return false; + } else if (currentCategory !== 'all') { + if (pdf.category !== currentCategory) return false; } - const matchesSearch = pdf.title.toLowerCase().includes(searchTerm) || - pdf.description.toLowerCase().includes(searchTerm) || - pdf.category.toLowerCase().includes(searchTerm) || - pdf.author.toLowerCase().includes(searchTerm); + // Optimize search using pre-calculated _searchStr + if (searchTerm && pdf._searchStr) { + if (!pdf._searchStr.includes(searchTerm)) return false; + } // Update return statement to include matchesClass - return matchesSemester && matchesClass && matchesCategory && matchesSearch; + return true; }); updatePDFCount(filteredPdfs.length); @@ -994,9 +1026,8 @@ function createPDFCard(pdf, favoritesList, index = 0, highlightRegex = null) { const heartIconClass = isFav ? 'fas' : 'far'; const btnActiveClass = isFav ? 'active' : ''; - const uploadDateObj = new Date(pdf.uploadDate); - const timeDiff = new Date() - uploadDateObj; - const isNew = timeDiff < (7 * 24 * 60 * 60 * 1000); // 7 days + // Use pre-calculated values + const isNew = pdf._isNew || false; const newBadgeHTML = isNew ? `NEW` @@ -1010,10 +1041,8 @@ function createPDFCard(pdf, favoritesList, index = 0, highlightRegex = null) { }; const categoryIcon = categoryIcons[pdf.category] || 'fa-file-pdf'; - // Formatting Date - const formattedDate = new Date(pdf.uploadDate).toLocaleDateString('en-US', { - year: 'numeric', month: 'short', day: 'numeric' - }); + // Use pre-calculated formatted date + const formattedDate = pdf._formattedDate || 'Unknown Date'; // Uses global escapeHtml() now