Skip to content

Commit c613be4

Browse files
committed
enhance: highlight new comments when shown; fix: sync local commentsViewedAt on comments injection to avoid double outline on item re-visit
1 parent 535afb8 commit c613be4

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

components/comment.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,13 @@ export default function Comment ({
144144
if (router.query.commentsViewedAt &&
145145
me?.id !== item.user?.id &&
146146
new Date(item.createdAt).getTime() > router.query.commentsViewedAt) {
147-
ref.current.classList.add('outline-new-comment')
147+
// an injected new comment has the newComments field, a different class is needed
148+
// to reliably outline every new comment
149+
if (item.newComments) {
150+
ref.current.classList.add('outline-new-injected-comment')
151+
} else {
152+
ref.current.classList.add('outline-new-comment')
153+
}
148154
}
149155
}, [item.id])
150156

components/show-new-comments.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { useCallback, useMemo } from 'react'
22
import { useApolloClient } from '@apollo/client'
33
import { COMMENT_WITH_NEW_RECURSIVE } from '../fragments/comments'
44
import styles from './comment.module.css'
5-
import { itemUpdateQuery, commentUpdateFragment } from './use-live-comments'
5+
import { itemUpdateQuery, commentUpdateFragment, getLatestCommentCreatedAt } from './use-live-comments'
66
import { updateAncestorsCommentCount } from '@/lib/comments'
77
import { COMMENT_DEPTH_LIMIT } from '@/lib/constants'
8+
import { commentsViewedAfterComment } from '@/lib/new-comments'
89

910
function prepareComments (client, newComments) {
1011
return (data) => {
@@ -34,6 +35,11 @@ function prepareComments (client, newComments) {
3435
const ancestors = data.path.split('.').slice(0, -1)
3536
updateAncestorsCommentCount(client.cache, ancestors, totalNComments)
3637

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+
3743
return {
3844
...data,
3945
comments: { ...data.comments, comments: [...freshNewComments, ...data.comments.comments] },
@@ -71,8 +77,6 @@ function collectAllNewComments (item, currentDepth = 1) {
7177
const allNewComments = [...(item.newComments || [])]
7278
if (item.comments?.comments && currentDepth < (COMMENT_DEPTH_LIMIT - 1)) {
7379
for (const comment of item.comments.comments) {
74-
console.log('comment', comment)
75-
console.log('currentDepth', currentDepth)
7680
allNewComments.push(...collectAllNewComments(comment, currentDepth + 1))
7781
}
7882
}

components/use-live-comments.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function mergeNewComment (item, newComment) {
127127
return { ...item, newComments: [...existingNewComments, newComment.id] }
128128
}
129129

130-
function getLatestCommentCreatedAt (comments, latest) {
130+
export function getLatestCommentCreatedAt (comments, latest) {
131131
return comments.reduce(
132132
(max, { createdAt }) => (createdAt > max ? createdAt : max),
133133
latest

styles/globals.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,10 +908,18 @@ div[contenteditable]:focus,
908908
box-shadow: inset 0 0 1px 1px rgba(0, 123, 190, 0.25);
909909
}
910910

911+
.outline-new-injected-comment {
912+
box-shadow: inset 0 0 1px 1px rgba(0, 123, 190, 0.25);
913+
}
914+
911915
.outline-new-comment.outline-new-comment-unset {
912916
box-shadow: none;
913917
}
914918

919+
.outline-new-injected-comment.outline-new-comment-unset {
920+
box-shadow: none;
921+
}
922+
915923
.outline-new-comment .outline-new-comment {
916924
box-shadow: none;
917925
}

0 commit comments

Comments
 (0)