Skip to content

Commit a2b6841

Browse files
committed
cleanup: better naming; fix: usecallback on live comments component; fix leak on useEffect because of missing sort
1 parent 274927d commit a2b6841

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

components/use-live-comments.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useQuery, useApolloClient } from '@apollo/client'
22
import { SSR } from '../lib/constants'
33
import { GET_NEW_COMMENTS, COMMENT_WITH_NEW } from '../fragments/comments'
44
import { ITEM_FULL } from '../fragments/items'
5-
import { useEffect, useState } from 'react'
5+
import { useCallback, useEffect, useState } from 'react'
66
import styles from './comment.module.css'
77

88
const POLL_INTERVAL = 1000 * 10 // 10 seconds
@@ -61,12 +61,14 @@ export default function useLiveComments (rootId, after, sort) {
6161
cacheNewComments(client, rootId, data.newComments.comments, sort)
6262
// check new comments created after the latest new comment
6363
setLatest(prevLatest => getLatestCommentCreatedAt(data.newComments.comments, prevLatest))
64-
}, [data, client, rootId])
64+
}, [data, client, rootId, sort])
6565

6666
return { polling, setPolling }
6767
}
6868

6969
function cacheNewComments (client, rootId, newComments, sort) {
70+
// update the item with the new comments
71+
// if the comment is a top level comment, update the item
7072
for (const newComment of newComments) {
7173
const { parentId } = newComment
7274
const topLevel = Number(parentId) === Number(rootId)
@@ -79,7 +81,7 @@ function cacheNewComments (client, rootId, newComments, sort) {
7981
}, (data) => {
8082
if (!data) return data
8183
// we return the entire item, not just the newComments
82-
return { item: mergeNewComments(data?.item, newComment) }
84+
return { item: mergeNewComment(data?.item, newComment) }
8385
})
8486
} else {
8587
// if the comment is a reply, update the parent comment
@@ -90,13 +92,15 @@ function cacheNewComments (client, rootId, newComments, sort) {
9092
}, (data) => {
9193
if (!data) return data
9294
// here we return the parent comment with the new comment added
93-
return mergeNewComments(data, newComment)
95+
return mergeNewComment(data, newComment)
9496
})
9597
}
9698
}
9799
}
98100

99-
function mergeNewComments (item, newComment) {
101+
// merge new comment into item's newComments
102+
// if the new comment is already in item's newComments or existing comments, do nothing
103+
function mergeNewComment (item, newComment) {
100104
const existingNewComments = item.newComments || []
101105
const existingComments = item.comments?.comments || []
102106

@@ -122,7 +126,7 @@ function getLatestCommentCreatedAt (comments, latest) {
122126
export function ShowNewComments ({ newComments = [], itemId, topLevel = false, sort }) {
123127
const client = useApolloClient()
124128

125-
const showNewComments = () => {
129+
const showNewComments = useCallback(() => {
126130
if (topLevel) {
127131
client.cache.updateQuery({
128132
query: ITEM_FULL,
@@ -134,7 +138,7 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
134138
return {
135139
item: {
136140
...item,
137-
comments: dedupeComments(item.comments, newComments),
141+
comments: injectComments(item.comments, newComments),
138142
newComments: []
139143
}
140144
}
@@ -149,14 +153,16 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
149153

150154
return {
151155
...data,
152-
comments: dedupeComments(data.comments, newComments),
156+
comments: injectComments(data.comments, newComments),
153157
newComments: []
154158
}
155159
})
156160
}
157-
}
161+
}, [client, itemId, newComments, sort, topLevel])
158162

159-
const dedupeComments = (existingComments = [], newComments = []) => {
163+
// inject new comments into existing comments
164+
// if the new comment is already in existing comments, do nothing
165+
const injectComments = (existingComments = [], newComments = []) => {
160166
const existingIds = new Set(existingComments.comments?.map(c => c.id))
161167
const filteredNew = newComments.filter(c => !existingIds.has(c.id))
162168
return {

0 commit comments

Comments
 (0)