03/21r2Mar 27 08:38
@@ -959,25 +988,55 @@
});
});
- // Sort papers by hit count (most read first) and show counts
+ // === SORTING ===
+ let hitTotals = {};
+
+ function sortPapers(mode) {
+ const container = document.querySelector('.p')?.parentElement;
+ if (!container) return;
+ const papers = [...container.querySelectorAll('.p')];
+
+ papers.sort((a, b) => {
+ if (mode === 'hits') {
+ return (hitTotals[b.dataset.paperId] || 0) - (hitTotals[a.dataset.paperId] || 0);
+ } else if (mode === 'updated') {
+ const ta = a.dataset.updated ? new Date(a.dataset.updated).getTime() : 0;
+ const tb = b.dataset.updated ? new Date(b.dataset.updated).getTime() : 0;
+ return tb - ta;
+ } else if (mode === 'created') {
+ const ca = a.dataset.created || '0';
+ const cb = b.dataset.created || '0';
+ return cb.localeCompare(ca);
+ }
+ return 0;
+ });
+ for (const p of papers) container.appendChild(p);
+ }
+
+ // Sort bar clicks
+ document.querySelectorAll('.sort-btn').forEach(btn => {
+ btn.addEventListener('click', () => {
+ document.querySelectorAll('.sort-btn').forEach(b => b.classList.remove('active'));
+ btn.classList.add('active');
+ sortPapers(btn.dataset.sort);
+ });
+ });
+
+ // Fetch hit counts, inject into meta rows, then apply default sort
(async () => {
try {
const res = await fetch('https://aesthetic.computer/api/paper-hit');
if (!res.ok) return;
const { stats } = await res.json();
- // Aggregate hits per paper (combine pdf + cards formats)
- const totals = {};
for (const s of stats) {
const id = s._id.paper;
- totals[id] = (totals[id] || 0) + s.count;
+ hitTotals[id] = (hitTotals[id] || 0) + s.count;
}
const container = document.querySelector('.p')?.parentElement;
if (!container) return;
- const papers = [...container.querySelectorAll('.p')];
- // Inject hit counts into meta rows
- for (const p of papers) {
+ for (const p of container.querySelectorAll('.p')) {
const id = p.dataset.paperId;
- const count = totals[id] || 0;
+ const count = hitTotals[id] || 0;
const metaRow = p.querySelector('.meta-row');
if (metaRow && count > 0) {
const span = document.createElement('span');
@@ -987,13 +1046,8 @@
metaRow.prepend(span);
}
}
- // Sort descending by hits
- papers.sort((a, b) => {
- const ha = totals[a.dataset.paperId] || 0;
- const hb = totals[b.dataset.paperId] || 0;
- return hb - ha;
- });
- for (const p of papers) container.appendChild(p);
+ // Apply default sort (top hits)
+ sortPapers('hits');
} catch {}
})();