@@ -2,7 +2,7 @@ import { useQuery, useApolloClient } from '@apollo/client'
2
2
import { SSR } from '../lib/constants'
3
3
import { GET_NEW_COMMENTS , COMMENT_WITH_NEW } from '../fragments/comments'
4
4
import { ITEM_FULL } from '../fragments/items'
5
- import { useEffect , useState } from 'react'
5
+ import { useCallback , useEffect , useState } from 'react'
6
6
import styles from './comment.module.css'
7
7
8
8
const POLL_INTERVAL = 1000 * 10 // 10 seconds
@@ -61,12 +61,14 @@ export default function useLiveComments (rootId, after, sort) {
61
61
cacheNewComments ( client , rootId , data . newComments . comments , sort )
62
62
// check new comments created after the latest new comment
63
63
setLatest ( prevLatest => getLatestCommentCreatedAt ( data . newComments . comments , prevLatest ) )
64
- } , [ data , client , rootId ] )
64
+ } , [ data , client , rootId , sort ] )
65
65
66
66
return { polling, setPolling }
67
67
}
68
68
69
69
function cacheNewComments ( client , rootId , newComments , sort ) {
70
+ // update the item with the new comments
71
+ // if the comment is a top level comment, update the item
70
72
for ( const newComment of newComments ) {
71
73
const { parentId } = newComment
72
74
const topLevel = Number ( parentId ) === Number ( rootId )
@@ -79,7 +81,7 @@ function cacheNewComments (client, rootId, newComments, sort) {
79
81
} , ( data ) => {
80
82
if ( ! data ) return data
81
83
// we return the entire item, not just the newComments
82
- return { item : mergeNewComments ( data ?. item , newComment ) }
84
+ return { item : mergeNewComment ( data ?. item , newComment ) }
83
85
} )
84
86
} else {
85
87
// if the comment is a reply, update the parent comment
@@ -90,13 +92,15 @@ function cacheNewComments (client, rootId, newComments, sort) {
90
92
} , ( data ) => {
91
93
if ( ! data ) return data
92
94
// here we return the parent comment with the new comment added
93
- return mergeNewComments ( data , newComment )
95
+ return mergeNewComment ( data , newComment )
94
96
} )
95
97
}
96
98
}
97
99
}
98
100
99
- function mergeNewComments ( item , newComment ) {
101
+ // merge new comment into item's newComments
102
+ // if the new comment is already in item's newComments or existing comments, do nothing
103
+ function mergeNewComment ( item , newComment ) {
100
104
const existingNewComments = item . newComments || [ ]
101
105
const existingComments = item . comments ?. comments || [ ]
102
106
@@ -122,7 +126,7 @@ function getLatestCommentCreatedAt (comments, latest) {
122
126
export function ShowNewComments ( { newComments = [ ] , itemId, topLevel = false , sort } ) {
123
127
const client = useApolloClient ( )
124
128
125
- const showNewComments = ( ) => {
129
+ const showNewComments = useCallback ( ( ) => {
126
130
if ( topLevel ) {
127
131
client . cache . updateQuery ( {
128
132
query : ITEM_FULL ,
@@ -134,7 +138,7 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
134
138
return {
135
139
item : {
136
140
...item ,
137
- comments : dedupeComments ( item . comments , newComments ) ,
141
+ comments : injectComments ( item . comments , newComments ) ,
138
142
newComments : [ ]
139
143
}
140
144
}
@@ -149,14 +153,16 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
149
153
150
154
return {
151
155
...data ,
152
- comments : dedupeComments ( data . comments , newComments ) ,
156
+ comments : injectComments ( data . comments , newComments ) ,
153
157
newComments : [ ]
154
158
}
155
159
} )
156
160
}
157
- }
161
+ } , [ client , itemId , newComments , sort , topLevel ] )
158
162
159
- const dedupeComments = ( existingComments = [ ] , newComments = [ ] ) => {
163
+ // inject new comments into existing comments
164
+ // if the new comment is already in existing comments, do nothing
165
+ const injectComments = ( existingComments = [ ] , newComments = [ ] ) => {
160
166
const existingIds = new Set ( existingComments . comments ?. map ( c => c . id ) )
161
167
const filteredNew = newComments . filter ( c => ! existingIds . has ( c . id ) )
162
168
return {
0 commit comments