Skip to content

Live updates to comment threads #2115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: master
Choose a base branch
from

Conversation

Soxasora
Copy link
Member

@Soxasora Soxasora commented Apr 18, 2025

Description

Closes #791
It features a useLiveComments hook that incrementally fetches comments newer than the last commentAt timestamp, performing cache updates to the newComments field of the parent Item/Comment.

New comments can be showed with a "X new comments" button that will retrieve fresh copies of the newComments from the cache and inject them in the parent's comment field.

Media

Desktop

Screen.Recording.2025-07-01.at.22.14.53.mp4

Mobile

Untitled.mp4

Additional Context

Live comments features:

  • 10 seconds polling
  • Orphan comments queuing
  • Top-level and nested replies dedicated flows
  • Duplicate prevention

Known bugs:

  • comments without item.comments are not subscribed to newComments

Checklist

Are your changes backwards compatible? Please answer below:
Yes

On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below:
6, strong QA but it might benefit from human comment testing

For frontend changes: Tested on mobile, light and dark mode? Please answer below:
Yes

Did you introduce any new environment variables? If so, call them out explicitly here:
n/a

Soxasora and others added 21 commits April 18, 2025 05:34
…fix leak on useEffect because of missing sort

atomic apollo cache manipulations; manage top sort not being present in item query cache
queue nested comments without a parent, retry on the next poll
fix commit messages
@Soxasora Soxasora marked this pull request as ready for review July 1, 2025 22:05
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@Soxasora
Copy link
Member Author

Soxasora commented Jul 4, 2025

Double-counting in totalNComments calculation

The prepareComments function counts of a parent (data):

  1. Its direct new children (freshNewComments.length)
  2. All item.comments comments nested under those new children (sum of each child's ncomments), not the comments that live in newComments.

This count is then propagated to the parent and its ancestors to update their ncomments field.

Repeated updates from multiple components: When ShowNewComments components are rendered at multiple levels of a comment tree, the ancestor comment count update logic is triggered repeatedly for the same ancestors, causing new comments to be counted multiple times.

Items/comments that resides in newComments are not counted by the ncomments field yet, as it is computed for comments inside item.comments.*

Additionally, the data.path.split('.') call in prepareComments lacks a null or undefined check for data.path, which can cause a runtime error if the path is missing or invalid.

A successful fragment that triggered the prepareComments function must have the path, this can't happen.

cursor[bot]

This comment was marked as outdated.

…ates; fix: nested comment structures

- new comments indicator for bottomed-out replies
- ncomments sync for parent and its ancestors
- limited comments fragment for comments that don't have CommentsRecursive
- reduce cache complexity by removing useless roundtrips

ux: live comments indicator on bottomedOut replies

fix: dedupe newComments before displaying ShowNewComments to avoid false positives

enhance: store ids of new comments in the cache, instead of carrying full comments that would get discarded anyway

hotfix: newComments deduplication ID mismatch, filter null comments from freshNewComments

fix: ncomments not updating for all comment levels; refactor: share Reply update ancestors' ncomments function with ShowNewComments

cleanup: better naming to indicate the total number of comments including nested comments

fix: increment parent comment ncomments

cleanup: Items that will have comments will always have a structure where item.comments is true

cleanup: reduce code complexity checking the nested comment update result instead of preventively reading the fragment

cleanup: avoid double-updating ncomments on parent

fix: don't use CommentsRecursive for bottomed-out comments

cleanup: better fragment naming; add TODO for absolute bottom comments
@huumn
Copy link
Member

huumn commented Jul 5, 2025

This looks good at a glance. One thing I'd be curious to see is how it looks on mobile with deeply nested comments.

@huumn
Copy link
Member

huumn commented Jul 5, 2025

If you're at the stage where you're open to enhancement suggestions. I think it'd be cool to:

  1. change the favicon when a tab I'm on has new comments
  2. give blue outlines of these freshly loaded comments (like we do when you visit a thread after a first visit and there are new comments)
  3. put this show new comment thing also at the top of the thread to load all new comments

@Soxasora
Copy link
Member Author

Soxasora commented Jul 5, 2025

Of course! These are pretty cool, going to implement them now ^^
I'll also make a mobile video example right after the new enhancements.

There's only a bug left: the deepest comment can't be updated because it lacks item.comments per item_comments_zaprank_with_me/_limited.

It's not a serious problem because you need to go to another page to see its nested comments, but the new comment indicator on view x replies on another page is a nice touch.
A workaround would be to just update the newComments field, but I'm comparing options to avoid bloat on the updateCommentFragment function... if it makes sense.

@huumn
Copy link
Member

huumn commented Jul 5, 2025

I recommend starting these enhancements in a new branch. I think this branch can live on its own.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Nested Comments Counted Twice

The totalNComments calculation double-counts nested comments. The code sums freshNewComments.length (direct new comments) and comment.ncomments (nested comments for each new comment). This results in double-counting when freshNewComments contains both a parent and its new child comment, as the child is counted directly and again as part of the parent's ncomments total.

components/show-new-comments.js#L25-L30

// count the total number of new comments including its nested new comments
let totalNComments = freshNewComments.length
for (const comment of freshNewComments) {
totalNComments += (comment.ncomments || 0)
}

Fix in CursorFix in Web


Was this report helpful? Give feedback by reacting with 👍 or 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Live updates to comment threads
2 participants