|
1 |
| -import { useCallback } from 'react' |
| 1 | +import { useCallback, useMemo } from 'react' |
2 | 2 | import { useApolloClient } from '@apollo/client'
|
3 | 3 | import { COMMENT_WITH_NEW } from '../fragments/comments'
|
4 | 4 | import styles from './comment.module.css'
|
@@ -36,25 +36,35 @@ function prepareComments (client, newComments) {
|
36 | 36 | }
|
37 | 37 |
|
38 | 38 | // 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 }) { |
40 | 40 | const client = useApolloClient()
|
41 | 41 |
|
| 42 | + const dedupedNewComments = useMemo(() => { |
| 43 | + return newComments.filter(c => !comments.some(cc => cc.id === c.id)) |
| 44 | + }, [newComments, comments]) |
| 45 | + |
42 | 46 | const showNewComments = useCallback(() => {
|
43 |
| - const payload = prepareComments(client, newComments) |
| 47 | + const payload = prepareComments(client, dedupedNewComments) |
44 | 48 |
|
45 | 49 | if (topLevel) {
|
46 | 50 | itemUpdateQuery(client, itemId, sort, payload)
|
47 | 51 | } else {
|
48 | 52 | commentUpdateFragment(client, itemId, payload)
|
49 | 53 | }
|
50 |
| - }, [client, itemId, newComments, topLevel, sort]) |
| 54 | + }, [client, itemId, dedupedNewComments, topLevel, sort]) |
| 55 | + |
| 56 | + if (dedupedNewComments.length === 0) { |
| 57 | + return null |
| 58 | + } |
51 | 59 |
|
52 | 60 | return (
|
53 | 61 | <div
|
54 | 62 | onClick={showNewComments}
|
55 | 63 | className={`${topLevel && `d-block fw-bold ${styles.comment} pb-2`} d-flex align-items-center gap-2 px-3 pointer`}
|
56 | 64 | >
|
57 |
| - {newComments.length > 1 ? `${newComments.length} new comments` : 'show new comment'} |
| 65 | + {dedupedNewComments.length > 1 |
| 66 | + ? `${dedupedNewComments.length} new comments` |
| 67 | + : 'show new comment'} |
58 | 68 | <div className={styles.newCommentDot} />
|
59 | 69 | </div>
|
60 | 70 | )
|
|
0 commit comments