Fix non-thread live comments recursion logic (#2324)

* fix: recurse through existing comments only if we're in the newComments subtree or if it's the start of a thread

* cleanup: better comment

* cleanup: re-order parameters, comment touchup
This commit is contained in:
soxa 2025-07-24 19:28:26 +02:00 committed by GitHub
parent 160b04ceaa
commit 9e2c35c641
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -69,8 +69,8 @@ function prepareComments (data, client, newComments) {
} }
// traverses all new comments and their children // traverses all new comments and their children
// if we're showing all new comments of a thread, we also consider their existing children // if it's a thread, or we're in a new comment subtree, we also consider their existing children
function traverseNewComments (client, item, onLevel, threadComment = false, currentDepth = 1) { function traverseNewComments (client, item, onLevel, currentDepth, inSubtree) {
// if we're at the depth limit, stop traversing, we've reached the bottom of the visible thread // if we're at the depth limit, stop traversing, we've reached the bottom of the visible thread
if (currentDepth >= COMMENT_DEPTH_LIMIT) return if (currentDepth >= COMMENT_DEPTH_LIMIT) return
@ -87,23 +87,24 @@ function traverseNewComments (client, item, onLevel, threadComment = false, curr
// at each level, we can execute a callback passing the current item's new comments, depth and ID // at each level, we can execute a callback passing the current item's new comments, depth and ID
onLevel(freshNewComments, currentDepth, item.id) onLevel(freshNewComments, currentDepth, item.id)
// traverse the new comment's new comments and their children
for (const newComment of freshNewComments) { for (const newComment of freshNewComments) {
traverseNewComments(client, newComment, onLevel, threadComment, currentDepth + 1) traverseNewComments(client, newComment, onLevel, currentDepth + 1, true)
} }
} }
// if we're showing all new comments of a thread // check for new comments in existing children
// we consider every child comment recursively // only if we're in a new comment subtree, or it's a thread
if (threadComment && item.comments?.comments) { if (inSubtree && item.comments?.comments) {
for (const child of item.comments.comments) { for (const child of item.comments.comments) {
traverseNewComments(client, child, onLevel, threadComment, currentDepth + 1) traverseNewComments(client, child, onLevel, currentDepth + 1, true)
} }
} }
} }
// recursively processes and displays all new comments // recursively processes and displays all new comments
// handles comment injection at each level, respecting depth limits // handles comment injection at each level, respecting depth limits
function injectNewComments (client, item, currentDepth, sort, threadComment = false) { function injectNewComments (client, item, sort, currentDepth, thread) {
traverseNewComments(client, item, (newComments, depth, itemId) => { traverseNewComments(client, item, (newComments, depth, itemId) => {
if (newComments.length > 0) { if (newComments.length > 0) {
// traverseNewComments also passes the depth of the current item // traverseNewComments also passes the depth of the current item
@ -114,11 +115,11 @@ function injectNewComments (client, item, currentDepth, sort, threadComment = fa
commentUpdateFragment(client, itemId, (data) => prepareComments(data, client, newComments)) commentUpdateFragment(client, itemId, (data) => prepareComments(data, client, newComments))
} }
} }
}, threadComment, currentDepth) }, currentDepth, thread)
} }
// counts all new comments of an item // counts all new comments of an item
function countAllNewComments (client, item, thread = false, currentDepth = 1) { function countAllNewComments (client, item, currentDepth, thread) {
let newCommentsCount = 0 let newCommentsCount = 0
let threadChildren = false let threadChildren = false
@ -130,7 +131,7 @@ function countAllNewComments (client, item, thread = false, currentDepth = 1) {
if (depth > 1 && newComments.length > 0) { if (depth > 1 && newComments.length > 0) {
threadChildren = true threadChildren = true
} }
}, thread, currentDepth) }, currentDepth, thread)
return { newCommentsCount, threadChildren } return { newCommentsCount, threadChildren }
} }
@ -164,8 +165,8 @@ export function ShowNewComments ({ topLevel, item, sort, depth = 0 }) {
const thread = item.path?.split('.').length === 2 const thread = item.path?.split('.').length === 2
// recurse through all new comments and their children // recurse through all new comments and their children
// if the item is a thread, we consider every existing child comment // if the item is a thread, we also consider all of their existing children
const { newCommentsCount, threadChildren } = countAllNewComments(client, item, thread, depth) const { newCommentsCount, threadChildren } = countAllNewComments(client, item, depth, thread)
// only if the item is a thread and its children have new comments, we show "show all new comments" // only if the item is a thread and its children have new comments, we show "show all new comments"
const threadComment = thread && threadChildren const threadComment = thread && threadChildren
@ -173,7 +174,7 @@ export function ShowNewComments ({ topLevel, item, sort, depth = 0 }) {
const showNewComments = useCallback(() => { const showNewComments = useCallback(() => {
// a top level comment doesn't pass depth, we pass its default value of 0 to signify this // a top level comment doesn't pass depth, we pass its default value of 0 to signify this
// child comments are injected from the depth they're at // child comments are injected from the depth they're at
injectNewComments(client, item, depth, sort, threadComment) injectNewComments(client, item, sort, depth, threadComment)
}, [client, sort, item, depth]) }, [client, sort, item, depth])
const text = !threadComment const text = !threadComment