diff --git a/packages/widget/src/components/SwapRoutes/SwapRoutes.tsx b/packages/widget/src/components/SwapRoutes/SwapRoutes.tsx index 170010484..4e9591c70 100644 --- a/packages/widget/src/components/SwapRoutes/SwapRoutes.tsx +++ b/packages/widget/src/components/SwapRoutes/SwapRoutes.tsx @@ -1,3 +1,4 @@ +/* eslint-disable react/no-array-index-key */ import { formatTokenAmount } from '@lifinance/widget/utils/format'; import { BoxProps, Skeleton, Typography } from '@mui/material'; import { useTranslation } from 'react-i18next'; @@ -11,13 +12,13 @@ import { Stack } from './SwapRoutes.style'; export const SwapRoutes: React.FC = ({ mb }) => { const { t } = useTranslation(); const navigate = useNavigate(); - const { routes: swapRoutes, isFetching, isFetched } = useSwapRoutes(); + const { routes: swapRoutes, isLoading } = useSwapRoutes(); const handleCardClick = () => { navigate(routes.swapRoutes); }; - if (!swapRoutes?.length && !isFetched && !isFetching) { + if (!swapRoutes?.length && !isLoading) { return null; } @@ -27,9 +28,10 @@ export const SwapRoutes: React.FC = ({ mb }) => { {t('swap.routes')} - {!swapRoutes?.length && isFetching - ? Array.from({ length: 2 }).map(() => ( + {isLoading + ? Array.from({ length: 2 }).map((_, index) => ( = ({ mb }) => { ?.slice(0, 2) .map((route, index) => ( { const { account } = useWallet(); - const { setValue } = useFormContext(); const [ fromChainId, fromTokenAddress, @@ -31,7 +28,17 @@ export const useSwapRoutes = () => { }); const [fromTokenAmount] = useDebouncedWatch([SwapFormKey.FromAmount], 500); const { token } = useToken(fromChainId, fromTokenAddress); - const { data, isFetching, isFetched } = useQuery( + const isEnabled = + Boolean(account.address) && + !isNaN(fromChainId) && + !isNaN(toChainId) && + Boolean(fromTokenAddress) && + Boolean(toTokenAddress) && + Boolean(fromTokenAmount) && + !isNaN(fromTokenAmount) && + !isNaN(slippage); + + const { data, isFetched, isLoading, status, fetchStatus } = useQuery( [ 'routes', account.address, @@ -82,38 +89,20 @@ export const useSwapRoutes = () => { }); }, { - enabled: - Boolean(account.address) && - !isNaN(fromChainId) && - !isNaN(toChainId) && - Boolean(fromTokenAddress) && - Boolean(toTokenAddress) && - Boolean(fromTokenAmount) && - !isNaN(fromTokenAmount) && - !isNaN(slippage), + enabled: isEnabled, refetchIntervalInBackground: true, refetchInterval: 60_000, - staleTime: 5_000, + staleTime: 30_000, // TODO: probably should be removed - cacheTime: 5_000, + cacheTime: 30_000, }, ); - useEffect(() => { - const route = data?.routes[0]; - if (route) { - setValue( - SwapFormKeyHelper.getAmountKey('to'), - formatTokenAmount(route.toAmount, route.toToken.decimals), - ); - } else { - setValue(SwapFormKeyHelper.getAmountKey('to'), ''); - } - }, [data?.routes, setValue]); - return { routes: data?.routes, - isFetching, + isLoading: isEnabled && isLoading, isFetched, + status, + fetchStatus, }; }; diff --git a/packages/widget/src/pages/SwapRoutesPage/SwapRoutesPage.tsx b/packages/widget/src/pages/SwapRoutesPage/SwapRoutesPage.tsx index e9806c7d3..52a213cd1 100644 --- a/packages/widget/src/pages/SwapRoutesPage/SwapRoutesPage.tsx +++ b/packages/widget/src/pages/SwapRoutesPage/SwapRoutesPage.tsx @@ -1,3 +1,4 @@ +/* eslint-disable react/no-array-index-key */ import { formatTokenAmount } from '@lifinance/widget/utils/format'; import { BoxProps, Skeleton } from '@mui/material'; import { useTranslation } from 'react-i18next'; @@ -7,17 +8,19 @@ import { Stack } from './SwapRoutesPage.style'; export const SwapRoutesPage: React.FC = ({ mb }) => { const { t } = useTranslation(); - const { routes, isFetching, isFetched } = useSwapRoutes(); + const { routes, isLoading } = useSwapRoutes(); - if (!routes?.length && !isFetched && !isFetching) { + if (!routes?.length && !isLoading) { + // TODO: make no routes message return null; } return ( - {!routes?.length && isFetching - ? Array.from({ length: 3 }).map(() => ( + {isLoading + ? Array.from({ length: 3 }).map((_, index) => ( = ({ mb }) => { )) : routes?.map((route, index) => (