fix(region-selector): prevent empty region reporting during sync#2636
fix(region-selector): prevent empty region reporting during sync#2636
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughReplaced an effect that auto-invoked onChange based on selectedRegion with a dedicated handler Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/components/RegionSelector/index.tsx (1)
86-94:⚠️ Potential issue | 🟠 MajorPrevent emitting stale region on external
valueprop updates.When the parent component updates the
valueprop, the sync effect queues asetSelectedRegionstate update while the emitter effect runs synchronously in the same effect flush. This causes the emitter to callonChangewith the previousselectedRegionbefore the sync completes. InWatchProviderSelector, this stale emission can unexpectedly triggersetActiveProvider([]).Proposed fix (emit only on actual selected-region transitions)
-import { useEffect, useMemo, useState } from 'react'; +import { useEffect, useMemo, useRef, useState } from 'react';+ const lastEmittedRegionRef = useRef<string | null>(null); + useEffect(() => { if (onChange && regions) { - if (selectedRegion) { - onChange(name, selectedRegion.iso_3166_1); - } else if (!value) { + const nextRegion = selectedRegion?.iso_3166_1 ?? ''; + if (lastEmittedRegionRef.current === nextRegion) { + return; + } + lastEmittedRegionRef.current = nextRegion; + + if (nextRegion) { + onChange(name, nextRegion); + } else if (!value) { onChange(name, ''); } } }, [onChange, selectedRegion, name, regions, value]);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/components/RegionSelector/index.tsx` around lines 86 - 94, The emitter effect is firing with a stale selectedRegion when the external value prop triggers a state update in the same render flush; to fix, change the useEffect (the effect that currently depends on [onChange, selectedRegion, name, regions, value]) so it only emits when selectedRegion actually transitions: add a prevSelectedRegionRef (or similar) to store the previous selectedRegion.iso_3166_1, compare prev vs current inside the effect, and only call onChange(name, selectedRegion.iso_3166_1) when they differ; after emitting update the ref. Ensure you still handle the else case for clearing (onChange(name, '') ) only when the previous selectedRegion was non-empty and the new one is empty to avoid stale emissions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@src/components/RegionSelector/index.tsx`:
- Around line 86-94: The emitter effect is firing with a stale selectedRegion
when the external value prop triggers a state update in the same render flush;
to fix, change the useEffect (the effect that currently depends on [onChange,
selectedRegion, name, regions, value]) so it only emits when selectedRegion
actually transitions: add a prevSelectedRegionRef (or similar) to store the
previous selectedRegion.iso_3166_1, compare prev vs current inside the effect,
and only call onChange(name, selectedRegion.iso_3166_1) when they differ; after
emitting update the ref. Ensure you still handle the else case for clearing
(onChange(name, '') ) only when the previous selectedRegion was non-empty and
the new one is empty to avoid stale emissions.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 38de2905-d9d2-4394-8e35-119b739d9b3e
📒 Files selected for processing (1)
src/components/RegionSelector/index.tsx
There was a problem hiding this comment.
Pull request overview
This PR addresses an issue in the discover filter UI where reopening the filter panel could clear selected streaming service providers due to RegionSelector emitting an empty region during its initial sync.
Changes:
- Prevent
RegionSelectorfrom emitting an empty region during initial state sync when a non-emptyvalueis provided. - Update the
useEffectdependency list to includevalueso the sync logic reacts to external prop changes.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Description
This PR fixes an issue where reopening the filter panel could clear the selected streaming service filters.
The problem was caused by
RegionSelectoremittingonChange('watchRegion', '')during its initial state sync, before the region value from the parent was applied.WatchProviderSelectorinterpreted this as a real change and cleared the selected providers, which also removed the filter from the URL.How Has This Been Tested?
Screenshots / Logs (if applicable)
Checklist:
pnpm buildpnpm i18n:extractSummary by CodeRabbit