Skip to content

Commit 1342317

Browse files
authored
Fix new comments navigator duplicate counts (#2607)
* fix: navigator dedupes by item.id instead of ref comparisons * fix: update comment count on comment unmount and on invalid refs being passed
1 parent 625ddff commit 1342317

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

components/comment.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ export default function Comment ({
187187
}
188188

189189
navigator.trackNewComment(ref, itemCreatedAt)
190+
191+
return () => {
192+
navigator.untrackNewComment(ref, { includeDescendants: true })
193+
}
190194
}, [item.id, root.lastCommentAt, root.meCommentsViewedAt])
191195

192196
const bottomedOut = depth === COMMENT_DEPTH_LIMIT || (item.comments?.comments.length === 0 && item.nDirectComments > 0)

components/use-comments-navigator.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export function useCommentsNavigator () {
4141
// prevent multiple updates in the same frame
4242
frameRef.current = true
4343
window.requestAnimationFrame(() => {
44+
// filter out disconnected refs before counting
45+
commentRefs.current = commentRefs.current.filter(item =>
46+
item.ref?.current?.isConnected
47+
)
4448
const next = commentRefs.current.length
4549
// transition to the new comment count
4650
startTransition?.(() => setCommentCount(next))
@@ -93,7 +97,11 @@ export function useCommentsNavigator () {
9397
const { includeDescendants = false, clearOutline = false } = options
9498

9599
const refNode = commentRef.current
96-
if (!refNode) return
100+
if (!refNode) {
101+
// update the comment count, the ref may be disconnected
102+
throttleCountUpdate()
103+
return
104+
}
97105

98106
const toRemove = commentRefs.current.filter(item => {
99107
const node = item?.ref?.current
@@ -117,8 +125,8 @@ export function useCommentsNavigator () {
117125

118126
if (toRemove.length) {
119127
commentRefs.current = commentRefs.current.filter(item => !toRemove.includes(item))
120-
throttleCountUpdate()
121128
}
129+
throttleCountUpdate()
122130
}, [throttleCountUpdate])
123131

124132
// scroll to the next new comment
@@ -128,7 +136,11 @@ export function useCommentsNavigator () {
128136

129137
const ref = list[0]?.ref
130138
const node = ref?.current
131-
if (!node) return
139+
if (!node) {
140+
// update the comment count, the ref may be disconnected
141+
throttleCountUpdate()
142+
return
143+
}
132144

133145
// smoothly scroll to the start of the comment
134146
node.scrollIntoView({ behavior: 'smooth', block: 'start' })
@@ -148,7 +160,7 @@ export function useCommentsNavigator () {
148160

149161
// if we reached the end, reset the navigator
150162
if (list.length === 1) clearCommentRefs()
151-
}, [clearCommentRefs, untrackNewComment])
163+
}, [clearCommentRefs, untrackNewComment, throttleCountUpdate])
152164

153165
// create the navigator object once
154166
if (!navigatorRef.current) {

0 commit comments

Comments
 (0)