From d4efacadc03ad5cfc8ccce23b56d2afd259deb29 Mon Sep 17 00:00:00 2001 From: soxa <6390896+Soxasora@users.noreply.github.com> Date: Fri, 18 Jul 2025 20:02:39 +0200 Subject: [PATCH] Fix count of viewed comments of a post (#2297) * fix: parse existingRootComments as Number to correctly add new comments to the count * return commentsViewedAt and commentsViewedNum parsed as Number * hotfix: commentsViewedNum accepts itemId directly * consistently receive itemId on new-comments functions that only uses IDs --- components/item-info.js | 2 +- components/item.js | 2 +- lib/new-comments.js | 19 +++++++++++-------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/components/item-info.js b/components/item-info.js index ec0a0958..63129fcd 100644 --- a/components/item-info.js +++ b/components/item-info.js @@ -110,7 +110,7 @@ export default function ItemInfo ({ } { - const viewedAt = commentsViewedAt(item) + const viewedAt = commentsViewedAt(item.id) if (viewedAt) { e.preventDefault() router.push( diff --git a/components/item.js b/components/item.js index 0ae1d0af..c1892ba4 100644 --- a/components/item.js +++ b/components/item.js @@ -29,7 +29,7 @@ import { useShowModal } from './modal' import { BoostHelp } from './adv-post-form' function onItemClick (e, router, item) { - const viewedAt = commentsViewedAt(item) + const viewedAt = commentsViewedAt(item.id) if (viewedAt) { e.preventDefault() if (e.ctrlKey || e.metaKey) { diff --git a/lib/new-comments.js b/lib/new-comments.js index 20ae8dd4..7578e806 100644 --- a/lib/new-comments.js +++ b/lib/new-comments.js @@ -8,20 +8,23 @@ export function commentsViewed (item) { } } -export function commentsViewedAfterComment (rootId, createdAt) { - window.localStorage.setItem(`${COMMENTS_VIEW_PREFIX}:${rootId}`, new Date(createdAt).getTime()) - const existingRootComments = window.localStorage.getItem(`${COMMENTS_NUM_PREFIX}:${rootId}`) || 0 - window.localStorage.setItem(`${COMMENTS_NUM_PREFIX}:${rootId}`, existingRootComments + 1) +export function commentsViewedAt (itemId) { + return Number(window.localStorage.getItem(`${COMMENTS_VIEW_PREFIX}:${itemId}`)) } -export function commentsViewedAt (item) { - return window.localStorage.getItem(`${COMMENTS_VIEW_PREFIX}:${item.id}`) +export function commentsViewedNum (itemId) { + return Number(window.localStorage.getItem(`${COMMENTS_NUM_PREFIX}:${itemId}`)) +} + +export function commentsViewedAfterComment (rootId, createdAt) { + window.localStorage.setItem(`${COMMENTS_VIEW_PREFIX}:${rootId}`, new Date(createdAt).getTime()) + window.localStorage.setItem(`${COMMENTS_NUM_PREFIX}:${rootId}`, commentsViewedNum(rootId) + 1) } export function newComments (item) { if (!item.parentId) { - const viewedAt = commentsViewedAt(item) - const viewNum = window.localStorage.getItem(`${COMMENTS_NUM_PREFIX}:${item.id}`) + const viewedAt = commentsViewedAt(item.id) + const viewNum = commentsViewedNum(item.id) if (viewedAt && viewNum) { return viewedAt < new Date(item.lastCommentAt).getTime() || viewNum < item.ncomments