Skip to content

fix(frontend): reset scroll position to top on new search (#1461) - #1472

Open
sanskar-singh-2403 wants to merge 1 commit into
AOSSIE-Org:mainfrom
sanskar-singh-2403:fix/search-scroll-reset
Open

fix(frontend): reset scroll position to top on new search (#1461)#1472
sanskar-singh-2403 wants to merge 1 commit into
AOSSIE-Org:mainfrom
sanskar-singh-2403:fix/search-scroll-reset

Conversation

@sanskar-singh-2403

@sanskar-singh-2403 sanskar-singh-2403 commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Addressed Issues:

Fixes #1461

Screenshots/Recordings:

DRIVE LINK

Additional Notes:

Root cause

The app has a single scroll container in layout.tsx (the overflow-y-auto div that wraps <Outlet />). Running a new search only changes the value/mode query params on /search, so the SearchResults page stays mounted and that container keeps its previous scrollTop. If the user had scrolled a long result set to the bottom and then ran a search that returns fewer items, the new results render at the top but the viewport stays parked below them, leaving the user looking at an empty area.

Fix (scroll-restoration only, as requested in the issue)

  1. layout.tsx: gave the scroll container a stable id="main-scroll-container" so it is addressable.

  2. SearchResults.tsx: added an effect that scrolls that container to the top whenever the search changes:

    useEffect(() => {
      document.getElementById('main-scroll-container')?.scrollTo({ top: 0 });
    }, [query, mode]);

    Keyed on [query, mode] so it fires for query changes, category changes, and mode switches (tag / semantic / people). It is a no-op if the container is not present. No changes to search logic, pagination, or caching.

  3. SearchResults.test.tsx: added a regression test asserting scrollTo({ top: 0 }) is called on a new search.

Verification (run locally on latest main)

  • npm test -- SearchResults -> 14/14 pass (incl. the new regression test)
  • npx jest allPages + PageSanity + Navbar -> 20/20 pass
  • npx tsc --noEmit -> clean
  • npx eslint --max-warnings 0 on the touched files -> clean
  • npx prettier --check on the touched files -> clean

AI Usage Disclosure:

We encourage contributors to use AI tools responsibly when creating Pull Requests. While AI can be a valuable aid, it is essential to ensure that your contributions meet the task requirements, build successfully, include relevant tests, and pass all linters. Submissions that do not meet these standards may be closed without warning to maintain the quality and integrity of the project. Please take the time to understand the changes you are proposing and their impact. AI slop is strongly discouraged and may lead to banning and blocking. Do not spam our repos with AI slop.

Check one of the checkboxes below:

  • This PR does not contain AI-generated code at all.
  • This PR contains AI-generated code. I have read the AI Usage Policy and this PR complies with this policy. I have tested the code locally and I am responsible for it.

I have used the following AI models and tools: ChatGPT

Checklist

  • My PR addresses a single issue, fixes a single bug or makes a single improvement.
  • My code follows the project's code style and conventions
  • If applicable, I have made corresponding changes or additions to the documentation
  • If applicable, I have made corresponding changes or additions to tests
  • My changes generate no new warnings or errors
  • I have joined the Discord server and I will share a link to this PR with the project maintainers there
  • I have read the Contribution Guidelines
  • Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.
  • I have filled this PR template completely and carefully, and I understand that my PR may be closed without review otherwise.

Summary by CodeRabbit

Bug Fixes

  • Search results now automatically return to the top when the search query or search mode changes.
  • Improved scrolling behavior in the main content area for a smoother browsing experience.
  • Search navigation now displays newly selected results from the beginning of the results view, making it easier to understand and review results when switching between searches.

@github-actions github-actions Bot added bug Something isn't working frontend labels Aug 7, 2026
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@sanskar-singh-2403, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 52 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 2b949335-b2ae-46f4-885f-c3455cc12715

📥 Commits

Reviewing files that changed from the base of the PR and between 36bc408 and 170a4ab.

📒 Files selected for processing (1)
  • frontend/src/pages/SearchResults/SearchResults.tsx

Walkthrough

The main content wrapper now has a stable DOM id. SearchResults resets that container to the top when the query or mode changes. A regression test verifies in-place navigation and scroll restoration.

Changes

Search scroll reset

Layer / File(s) Summary
Wire and validate search scroll reset
frontend/src/layout/layout.tsx, frontend/src/pages/SearchResults/SearchResults.tsx, frontend/src/pages/__tests__/SearchResults.test.tsx
The layout exposes main-scroll-container. SearchResults calls scrollTo({ top: 0 }) when query or mode changes. The regression test navigates between queries while the component remains mounted and verifies the reset.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: TypeScript/JavaScript

Poem

A rabbit checked the page with care,
Then hopped the scroll back to the square.
New results now appear in view,
The test confirms the hop is true.
“No hidden images!” the rabbit cheered.

Sequence Diagram(s)

sequenceDiagram
  participant SearchResults
  participant MainScrollContainer
  participant SearchResultsTest
  SearchResultsTest->>SearchResults: Navigate to a new search query
  SearchResults->>MainScrollContainer: Call scrollTo({ top: 0 })
  MainScrollContainer-->>SearchResultsTest: Update scrollTop to 0
  SearchResultsTest->>SearchResults: Verify the new results render
Loading
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes resetting the frontend scroll position after a new search.
Linked Issues check ✅ Passed The changes implement scroll reset for query or mode changes and add a regression test, satisfying issue #1461 without changing search behavior.
Out of Scope Changes check ✅ Passed All changes support the scroll-restoration fix and its regression test; no unrelated code changes are present.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
frontend/src/pages/__tests__/SearchResults.test.tsx (1)

384-385: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use a typed scrollTo mock.

scrollToMock as unknown as typeof window.scrollTo bypasses the TypeScript check. Give the mock an HTMLElement['scrollTo']-compatible signature or define the mocked property without the double assertion. Please confirm the replacement with the repository TypeScript check.

As per coding guidelines: “In frontend TypeScript, type every export and API boundary, do not add any, and do not use as to silence real type errors.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/src/pages/__tests__/SearchResults.test.tsx` around lines 384 - 385,
Replace the double assertion assigned to scrollContainer.scrollTo with a
properly typed mock matching HTMLElement["scrollTo"], or define the mocked
property with that compatible type. Keep the existing test behavior, avoid any
and assertion-based type suppression, and verify the change with the repository
TypeScript check.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@frontend/src/pages/__tests__/SearchResults.test.tsx`:
- Around line 377-405: Update the “resets the main scroll container…” test to
exercise a new-search transition while keeping the same SearchResults render
mounted: perform the initial search, clear scrollToMock, set a non-zero
scrollTop, navigate to a different query or mode through the rendered UI, then
assert the scroll container’s resulting scroll state is zero. Preserve the
existing Jest and React Testing Library setup and verify user-visible behavior
rather than effect implementation details.

---

Nitpick comments:
In `@frontend/src/pages/__tests__/SearchResults.test.tsx`:
- Around line 384-385: Replace the double assertion assigned to
scrollContainer.scrollTo with a properly typed mock matching
HTMLElement["scrollTo"], or define the mocked property with that compatible
type. Keep the existing test behavior, avoid any and assertion-based type
suppression, and verify the change with the repository TypeScript check.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 23669a3d-08fd-4c3a-8ebe-bdfd71db8db5

📥 Commits

Reviewing files that changed from the base of the PR and between 3a6143d and e51c813.

📒 Files selected for processing (3)
  • frontend/src/layout/layout.tsx
  • frontend/src/pages/SearchResults/SearchResults.tsx
  • frontend/src/pages/__tests__/SearchResults.test.tsx

Comment thread frontend/src/pages/__tests__/SearchResults.test.tsx Outdated
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@gitcordapp

gitcordapp Bot commented Aug 7, 2026

Copy link
Copy Markdown

Link your account with Gitcord

Thanks for opening this PR, @sanskar-singh-2403!

To receive Discord notifications and contributor tracking for this organization:

  1. Join Discord: https://discord.gg/hjUhu33uAn
  2. In Discord, run /link sanskar-singh-2403
  3. Paste the verification code into your GitHub bio (or a public gist)
  4. Click Verify in Discord (or run /verify-link sanskar-singh-2403)

Once linked, Gitcord can notify you about reviews, merges, and more.

Posted by Gitcord

@sanskar-singh-2403

Copy link
Copy Markdown
Contributor Author

@rohan-pandeyy this is ready to be reviewed, Thanks!

@rohan-pandeyy

Copy link
Copy Markdown
Member

@sanskar-singh-2403 please attach a demonstration video

@sanskar-singh-2403

Copy link
Copy Markdown
Contributor Author

@rohan-pandeyy PFA

DRIVE

Comment thread frontend/src/pages/SearchResults/SearchResults.tsx Outdated
@sanskar-singh-2403
sanskar-singh-2403 force-pushed the fix/search-scroll-reset branch 2 times, most recently from 952fddb to 36bc408 Compare August 7, 2026 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working frontend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Search results appear empty when scroll position isn't reset after search

3 participants