@@ -6,52 +6,15 @@ import { useCallback, useEffect, useState } from 'react'
6
6
import styles from './comment.module.css'
7
7
8
8
const POLL_INTERVAL = 1000 * 10 // 10 seconds
9
- const ACTIVITY_TIMEOUT = 1000 * 60 * 30 // 30 minutes
10
- const ACTIVITY_CHECK_INTERVAL = 1000 * 60 // 1 minute
11
9
12
10
export default function useLiveComments ( rootId , after , sort ) {
13
11
const client = useApolloClient ( )
14
12
const [ latest , setLatest ] = useState ( after )
15
- const [ polling , setPolling ] = useState ( true )
16
- const [ engagedAt , setEngagedAt ] = useState ( new Date ( ) )
17
-
18
- // reset engagedAt when polling is toggled
19
- useEffect ( ( ) => {
20
- if ( polling ) {
21
- setEngagedAt ( new Date ( ) )
22
- }
23
- } , [ polling ] )
24
-
25
- useEffect ( ( ) => {
26
- const checkActivity = ( ) => {
27
- const now = new Date ( )
28
- const timeSinceEngaged = now . getTime ( ) - engagedAt . getTime ( )
29
- const isActive = document . visibilityState === 'visible'
30
-
31
- // poll only if the user is active and has been active in the last 30 minutes
32
- if ( timeSinceEngaged < ACTIVITY_TIMEOUT ) {
33
- setPolling ( isActive )
34
- } else {
35
- setPolling ( false )
36
- }
37
- }
38
-
39
- // check activity every minute
40
- const interval = setInterval ( checkActivity , ACTIVITY_CHECK_INTERVAL )
41
- // check activity also on visibility change
42
- document . addEventListener ( 'visibilitychange' , checkActivity )
43
-
44
- return ( ) => {
45
- // cleanup
46
- document . removeEventListener ( 'visibilitychange' , checkActivity )
47
- clearInterval ( interval )
48
- }
49
- } , [ engagedAt ] )
50
13
51
14
const { data } = useQuery ( GET_NEW_COMMENTS , SSR
52
15
? { }
53
16
: {
54
- pollInterval : polling ? POLL_INTERVAL : null ,
17
+ pollInterval : POLL_INTERVAL ,
55
18
variables : { rootId, after : latest }
56
19
} )
57
20
@@ -62,34 +25,37 @@ export default function useLiveComments (rootId, after, sort) {
62
25
// check new comments created after the latest new comment
63
26
setLatest ( prevLatest => getLatestCommentCreatedAt ( data . newComments . comments , prevLatest ) )
64
27
} , [ data , client , rootId , sort ] )
65
-
66
- return { polling, setPolling }
67
28
}
68
29
69
30
function cacheNewComments ( client , rootId , newComments , sort ) {
70
31
// update the item with the new comments
71
32
// if the comment is a top level comment, update the item
72
33
for ( const newComment of newComments ) {
34
+ console . log ( 'newComment' , newComment )
73
35
const { parentId } = newComment
74
36
const topLevel = Number ( parentId ) === Number ( rootId )
75
37
76
38
// if the comment is a top level comment, update the item
77
39
if ( topLevel ) {
40
+ console . log ( 'topLevel' , topLevel )
78
41
client . cache . updateQuery ( {
79
42
query : ITEM_FULL ,
80
43
variables : { id : rootId , sort } // TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
81
44
} , ( data ) => {
45
+ console . log ( 'data' , data )
82
46
if ( ! data ) return data
83
47
// we return the entire item, not just the newComments
84
48
return { item : mergeNewComment ( data ?. item , newComment ) }
85
49
} )
86
50
} else {
87
51
// if the comment is a reply, update the parent comment
52
+ console . log ( 'reply' , parentId )
88
53
client . cache . updateFragment ( {
89
54
id : `Item:${ parentId } ` ,
90
55
fragment : COMMENT_WITH_NEW ,
91
56
fragmentName : 'CommentWithNew'
92
57
} , ( data ) => {
58
+ console . log ( 'dataer' , data )
93
59
if ( ! data ) return data
94
60
// here we return the parent comment with the new comment added
95
61
return mergeNewComment ( data , newComment )
@@ -128,10 +94,12 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
128
94
129
95
const showNewComments = useCallback ( ( ) => {
130
96
if ( topLevel ) {
97
+ console . log ( 'topLevel' , topLevel )
131
98
client . cache . updateQuery ( {
132
99
query : ITEM_FULL ,
133
100
variables : { id : itemId , sort } // TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
134
101
} , ( data ) => {
102
+ console . log ( 'data' , data )
135
103
if ( ! data ) return data
136
104
const { item } = data
137
105
@@ -144,11 +112,13 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
144
112
}
145
113
} )
146
114
} else {
115
+ console . log ( 'reply' , itemId )
147
116
client . cache . updateFragment ( {
148
117
id : `Item:${ itemId } ` ,
149
118
fragment : COMMENT_WITH_NEW ,
150
119
fragmentName : 'CommentWithNew'
151
120
} , ( data ) => {
121
+ console . log ( 'data' , data )
152
122
if ( ! data ) return data
153
123
154
124
return {
@@ -163,7 +133,7 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
163
133
// inject new comments into existing comments
164
134
// if the new comment is already in existing comments, do nothing
165
135
const injectComments = ( existingComments = [ ] , newComments = [ ] ) => {
166
- const existingIds = new Set ( existingComments . comments ?. map ( c => c . id ) )
136
+ const existingIds = new Set ( existingComments . map ( c => c . id ) )
167
137
const filteredNew = newComments . filter ( c => ! existingIds . has ( c . id ) )
168
138
return {
169
139
...existingComments ,
0 commit comments