@@ -15,25 +15,32 @@ import {
15
15
} from "../fiat/currencies.js" ;
16
16
import type { SupportedChainAndTokens } from "../swap/useSwapSupportedChains.js" ;
17
17
18
- // handle states for token and chain selection
18
+ type SupportedSourcesInputData = {
19
+ chain : Chain ;
20
+ tokens : {
21
+ address : string ;
22
+ buyWithCryptoEnabled : boolean ;
23
+ buyWithFiatEnabled : boolean ;
24
+ name : string ;
25
+ symbol : string ;
26
+ } [ ] ;
27
+ } ;
19
28
20
- export function useUISelectionStates ( options : {
29
+ // handle states for token and chain selection
30
+ export function useToTokenSelectionStates ( options : {
21
31
payOptions : PayUIOptions ;
22
32
supportedDestinations : SupportedChainAndTokens ;
23
33
} ) {
24
- const activeChain = useActiveWalletChain ( ) ;
25
34
const { payOptions, supportedDestinations } = options ;
26
-
35
+ // --------------------------------------------------------------------------
27
36
// buy token amount ---------------------------------------------------------
28
37
// NOTE - for transaction / direct payment modes, the token amount is set when the user tap continue
29
38
const prefillBuy = ( payOptions as FundWalletOptions ) ?. prefillBuy ;
39
+ const activeChain = useActiveWalletChain ( ) ;
30
40
const initialTokenAmount = prefillBuy ?. amount || "" ;
31
-
32
41
const [ tokenAmount , setTokenAmount ] = useState < string > ( initialTokenAmount ) ;
33
42
const deferredTokenAmount = useDebouncedValue ( tokenAmount , 300 ) ;
34
43
35
- // --------------------------------------------------------------------------
36
-
37
44
// Destination chain and token selection -----------------------------------
38
45
const [ toChain , setToChain ] = useState < Chain > (
39
46
// use prefill chain if available
@@ -52,27 +59,73 @@ export function useUISelectionStates(options: {
52
59
( payOptions . mode === "direct_payment" && payOptions . paymentInfo . token ) ||
53
60
NATIVE_TOKEN ,
54
61
) ;
62
+
63
+ return {
64
+ toChain,
65
+ setToChain,
66
+ toToken,
67
+ setToToken,
68
+ tokenAmount,
69
+ setTokenAmount,
70
+ deferredTokenAmount,
71
+ } ;
72
+ }
73
+
74
+ export function useFromTokenSelectionStates ( options : {
75
+ payOptions : PayUIOptions ;
76
+ supportedSources : SupportedSourcesInputData [ ] ;
77
+ } ) {
78
+ const { payOptions, supportedSources } = options ;
79
+
55
80
// --------------------------------------------------------------------------
81
+ const firstSupportedSource = supportedSources ?. length
82
+ ? supportedSources [ 0 ]
83
+ : undefined ;
56
84
57
85
// Source token and chain selection ---------------------------------------------------
58
- const [ fromChain , setFromChain ] = useState < Chain > (
59
- // use prefill chain if available
86
+ const [ fromChain_ , setFromChain ] = useState < Chain > ( ) ;
87
+
88
+ // use prefill chain if available
89
+ const fromChainDevSpecified =
60
90
( payOptions . buyWithCrypto !== false &&
61
91
payOptions . buyWithCrypto ?. prefillSource ?. chain ) ||
62
- ( payOptions . mode === "transaction" && payOptions . transaction ?. chain ) ||
63
- ( payOptions . mode === "direct_payment" && payOptions . paymentInfo ?. chain ) ||
64
- // default to polygon
65
- polygon ,
66
- ) ;
92
+ ( payOptions . mode === "transaction" && payOptions . transaction ?. chain ) ||
93
+ ( payOptions . mode === "direct_payment" && payOptions . paymentInfo ?. chain ) ;
94
+
95
+ const fromChainFromApi = firstSupportedSource ?. chain
96
+ ? firstSupportedSource . chain
97
+ : undefined ;
67
98
68
- const [ fromToken , setFromToken ] = useState < ERC20OrNativeToken > (
69
- // use prefill token if available
99
+ const fromChain =
100
+ fromChain_ || fromChainDevSpecified || fromChainFromApi || polygon ;
101
+
102
+ const [ fromToken_ , setFromToken ] = useState < ERC20OrNativeToken > ( ) ;
103
+
104
+ // use prefill token if available
105
+ const fromTokenDevSpecified =
70
106
( payOptions . buyWithCrypto !== false &&
71
107
payOptions . buyWithCrypto ?. prefillSource ?. token ) ||
72
- ( payOptions . mode === "direct_payment" && payOptions . paymentInfo . token ) ||
73
- // default to native token
74
- NATIVE_TOKEN ,
75
- ) ;
108
+ ( payOptions . mode === "direct_payment" && payOptions . paymentInfo . token ) ;
109
+
110
+ // May be updated in the future
111
+ const fromTokenFromApi = NATIVE_TOKEN ;
112
+
113
+ // supported tokens query in here
114
+ const fromToken =
115
+ fromToken_ || fromTokenDevSpecified || fromTokenFromApi || NATIVE_TOKEN ;
116
+
117
+ return {
118
+ fromChain,
119
+ setFromChain,
120
+ fromToken,
121
+ setFromToken,
122
+ } ;
123
+ }
124
+
125
+ export function useFiatCurrencySelectionStates ( options : {
126
+ payOptions : PayUIOptions ;
127
+ } ) {
128
+ const { payOptions } = options ;
76
129
77
130
// --------------------------------------------------------------------------
78
131
const devSpecifiedDefaultCurrency =
@@ -89,20 +142,7 @@ export function useUISelectionStates(options: {
89
142
) ;
90
143
91
144
return {
92
- tokenAmount,
93
- setTokenAmount,
94
-
95
- toChain,
96
- setToChain,
97
- deferredTokenAmount,
98
- fromChain,
99
- setFromChain,
100
- toToken,
101
- setToToken,
102
- fromToken,
103
- setFromToken,
104
145
selectedCurrency,
105
-
106
146
setSelectedCurrency,
107
147
} ;
108
148
}
0 commit comments