Skip to content

Commit 3d8fd6f

Browse files
committed
atomic apollo cache manipulations; manage top sort not being present in Item query cache
1 parent 94ac6c5 commit 3d8fd6f

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

components/use-live-comments.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ import styles from './comment.module.css'
77

88
const POLL_INTERVAL = 1000 * 10 // 10 seconds
99

10+
function itemUpdateQuery (client, id, sort, fn) {
11+
client.cache.updateQuery({
12+
query: ITEM_FULL,
13+
variables: sort === 'top' ? { id } : { id, sort }
14+
}, (data) => fn(data))
15+
}
16+
17+
function commentUpdateFragment (client, id, fn) {
18+
client.cache.updateFragment({
19+
id: `Item:${id}`,
20+
fragment: COMMENT_WITH_NEW,
21+
fragmentName: 'CommentWithNew'
22+
}, (data) => fn(data))
23+
}
24+
1025
export default function useLiveComments (rootId, after, sort) {
1126
const client = useApolloClient()
1227
const [latest, setLatest] = useState(after)
@@ -38,26 +53,15 @@ function cacheNewComments (client, rootId, newComments, sort) {
3853
// if the comment is a top level comment, update the item
3954
if (topLevel) {
4055
console.log('topLevel', topLevel)
41-
client.cache.updateQuery({
42-
query: ITEM_FULL,
43-
variables: { id: rootId, sort } // TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
44-
}, (data) => {
45-
console.log('data', data)
56+
itemUpdateQuery(client, rootId, sort, (data) => {
4657
if (!data) return data
47-
// we return the entire item, not just the newComments
4858
return { item: mergeNewComment(data?.item, newComment) }
4959
})
5060
} else {
5161
// if the comment is a reply, update the parent comment
5262
console.log('reply', parentId)
53-
client.cache.updateFragment({
54-
id: `Item:${parentId}`,
55-
fragment: COMMENT_WITH_NEW,
56-
fragmentName: 'CommentWithNew'
57-
}, (data) => {
58-
console.log('dataer', data)
63+
commentUpdateFragment(client, parentId, (data) => {
5964
if (!data) return data
60-
// here we return the parent comment with the new comment added
6165
return mergeNewComment(data, newComment)
6266
})
6367
}
@@ -95,10 +99,7 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
9599
const showNewComments = useCallback(() => {
96100
if (topLevel) {
97101
console.log('topLevel', topLevel)
98-
client.cache.updateQuery({
99-
query: ITEM_FULL,
100-
variables: { id: itemId, sort } // TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
101-
}, (data) => {
102+
itemUpdateQuery(client, itemId, sort, (data) => {
102103
console.log('data', data)
103104
if (!data) return data
104105
const { item } = data
@@ -113,11 +114,7 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
113114
})
114115
} else {
115116
console.log('reply', itemId)
116-
client.cache.updateFragment({
117-
id: `Item:${itemId}`,
118-
fragment: COMMENT_WITH_NEW,
119-
fragmentName: 'CommentWithNew'
120-
}, (data) => {
117+
commentUpdateFragment(client, itemId, (data) => {
121118
console.log('data', data)
122119
if (!data) return data
123120

@@ -128,12 +125,12 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
128125
}
129126
})
130127
}
131-
}, [client, itemId, newComments, sort, topLevel])
128+
}, [client, itemId, newComments, topLevel, sort])
132129

133130
// inject new comments into existing comments
134131
// if the new comment is already in existing comments, do nothing
135132
const injectComments = (existingComments = [], newComments = []) => {
136-
const existingIds = new Set(existingComments.map(c => c.id))
133+
const existingIds = new Set(existingComments.comments.map(c => c.id))
137134
const filteredNew = newComments.filter(c => !existingIds.has(c.id))
138135
return {
139136
...existingComments,

0 commit comments

Comments
 (0)