Skip to content

Commit 6fba7fb

Browse files
committed
cleanup: remove unused nested comment counting, simpler topLevel/nested logic
1 parent 901e813 commit 6fba7fb

File tree

1 file changed

+5
-30
lines changed

1 file changed

+5
-30
lines changed

components/show-new-comments.js

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@ function dedupeNewComments (newComments, comments) {
1919
return newComments.filter(id => !existingIds.has(id))
2020
}
2121

22-
// recursively collects all new comments from an item and its children
23-
// by respecting the depth limit, we avoid collecting new comments to inject in places
24-
// that are too deep in the tree
25-
function collectAllNewComments (item, currentDepth = 1) {
26-
const allNewComments = [...(item.newComments || [])]
27-
if (item.comments?.comments && currentDepth < (COMMENT_DEPTH_LIMIT - 1)) {
28-
for (const comment of item.comments.comments) {
29-
allNewComments.push(...collectAllNewComments(comment, currentDepth + 1))
30-
}
31-
}
32-
return allNewComments
33-
}
34-
3522
// prepares and creates a new comments fragment for injection into the cache
3623
// returns a function that can be used to update an item's comments field
3724
function prepareComments (client, newComments) {
@@ -114,26 +101,14 @@ export const ShowNewComments = ({ topLevel, sort, comments, itemId, item, setHas
114101
// if item is provided, we're showing all new comments for a thread,
115102
// otherwise we're showing new comments for a comment
116103
const isThread = !topLevel && item?.path.split('.').length === 2
117-
const allNewComments = useMemo(() => {
118-
if (item) {
119-
// TODO: well are we only collecting all new comments just for a fancy UI?
120-
// TODO2: also, we're not deduping new comments here, so we're showing duplicates
121-
return collectAllNewComments(item, depth)
122-
}
123-
return dedupeNewComments(newComments, comments)
124-
}, [isThread, item, newComments, comments, depth])
104+
const allNewComments = useMemo(() => dedupeNewComments(newComments, comments), [newComments, comments])
125105

126106
const showNewComments = useCallback(() => {
127-
if (isThread) {
128-
showAllNewCommentsRecursively(client, item, depth)
107+
if (topLevel) {
108+
const payload = prepareComments(client, allNewComments)
109+
itemUpdateQuery(client, itemId, sort, payload)
129110
} else {
130-
if (topLevel) {
131-
// fetch the latest version of the comments from the cache by their ids
132-
const payload = prepareComments(client, allNewComments)
133-
itemUpdateQuery(client, itemId, sort, payload)
134-
} else {
135-
showAllNewCommentsRecursively(client, item, depth)
136-
}
111+
showAllNewCommentsRecursively(client, item, depth)
137112
}
138113
setHasNewComments(false)
139114
}, [client, itemId, allNewComments, topLevel, sort, item])

0 commit comments

Comments
 (0)