@@ -42,14 +42,71 @@ function prepareComments (client, newComments) {
42
42
}
43
43
}
44
44
45
+ function showAllNewCommentsRecursively ( client , item ) {
46
+ // handle new comments at this item level
47
+ if ( item . newComments && item . newComments . length > 0 ) {
48
+ const dedupedNewComments = dedupeNewComments ( item . newComments , item . comments ?. comments )
49
+
50
+ if ( dedupedNewComments . length > 0 ) {
51
+ const payload = prepareComments ( client , dedupedNewComments )
52
+ commentUpdateFragment ( client , item . id , payload )
53
+ }
54
+ }
55
+
56
+ // recursively handle new comments in child comments
57
+ if ( item . comments ?. comments ) {
58
+ for ( const childComment of item . comments . comments ) {
59
+ showAllNewCommentsRecursively ( client , childComment )
60
+ }
61
+ }
62
+ }
63
+
64
+ function dedupeNewComments ( newComments , comments ) {
65
+ const existingIds = new Set ( comments . map ( c => c . id ) )
66
+ return newComments . filter ( id => ! existingIds . has ( id ) )
67
+ }
68
+
69
+ function collectAllNewComments ( item ) {
70
+ const allNewComments = [ ...( item . newComments || [ ] ) ]
71
+ if ( item . comments ?. comments ) {
72
+ for ( const comment of item . comments . comments ) {
73
+ allNewComments . push ( ...collectAllNewComments ( comment ) )
74
+ }
75
+ }
76
+ return allNewComments
77
+ }
78
+
79
+ // TODO: merge this with ShowNewComments
80
+ export function ShowAllNewComments ( { item } ) {
81
+ const client = useApolloClient ( )
82
+
83
+ const newComments = useMemo ( ( ) => collectAllNewComments ( item ) , [ item ] )
84
+
85
+ const showNewComments = useCallback ( ( ) => {
86
+ showAllNewCommentsRecursively ( client , item )
87
+ } , [ client , item ] )
88
+
89
+ if ( newComments . length === 0 ) {
90
+ return null
91
+ }
92
+
93
+ return (
94
+ < div
95
+ onClick = { showNewComments }
96
+ className = 'd-flex align-items-center gap-2 px-3 pointer'
97
+ >
98
+ { newComments . length > 1
99
+ ? `show all ${ newComments . length } new comments`
100
+ : 'show new comment' }
101
+ < div className = { styles . newCommentDot } />
102
+ </ div >
103
+ )
104
+ }
105
+
45
106
// ShowNewComments is a component that dedupes, refreshes and injects newComments into the comments field
46
107
export function ShowNewComments ( { topLevel = false , comments, newComments = [ ] , itemId, sort } ) {
47
108
const client = useApolloClient ( )
48
-
49
- const dedupedNewComments = useMemo ( ( ) => {
50
- const existingIds = new Set ( comments . map ( c => c . id ) )
51
- return newComments . filter ( id => ! existingIds . has ( id ) )
52
- } , [ newComments , comments ] )
109
+ const dedupedNewComments = useMemo ( ( ) => dedupeNewComments ( newComments , comments ) , [ newComments , comments ] )
53
110
54
111
const showNewComments = useCallback ( ( ) => {
55
112
// fetch the latest version of the comments from the cache by their ids
0 commit comments