Skip to content

Commit b4f2ae2

Browse files
committed
fix: don't use CommentsRecursive for bottomed-out comments
1 parent 5b1d121 commit b4f2ae2

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

components/use-live-comments.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useQuery, useApolloClient } from '@apollo/client'
22
import { SSR } from '../lib/constants'
3-
import { GET_NEW_COMMENTS, COMMENT_WITH_NEW } from '../fragments/comments'
3+
import { GET_NEW_COMMENTS, COMMENT_WITH_NEW, COMMENT_WITH_NEW_LIMITED } from '../fragments/comments'
44
import { ITEM_FULL } from '../fragments/items'
55
import { useEffect, useRef, useState } from 'react'
66

@@ -60,7 +60,7 @@ export function itemUpdateQuery (client, id, sort, fn) {
6060

6161
// update the newComments field of a nested comment fragment
6262
export function commentUpdateFragment (client, id, fn) {
63-
const result = client.cache.updateFragment({
63+
let result = client.cache.updateFragment({
6464
id: `Item:${id}`,
6565
fragment: COMMENT_WITH_NEW,
6666
fragmentName: 'CommentWithNew'
@@ -69,6 +69,19 @@ export function commentUpdateFragment (client, id, fn) {
6969
return fn(data)
7070
})
7171

72+
// sometimes comments can reach their depth limit, and lack adherence to the CommentsRecursive fragment
73+
// for this reason, we update the fragment with a limited version that only includes the CommentFields fragment
74+
if (!result) {
75+
result = client.cache.updateFragment({
76+
id: `Item:${id}`,
77+
fragment: COMMENT_WITH_NEW_LIMITED,
78+
fragmentName: 'CommentWithNewLimited'
79+
}, (data) => {
80+
if (!data) return data
81+
return fn(data)
82+
})
83+
}
84+
7285
return result
7386
}
7487

fragments/comments.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ export const COMMENT_WITH_NEW = gql`
133133
}
134134
`
135135

136+
export const COMMENT_WITH_NEW_LIMITED = gql`
137+
${COMMENT_FIELDS}
138+
139+
fragment CommentWithNewLimited on Item {
140+
...CommentFields
141+
comments {
142+
comments {
143+
...CommentFields
144+
}
145+
}
146+
newComments @client
147+
}
148+
`
149+
136150
export const GET_NEW_COMMENTS = gql`
137151
${COMMENTS}
138152

0 commit comments

Comments
 (0)