diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..e71483e --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-04-14 - Pre-calculate derived search fields for filter loops +**Learning:** High-frequency render/filter loops can be severely bottlenecked by inline string and date manipulation functions (like `.toLowerCase()` and `new Date().toLocaleDateString()`). +**Action:** Always pre-calculate and index derived properties (like search strings and formatted dates) immediately after data load and attach them to the objects, reducing the cost of per-render calculations and significantly improving list rendering/filtering performance. diff --git a/script.js b/script.js index 22f399d..e236fee 100644 --- a/script.js +++ b/script.js @@ -416,6 +416,32 @@ async function syncClassSwitcher() { renderSemesterTabs(); } + +// ⚡ Bolt: Pre-calculates derived properties to avoid expensive per-render calculations +function prepareSearchIndex(pdfs) { + const dateFormatter = new Intl.DateTimeFormat("en-US", { + year: "numeric", month: "short", day: "numeric" + }); + const now = new Date(); + const SEVEN_DAYS = 7 * 24 * 60 * 60 * 1000; + + pdfs.forEach(pdf => { + if (!pdf._searchStr) { + pdf._searchStr = `${pdf.title} ${pdf.description} ${pdf.category} ${pdf.author}`.toLowerCase(); + } + if (!pdf._formattedDate) { + const uploadDate = new Date(pdf.uploadDate); + if (!isNaN(uploadDate)) { + pdf._formattedDate = dateFormatter.format(uploadDate); + pdf._isNew = (now - uploadDate) < SEVEN_DAYS; + } else { + pdf._formattedDate = "Unknown Date"; + pdf._isNew = false; + } + } + }); +} + async function loadPDFDatabase() { if (isMaintenanceActive) return; @@ -454,6 +480,7 @@ async function loadPDFDatabase() { if (shouldUseCache) { pdfDatabase = cachedData; + prepareSearchIndex(pdfDatabase); // --- FIX: CALL THIS TO POPULATE UI --- syncClassSwitcher(); renderSemesterTabs(); @@ -477,6 +504,7 @@ async function loadPDFDatabase() { data: pdfDatabase })); + prepareSearchIndex(pdfDatabase); // --- FIX: CALL THIS TO POPULATE UI --- syncClassSwitcher(); renderPDFs(); @@ -905,26 +933,23 @@ 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); + if (!favorites.includes(pdf.id)) return false; } else { - matchesCategory = currentCategory === 'all' || pdf.category === currentCategory; + if (currentCategory !== 'all' && 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); + 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 +1019,7 @@ 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 + const isNew = pdf._isNew || false; const newBadgeHTML = isNew ? `NEW` @@ -1011,9 +1034,7 @@ 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' - }); + const formattedDate = pdf._formattedDate || 'Unknown Date'; // Uses global escapeHtml() now