Skip to content

Commit

Permalink
fix: incorrect loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
chefjackson committed Sep 11, 2024
1 parent cc157c6 commit 57031e6
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions apps/web/src/views/Swap/V3Swap/hooks/useSwapBestTrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useUserSingleHopOnly } from '@pancakeswap/utils/user'

import { useCurrency } from 'hooks/Tokens'
import { useBestAMMTrade, useBestTradeFromApi } from 'hooks/useBestAMMTrade'
import { useDeferredValue, useMemo } from 'react'
import { useCallback, useDeferredValue, useMemo, useState } from 'react'
import { Field } from 'state/swap/actions'
import { useSwapState } from 'state/swap/hooks'
import {
Expand Down Expand Up @@ -59,13 +59,39 @@ export function useSwapBestOrder({ maxHops }: Options = {}) {
stableSwap: stableSwapEnable,
trackPerf: true,
})
const [loading, setLoading] = useState(false)
const refresh = useCallback(async () => {
try {
setLoading(true)
const res = await refetch()
return res
} finally {
setLoading(false)
}
}, [refetch])

const isAutoRefetch = useMemo(
() =>
!loading &&
fetchStatus === 'fetching' &&
amount &&
inputCurrency &&
outputCurrency &&
data?.trade &&
amount.toExact() === (isExactIn ? data.trade.inputAmount.toExact() : data.trade.outputAmount.toExact()) &&
data.trade.inputAmount.currency.equals(inputCurrency) &&
data.trade.outputAmount.currency.equals(outputCurrency),
[loading, fetchStatus, amount, data?.trade, isExactIn, inputCurrency, outputCurrency],
)

return {
enabled: xEnabled,
refresh: refetch,
refresh,
isStale,
error,
isLoading: useDeferredValue(Boolean(fetchStatus === 'fetching' || (typedValue && !data && !error))),
isLoading: useDeferredValue(
Boolean((fetchStatus === 'fetching' && !isAutoRefetch) || (typedValue && !data && !error)),
),
order: typedValue ? data : undefined,
}
}
Expand Down

0 comments on commit 57031e6

Please sign in to comment.