Skip to content

Commit 0c2d6f8

Browse files
committed
fix: dedupe newComments before displaying ShowNewComments to avoid false positives
1 parent 9e14ddb commit 0c2d6f8

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

components/comment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export default function Comment ({
263263
{root.bounty && !bountyPaid && <PayBounty item={item} />}
264264
<div className='ms-auto'>
265265
{item.newComments?.length > 0 && (
266-
<ShowNewComments newComments={item.newComments} itemId={item.id} />
266+
<ShowNewComments comments={item.comments.comments} newComments={item.newComments} itemId={item.id} />
267267
)}
268268
</div>
269269
</Reply>}

components/comments.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export default function Comments ({
9393
/>
9494
: null}
9595
{newComments?.length > 0 && (
96-
<ShowNewComments topLevel newComments={newComments} itemId={parentId} sort={router.query.sort} />
96+
<ShowNewComments topLevel comments={comments} newComments={newComments} itemId={parentId} sort={router.query.sort} />
9797
)}
9898
{pins.map(item => (
9999
<Fragment key={item.id}>

components/show-new-comments.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback } from 'react'
1+
import { useCallback, useMemo } from 'react'
22
import { useApolloClient } from '@apollo/client'
33
import { COMMENT_WITH_NEW } from '../fragments/comments'
44
import styles from './comment.module.css'
@@ -36,25 +36,35 @@ function prepareComments (client, newComments) {
3636
}
3737

3838
// ShowNewComments is a component that dedupes, refreshes and injects newComments into the comments field
39-
export function ShowNewComments ({ newComments = [], itemId, topLevel = false, sort }) {
39+
export function ShowNewComments ({ topLevel = false, comments, newComments = [], itemId, sort }) {
4040
const client = useApolloClient()
4141

42+
const dedupedNewComments = useMemo(() => {
43+
return newComments.filter(c => !comments.some(cc => cc.id === c.id))
44+
}, [newComments, comments])
45+
4246
const showNewComments = useCallback(() => {
43-
const payload = prepareComments(client, newComments)
47+
const payload = prepareComments(client, dedupedNewComments)
4448

4549
if (topLevel) {
4650
itemUpdateQuery(client, itemId, sort, payload)
4751
} else {
4852
commentUpdateFragment(client, itemId, payload)
4953
}
50-
}, [client, itemId, newComments, topLevel, sort])
54+
}, [client, itemId, dedupedNewComments, topLevel, sort])
55+
56+
if (dedupedNewComments.length === 0) {
57+
return null
58+
}
5159

5260
return (
5361
<div
5462
onClick={showNewComments}
5563
className={`${topLevel && `d-block fw-bold ${styles.comment} pb-2`} d-flex align-items-center gap-2 px-3 pointer`}
5664
>
57-
{newComments.length > 1 ? `${newComments.length} new comments` : 'show new comment'}
65+
{dedupedNewComments.length > 1
66+
? `${dedupedNewComments.length} new comments`
67+
: 'show new comment'}
5868
<div className={styles.newCommentDot} />
5969
</div>
6070
)

0 commit comments

Comments
 (0)