@@ -4,6 +4,7 @@ import { COMMENT_WITH_NEW_RECURSIVE } from '../fragments/comments'
4
4
import styles from './comment.module.css'
5
5
import { itemUpdateQuery , commentUpdateFragment } from './use-live-comments'
6
6
import { updateAncestorsCommentCount } from '@/lib/comments'
7
+ import { COMMENT_DEPTH_LIMIT } from '@/lib/constants'
7
8
8
9
function prepareComments ( client , newComments ) {
9
10
return ( data ) => {
@@ -42,7 +43,7 @@ function prepareComments (client, newComments) {
42
43
}
43
44
}
44
45
45
- function showAllNewCommentsRecursively ( client , item ) {
46
+ function showAllNewCommentsRecursively ( client , item , currentDepth = 1 ) {
46
47
// handle new comments at this item level
47
48
if ( item . newComments && item . newComments . length > 0 ) {
48
49
const dedupedNewComments = dedupeNewComments ( item . newComments , item . comments ?. comments )
@@ -54,9 +55,9 @@ function showAllNewCommentsRecursively (client, item) {
54
55
}
55
56
56
57
// recursively handle new comments in child comments
57
- if ( item . comments ?. comments ) {
58
+ if ( item . comments ?. comments && currentDepth < ( COMMENT_DEPTH_LIMIT - 1 ) ) {
58
59
for ( const childComment of item . comments . comments ) {
59
- showAllNewCommentsRecursively ( client , childComment )
60
+ showAllNewCommentsRecursively ( client , childComment , currentDepth + 1 )
60
61
}
61
62
}
62
63
}
@@ -66,33 +67,35 @@ function dedupeNewComments (newComments, comments) {
66
67
return newComments . filter ( id => ! existingIds . has ( id ) )
67
68
}
68
69
69
- function collectAllNewComments ( item ) {
70
+ function collectAllNewComments ( item , currentDepth = 1 ) {
70
71
const allNewComments = [ ...( item . newComments || [ ] ) ]
71
- if ( item . comments ?. comments ) {
72
+ if ( item . comments ?. comments && currentDepth < ( COMMENT_DEPTH_LIMIT - 1 ) ) {
72
73
for ( const comment of item . comments . comments ) {
73
- allNewComments . push ( ...collectAllNewComments ( comment ) )
74
+ console . log ( 'comment' , comment )
75
+ console . log ( 'currentDepth' , currentDepth )
76
+ allNewComments . push ( ...collectAllNewComments ( comment , currentDepth + 1 ) )
74
77
}
75
78
}
76
79
return allNewComments
77
80
}
78
81
79
- // TODO: Fix bug where new comments out of depth are not shown
80
- export function ShowNewComments ( { topLevel, sort, comments, itemId, item, setHasNewComments, newComments = [ ] } ) {
82
+ export function ShowNewComments ( { topLevel, sort, comments, itemId, item, setHasNewComments, newComments = [ ] , depth = 1 } ) {
81
83
const client = useApolloClient ( )
82
84
83
85
// if item is provided, we're showing all new comments for a thread,
84
86
// otherwise we're showing new comments for a comment
85
87
const isThread = ! topLevel && item ?. path . split ( '.' ) . length === 2
86
88
const allNewComments = useMemo ( ( ) => {
87
89
if ( isThread ) {
88
- return collectAllNewComments ( item )
90
+ // TODO: well are we only collecting all new comments just for a fancy UI?
91
+ return collectAllNewComments ( item , depth )
89
92
}
90
93
return dedupeNewComments ( newComments , comments )
91
- } , [ isThread , item , newComments , comments ] )
94
+ } , [ isThread , item , newComments , comments , depth ] )
92
95
93
96
const showNewComments = useCallback ( ( ) => {
94
97
if ( isThread ) {
95
- showAllNewCommentsRecursively ( client , item )
98
+ showAllNewCommentsRecursively ( client , item , depth )
96
99
} else {
97
100
// fetch the latest version of the comments from the cache by their ids
98
101
const payload = prepareComments ( client , allNewComments )
0 commit comments