@@ -76,52 +76,36 @@ function collectAllNewComments (item) {
76
76
return allNewComments
77
77
}
78
78
79
- // TODO: merge this with ShowNewComments
80
- export function ShowAllNewComments ( { item, setHasNewComments } ) {
79
+ export function ShowNewComments ( { sort, comments, newComments = [ ] , itemId, item, setHasNewComments } ) {
81
80
const client = useApolloClient ( )
82
81
83
- const newComments = useMemo ( ( ) => collectAllNewComments ( item ) , [ item ] )
84
-
85
- const showNewComments = useCallback ( ( ) => {
86
- showAllNewCommentsRecursively ( client , item )
87
- setHasNewComments ( false )
88
- } , [ client , item ] )
89
-
90
- if ( newComments . length === 0 ) {
91
- return null
92
- }
93
-
94
- return (
95
- < div
96
- onClick = { showNewComments }
97
- className = 'd-flex align-items-center gap-2 px-3 pointer'
98
- >
99
- { newComments . length > 1
100
- ? `show all ${ newComments . length } new comments`
101
- : 'show new comment' }
102
- < div className = { styles . newCommentDot } />
103
- </ div >
104
- )
105
- }
106
-
107
- // ShowNewComments is a component that dedupes, refreshes and injects newComments into the comments field
108
- export function ShowNewComments ( { topLevel = false , comments, newComments = [ ] , itemId, sort, setHasNewComments } ) {
109
- const client = useApolloClient ( )
110
- const dedupedNewComments = useMemo ( ( ) => dedupeNewComments ( newComments , comments ) , [ newComments , comments ] )
82
+ const topLevel = ! ! sort
83
+ // if item is provided, we're showing all new comments for a thread,
84
+ // otherwise we're showing new comments for a comment
85
+ const isThread = ! ! item
86
+ const allNewComments = useMemo ( ( ) => {
87
+ if ( isThread ) {
88
+ return collectAllNewComments ( item )
89
+ }
90
+ return dedupeNewComments ( newComments , comments )
91
+ } , [ isThread , item , newComments , comments ] )
111
92
112
93
const showNewComments = useCallback ( ( ) => {
113
- // fetch the latest version of the comments from the cache by their ids
114
- const payload = prepareComments ( client , dedupedNewComments )
115
-
116
- if ( topLevel ) {
117
- itemUpdateQuery ( client , itemId , sort , payload )
94
+ if ( isThread ) {
95
+ showAllNewCommentsRecursively ( client , item )
118
96
} else {
119
- commentUpdateFragment ( client , itemId , payload )
97
+ // fetch the latest version of the comments from the cache by their ids
98
+ const payload = prepareComments ( client , allNewComments )
99
+ if ( topLevel ) {
100
+ itemUpdateQuery ( client , itemId , sort , payload )
101
+ } else {
102
+ commentUpdateFragment ( client , itemId , payload )
103
+ }
120
104
}
121
105
setHasNewComments ( false )
122
- } , [ client , itemId , dedupedNewComments , topLevel , sort ] )
106
+ } , [ client , itemId , allNewComments , topLevel , sort ] )
123
107
124
- if ( dedupedNewComments . length === 0 ) {
108
+ if ( allNewComments . length === 0 ) {
125
109
return null
126
110
}
127
111
@@ -130,8 +114,8 @@ export function ShowNewComments ({ topLevel = false, comments, newComments = [],
130
114
onClick = { showNewComments }
131
115
className = { `${ topLevel && `d-block fw-bold ${ styles . comment } pb-2` } d-flex align-items-center gap-2 px-3 pointer` }
132
116
>
133
- { dedupedNewComments . length > 1
134
- ? `${ dedupedNewComments . length } new comments`
117
+ { allNewComments . length > 1
118
+ ? `${ isThread ? 'show all ' : '' } ${ allNewComments . length } new comments`
135
119
: 'show new comment' }
136
120
< div className = { styles . newCommentDot } />
137
121
</ div >
0 commit comments