verify country code we get from geo

This commit is contained in:
Samuel Newman
2025-11-12 19:03:15 +02:00
parent 1d53c38f73
commit fbdb599c79
2 changed files with 22 additions and 5 deletions
@@ -3,14 +3,15 @@ import {Image} from 'expo-image'
import {msg} from '@lingui/macro' import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react' 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 {isWeb} from '#/platform/detection'
import {useGeolocationStatus} from '#/state/geolocation' import {useGeolocationStatus} from '#/state/geolocation'
import {atoms as a, web} from '#/alf' import {atoms as a, web} from '#/alf'
import * as Select from '#/components/Select' import * as Select from '#/components/Select'
export const DEFAULT_PHONE_COUNTRY = 'US'
/** /**
* Country picker for a phone number input * Country picker for a phone number input
* *
@@ -27,7 +28,9 @@ export function InternationalPhoneCodeSelect({
const {_, i18n} = useLingui() const {_, i18n} = useLingui()
const {location} = useGeolocationStatus() const {location} = useGeolocationStatus()
const defaultCountry = location?.countryCode || DEFAULT_PHONE_COUNTRY const defaultCountry = useMemo(() => {
return getDefaultCountry(location)
}, [location])
const items = useMemo(() => { const items = useMemo(() => {
const telCountryMap = getCountriesWithTelephoneCodes(i18n) const telCountryMap = getCountriesWithTelephoneCodes(i18n)
+15 -1
View File
@@ -1,4 +1,4 @@
import {type I18n} from '@lingui/core' import {type I18n, i18n} from '@lingui/core'
import {msg} from '@lingui/macro' import {msg} from '@lingui/macro'
export function getCountriesWithTelephoneCodes(i18n: I18n) { 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
}