@@ -36,8 +36,8 @@ function collectAllNewComments (item, currentDepth = 1) {
36
36
// returns a function that can be used to update an item's comments field
37
37
function prepareComments ( client , newComments ) {
38
38
return ( data ) => {
39
- // newComments is an array of comment ids that allows us
40
- // to read the latest newComments from the cache, guaranteeing that we're not reading stale data
39
+ // newComments is an array of comment ids that allows usto read the latest newComments from the cache,
40
+ // guaranteeing that we're not reading stale data
41
41
const freshNewComments = newComments . map ( id => {
42
42
const fragment = client . cache . readFragment ( {
43
43
id : `Item:${ id } ` ,
@@ -49,6 +49,7 @@ function prepareComments (client, newComments) {
49
49
return null
50
50
}
51
51
52
+ // marking it as injected so that the new comment can be outlined
52
53
return { ...fragment , injected : true }
53
54
} ) . filter ( Boolean )
54
55
@@ -92,9 +93,17 @@ function showAllNewCommentsRecursively (client, item, currentDepth = 1) {
92
93
}
93
94
}
94
95
96
+ // read the updated item from the cache
97
+ // this is necessary because the item may have been updated by the time we get to the child comments
98
+ const updatedItem = client . cache . readFragment ( {
99
+ id : `Item:${ item . id } ` ,
100
+ fragment : COMMENT_WITH_NEW_RECURSIVE ,
101
+ fragmentName : 'CommentWithNewRecursive'
102
+ } )
103
+
95
104
// recursively handle new comments in child comments
96
- if ( item . comments ?. comments && currentDepth < ( COMMENT_DEPTH_LIMIT - 1 ) ) {
97
- for ( const childComment of item . comments . comments ) {
105
+ if ( updatedItem . comments ?. comments && currentDepth < ( COMMENT_DEPTH_LIMIT - 1 ) ) {
106
+ for ( const childComment of updatedItem . comments . comments ) {
98
107
showAllNewCommentsRecursively ( client , childComment , currentDepth + 1 )
99
108
}
100
109
}
@@ -106,7 +115,7 @@ export const ShowNewComments = ({ topLevel, sort, comments, itemId, item, setHas
106
115
// otherwise we're showing new comments for a comment
107
116
const isThread = ! topLevel && item ?. path . split ( '.' ) . length === 2
108
117
const allNewComments = useMemo ( ( ) => {
109
- if ( isThread ) {
118
+ if ( item ) {
110
119
// TODO: well are we only collecting all new comments just for a fancy UI?
111
120
// TODO2: also, we're not deduping new comments here, so we're showing duplicates
112
121
return collectAllNewComments ( item , depth )
@@ -118,16 +127,16 @@ export const ShowNewComments = ({ topLevel, sort, comments, itemId, item, setHas
118
127
if ( isThread ) {
119
128
showAllNewCommentsRecursively ( client , item , depth )
120
129
} else {
121
- // fetch the latest version of the comments from the cache by their ids
122
- const payload = prepareComments ( client , allNewComments )
123
130
if ( topLevel ) {
131
+ // fetch the latest version of the comments from the cache by their ids
132
+ const payload = prepareComments ( client , allNewComments )
124
133
itemUpdateQuery ( client , itemId , sort , payload )
125
134
} else {
126
- commentUpdateFragment ( client , itemId , payload )
135
+ showAllNewCommentsRecursively ( client , item , depth )
127
136
}
128
137
}
129
138
setHasNewComments ( false )
130
- } , [ client , itemId , allNewComments , topLevel , sort ] )
139
+ } , [ client , itemId , allNewComments , topLevel , sort , item ] )
131
140
132
141
if ( allNewComments . length === 0 ) {
133
142
return null
@@ -138,9 +147,7 @@ export const ShowNewComments = ({ topLevel, sort, comments, itemId, item, setHas
138
147
onClick = { showNewComments }
139
148
className = { `${ topLevel && `d-block fw-bold ${ styles . comment } pb-2` } d-flex align-items-center gap-2 px-3 pointer` }
140
149
>
141
- { allNewComments . length > 1
142
- ? `${ isThread ? 'show all ' : '' } ${ allNewComments . length } new comments`
143
- : 'show new comment' }
150
+ show { isThread ? 'all' : '' } new comments
144
151
< div className = { styles . newCommentDot } />
145
152
</ div >
146
153
)
0 commit comments