@@ -19,19 +19,6 @@ function dedupeNewComments (newComments, comments) {
19
19
return newComments . filter ( id => ! existingIds . has ( id ) )
20
20
}
21
21
22
- // recursively collects all new comments from an item and its children
23
- // by respecting the depth limit, we avoid collecting new comments to inject in places
24
- // that are too deep in the tree
25
- function collectAllNewComments ( item , currentDepth = 1 ) {
26
- const allNewComments = [ ...( item . newComments || [ ] ) ]
27
- if ( item . comments ?. comments && currentDepth < ( COMMENT_DEPTH_LIMIT - 1 ) ) {
28
- for ( const comment of item . comments . comments ) {
29
- allNewComments . push ( ...collectAllNewComments ( comment , currentDepth + 1 ) )
30
- }
31
- }
32
- return allNewComments
33
- }
34
-
35
22
// prepares and creates a new comments fragment for injection into the cache
36
23
// returns a function that can be used to update an item's comments field
37
24
function prepareComments ( client , newComments ) {
@@ -114,26 +101,14 @@ export const ShowNewComments = ({ topLevel, sort, comments, itemId, item, setHas
114
101
// if item is provided, we're showing all new comments for a thread,
115
102
// otherwise we're showing new comments for a comment
116
103
const isThread = ! topLevel && item ?. path . split ( '.' ) . length === 2
117
- const allNewComments = useMemo ( ( ) => {
118
- if ( item ) {
119
- // TODO: well are we only collecting all new comments just for a fancy UI?
120
- // TODO2: also, we're not deduping new comments here, so we're showing duplicates
121
- return collectAllNewComments ( item , depth )
122
- }
123
- return dedupeNewComments ( newComments , comments )
124
- } , [ isThread , item , newComments , comments , depth ] )
104
+ const allNewComments = useMemo ( ( ) => dedupeNewComments ( newComments , comments ) , [ newComments , comments ] )
125
105
126
106
const showNewComments = useCallback ( ( ) => {
127
- if ( isThread ) {
128
- showAllNewCommentsRecursively ( client , item , depth )
107
+ if ( topLevel ) {
108
+ const payload = prepareComments ( client , allNewComments )
109
+ itemUpdateQuery ( client , itemId , sort , payload )
129
110
} else {
130
- if ( topLevel ) {
131
- // fetch the latest version of the comments from the cache by their ids
132
- const payload = prepareComments ( client , allNewComments )
133
- itemUpdateQuery ( client , itemId , sort , payload )
134
- } else {
135
- showAllNewCommentsRecursively ( client , item , depth )
136
- }
111
+ showAllNewCommentsRecursively ( client , item , depth )
137
112
}
138
113
setHasNewComments ( false )
139
114
} , [ client , itemId , allNewComments , topLevel , sort , item ] )
0 commit comments