|
1 |
| -import { useCallback, useMemo } from 'react' |
| 1 | +import { useCallback, useMemo, useEffect } from 'react' |
2 | 2 | import { useApolloClient } from '@apollo/client'
|
3 |
| -import { COMMENT_WITH_NEW_RECURSIVE } from '../fragments/comments' |
4 | 3 | import styles from './comment.module.css'
|
5 |
| -import { itemUpdateQuery, commentUpdateFragment, getLatestCommentCreatedAt } from './use-live-comments' |
6 |
| -import { updateAncestorsCommentCount } from '@/lib/comments' |
7 |
| -import { COMMENT_DEPTH_LIMIT } from '@/lib/constants' |
8 |
| -import { commentsViewedAfterComment } from '@/lib/new-comments' |
9 |
| - |
10 |
| -function prepareComments (client, newComments) { |
11 |
| - return (data) => { |
12 |
| - // newComments is an array of comment ids that allows us |
13 |
| - // to read the latest newComments from the cache, guaranteeing that we're not reading stale data |
14 |
| - const freshNewComments = newComments.map(id => { |
15 |
| - const fragment = client.cache.readFragment({ |
16 |
| - id: `Item:${id}`, |
17 |
| - fragment: COMMENT_WITH_NEW_RECURSIVE, |
18 |
| - fragmentName: 'CommentWithNewRecursive' |
19 |
| - }) |
20 |
| - |
21 |
| - if (!fragment) { |
22 |
| - return null |
23 |
| - } |
24 |
| - |
25 |
| - return fragment |
26 |
| - }).filter(Boolean) |
27 |
| - |
28 |
| - // count the total number of new comments including its nested new comments |
29 |
| - let totalNComments = freshNewComments.length |
30 |
| - for (const comment of freshNewComments) { |
31 |
| - totalNComments += (comment.ncomments || 0) |
32 |
| - } |
33 |
| - |
34 |
| - // update all ancestors, but not the item itself |
35 |
| - const ancestors = data.path.split('.').slice(0, -1) |
36 |
| - updateAncestorsCommentCount(client.cache, ancestors, totalNComments) |
37 |
| - |
38 |
| - // update commentsViewedAt with the most recent comment |
39 |
| - const latestCommentCreatedAt = getLatestCommentCreatedAt(freshNewComments, data.createdAt) |
40 |
| - const rootId = data.path.split('.')[0] |
41 |
| - commentsViewedAfterComment(rootId, latestCommentCreatedAt) |
42 |
| - |
43 |
| - return { |
44 |
| - ...data, |
45 |
| - comments: { ...data.comments, comments: [...freshNewComments, ...data.comments.comments] }, |
46 |
| - ncomments: data.ncomments + totalNComments, |
47 |
| - newComments: [] |
48 |
| - } |
49 |
| - } |
50 |
| -} |
51 |
| - |
52 |
| -function showAllNewCommentsRecursively (client, item, currentDepth = 1) { |
53 |
| - // handle new comments at this item level |
54 |
| - if (item.newComments && item.newComments.length > 0) { |
55 |
| - const dedupedNewComments = dedupeNewComments(item.newComments, item.comments?.comments) |
56 |
| - |
57 |
| - if (dedupedNewComments.length > 0) { |
58 |
| - const payload = prepareComments(client, dedupedNewComments) |
59 |
| - commentUpdateFragment(client, item.id, payload) |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| - // recursively handle new comments in child comments |
64 |
| - if (item.comments?.comments && currentDepth < (COMMENT_DEPTH_LIMIT - 1)) { |
65 |
| - for (const childComment of item.comments.comments) { |
66 |
| - showAllNewCommentsRecursively(client, childComment, currentDepth + 1) |
67 |
| - } |
68 |
| - } |
69 |
| -} |
70 |
| - |
71 |
| -function dedupeNewComments (newComments, comments) { |
72 |
| - const existingIds = new Set(comments.map(c => c.id)) |
73 |
| - return newComments.filter(id => !existingIds.has(id)) |
74 |
| -} |
75 |
| - |
76 |
| -function collectAllNewComments (item, currentDepth = 1) { |
77 |
| - const allNewComments = [...(item.newComments || [])] |
78 |
| - if (item.comments?.comments && currentDepth < (COMMENT_DEPTH_LIMIT - 1)) { |
79 |
| - for (const comment of item.comments.comments) { |
80 |
| - allNewComments.push(...collectAllNewComments(comment, currentDepth + 1)) |
81 |
| - } |
82 |
| - } |
83 |
| - return allNewComments |
84 |
| -} |
| 4 | +import { itemUpdateQuery, commentUpdateFragment } from './use-live-comments' |
| 5 | +import { prepareComments, dedupeNewComments, collectAllNewComments, showAllNewCommentsRecursively } from '@/lib/comments' |
85 | 6 |
|
86 | 7 | export function ShowNewComments ({ topLevel, sort, comments, itemId, item, setHasNewComments, newComments = [], depth = 1 }) {
|
87 | 8 | const client = useApolloClient()
|
@@ -112,6 +33,16 @@ export function ShowNewComments ({ topLevel, sort, comments, itemId, item, setHa
|
112 | 33 | setHasNewComments(false)
|
113 | 34 | }, [client, itemId, allNewComments, topLevel, sort])
|
114 | 35 |
|
| 36 | + useEffect(() => { |
| 37 | + const handleVisibilityChange = () => { |
| 38 | + if (document.hidden && allNewComments.length > 0) { |
| 39 | + showNewComments() |
| 40 | + } |
| 41 | + } |
| 42 | + document.addEventListener('visibilitychange', handleVisibilityChange) |
| 43 | + return () => document.removeEventListener('visibilitychange', handleVisibilityChange) |
| 44 | + }, [showNewComments]) |
| 45 | + |
115 | 46 | if (allNewComments.length === 0) {
|
116 | 47 | return null
|
117 | 48 | }
|
|
0 commit comments