@@ -9,7 +9,7 @@ const POLL_INTERVAL = 1000 * 10 // 10 seconds
9
9
const ACTIVITY_TIMEOUT = 1000 * 60 * 30 // 30 minutes
10
10
const ACTIVITY_CHECK_INTERVAL = 1000 * 60 // 1 minute
11
11
12
- export default function useLiveComments ( rootId , after ) {
12
+ export default function useLiveComments ( rootId , after , sort ) {
13
13
const client = useApolloClient ( )
14
14
const [ latest , setLatest ] = useState ( after )
15
15
const [ polling , setPolling ] = useState ( true )
@@ -58,15 +58,15 @@ export default function useLiveComments (rootId, after) {
58
58
useEffect ( ( ) => {
59
59
if ( ! data ?. newComments ) return
60
60
61
- cacheNewComments ( client , rootId , data . newComments . comments )
61
+ cacheNewComments ( client , rootId , data . newComments . comments , sort )
62
62
// check new comments created after the latest new comment
63
63
setLatest ( prevLatest => getLatestCommentCreatedAt ( data . newComments . comments , prevLatest ) )
64
64
} , [ data , client , rootId ] )
65
65
66
66
return { polling, setPolling }
67
67
}
68
68
69
- function cacheNewComments ( client , rootId , newComments ) {
69
+ function cacheNewComments ( client , rootId , newComments , sort ) {
70
70
for ( const newComment of newComments ) {
71
71
const { parentId } = newComment
72
72
const topLevel = Number ( parentId ) === Number ( rootId )
@@ -75,7 +75,7 @@ function cacheNewComments (client, rootId, newComments) {
75
75
if ( topLevel ) {
76
76
client . cache . updateQuery ( {
77
77
query : ITEM_FULL ,
78
- variables : { id : rootId }
78
+ variables : { id : rootId , sort } // TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
79
79
} , ( data ) => {
80
80
if ( ! data ) return data
81
81
// we return the entire item, not just the newComments
@@ -119,14 +119,14 @@ function getLatestCommentCreatedAt (comments, latest) {
119
119
return new Date ( maxTimestamp ) . toISOString ( )
120
120
}
121
121
122
- export function ShowNewComments ( { newComments = [ ] , itemId, topLevel = false } ) {
122
+ export function ShowNewComments ( { newComments = [ ] , itemId, topLevel = false , sort } ) {
123
123
const client = useApolloClient ( )
124
124
125
125
const showNewComments = ( ) => {
126
126
if ( topLevel ) {
127
127
client . cache . updateQuery ( {
128
128
query : ITEM_FULL ,
129
- variables : { id : itemId }
129
+ variables : { id : itemId , sort } // TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
130
130
} , ( data ) => {
131
131
if ( ! data ) return data
132
132
const { item } = data
@@ -166,13 +166,12 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false })
166
166
}
167
167
168
168
return (
169
- < span onClick = { showNewComments } >
170
- < div className = { ! topLevel ? styles . comments : 'pb-2' } >
171
- < div className = { `d-block fw-bold ${ styles . comment } pb-2 ps-3 d-flex align-items-center gap-2 pointer` } >
172
- load new comments
173
- < div className = { styles . newCommentDot } />
174
- </ div >
175
- </ div >
176
- </ span >
169
+ < div
170
+ onClick = { showNewComments }
171
+ className = { `${ topLevel && `d-block fw-bold ${ styles . comment } pb-2` } d-flex align-items-center gap-2 px-3 pointer` }
172
+ >
173
+ load new comments
174
+ < div className = { styles . newCommentDot } />
175
+ </ div >
177
176
)
178
177
}
0 commit comments