Skip to content

Fix: scroll to bottom on thread load - #460

Closed
urjitc wants to merge 3 commits into
mainfrom
capy/scroll-bottom-thread-load
Closed

Fix: scroll to bottom on thread load#460
urjitc wants to merge 3 commits into
mainfrom
capy/scroll-bottom-thread-load

Conversation

@urjitc

@urjitc urjitc commented Apr 26, 2026

Copy link
Copy Markdown
Member

Fixes thread loading dropping users at the top of history instead of the latest exchange. <Messages> now calls scrollToIndex(lastIndex, { align: "end" }) on mount when messages exist and we're not streaming, landing the user at the bottom of the thread (typically the most recent assistant response). The existing pin-to-top behavior for new turns remains unchanged.

Open ENG-068 ENG-068

Summary by CodeRabbit

  • Bug Fixes
    • Improved message viewport auto-scrolling behavior when loading or switching between conversation threads for a smoother experience.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@vercel

vercel Bot commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
thinkex Ready Ready Preview, Comment Apr 26, 2026 7:06am

Request Review

@urjitc urjitc added the capy Generated by capy.ai label Apr 26, 2026 — with Capy AI
@github-project-automation github-project-automation Bot moved this to Backlog in Dev Board Apr 26, 2026
@coderabbitai

coderabbitai Bot commented Apr 26, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@capy-ai[bot] has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 55 minutes and 52 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 55 minutes and 52 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 63dc3d3a-5e54-47a2-bed6-7de466929347

📥 Commits

Reviewing files that changed from the base of the PR and between 83ca5c8 and 0a879b0.

📒 Files selected for processing (2)
  • src/components/chat/Messages.tsx
  • src/components/chat/ThreadBody.tsx
📝 Walkthrough

Walkthrough

Adds a useLayoutEffect hook to the Messages component that auto-scrolls the virtualized message viewport to the end when a thread loads or switches, using the Virtua list handle to perform the scroll operation via requestAnimationFrame.

Changes

Cohort / File(s) Summary
Auto-scroll on mount
src/components/chat/Messages.tsx
Introduces useLayoutEffect with early-return during streaming or when no messages exist; schedules scroll using requestAnimationFrame and Virtua list handle with empty dependency array to prevent scroll jank.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested reviewers

  • cubic-dev-ai

Poem

🐰 Hop hop, scroll scroll, the messages now flow,
At thread load we bound to the very last row,
No jank, no jump, just smooth animation,
A virtualized list reaching its destination!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix: scroll to bottom on thread load' directly and clearly summarizes the main change: adding auto-scroll functionality when a thread is loaded.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch capy/scroll-bottom-thread-load

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps

greptile-apps Bot commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a mount-time useLayoutEffect in MessagesImpl that calls scrollToIndex(lastIndex, { align: "end" }) (via requestAnimationFrame) when a thread loads with existing messages and streaming isn't active. A useRef gate ensures the scroll fires exactly once per component mount, and because <Messages> unmounts on every thread switch, the gate resets naturally for each new thread.

Confidence Score: 5/5

Safe to merge — the change is a focused, well-guarded one-shot scroll on mount with no regressions to the existing pin-to-top or streaming flows.

No P0 or P1 issues found. The ref gate, the isStreaming guard, and the rAF deferral are all consistent with the existing patterns in the file. The architectural assumption that unmounts on thread switch is clearly documented in comments and enforced by ChatProvider/ThreadBody.

No files require special attention.

Important Files Changed

Filename Overview
src/components/chat/Messages.tsx Adds a one-shot useLayoutEffect that scrolls to the last message index on mount when messages exist and the thread isn't streaming, landing users at the bottom of a loaded thread.

Reviews (1): Last reviewed commit: "Scroll to bottom when thread loads so la..." | Re-trigger Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

2 issues found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/components/chat/Messages.tsx">

<violation number="1" location="src/components/chat/Messages.tsx:140">
P2: Custom agent: **Flag AI Slop and Fabricated Changes**

Behavior-changing thread-load scroll fix was added without regression test coverage for the new initial-scroll path.</violation>

<violation number="2" location="src/components/chat/Messages.tsx:146">
P2: This early return leaves the initial bottom-scroll effect armed during active turns, so it can fire right after streaming ends and cause an unexpected jump to bottom.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread src/components/chat/Messages.tsx Outdated
Comment thread src/components/chat/Messages.tsx
urjitc and others added 2 commits April 26, 2026 06:53
…mount

`<Messages>` already remounts on every thread load (ChatProvider clears
messages, ThreadBody routes through the skeleton during hydration), so
React's mount lifecycle is the natural one-shot signal. Drops the
ref guard and the messages.length/isStreaming deps that were causing
a post-turn scroll jolt in brand-new threads.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Today Messages naturally remounts on thread switch because
ChatProvider.selectThread calls setMessages([]) and ThreadBody routes
through the loading skeleton during hydration. With the planned
ChatRuntimesProvider + BoundChatProvider rebind pattern, useChat will
swap to a cached Chat instance that already holds messages — there's no
length-zero gap, so React would reconcile in place and the mount-once
scroll effect would stop firing per thread.

Keying by threadId guarantees a fresh mount per thread regardless of
how the runtime is wired upstream. Costs nothing today and resets all
per-thread state (lastMeasuredUser, prevStatusRef, initialTurnPinnedRef,
virtua cache) cleanly across switches.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@urjitc urjitc closed this Apr 26, 2026
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Dev Board Apr 26, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

0 issues found across 2 files (changes from recent commits).

Auto-approved: Isolated UI fix for scroll position on thread load using standard React patterns. Low risk of side effects.

@urjitc
urjitc deleted the capy/scroll-bottom-thread-load branch June 29, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

capy Generated by capy.ai

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant