@@ -2,8 +2,7 @@ import { useQuery, useApolloClient } from '@apollo/client'
2
2
import { SSR } from '../lib/constants'
3
3
import { GET_NEW_COMMENTS , COMMENT_WITH_NEW } from '../fragments/comments'
4
4
import { ITEM_FULL } from '../fragments/items'
5
- import { useCallback , useEffect , useRef , useState } from 'react'
6
- import styles from './comment.module.css'
5
+ import { useEffect , useRef , useState } from 'react'
7
6
8
7
const POLL_INTERVAL = 1000 * 10 // 10 seconds
9
8
@@ -40,7 +39,7 @@ export default function useLiveComments (rootId, after, sort) {
40
39
}
41
40
42
41
// the item query is used to update the item's newComments field
43
- function itemUpdateQuery ( client , id , sort , fn ) {
42
+ export function itemUpdateQuery ( client , id , sort , fn ) {
44
43
client . cache . updateQuery ( {
45
44
query : ITEM_FULL ,
46
45
// updateQuery needs the correct variables to update the correct item
@@ -53,7 +52,7 @@ function itemUpdateQuery (client, id, sort, fn) {
53
52
}
54
53
55
54
// update the newComments field of a nested comment fragment
56
- function commentUpdateFragment ( client , id , fn ) {
55
+ export function commentUpdateFragment ( client , id , fn ) {
57
56
client . cache . updateFragment ( {
58
57
id : `Item:${ id } ` ,
59
58
fragment : COMMENT_WITH_NEW ,
@@ -122,60 +121,3 @@ function getLatestCommentCreatedAt (comments, latest) {
122
121
// convert back to ISO string
123
122
return new Date ( maxTimestamp ) . toISOString ( )
124
123
}
125
-
126
- // ShowNewComments is a component that dedupes, refreshes and injects newComments into the comments field
127
- export function ShowNewComments ( { newComments = [ ] , itemId, topLevel = false , sort } ) {
128
- const client = useApolloClient ( )
129
-
130
- const showNewComments = useCallback ( ( ) => {
131
- const payload = ( data ) => {
132
- // TODO: it might be sane to pass the cache ref to the ShowNewComments component
133
- // TODO: and use it to read the latest newComments from the cache
134
- // newComments can have themselves new comments between the time the button is clicked and the query is executed
135
- // so we need to read the latest newComments from the cache
136
- const freshNewComments = newComments . map ( c => {
137
- const fragment = client . cache . readFragment ( {
138
- id : `Item:${ c . id } ` ,
139
- fragment : COMMENT_WITH_NEW ,
140
- fragmentName : 'CommentWithNew'
141
- } )
142
- // if the comment is not in the cache, return the original comment
143
- return fragment || c
144
- } )
145
-
146
- // deduplicate the fresh new comments with the existing comments
147
- const dedupedComments = dedupeComments ( data . comments . comments , freshNewComments )
148
-
149
- return {
150
- ...data ,
151
- comments : { ...data . comments , comments : dedupedComments } ,
152
- ncomments : data . ncomments + ( dedupedComments . length || 0 ) ,
153
- newComments : [ ]
154
- }
155
- }
156
-
157
- if ( topLevel ) {
158
- itemUpdateQuery ( client , itemId , sort , payload )
159
- } else {
160
- commentUpdateFragment ( client , itemId , payload )
161
- }
162
- } , [ client , itemId , newComments , topLevel , sort ] )
163
-
164
- return (
165
- < div
166
- onClick = { showNewComments }
167
- className = { `${ topLevel && `d-block fw-bold ${ styles . comment } pb-2` } d-flex align-items-center gap-2 px-3 pointer` }
168
- >
169
- { newComments . length > 1 ? `${ newComments . length } new comments` : 'show new comment' }
170
- < div className = { styles . newCommentDot } />
171
- </ div >
172
- )
173
- }
174
-
175
- // even though we already deduplicated comments during the newComments merge,
176
- // refetches, client-side navigation, etc. can cause duplicates to appear,
177
- // so we'll make sure to deduplicate them here, by id
178
- function dedupeComments ( existing = [ ] , incoming = [ ] ) {
179
- const existingIds = new Set ( existing . map ( c => c . id ) )
180
- return [ ...incoming . filter ( c => ! existingIds . has ( c . id ) ) , ...existing ]
181
- }
0 commit comments