Skip to content

Commit f710457

Browse files
committed
fix: while showing new comments, also update ncomments for UI and pagination
1 parent a29f9e3 commit f710457

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

components/use-live-comments.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,6 @@ function mergeNewComment (item, newComment) {
111111
return { ...item, newComments: [...existingNewComments, newComment] }
112112
}
113113

114-
// even though we already deduplicated comments during the newComments merge
115-
// refetches, client-side navigation, etc. can cause duplicates to appear
116-
// we'll make sure to deduplicate them here, by id
117-
function dedupeComments (existing = [], incoming = []) {
118-
const existingIds = new Set(existing.map(c => c.id))
119-
return [...incoming.filter(c => !existingIds.has(c.id)), ...existing]
120-
}
121-
122114
function getLatestCommentCreatedAt (comments, latest) {
123115
if (comments.length === 0) return latest
124116

@@ -151,9 +143,13 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
151143
return fragment || c
152144
})
153145

146+
// deduplicate the fresh new comments with the existing comments
147+
const dedupedComments = dedupeComments(data.comments.comments, freshNewComments)
148+
154149
return {
155150
...data,
156-
comments: { ...data.comments, comments: dedupeComments(data.comments.comments, freshNewComments) },
151+
comments: { ...data.comments, comments: dedupedComments },
152+
ncomments: data.ncomments + (dedupedComments.length || 0),
157153
newComments: []
158154
}
159155
}
@@ -170,8 +166,16 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
170166
onClick={showNewComments}
171167
className={`${topLevel && `d-block fw-bold ${styles.comment} pb-2`} d-flex align-items-center gap-2 px-3 pointer`}
172168
>
173-
{newComments.length > 0 ? `${newComments.length} new comments` : 'new comment'}
169+
{newComments.length > 1 ? `${newComments.length} new comments` : 'show new comment'}
174170
<div className={styles.newCommentDot} />
175171
</div>
176172
)
177173
}
174+
175+
// even though we already deduplicated comments during the newComments merge,
176+
// refetches, client-side navigation, etc. can cause duplicates to appear,
177+
// so we'll make sure to deduplicate them here, by id
178+
function dedupeComments (existing = [], incoming = []) {
179+
const existingIds = new Set(existing.map(c => c.id))
180+
return [...incoming.filter(c => !existingIds.has(c.id)), ...existing]
181+
}

0 commit comments

Comments
 (0)