Skip to content

Commit 274927d

Browse files
committed
fix: make updateQuery sort-aware to correctly inject the comment in the correct Item query
1 parent 6f417cc commit 274927d

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

components/comments.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ export default function Comments ({
8989
commentSats, comments, commentsCursor, fetchMoreComments, ncomments, newComments, lastCommentAt, ...props
9090
}) {
9191
const router = useRouter()
92+
// TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
93+
const sort = router.query.sort || defaultCommentSort(pinned, bio, parentCreatedAt)
9294
// update item.newComments in cache
93-
const { polling: livePolling, setPolling: setLivePolling } = useLiveComments(parentId, lastCommentAt || parentCreatedAt)
95+
const { polling: livePolling, setPolling: setLivePolling } = useLiveComments(parentId, lastCommentAt || parentCreatedAt, sort)
9496

9597
const pins = useMemo(() => comments?.filter(({ position }) => !!position).sort((a, b) => a.position - b.position), [comments])
9698

@@ -113,7 +115,8 @@ export default function Comments ({
113115
/>
114116
: null}
115117
{newComments?.length > 0 && (
116-
<ShowNewComments topLevel newComments={newComments} itemId={parentId} />
118+
// TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
119+
<ShowNewComments topLevel newComments={newComments} itemId={parentId} sort={sort} />
117120
)}
118121
{pins.map(item => (
119122
<Fragment key={item.id}>

components/use-live-comments.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const POLL_INTERVAL = 1000 * 10 // 10 seconds
99
const ACTIVITY_TIMEOUT = 1000 * 60 * 30 // 30 minutes
1010
const ACTIVITY_CHECK_INTERVAL = 1000 * 60 // 1 minute
1111

12-
export default function useLiveComments (rootId, after) {
12+
export default function useLiveComments (rootId, after, sort) {
1313
const client = useApolloClient()
1414
const [latest, setLatest] = useState(after)
1515
const [polling, setPolling] = useState(true)
@@ -58,15 +58,15 @@ export default function useLiveComments (rootId, after) {
5858
useEffect(() => {
5959
if (!data?.newComments) return
6060

61-
cacheNewComments(client, rootId, data.newComments.comments)
61+
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))
6464
}, [data, client, rootId])
6565

6666
return { polling, setPolling }
6767
}
6868

69-
function cacheNewComments (client, rootId, newComments) {
69+
function cacheNewComments (client, rootId, newComments, sort) {
7070
for (const newComment of newComments) {
7171
const { parentId } = newComment
7272
const topLevel = Number(parentId) === Number(rootId)
@@ -75,7 +75,7 @@ function cacheNewComments (client, rootId, newComments) {
7575
if (topLevel) {
7676
client.cache.updateQuery({
7777
query: ITEM_FULL,
78-
variables: { id: rootId }
78+
variables: { id: rootId, sort } // TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
7979
}, (data) => {
8080
if (!data) return data
8181
// we return the entire item, not just the newComments
@@ -119,14 +119,14 @@ function getLatestCommentCreatedAt (comments, latest) {
119119
return new Date(maxTimestamp).toISOString()
120120
}
121121

122-
export function ShowNewComments ({ newComments = [], itemId, topLevel = false }) {
122+
export function ShowNewComments ({ newComments = [], itemId, topLevel = false, sort }) {
123123
const client = useApolloClient()
124124

125125
const showNewComments = () => {
126126
if (topLevel) {
127127
client.cache.updateQuery({
128128
query: ITEM_FULL,
129-
variables: { id: itemId }
129+
variables: { id: itemId, sort } // TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
130130
}, (data) => {
131131
if (!data) return data
132132
const { item } = data
@@ -166,13 +166,12 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false })
166166
}
167167

168168
return (
169-
<span onClick={showNewComments}>
170-
<div className={!topLevel ? styles.comments : 'pb-2'}>
171-
<div className={`d-block fw-bold ${styles.comment} pb-2 ps-3 d-flex align-items-center gap-2 pointer`}>
172-
load new comments
173-
<div className={styles.newCommentDot} />
174-
</div>
175-
</div>
176-
</span>
169+
<div
170+
onClick={showNewComments}
171+
className={`${topLevel && `d-block fw-bold ${styles.comment} pb-2`} d-flex align-items-center gap-2 px-3 pointer`}
172+
>
173+
load new comments
174+
<div className={styles.newCommentDot} />
175+
</div>
177176
)
178177
}

0 commit comments

Comments
 (0)