Skip to content

Commit c7095a7

Browse files
committed
enhance: direct latest comment createdAt calc with reduce
1 parent d025407 commit c7095a7

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

components/use-live-comments.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default function useLiveComments (rootId, after, sort) {
3535

3636
// update latest timestamp to the latest comment created at
3737
setLatest(prevLatest => getLatestCommentCreatedAt(data.newComments.comments, prevLatest))
38+
console.log('latest', latest)
3839
}, [data, client, rootId, sort])
3940
}
4041

@@ -111,13 +112,8 @@ function mergeNewComment (item, newComment) {
111112
}
112113

113114
function getLatestCommentCreatedAt (comments, latest) {
114-
if (comments.length === 0) return latest
115-
116-
// timestamp comparison via Math.max on bare timestamps
117-
// convert all createdAt to timestamps
118-
const timestamps = comments.map(c => new Date(c.createdAt).getTime())
119-
// find the latest timestamp
120-
const maxTimestamp = Math.max(...timestamps, new Date(latest).getTime())
121-
// convert back to ISO string
122-
return new Date(maxTimestamp).toISOString()
115+
return comments.reduce(
116+
(max, { createdAt }) => (createdAt > max ? createdAt : max),
117+
latest
118+
)
123119
}

0 commit comments

Comments
 (0)