Skip to content

Conversation

@georgeweiler
Copy link
Contributor

@georgeweiler georgeweiler commented Jan 15, 2026

Explanation

Refactors userRegion state in RampsController to store full Country and State objects instead of just the region code string.

Changes

  • Changed userRegion from string | null to UserRegion | null
  • UserRegion type contains:
    • country: Country - Full country object
    • state: State | null - State object if applicable, null otherwise
    • regionCode: string - Normalized region code (e.g., "us-ut", "fr")
  • Updated setUserRegion() and updateUserRegion() to fetch countries and find matching Country/State objects
  • Updated internal methods to use userRegion.regionCode for API calls
  • Exported UserRegion type for consumers
  • Removed redundant normalization calls since regionCode is normalized at creation

Benefits

  • Consumers can access full region data (e.g., country.phone.template) without re-parsing region codes
  • Single source of truth for region data
  • Eliminates duplicate region matching logic across consumers
  • Type-safe access to country and state objects

Breaking Changes

The userRegion variable name remains the same but the type changed from string to UserRegion.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Introduces structured region handling in RampsController.

  • BREAKING: userRegion now stores a UserRegion object (country, state|null, regionCode); access code via userRegion.regionCode. Exports UserRegion.
  • Refactors updateUserRegion()/setUserRegion() to fetch getCountries(), map codes to Country/State, avoid overwriting existing region unless forceRefresh, and clear tokens on region change. Adds robust handling for missing/failed geolocation/countries/eligibility.
  • Adds findRegionFromCode() helper; updates init() to skip geolocation when region already set and to fetch tokens using regionCode.
  • Updates updateEligibility()/getTokens() to use userRegion.regionCode and guard state updates by current region; expands tests for caching, race conditions, and error cases.

Written by Cursor Bugbot for commit eda30d6. This will update automatically on new commits. Configure here.

@georgeweiler georgeweiler requested a review from a team as a code owner January 15, 2026 22:51
@georgeweiler georgeweiler requested a review from a team as a code owner January 15, 2026 22:54
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

// This ensures that once a region is set (either via geolocation or manual selection),
// it will not be overwritten by subsequent geolocation fetches.
if (this.state.userRegion && !options?.forceRefresh) {
return this.state.userRegion;
Copy link

Choose a reason for hiding this comment

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

Missing migration for persisted string userRegion causes silent failures

High Severity

Since userRegion has persist: true, users upgrading from a previous version where userRegion was a string will have that string loaded into state. The truthiness check at line 521 passes for a string like "us", causing updateUserRegion() to return the string directly instead of a UserRegion object. Subsequently, all userRegion?.regionCode accesses return undefined (strings lack this property), causing the userRegionCode === undefined checks in updateEligibility and getTokens to always pass, leading to incorrect state updates regardless of the actual region.

Additional Locations (1)

Fix in Cursor Fix in Web

@georgeweiler georgeweiler added this pull request to the merge queue Jan 16, 2026
Merged via the queue into main with commit ca11ec3 Jan 16, 2026
294 checks passed
@georgeweiler georgeweiler deleted the ramps-controller-user-region-update branch January 16, 2026 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants