22// SPDX-License-Identifier: BUSL-1.1
33// NOTE: Converts to Apache-2.0 on 2029-06-11 per LICENSE.
44
5- import { alpha2ToAlpha3 } from 'i18n-iso-countries' ;
6- import { memo , useCallback , useEffect , useMemo , useState } from 'react' ;
5+ import { memo , useCallback } from 'react' ;
76import { FlatList , TouchableOpacity , View } from 'react-native' ;
87import { Spinner , XStack , YStack } from 'tamagui' ;
9- import { useNavigation } from '@react-navigation/native' ;
10- import type { NativeStackNavigationProp } from '@react-navigation/native-stack' ;
118
129import { commonNames } from '@selfxyz/common/constants/countries' ;
13- import { SdkEvents , useSelfClient } from '@selfxyz/mobile-sdk-alpha' ;
10+ import {
11+ SdkEvents ,
12+ useCountries ,
13+ useSelfClient ,
14+ } from '@selfxyz/mobile-sdk-alpha' ;
1415
1516import { RoundFlag } from '@/components/flag/RoundFlag' ;
1617import { DocumentFlowNavBar } from '@/components/NavBar/DocumentFlowNavBar' ;
1718import { BodyText } from '@/components/typography/BodyText' ;
18- import type { RootStackParamList } from '@/navigation' ;
1919import { black , slate100 , slate500 } from '@/utils/colors' ;
2020import { advercase , dinot } from '@/utils/fonts' ;
2121import { buttonTap } from '@/utils/haptic' ;
22- import { getCountry } from '@/utils/locale' ;
23-
24- interface CountryData {
25- [ countryCode : string ] : string [ ] ;
26- }
2722
2823interface CountryListItem {
2924 key : string ;
@@ -61,13 +56,11 @@ const CountryItem = memo<{
6156CountryItem . displayName = 'CountryItem' ;
6257
6358const CountryPickerScreen : React . FC = ( ) => {
64- const [ countryData , setCountryData ] = useState < CountryData > ( { } ) ;
65- const [ loading , setLoading ] = useState ( true ) ;
66- const [ userCountryCode , setUserCountryCode ] = useState < string | null > ( null ) ;
67- const navigation =
68- useNavigation < NativeStackNavigationProp < RootStackParamList > > ( ) ;
6959 const selfClient = useSelfClient ( ) ;
7060
61+ const { countryData, countryList, loading, userCountryCode, showSuggestion } =
62+ useCountries ( ) ;
63+
7164 const onPressCountry = useCallback (
7265 ( countryCode : string ) => {
7366 buttonTap ( ) ;
@@ -91,78 +84,16 @@ const CountryPickerScreen: React.FC = () => {
9184 countryName : countryName ,
9285 documentTypes : documentTypes ,
9386 } ) ;
94-
95- navigation . navigate ( 'IDPicker' , {
96- countryCode,
97- documentTypes,
98- } as never ) ;
9987 } else {
100- navigation . navigate ( 'ComingSoon' , { countryCode } as never ) ;
88+ selfClient . emit ( SdkEvents . PROVING_PASSPORT_NOT_SUPPORTED , {
89+ countryCode : countryCode ,
90+ documentCategory : null ,
91+ } ) ;
10192 }
10293 } ,
103- [ countryData , navigation , selfClient ] ,
94+ [ countryData , selfClient ] ,
10495 ) ;
10596
106- useEffect ( ( ) => {
107- const fetchCountryData = async ( ) => {
108- try {
109- const response = await fetch ( 'https://api.staging.self.xyz/id-picker' ) ;
110- const result = await response . json ( ) ;
111-
112- if ( result . status === 'success' ) {
113- setCountryData ( result . data ) ;
114- if ( __DEV__ ) {
115- console . log ( 'Set country data:' , result . data ) ;
116- }
117- } else {
118- console . error ( 'API returned non-success status:' , result . status ) ;
119- }
120- } catch ( error ) {
121- console . error ( 'Error fetching country data:' , error ) ;
122- } finally {
123- setLoading ( false ) ;
124- }
125- } ;
126-
127- fetchCountryData ( ) ;
128- } , [ ] ) ;
129-
130- useEffect ( ( ) => {
131- try {
132- const countryCode2Letter = getCountry ( ) ; // Returns 2-letter code like "US"
133- if ( countryCode2Letter ) {
134- const countryCode3Letter = alpha2ToAlpha3 ( countryCode2Letter ) ;
135- if (
136- countryCode3Letter &&
137- commonNames [ countryCode3Letter as keyof typeof commonNames ]
138- ) {
139- setUserCountryCode ( countryCode3Letter ) ;
140- if ( __DEV__ ) {
141- console . log ( 'Detected user country:' , countryCode3Letter ) ;
142- }
143- }
144- }
145- } catch ( error ) {
146- console . error ( 'Error detecting user country:' , error ) ;
147- }
148- } , [ ] ) ;
149-
150- const countryList = useMemo ( ( ) => {
151- const allCountries = Object . keys ( countryData ) . map ( countryCode => ( {
152- key : countryCode ,
153- countryCode,
154- } ) ) ;
155-
156- // Exclude user country from main list since it's shown separately
157- if ( userCountryCode && countryData [ userCountryCode ] ) {
158- return allCountries . filter ( c => c . countryCode !== userCountryCode ) ;
159- }
160-
161- return allCountries ;
162- } , [ countryData , userCountryCode ] ) ;
163-
164- const showSuggestion = userCountryCode && countryData [ userCountryCode ] ;
165-
16697 const renderItem = useCallback (
16798 ( { item } : { item : CountryListItem } ) => (
16899 < CountryItem countryCode = { item . countryCode } onSelect = { onPressCountry } />
@@ -219,7 +150,9 @@ const CountryPickerScreen: React.FC = () => {
219150 SUGGESTION
220151 </ BodyText >
221152 < CountryItem
222- countryCode = { userCountryCode }
153+ countryCode = {
154+ userCountryCode as string /*safe due to showSuggestion*/
155+ }
223156 onSelect = { onPressCountry }
224157 />
225158 < BodyText
0 commit comments