Skip to content

Commit d1abb2e

Browse files
committed
fix: avoid trying to show new comments even after the depth limit; todo: two recursive counts might be too much
1 parent ee18316 commit d1abb2e

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

components/comment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export default function Comment ({
262262
<Reply depth={depth + 1} item={item} replyOpen={replyOpen} onCancelQuote={cancelQuote} onQuoteReply={quoteReply} quote={quote}>
263263
{root.bounty && !bountyPaid && <PayBounty item={item} />}
264264
<div className='ms-auto'>
265-
<ShowNewComments comments={item.comments.comments} newComments={item.newComments} itemId={item.id} item={item} setHasNewComments={setHasNewComments} />
265+
<ShowNewComments comments={item.comments.comments} newComments={item.newComments} itemId={item.id} item={item} setHasNewComments={setHasNewComments} depth={depth} />
266266
</div>
267267
</Reply>}
268268
{children}

components/show-new-comments.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { COMMENT_WITH_NEW_RECURSIVE } from '../fragments/comments'
44
import styles from './comment.module.css'
55
import { itemUpdateQuery, commentUpdateFragment } from './use-live-comments'
66
import { updateAncestorsCommentCount } from '@/lib/comments'
7+
import { COMMENT_DEPTH_LIMIT } from '@/lib/constants'
78

89
function prepareComments (client, newComments) {
910
return (data) => {
@@ -42,7 +43,7 @@ function prepareComments (client, newComments) {
4243
}
4344
}
4445

45-
function showAllNewCommentsRecursively (client, item) {
46+
function showAllNewCommentsRecursively (client, item, currentDepth = 1) {
4647
// handle new comments at this item level
4748
if (item.newComments && item.newComments.length > 0) {
4849
const dedupedNewComments = dedupeNewComments(item.newComments, item.comments?.comments)
@@ -54,9 +55,9 @@ function showAllNewCommentsRecursively (client, item) {
5455
}
5556

5657
// recursively handle new comments in child comments
57-
if (item.comments?.comments) {
58+
if (item.comments?.comments && currentDepth < (COMMENT_DEPTH_LIMIT - 1)) {
5859
for (const childComment of item.comments.comments) {
59-
showAllNewCommentsRecursively(client, childComment)
60+
showAllNewCommentsRecursively(client, childComment, currentDepth + 1)
6061
}
6162
}
6263
}
@@ -66,33 +67,35 @@ function dedupeNewComments (newComments, comments) {
6667
return newComments.filter(id => !existingIds.has(id))
6768
}
6869

69-
function collectAllNewComments (item) {
70+
function collectAllNewComments (item, currentDepth = 1) {
7071
const allNewComments = [...(item.newComments || [])]
71-
if (item.comments?.comments) {
72+
if (item.comments?.comments && currentDepth < (COMMENT_DEPTH_LIMIT - 1)) {
7273
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))
7477
}
7578
}
7679
return allNewComments
7780
}
7881

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 }) {
8183
const client = useApolloClient()
8284

8385
// if item is provided, we're showing all new comments for a thread,
8486
// otherwise we're showing new comments for a comment
8587
const isThread = !topLevel && item?.path.split('.').length === 2
8688
const allNewComments = useMemo(() => {
8789
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)
8992
}
9093
return dedupeNewComments(newComments, comments)
91-
}, [isThread, item, newComments, comments])
94+
}, [isThread, item, newComments, comments, depth])
9295

9396
const showNewComments = useCallback(() => {
9497
if (isThread) {
95-
showAllNewCommentsRecursively(client, item)
98+
showAllNewCommentsRecursively(client, item, depth)
9699
} else {
97100
// fetch the latest version of the comments from the cache by their ids
98101
const payload = prepareComments(client, allNewComments)

0 commit comments

Comments
 (0)