limit sidebar comments to 20

This commit is contained in:
2026-07-14 23:59:54 +02:00
parent 1c7bf20d6d
commit d80aa3b96b

View File

@@ -584,6 +584,7 @@
const SIDEBAR_PAGE_LIMIT = 15; const SIDEBAR_PAGE_LIMIT = 15;
const SIDEBAR_INITIAL_LIMIT = 15; const SIDEBAR_INITIAL_LIMIT = 15;
const SIDEBAR_MAX_COMMENTS = 20;
const loadActivity = async (silent = false) => { const loadActivity = async (silent = false) => {
const container = document.getElementById('sidebar-activity-container'); const container = document.getElementById('sidebar-activity-container');
@@ -609,11 +610,12 @@
const data = await res.json(); const data = await res.json();
if (data.success && data.comments && data.comments.length > 0) { if (data.success && data.comments && data.comments.length > 0) {
window._sidebarActivityCache = data.comments.map(c => ({ window._sidebarActivityCache = data.comments.slice(0, SIDEBAR_MAX_COMMENTS).map(c => ({
...c, ...c,
body: c.content || c.body body: c.content || c.body
})); }));
hasMore = data.hasMore === true || data.comments.length === SIDEBAR_INITIAL_LIMIT; hasMore = (data.hasMore === true || data.comments.length === SIDEBAR_INITIAL_LIMIT)
&& window._sidebarActivityCache.length < SIDEBAR_MAX_COMMENTS;
renderFromCache(); renderFromCache();
// Also check after a delay to account for image/emoji loading shifts // Also check after a delay to account for image/emoji loading shifts
setTimeout(checkOverflow, 500); setTimeout(checkOverflow, 500);
@@ -662,17 +664,21 @@
if (data.success && data.comments && data.comments.length > 0) { if (data.success && data.comments && data.comments.length > 0) {
currentPage++; currentPage++;
hasMore = data.hasMore === true;
// Append only comments not already in the cache // Append only comments not already in the cache, up to the global cap
const remaining = SIDEBAR_MAX_COMMENTS - window._sidebarActivityCache.length;
const existingIds = new Set(window._sidebarActivityCache.map(c => String(c.id))); const existingIds = new Set(window._sidebarActivityCache.map(c => String(c.id)));
const newComments = data.comments.filter(c => !existingIds.has(String(c.id))).map(c => ({ const newComments = data.comments
...c, .filter(c => !existingIds.has(String(c.id)))
body: c.content || c.body .slice(0, remaining)
})); .map(c => ({ ...c, body: c.content || c.body }));
window._sidebarActivityCache.push(...newComments); window._sidebarActivityCache.push(...newComments);
// Stop loading more if the hard cap is reached
hasMore = data.hasMore === true
&& window._sidebarActivityCache.length < SIDEBAR_MAX_COMMENTS;
// Append new items to DOM // Append new items to DOM
let html = ''; let html = '';
newComments.forEach(c => { html += renderActivityItem(c); }); newComments.forEach(c => { html += renderActivityItem(c); });
@@ -696,6 +702,14 @@
// Auto-play converted GIF videos // Auto-play converted GIF videos
container.querySelectorAll('video.autoplay-gif').forEach(v => { v.autoplay = true; v.muted = true; v.play().catch(() => { v.addEventListener('canplay', () => v.play().catch(() => { }), { once: true }); }); }); container.querySelectorAll('video.autoplay-gif').forEach(v => { v.autoplay = true; v.muted = true; v.play().catch(() => { v.addEventListener('canplay', () => v.play().catch(() => { }), { once: true }); }); });
} }
if (!hasMore) {
// Show end-of-feed indicator
const end = document.createElement('div');
end.style.cssText = 'text-align:center;padding:8px 0;font-size:0.75em;color:#444;';
end.textContent = window.f0ckI18n?.sidebar_end_of_activity || '─ end of activity ─';
container.appendChild(end);
}
} else { } else {
hasMore = false; hasMore = false;
// Show end-of-feed indicator // Show end-of-feed indicator
@@ -722,7 +736,7 @@
return; return;
} }
// 2. Update cache (prepend, no hard cap — infinite scroll handles depth) // 2. Update cache (prepend, capped at SIDEBAR_MAX_COMMENTS)
const newItem = { const newItem = {
...data, ...data,
body: data.body || data.content, body: data.body || data.content,
@@ -730,6 +744,15 @@
}; };
window._sidebarActivityCache.unshift(newItem); window._sidebarActivityCache.unshift(newItem);
// Trim cache to the hard cap and remove the evicted DOM node (if any)
if (window._sidebarActivityCache.length > SIDEBAR_MAX_COMMENTS) {
const evicted = window._sidebarActivityCache.pop();
if (container && evicted) {
const evictedEl = document.getElementById('sc' + evicted.id);
if (evictedEl) evictedEl.remove();
}
}
// Update DOM if visible // Update DOM if visible
if (container) { if (container) {
const html = renderActivityItem(newItem); const html = renderActivityItem(newItem);