@@ -2,9 +2,10 @@ import { useMemo, useState } from 'react';
2
2
import { useDispatch , useSelector } from 'react-redux' ;
3
3
4
4
import { A , pipe } from '@mobily/ts-belt' ;
5
- import { CommonActions , useNavigation } from '@react-navigation/native' ;
5
+ import { CommonActions , useNavigation , useRoute } from '@react-navigation/native' ;
6
6
import { isRejected } from '@reduxjs/toolkit' ;
7
7
8
+ import { UnreachableCaseError } from '@suite-common/suite-utils' ;
8
9
import {
9
10
type AccountType ,
10
11
NORMAL_ACCOUNT_TYPE ,
@@ -49,8 +50,10 @@ export type AddCoinAccountNavigationProps = StackToStackCompositeNavigationProps
49
50
RootStackParamList
50
51
> ;
51
52
53
+ export type AddCoinEnabledAccountType = Exclude < AccountType , 'coinjoin' | 'imported' | 'ledger' > ;
54
+
52
55
export const accountTypeTranslationKeys : Record <
53
- Exclude < AccountType , 'coinjoin' | 'imported' | 'ledger' > ,
56
+ AddCoinEnabledAccountType ,
54
57
{ titleKey : TxKeyPath ; subtitleKey : TxKeyPath ; descKey : TxKeyPath }
55
58
> = {
56
59
normal : {
@@ -78,6 +81,7 @@ export const accountTypeTranslationKeys: Record<
78
81
export const useAddCoinAccount = ( ) => {
79
82
const dispatch = useDispatch ( ) ;
80
83
const { translate } = useTranslate ( ) ;
84
+ const { name : routeName } = useRoute ( ) ;
81
85
82
86
const supportedNetworkSymbols = useSelector ( selectDiscoveryNetworkSymbols ) ;
83
87
const deviceAccounts = useSelector ( ( state : AccountsRootState & DeviceRootState ) =>
@@ -98,7 +102,7 @@ export const useAddCoinAccount = () => {
98
102
} = useAddCoinAccountAlerts ( ) ;
99
103
100
104
const [ networkSymbolWithTypeToBeAdded , setNetworkSymbolWithTypeToBeAdded ] = useState <
101
- [ NetworkSymbol , AccountType ] | null
105
+ [ NetworkSymbol , AddCoinEnabledAccountType ] | null
102
106
> ( null ) ;
103
107
104
108
const availableNetworkAccountTypes = useMemo ( ( ) => {
@@ -133,7 +137,11 @@ export const useAddCoinAccount = () => {
133
137
134
138
const setDefaultAccountToBeAdded = ( symbol : NetworkSymbol ) => {
135
139
const defaultType = getAvailableAccountTypesForNetworkSymbol ( { symbol } ) [ 0 ] ;
136
- setNetworkSymbolWithTypeToBeAdded ( [ symbol , defaultType ] ) ;
140
+ const type = defaultType as AddCoinEnabledAccountType ;
141
+
142
+ if ( type ) {
143
+ setNetworkSymbolWithTypeToBeAdded ( [ symbol , type ] ) ;
144
+ }
137
145
} ;
138
146
139
147
const navigateToSuccessorScreen = ( {
@@ -178,9 +186,64 @@ export const useAddCoinAccount = () => {
178
186
} ,
179
187
} ) ;
180
188
break ;
189
+ case 'trade' :
190
+ if (
191
+ routeName === AddCoinAccountStackRoutes . AddCoinDiscoveryFinished ||
192
+ routeName === AddCoinAccountStackRoutes . SelectAccountType
193
+ ) {
194
+ navigation . popToTop ( ) ;
195
+ }
196
+ break ;
197
+
198
+ default :
199
+ throw new UnreachableCaseError ( flowType ) ;
181
200
}
182
201
} ;
183
202
203
+ const navigateToFailureScreen = ( {
204
+ flowType,
205
+ errorString,
206
+ } : {
207
+ flowType : AddCoinFlowType ;
208
+ errorString : string | undefined ;
209
+ } ) => {
210
+ let screen : AppTabsRoutes ;
211
+ switch ( flowType ) {
212
+ case 'home' :
213
+ case 'receive' :
214
+ screen = AppTabsRoutes . HomeStack ;
215
+ break ;
216
+ case 'accounts' :
217
+ screen = AppTabsRoutes . AccountsStack ;
218
+ break ;
219
+ case 'trade' :
220
+ screen = AppTabsRoutes . TradeStack ;
221
+ break ;
222
+
223
+ default :
224
+ throw new UnreachableCaseError ( flowType ) ;
225
+ }
226
+
227
+ if ( errorString === 'Passphrase is incorrect' ) {
228
+ showPassphraseAuthAlert ( ) ;
229
+ } else {
230
+ showGeneralErrorAlert ( ) ;
231
+ }
232
+ navigation . dispatch (
233
+ CommonActions . reset ( {
234
+ index : 0 ,
235
+ routes : [
236
+ {
237
+ name : RootStackRoutes . AppTabs ,
238
+ params : {
239
+ screen,
240
+ } ,
241
+ } ,
242
+ ] ,
243
+ } ) ,
244
+ ) ;
245
+ } ;
246
+
184
247
const clearNetworkWithTypeToBeAdded = ( ) => {
185
248
setNetworkSymbolWithTypeToBeAdded ( null ) ;
186
249
} ;
@@ -211,13 +274,16 @@ export const useAddCoinAccount = () => {
211
274
return true ;
212
275
} ;
213
276
214
- const handleAccountTypeSelection = flowType => {
277
+ const handleAccountTypeSelection = ( flowType : AddCoinFlowType ) => {
215
278
if ( networkSymbolWithTypeToBeAdded ) {
216
279
clearNetworkWithTypeToBeAdded ( ) ;
217
- navigation . navigate ( AddCoinAccountStackRoutes . SelectAccountType , {
218
- accountType : networkSymbolWithTypeToBeAdded [ 1 ] ?? NORMAL_ACCOUNT_TYPE ,
219
- networkSymbol : networkSymbolWithTypeToBeAdded [ 0 ] ,
220
- flowType,
280
+ navigation . navigate ( RootStackRoutes . AddCoinAccountStack , {
281
+ screen : AddCoinAccountStackRoutes . SelectAccountType ,
282
+ params : {
283
+ accountType : networkSymbolWithTypeToBeAdded [ 1 ] ?? NORMAL_ACCOUNT_TYPE ,
284
+ networkSymbol : networkSymbolWithTypeToBeAdded [ 0 ] ,
285
+ flowType,
286
+ } ,
221
287
} ) ;
222
288
}
223
289
} ;
@@ -281,29 +347,7 @@ export const useAddCoinAccount = () => {
281
347
! firstHiddenEmptyAccount && // Do not show error if we are just making the first hidden empty account visible
282
348
isRejected ( newAccountResult )
283
349
) {
284
- let screen = AppTabsRoutes . HomeStack ;
285
- if ( flowType === 'accounts' ) {
286
- screen = AppTabsRoutes . AccountsStack ;
287
- }
288
-
289
- if ( newAccountResult . payload === 'Passphrase is incorrect' ) {
290
- showPassphraseAuthAlert ( ) ;
291
- } else {
292
- showGeneralErrorAlert ( ) ;
293
- }
294
- navigation . dispatch (
295
- CommonActions . reset ( {
296
- index : 0 ,
297
- routes : [
298
- {
299
- name : RootStackRoutes . AppTabs ,
300
- params : {
301
- screen,
302
- } ,
303
- } ,
304
- ] ,
305
- } ) ,
306
- ) ;
350
+ navigateToFailureScreen ( { flowType, errorString : newAccountResult . payload } ) ;
307
351
}
308
352
} ;
309
353
@@ -322,10 +366,21 @@ export const useAddCoinAccount = () => {
322
366
323
367
if ( ! enabledDiscoveryNetworkSymbols . includes ( symbol ) ) {
324
368
clearNetworkWithTypeToBeAdded ( ) ;
325
- navigation . replace ( AddCoinAccountStackRoutes . AddCoinDiscoveryRunning , {
326
- networkSymbol : symbol ,
327
- flowType,
328
- } ) ;
369
+
370
+ if ( flowType === 'trade' ) {
371
+ navigation . navigate ( RootStackRoutes . AddCoinAccountStack , {
372
+ screen : AddCoinAccountStackRoutes . AddCoinDiscoveryRunning ,
373
+ params : {
374
+ networkSymbol : symbol ,
375
+ flowType,
376
+ } ,
377
+ } ) ;
378
+ } else {
379
+ navigation . replace ( AddCoinAccountStackRoutes . AddCoinDiscoveryRunning , {
380
+ networkSymbol : symbol ,
381
+ flowType,
382
+ } ) ;
383
+ }
329
384
330
385
return ;
331
386
}
@@ -339,6 +394,18 @@ export const useAddCoinAccount = () => {
339
394
}
340
395
} ;
341
396
397
+ const handleAccountTypeConfirmation = async ( flowType : AddCoinFlowType ) => {
398
+ if ( networkSymbolWithTypeToBeAdded ) {
399
+ clearNetworkWithTypeToBeAdded ( ) ;
400
+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
401
+ await addCoinAccount ( {
402
+ symbol : networkSymbolWithTypeToBeAdded [ 0 ] ,
403
+ accountType : networkSymbolWithTypeToBeAdded [ 1 ] ,
404
+ flowType,
405
+ } ) ;
406
+ }
407
+ } ;
408
+
342
409
return {
343
410
supportedNetworkSymbols,
344
411
onSelectedNetworkItem,
@@ -349,5 +416,6 @@ export const useAddCoinAccount = () => {
349
416
clearNetworkWithTypeToBeAdded,
350
417
getAccountTypeToBeAddedName,
351
418
handleAccountTypeSelection,
419
+ handleAccountTypeConfirmation,
352
420
} ;
353
421
} ;
0 commit comments