Fixed Filters on Search Result page - #23
Conversation
WalkthroughA single hook was modified to scope filter fetches to the active search query by augmenting filter parameters with Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/hooks/useProductListing.ts (1)
130-134: Consider resettingactiveFilterswhensearchQuerychanges.Now that filters are correctly scoped to the search query, stale
activeFilters(e.g. option values selected for a previous search) will persist across search query changes. This could produce empty or incorrect results when a user types a new search term while filters from the old search are still applied.A lightweight fix would be to reset filters inside this effect:
Suggested approach
useEffect(() => { if (storeLoading) return; - loadProducts(activeFilters, searchQuery); + const resetFilters: ActiveFilters = { optionValues: [] }; + filtersRef.current = resetFilters; + setActiveFilters(resetFilters); + loadProducts(resetFilters, searchQuery); // eslint-disable-next-line react-hooks/exhaustive-deps }, [storeLoading, searchQuery]);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/hooks/useProductListing.ts` around lines 130 - 134, The effect that runs on searchQuery change (useEffect watching [storeLoading, searchQuery]) should reset activeFilters before calling loadProducts to avoid stale filters; modify the effect to call the filter-resetter (e.g., setActiveFilters([]) or resetActiveFilters()) when searchQuery changes and storeLoading is false, then call loadProducts(activeFilters, searchQuery) (or pass the cleared filters explicitly) so loadProducts uses the fresh filter state; ensure you use the existing activeFilters and setter names from this hook.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/hooks/useProductListing.ts`:
- Around line 130-134: The effect that runs on searchQuery change (useEffect
watching [storeLoading, searchQuery]) should reset activeFilters before calling
loadProducts to avoid stale filters; modify the effect to call the
filter-resetter (e.g., setActiveFilters([]) or resetActiveFilters()) when
searchQuery changes and storeLoading is false, then call
loadProducts(activeFilters, searchQuery) (or pass the cleared filters
explicitly) so loadProducts uses the fresh filter state; ensure you use the
existing activeFilters and setter names from this hook.
Summary by CodeRabbit