Skip to content

fix(TC-2443): prevent redundant autocomplete API calls on Search page navigation [Backport release/0.5.z]#1136

Merged
carlosthe19916 merged 1 commit into
release/0.5.zfrom
backport-1116-to-release/0.5.z
Jul 2, 2026
Merged

fix(TC-2443): prevent redundant autocomplete API calls on Search page navigation [Backport release/0.5.z]#1136
carlosthe19916 merged 1 commit into
release/0.5.zfrom
backport-1116-to-release/0.5.z

Conversation

@trustify-ci-bot

@trustify-ci-bot trustify-ci-bot Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Description

Backport of #1116 to release/0.5.z.

Summary by Sourcery

Prevent redundant search autocomplete API calls when navigating the Search page and synchronizing with URL-based filters.

Bug Fixes:

  • Avoid triggering autocomplete fetches when the search input is empty, whitespace-only, or matches the currently applied filter value after navigation or submission.
  • Ensure the search input does not temporarily display stale filter text while filters are being cleared.

Tests:

  • Add unit tests for SearchMenu to verify autocomplete fetch gating behavior across typing, submitting, clearing, and filter reset scenarios.

… navigation (#1116)

Signed-off-by: Carlos Feria <2582866+carlosthe19916@users.noreply.github.com>
(cherry picked from commit 0ce3117)
@sourcery-ai

sourcery-ai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Backports the SearchMenu autocomplete gating fix to avoid redundant autocomplete API calls after navigation or filter changes by stabilizing the applied search value, tightening when autocomplete is enabled, and adding regression tests around the new behavior.

Sequence diagram for SearchMenu search submission and filter sync

sequenceDiagram
  actor User
  participant SearchMenu
  participant SbomSearchContext
  participant AutocompleteFetchers

  User->>SearchMenu: type into input
  SearchMenu->>SearchMenu: setSearchValue
  SearchMenu->>SearchMenu: setIsSearchValueDirty(true)
  SearchMenu->>SearchMenu: useDebounceValue(searchValue)
  SearchMenu->>AutocompleteFetchers: useAllEntities(debouncedSearchValue, false)

  User->>SearchMenu: click Search button
  SearchMenu->>SearchMenu: setIsSearchValueDirty(false)
  SearchMenu->>SearchMenu: setIsAutocompleteOpen(false)
  SearchMenu->>SearchMenu: submittedSearchValueRef.current = searchValue
  SearchMenu->>SbomSearchContext: onChangeSearch(searchValue)
  SearchMenu->>AutocompleteFetchers: useAllEntities(debouncedSearchValue, true)

  SbomSearchContext-->>SearchMenu: filterState.filterValues updated
  SearchMenu->>SearchMenu: appliedSearchValue extracted
  SearchMenu->>SearchMenu: useEffect([appliedSearchValue, isSearchValueDirty])
  alt submittedSearchValueRef.current === appliedSearchValue
    SearchMenu->>SearchMenu: submittedSearchValueRef.current = null
    SearchMenu->>SearchMenu: setSearchValue(appliedSearchValue)
  else isSearchValueDirty === false and no submitted value
    SearchMenu->>SearchMenu: setSearchValue(appliedSearchValue)
  end

  User->>SearchMenu: click Reset button
  SearchMenu->>SearchMenu: setSearchValue("")
  SearchMenu->>SearchMenu: setIsSearchValueDirty(false)
  SearchMenu->>SearchMenu: setIsAutocompleteOpen(false)
  SearchMenu->>SearchMenu: submittedSearchValueRef.current = ""
  SearchMenu->>SbomSearchContext: onChangeSearch("")
  SearchMenu->>AutocompleteFetchers: useAllEntities("", true)
Loading

File-Level Changes

Change Details Files
Stabilize how the search input is derived from filter state and track the last submitted value to avoid UI desync and redundant updates.
  • Introduce an appliedSearchValue derived once per render from filterState.filterValues with a stable empty-string fallback.
  • Add submittedSearchValueRef to remember the last submitted search term across renders and navigation.
  • Update the effect that syncs searchValue from table controls to use appliedSearchValue, skip updates while the input is dirty, and only resync when the applied search equals the last submitted value.
client/src/app/pages/search/components/SearchMenu.tsx
Gate autocomplete fetching so it only runs for meaningful, unsent user input and is disabled after submit or clear.
  • Add a disableAutocomplete flag that is true when the input is not dirty or the debounced value trims to an empty string.
  • Pass disableAutocomplete to useAllEntities instead of the previous !isSearchValueDirty condition to better control when backend autocomplete calls occur.
  • On clear and submit, reset isSearchValueDirty, close the autocomplete dropdown, update submittedSearchValueRef, and ensure onChangeSearch is called with the correct value.
client/src/app/pages/search/components/SearchMenu.tsx
Add regression tests for SearchMenu autocomplete gating and input behavior around submit, clear, and filter synchronization.
  • Mock advisory, package, SBOM, and vulnerability query hooks and capture their disable flags for inspection.
  • Add helper utilities to check that all autocomplete-related fetches are enabled or disabled consistently across resources.
  • Write tests covering initial mount with empty input, typing non-empty and whitespace-only queries, behavior after submit and clear, and prevention of stale filter text flashing into the input when filters are reset or context is rerendered.
client/src/app/pages/search/components/SearchMenu.test.tsx

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@carlosthe19916 carlosthe19916 merged commit 96c9508 into release/0.5.z Jul 2, 2026
13 checks passed
@github-project-automation github-project-automation Bot moved this to Done in Trustify Jul 2, 2026

@sourcery-ai sourcery-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.

Hey - I've left some high level feedback:

  • The disableAutocomplete boolean is derived as a negation of isSearchValueDirty and then passed through multiple layers; consider renaming it (e.g., shouldDisableAutocomplete) or inverting the condition at the call site to improve readability of the on/off logic across the component.
  • The submittedSearchValueRef lifecycle (null vs empty string) is subtle and drives multiple branches in the effect; adding a small helper function or encapsulating this state machine into a custom hook would make the intent and transitions easier to reason about and maintain.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `disableAutocomplete` boolean is derived as a negation of `isSearchValueDirty` and then passed through multiple layers; consider renaming it (e.g., `shouldDisableAutocomplete`) or inverting the condition at the call site to improve readability of the on/off logic across the component.
- The `submittedSearchValueRef` lifecycle (null vs empty string) is subtle and drives multiple branches in the effect; adding a small helper function or encapsulating this state machine into a custom hook would make the intent and transitions easier to reason about and maintain.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 51.10%. Comparing base (569e699) to head (d165aa3).
⚠️ Report is 1 commits behind head on release/0.5.z.

Files with missing lines Patch % Lines
...ent/src/app/pages/search/components/SearchMenu.tsx 88.88% 2 Missing ⚠️
Additional details and impacted files
@@                Coverage Diff                @@
##           release/0.5.z    #1136      +/-   ##
=================================================
+ Coverage          50.59%   51.10%   +0.50%     
=================================================
  Files                256      256              
  Lines               5506     5522      +16     
  Branches            1667     1672       +5     
=================================================
+ Hits                2786     2822      +36     
+ Misses              2450     2432      -18     
+ Partials             270      268       -2     
Flag Coverage Δ
unit 6.12% <88.88%> (+4.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant