From fbdb599c7942cdb730fcee52340e382b6e79d378 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Wed, 12 Nov 2025 19:03:15 +0200 Subject: [PATCH] verify country code we get from geo --- src/components/InternationalPhoneCodeSelect.tsx | 11 +++++++---- src/lib/international-telephone-codes.ts | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/components/InternationalPhoneCodeSelect.tsx b/src/components/InternationalPhoneCodeSelect.tsx index 3a20108452..6541671794 100644 --- a/src/components/InternationalPhoneCodeSelect.tsx +++ b/src/components/InternationalPhoneCodeSelect.tsx @@ -3,14 +3,15 @@ import {Image} from 'expo-image' import {msg} from '@lingui/macro' import {useLingui} from '@lingui/react' -import {getCountriesWithTelephoneCodes} from '#/lib/international-telephone-codes' +import { + getCountriesWithTelephoneCodes, + getDefaultCountry, +} from '#/lib/international-telephone-codes' import {isWeb} from '#/platform/detection' import {useGeolocationStatus} from '#/state/geolocation' import {atoms as a, web} from '#/alf' import * as Select from '#/components/Select' -export const DEFAULT_PHONE_COUNTRY = 'US' - /** * Country picker for a phone number input * @@ -27,7 +28,9 @@ export function InternationalPhoneCodeSelect({ const {_, i18n} = useLingui() const {location} = useGeolocationStatus() - const defaultCountry = location?.countryCode || DEFAULT_PHONE_COUNTRY + const defaultCountry = useMemo(() => { + return getDefaultCountry(location) + }, [location]) const items = useMemo(() => { const telCountryMap = getCountriesWithTelephoneCodes(i18n) diff --git a/src/lib/international-telephone-codes.ts b/src/lib/international-telephone-codes.ts index cbdc0cdad5..8750f773d2 100644 --- a/src/lib/international-telephone-codes.ts +++ b/src/lib/international-telephone-codes.ts @@ -1,4 +1,4 @@ -import {type I18n} from '@lingui/core' +import {type I18n, i18n} from '@lingui/core' import {msg} from '@lingui/macro' export function getCountriesWithTelephoneCodes(i18n: I18n) { @@ -1505,3 +1505,17 @@ export function getCountriesWithTelephoneCodes(i18n: I18n) { }, } } + +const DEFAULT_PHONE_COUNTRY = 'US' + +export function getDefaultCountry(location?: {countryCode?: string}) { + const supportedCountries = Object.keys(getCountriesWithTelephoneCodes(i18n)) // ok to use default i18n here because we don't use the name + + if ( + location?.countryCode && + supportedCountries.includes(location.countryCode.toUpperCase()) + ) { + return location.countryCode.toUpperCase() + } + return DEFAULT_PHONE_COUNTRY +}