diff --git a/components/Input/CurrencyFormField.tsx b/components/Input/CurrencyFormField.tsx index 5e60f057e..511b7b832 100644 --- a/components/Input/CurrencyFormField.tsx +++ b/components/Input/CurrencyFormField.tsx @@ -1,5 +1,5 @@ import { useFormikContext } from "formik"; -import { FC, useCallback, useEffect, useMemo, useState } from "react"; +import { Dispatch, FC, SetStateAction, useCallback, useEffect, useMemo, useState } from "react"; import { SwapDirection, SwapFormValues } from "../DTOs/SwapFormValues"; import { SelectMenuItem } from "../Select/Shared/Props/selectMenuItem"; import PopoverSelectWrapper from "../Select/Popover/PopoverSelectWrapper"; @@ -31,6 +31,8 @@ const CurrencyFormField: FC<{ direction: SwapDirection }> = ({ direction }) => { setFieldValue, } = useFormikContext(); + const [currencyIsSetManually, setCurrencyIsSetManually] = useState(false) + const { to, fromCurrency, toCurrency, from, currencyGroup, destination_address } = values const name = direction === 'from' ? 'fromCurrency' : 'toCurrency'; const query = useQueryState() @@ -94,7 +96,6 @@ const CurrencyFormField: FC<{ direction: SwapDirection }> = ({ direction }) => { } }, [to, query, routes]) - useEffect(() => { if (direction !== "from") return @@ -127,8 +128,15 @@ const CurrencyFormField: FC<{ direction: SwapDirection }> = ({ direction }) => { if (name === "toCurrency" && toCurrency && !isLoading && routes) { const value = routes.data?.find(r => r.name === to?.name)?.tokens?.find(r => r.symbol === toCurrency?.symbol) if (!value) return - - setFieldValue(name, value) + + if (!currencyIsSetManually && (value?.status !== "active" || error?.code === LSAPIKnownErrorCode.ROUTE_NOT_FOUND_ERROR)) { + const default_currency = currencyMenuItems?.find(c => c.baseObject?.status === "active") + if (default_currency) { + setFieldValue(name, default_currency.baseObject, true) + } + } else { + setFieldValue(name, value) + } } }, [fromCurrency, currencyGroup, name, to, routes, error, isLoading]) @@ -137,7 +145,14 @@ const CurrencyFormField: FC<{ direction: SwapDirection }> = ({ direction }) => { const value = routes.data?.find(r => r.name === from?.name)?.tokens?.find(r => r.symbol === fromCurrency?.symbol) if (!value) return - setFieldValue(name, value) + if (!currencyIsSetManually && (value?.status !== "active" || error?.code === LSAPIKnownErrorCode.ROUTE_NOT_FOUND_ERROR)) { + const default_currency = currencyMenuItems?.find(c => c.baseObject?.status === "active") + if (default_currency) { + setFieldValue(name, default_currency.baseObject, true) + } + } else { + setFieldValue(name, value) + } } }, [toCurrency, currencyGroup, name, from, routes, error, isLoading]) @@ -158,9 +173,9 @@ const CurrencyFormField: FC<{ direction: SwapDirection }> = ({ direction }) => { } }, [network, token]) - const handleSelect = useCallback((item: SelectMenuItem) => { setFieldValue(name, item.baseObject, true) + setCurrencyIsSetManually(true) }, [name, direction, toCurrency, fromCurrency, from, to]) const isLocked = direction === 'from' ? query?.lockFromAsset diff --git a/components/Select/Popover/PopoverSelectWrapper.tsx b/components/Select/Popover/PopoverSelectWrapper.tsx index 823ebff34..33c5b8994 100644 --- a/components/Select/Popover/PopoverSelectWrapper.tsx +++ b/components/Select/Popover/PopoverSelectWrapper.tsx @@ -26,7 +26,7 @@ export default function PopoverSelectWrapper({ const handleSelect = useCallback((item: SelectMenuItem) => { setValue(item) setShowModal(false) - }, []) + }, [setValue]) if (!values) return diff --git a/components/Swap/Withdraw/Wallet/SolanaWalletWithdraw.tsx b/components/Swap/Withdraw/Wallet/SolanaWalletWithdraw.tsx index d517a2a73..1db09e57a 100644 --- a/components/Swap/Withdraw/Wallet/SolanaWalletWithdraw.tsx +++ b/components/Swap/Withdraw/Wallet/SolanaWalletWithdraw.tsx @@ -58,7 +58,7 @@ const SolanaWalletWithdrawStep: FC = ({ network, callData, sw if (!wallet) { return - } + } return (
diff --git a/context/validationErrorContext.tsx b/context/validationErrorContext.tsx index b31e362f7..0db678934 100644 --- a/context/validationErrorContext.tsx +++ b/context/validationErrorContext.tsx @@ -87,14 +87,14 @@ export const ValidationProvider: React.FC<{ children: ReactNode }> = ({ children } } else if (currencyGroup?.status === 'not_found' || toCurrency?.status === 'not_found' || fromCurrency?.status === 'not_found') { - + validationMessage = 'Please change one of the selected tokens'; validationDetails = { title: 'Route Unavailable', type: 'warning', icon: }; } return ( {children}