|
1 | 1 | import { ethers } from "ethers"; |
2 | 2 | import { useDispatch, useSelector } from "react-redux"; |
3 | | -import { useContext, useEffect, useState, useRef } from "react"; |
| 3 | +import { useContext, useEffect, useState } from "react"; |
4 | 4 | import { Currency, Network, onNetworkChange, onTokenChange } from "../types"; |
5 | 5 | import { NATIVE_TOKEN_ADDRESS } from "../consts"; |
6 | 6 |
|
@@ -96,7 +96,9 @@ export const Input = ({ |
96 | 96 | const sameChainSwapsEnabled = useSelector( |
97 | 97 | (state: any) => state.customSettings.sameChainSwapsEnabled |
98 | 98 | ); |
99 | | - const initialAmount = useSelector((state:any)=> state.customSettings.initialAmount); |
| 99 | + const initialAmount = useSelector( |
| 100 | + (state: any) => state.customSettings.initialAmount |
| 101 | + ); |
100 | 102 |
|
101 | 103 | function updateNetwork(network: Network) { |
102 | 104 | dispatch(setSourceChain(network?.chainId)); |
@@ -142,7 +144,7 @@ export const Input = ({ |
142 | 144 | ) ?? _supportedNetworks?.[0] |
143 | 145 | ); |
144 | 146 | } |
145 | | - }, [allNetworks]); |
| 147 | + }, [allNetworks, defaultSourceNetwork]); |
146 | 148 |
|
147 | 149 | // For Input & tokens |
148 | 150 | const inputAmountFromReduxState = useSelector( |
@@ -233,6 +235,18 @@ export const Input = ({ |
233 | 235 | } |
234 | 236 | }, [allSourceTokens]); |
235 | 237 |
|
| 238 | + // to set default source token when changed |
| 239 | + useEffect(() => { |
| 240 | + if (defaultSourceTokenAddress && allSourceTokens) { |
| 241 | + const _token = |
| 242 | + allSourceTokens?.filter( |
| 243 | + (x: Currency) => |
| 244 | + x.address.toLowerCase() === defaultSourceTokenAddress.toLowerCase() |
| 245 | + )?.[0] ?? fallbackToUSDC(); |
| 246 | + _setSourceToken(_token); |
| 247 | + } |
| 248 | + }, [defaultSourceTokenAddress, allSourceTokens]); |
| 249 | + |
236 | 250 | const [_sourceToken, _setSourceToken] = useState<Currency>(); |
237 | 251 | useDebounce( |
238 | 252 | () => { |
@@ -305,21 +319,10 @@ export const Input = ({ |
305 | 319 | } else formateAndParseAmount(balance); |
306 | 320 | } |
307 | 321 |
|
308 | | - // to set the initialAmount if any. To be executed only on first render |
309 | | - const firstRender = useRef(true); |
| 322 | + // to set the initialAmount if any |
310 | 323 | useEffect(() => { |
311 | | - if (initialAmount && sourceToken && firstRender.current) { |
312 | | - const truncatedValue = truncateDecimalValue( |
313 | | - initialAmount, |
314 | | - sourceToken?.decimals |
315 | | - ); |
316 | | - onChangeInput(truncatedValue); |
317 | | - firstRender.current = false; |
318 | | - } else if (!firstRender.current) { |
319 | | - // Reset source amount on mount |
320 | | - inputAmountFromReduxState && dispatch(setSourceAmount(null)); |
321 | | - } |
322 | | - }, [initialAmount, sourceToken]); |
| 324 | + if (initialAmount) onChangeInput(initialAmount); |
| 325 | + }, [initialAmount]); |
323 | 326 |
|
324 | 327 | useEffect(() => { |
325 | 328 | // resetting the source chain on unmount |
|
0 commit comments