Skip to content

Commit 901e813

Browse files
committed
enhance: show all new comments recursively at every level but top level; cleanup: reduce complexity
enhancements: - while recursively showing new comments, get their updated version to inject also even newer comments cleanups: - don't show how many comments are there to inject, to avoid useless complexity - comments
1 parent 0fa97fe commit 901e813

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

components/show-new-comments.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ function collectAllNewComments (item, currentDepth = 1) {
3636
// returns a function that can be used to update an item's comments field
3737
function prepareComments (client, newComments) {
3838
return (data) => {
39-
// newComments is an array of comment ids that allows us
40-
// to read the latest newComments from the cache, guaranteeing that we're not reading stale data
39+
// newComments is an array of comment ids that allows usto read the latest newComments from the cache,
40+
// guaranteeing that we're not reading stale data
4141
const freshNewComments = newComments.map(id => {
4242
const fragment = client.cache.readFragment({
4343
id: `Item:${id}`,
@@ -49,6 +49,7 @@ function prepareComments (client, newComments) {
4949
return null
5050
}
5151

52+
// marking it as injected so that the new comment can be outlined
5253
return { ...fragment, injected: true }
5354
}).filter(Boolean)
5455

@@ -92,9 +93,17 @@ function showAllNewCommentsRecursively (client, item, currentDepth = 1) {
9293
}
9394
}
9495

96+
// read the updated item from the cache
97+
// this is necessary because the item may have been updated by the time we get to the child comments
98+
const updatedItem = client.cache.readFragment({
99+
id: `Item:${item.id}`,
100+
fragment: COMMENT_WITH_NEW_RECURSIVE,
101+
fragmentName: 'CommentWithNewRecursive'
102+
})
103+
95104
// recursively handle new comments in child comments
96-
if (item.comments?.comments && currentDepth < (COMMENT_DEPTH_LIMIT - 1)) {
97-
for (const childComment of item.comments.comments) {
105+
if (updatedItem.comments?.comments && currentDepth < (COMMENT_DEPTH_LIMIT - 1)) {
106+
for (const childComment of updatedItem.comments.comments) {
98107
showAllNewCommentsRecursively(client, childComment, currentDepth + 1)
99108
}
100109
}
@@ -106,7 +115,7 @@ export const ShowNewComments = ({ topLevel, sort, comments, itemId, item, setHas
106115
// otherwise we're showing new comments for a comment
107116
const isThread = !topLevel && item?.path.split('.').length === 2
108117
const allNewComments = useMemo(() => {
109-
if (isThread) {
118+
if (item) {
110119
// TODO: well are we only collecting all new comments just for a fancy UI?
111120
// TODO2: also, we're not deduping new comments here, so we're showing duplicates
112121
return collectAllNewComments(item, depth)
@@ -118,16 +127,16 @@ export const ShowNewComments = ({ topLevel, sort, comments, itemId, item, setHas
118127
if (isThread) {
119128
showAllNewCommentsRecursively(client, item, depth)
120129
} else {
121-
// fetch the latest version of the comments from the cache by their ids
122-
const payload = prepareComments(client, allNewComments)
123130
if (topLevel) {
131+
// fetch the latest version of the comments from the cache by their ids
132+
const payload = prepareComments(client, allNewComments)
124133
itemUpdateQuery(client, itemId, sort, payload)
125134
} else {
126-
commentUpdateFragment(client, itemId, payload)
135+
showAllNewCommentsRecursively(client, item, depth)
127136
}
128137
}
129138
setHasNewComments(false)
130-
}, [client, itemId, allNewComments, topLevel, sort])
139+
}, [client, itemId, allNewComments, topLevel, sort, item])
131140

132141
if (allNewComments.length === 0) {
133142
return null
@@ -138,9 +147,7 @@ export const ShowNewComments = ({ topLevel, sort, comments, itemId, item, setHas
138147
onClick={showNewComments}
139148
className={`${topLevel && `d-block fw-bold ${styles.comment} pb-2`} d-flex align-items-center gap-2 px-3 pointer`}
140149
>
141-
{allNewComments.length > 1
142-
? `${isThread ? 'show all ' : ''}${allNewComments.length} new comments`
143-
: 'show new comment'}
150+
show {isThread ? 'all' : ''} new comments
144151
<div className={styles.newCommentDot} />
145152
</div>
146153
)

0 commit comments

Comments
 (0)