Skip to content

Commit 94ac6c5

Browse files
committed
reset this commit
1 parent a2b6841 commit 94ac6c5

File tree

2 files changed

+14
-65
lines changed

2 files changed

+14
-65
lines changed

components/comments.js

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import { useRouter } from 'next/router'
99
import MoreFooter from './more-footer'
1010
import { FULL_COMMENTS_THRESHOLD } from '@/lib/constants'
1111
import useLiveComments, { ShowNewComments } from './use-live-comments'
12-
import ActionTooltip from './action-tooltip'
13-
import classNames from 'classnames'
1412

15-
export function CommentsHeader ({ handleSort, pinned, bio, parentCreatedAt, commentSats, livePolling, setLivePolling }) {
13+
export function CommentsHeader ({ handleSort, pinned, bio, parentCreatedAt, commentSats }) {
1614
const router = useRouter()
1715
const sort = router.query.sort || defaultCommentSort(pinned, bio, parentCreatedAt)
1816

@@ -31,25 +29,6 @@ export function CommentsHeader ({ handleSort, pinned, bio, parentCreatedAt, comm
3129
<Nav.Item className='text-muted'>
3230
{numWithUnits(commentSats)}
3331
</Nav.Item>
34-
{livePolling
35-
? (
36-
<Nav.Item className='ps-2'>
37-
<ActionTooltip notForm overlayText='comments are live'>
38-
<div className={styles.newCommentDot} />
39-
</ActionTooltip>
40-
</Nav.Item>
41-
)
42-
: (
43-
<Nav.Item className='ps-2'>
44-
<ActionTooltip notForm overlayText='click to resume live comments'>
45-
<div
46-
className={classNames(styles.newCommentDot, styles.paused)}
47-
onClick={() => setLivePolling(true)}
48-
style={{ cursor: 'pointer' }}
49-
/>
50-
</ActionTooltip>
51-
</Nav.Item>
52-
)}
5332
<div className='ms-auto d-flex'>
5433
<Nav.Item>
5534
<Nav.Link
@@ -92,7 +71,7 @@ export default function Comments ({
9271
// TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
9372
const sort = router.query.sort || defaultCommentSort(pinned, bio, parentCreatedAt)
9473
// update item.newComments in cache
95-
const { polling: livePolling, setPolling: setLivePolling } = useLiveComments(parentId, lastCommentAt || parentCreatedAt, sort)
74+
useLiveComments(parentId, lastCommentAt || parentCreatedAt, sort)
9675

9776
const pins = useMemo(() => comments?.filter(({ position }) => !!position).sort((a, b) => a.position - b.position), [comments])
9877

@@ -101,7 +80,7 @@ export default function Comments ({
10180
{comments?.length > 0
10281
? <CommentsHeader
10382
commentSats={commentSats} parentCreatedAt={parentCreatedAt}
104-
pinned={pinned} bio={bio} livePolling={livePolling} setLivePolling={setLivePolling} handleSort={sort => {
83+
pinned={pinned} bio={bio} handleSort={sort => {
10584
const { commentsViewedAt, commentId, ...query } = router.query
10685
delete query.nodata
10786
router.push({

components/use-live-comments.js

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,15 @@ import { useCallback, useEffect, useState } from 'react'
66
import styles from './comment.module.css'
77

88
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
119

1210
export default function useLiveComments (rootId, after, sort) {
1311
const client = useApolloClient()
1412
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])
5013

5114
const { data } = useQuery(GET_NEW_COMMENTS, SSR
5215
? {}
5316
: {
54-
pollInterval: polling ? POLL_INTERVAL : null,
17+
pollInterval: POLL_INTERVAL,
5518
variables: { rootId, after: latest }
5619
})
5720

@@ -62,34 +25,37 @@ export default function useLiveComments (rootId, after, sort) {
6225
// check new comments created after the latest new comment
6326
setLatest(prevLatest => getLatestCommentCreatedAt(data.newComments.comments, prevLatest))
6427
}, [data, client, rootId, sort])
65-
66-
return { polling, setPolling }
6728
}
6829

6930
function cacheNewComments (client, rootId, newComments, sort) {
7031
// update the item with the new comments
7132
// if the comment is a top level comment, update the item
7233
for (const newComment of newComments) {
34+
console.log('newComment', newComment)
7335
const { parentId } = newComment
7436
const topLevel = Number(parentId) === Number(rootId)
7537

7638
// if the comment is a top level comment, update the item
7739
if (topLevel) {
40+
console.log('topLevel', topLevel)
7841
client.cache.updateQuery({
7942
query: ITEM_FULL,
8043
variables: { id: rootId, sort } // TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
8144
}, (data) => {
45+
console.log('data', data)
8246
if (!data) return data
8347
// we return the entire item, not just the newComments
8448
return { item: mergeNewComment(data?.item, newComment) }
8549
})
8650
} else {
8751
// if the comment is a reply, update the parent comment
52+
console.log('reply', parentId)
8853
client.cache.updateFragment({
8954
id: `Item:${parentId}`,
9055
fragment: COMMENT_WITH_NEW,
9156
fragmentName: 'CommentWithNew'
9257
}, (data) => {
58+
console.log('dataer', data)
9359
if (!data) return data
9460
// here we return the parent comment with the new comment added
9561
return mergeNewComment(data, newComment)
@@ -128,10 +94,12 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
12894

12995
const showNewComments = useCallback(() => {
13096
if (topLevel) {
97+
console.log('topLevel', topLevel)
13198
client.cache.updateQuery({
13299
query: ITEM_FULL,
133100
variables: { id: itemId, sort } // TODO-LIVE: ok now item updates thanks to sort awareness, but please CLEAN THIS UP
134101
}, (data) => {
102+
console.log('data', data)
135103
if (!data) return data
136104
const { item } = data
137105

@@ -144,11 +112,13 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
144112
}
145113
})
146114
} else {
115+
console.log('reply', itemId)
147116
client.cache.updateFragment({
148117
id: `Item:${itemId}`,
149118
fragment: COMMENT_WITH_NEW,
150119
fragmentName: 'CommentWithNew'
151120
}, (data) => {
121+
console.log('data', data)
152122
if (!data) return data
153123

154124
return {
@@ -163,7 +133,7 @@ export function ShowNewComments ({ newComments = [], itemId, topLevel = false, s
163133
// inject new comments into existing comments
164134
// if the new comment is already in existing comments, do nothing
165135
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))
167137
const filteredNew = newComments.filter(c => !existingIds.has(c.id))
168138
return {
169139
...existingComments,

0 commit comments

Comments
 (0)