@@ -7,6 +7,21 @@ import styles from './comment.module.css'
7
7
8
8
const POLL_INTERVAL = 1000 * 10 // 10 seconds
9
9
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
+
10
25
export default function useLiveComments ( rootId , after , sort ) {
11
26
const client = useApolloClient ( )
12
27
const [ latest , setLatest ] = useState ( after )
@@ -38,26 +53,15 @@ function cacheNewComments (client, rootId, newComments, sort) {
38
53
// if the comment is a top level comment, update the item
39
54
if ( topLevel ) {
40
55
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 ) => {
46
57
if ( ! data ) return data
47
- // we return the entire item, not just the newComments
48
58
return { item : mergeNewComment ( data ?. item , newComment ) }
49
59
} )
50
60
} else {
51
61
// if the comment is a reply, update the parent comment
52
62
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 ) => {
59
64
if ( ! data ) return data
60
- // here we return the parent comment with the new comment added
61
65
return mergeNewComment ( data , newComment )
62
66
} )
63
67
}
@@ -95,10 +99,7 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
95
99
const showNewComments = useCallback ( ( ) => {
96
100
if ( topLevel ) {
97
101
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 ) => {
102
103
console . log ( 'data' , data )
103
104
if ( ! data ) return data
104
105
const { item } = data
@@ -113,11 +114,7 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
113
114
} )
114
115
} else {
115
116
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 ) => {
121
118
console . log ( 'data' , data )
122
119
if ( ! data ) return data
123
120
@@ -128,12 +125,12 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
128
125
}
129
126
} )
130
127
}
131
- } , [ client , itemId , newComments , sort , topLevel ] )
128
+ } , [ client , itemId , newComments , topLevel , sort ] )
132
129
133
130
// inject new comments into existing comments
134
131
// if the new comment is already in existing comments, do nothing
135
132
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 ) )
137
134
const filteredNew = newComments . filter ( c => ! existingIds . has ( c . id ) )
138
135
return {
139
136
...existingComments ,
0 commit comments