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
This commit is contained in:
soxa 2025-08-17 18:43:19 +02:00 committed by GitHub
parent baa92c0ddc
commit a3c5a33bc4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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