From a3c5a33bc473ee6a236d08b507aaa2ecd79e777f Mon Sep 17 00:00:00 2001 From: soxa <6390896+Soxasora@users.noreply.github.com> Date: Sun, 17 Aug 2025 18:43:19 +0200 Subject: [PATCH] Fix paginated comments button not showing on comment injection/creation (#2426) * fix: also tick nDirectComments to respect pagination logic * update nDirectComments only for the provided parentId * explicit type conversion --- components/reply.js | 2 +- components/use-live-comments.js | 4 +++- lib/comments.js | 9 ++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/components/reply.js b/components/reply.js index e89816c7..1d3ab77c 100644 --- a/components/reply.js +++ b/components/reply.js @@ -83,7 +83,7 @@ export default forwardRef(function Reply ({ const ancestors = item.path.split('.') // update all ancestors - updateAncestorsCommentCount(cache, ancestors, 1) + updateAncestorsCommentCount(cache, ancestors, 1, parentId) // so that we don't see indicator for our own comments, we record this comments as the latest time // but we also have record num comments, in case someone else commented when we did diff --git a/components/use-live-comments.js b/components/use-live-comments.js index 8016279b..59fa449c 100644 --- a/components/use-live-comments.js +++ b/components/use-live-comments.js @@ -42,6 +42,7 @@ function prepareComments (item, cache, newComment) { ? { ...item, ncomments: item.ncomments + totalNComments, + nDirectComments: item.nDirectComments + 1, comments: { ...item.comments, comments: [injectedComment, ...item.comments.comments] @@ -50,7 +51,8 @@ function prepareComments (item, cache, newComment) { // when the fragment doesn't have a comments field, we just update stats fields : { ...item, - ncomments: item.ncomments + totalNComments + ncomments: item.ncomments + totalNComments, + nDirectComments: item.nDirectComments + 1 } return payload diff --git a/lib/comments.js b/lib/comments.js index dbe3e9ec..15e7a80f 100644 --- a/lib/comments.js +++ b/lib/comments.js @@ -2,7 +2,7 @@ import { COMMENT_WITH_NEW_RECURSIVE, COMMENT_WITH_NEW_LIMITED, COMMENT_WITH_NEW_ import { ITEM_FULL } from '../fragments/items' // updates the ncomments field of all ancestors of an item/comment in the cache -export function updateAncestorsCommentCount (cache, ancestors, increment) { +export function updateAncestorsCommentCount (cache, ancestors, increment, parentId) { // update all ancestors ancestors.forEach(id => { cache.modify({ @@ -10,6 +10,13 @@ export function updateAncestorsCommentCount (cache, ancestors, increment) { fields: { ncomments (existingNComments = 0) { return existingNComments + increment + }, + nDirectComments (existingNDirectComments = 0) { + // only increment nDirectComments for the immediate parent + if (parentId && Number(id) === Number(parentId)) { + return existingNDirectComments + 1 + } + return existingNDirectComments } }, optimistic: true