Skip to content

Commit

Permalink
fix: gracefully handle swapper account loading (#7741)
Browse files Browse the repository at this point in the history
* fix: gracefully handle swapper account loading

* fix: loading button

---------

Co-authored-by: woody <[email protected]>
  • Loading branch information
NeOMakinG and woodenfurniture authored Sep 12, 2024
1 parent 67591cd commit 0c069b5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import { getMixPanel } from 'lib/mixpanel/mixPanelSingleton'
import { MixPanelEvent } from 'lib/mixpanel/types'
import { isKeplrHDWallet } from 'lib/utils'
import { selectIsSnapshotApiQueriesPending, selectVotingPower } from 'state/apis/snapshot/selectors'
import { selectHasUserEnteredAmount, selectInputSellAsset } from 'state/slices/selectors'
import {
selectHasUserEnteredAmount,
selectInputSellAsset,
selectIsAccountMetadataLoading,
} from 'state/slices/selectors'
import {
selectActiveQuote,
selectFirstHop,
Expand Down Expand Up @@ -77,6 +81,7 @@ export const TradeInput = ({ isCompact, tradeInputRef }: TradeInputProps) => {
const [shouldShowArbitrumBridgeAcknowledgement, setShouldShowArbitrumBridgeAcknowledgement] =
useState(false)
const isKeplr = useMemo(() => !!wallet && isKeplrHDWallet(wallet), [wallet])
const isAccountMetadataLoading = useAppSelector(selectIsAccountMetadataLoading)

const sellAsset = useAppSelector(selectInputSellAsset)
const tradeQuoteStep = useAppSelector(selectFirstHop)
Expand Down Expand Up @@ -113,6 +118,7 @@ export const TradeInput = ({ isCompact, tradeInputRef }: TradeInputProps) => {

const isLoading = useMemo(
() =>
isAccountMetadataLoading ||
(!isAnyTradeQuoteLoaded && !isTradeQuoteRequestAborted) ||
isConfirmationLoading ||
// Only consider snapshot API queries as pending if we don't have voting power yet
Expand All @@ -124,6 +130,7 @@ export const TradeInput = ({ isCompact, tradeInputRef }: TradeInputProps) => {
isTradeQuoteRequestAborted,
isConfirmationLoading,
isVotingPowerLoading,
isAccountMetadataLoading,
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ export const ConfirmSummary = ({
translate,
])

const buttonText = useMemo(() => {
return <Text translation={quoteStatusTranslation} />
}, [quoteStatusTranslation])

return (
<>
<CardFooter
Expand Down Expand Up @@ -303,14 +307,16 @@ export const ConfirmSummary = ({
/>

<Button
isLoading={isAccountMetadataLoading}
loadingText={buttonText}
type='submit'
colorScheme={quoteHasError ? 'red' : 'blue'}
size='lg-multiline'
data-test='trade-form-preview-button'
isDisabled={shouldDisablePreviewButton}
mx={-2}
>
<Text translation={quoteStatusTranslation} />
{buttonText}
</Button>
</CardFooter>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useModal } from 'hooks/useModal/useModal'
import { useWallet } from 'hooks/useWallet/useWallet'
import { useWalletSupportsChain } from 'hooks/useWalletSupportsChain/useWalletSupportsChain'
import { parseAddressInputWithChainId } from 'lib/address/address'
import { selectAccountIdsByAssetId } from 'state/slices/selectors'
import { selectAccountIdsByAssetId, selectIsAccountMetadataLoading } from 'state/slices/selectors'
import { selectInputBuyAsset } from 'state/slices/tradeInputSlice/selectors'
import { tradeInput } from 'state/slices/tradeInputSlice/tradeInputSlice'
import { useAppDispatch, useAppSelector } from 'state/store'
Expand All @@ -28,6 +28,7 @@ type ManualAddressEntryProps = {
export const ManualAddressEntry: FC<ManualAddressEntryProps> = memo(
({ description, shouldForceManualAddressEntry }: ManualAddressEntryProps): JSX.Element | null => {
const dispatch = useAppDispatch()
const isAccountMetadataLoading = useAppSelector(selectIsAccountMetadataLoading)

const {
formState: { isValidating },
Expand Down Expand Up @@ -57,6 +58,7 @@ export const ManualAddressEntry: FC<ManualAddressEntryProps> = memo(
const { manualReceiveAddress } = useReceiveAddress(useReceiveAddressArgs)

const shouldShowManualReceiveAddressInput = useMemo(() => {
if (isAccountMetadataLoading) return false
if (shouldForceManualAddressEntry) return true
if (manualReceiveAddress) return false
// Ledger "supports" all chains, but may not have them connected
Expand All @@ -69,6 +71,7 @@ export const ManualAddressEntry: FC<ManualAddressEntryProps> = memo(
wallet,
walletSupportsBuyAssetChain,
shouldForceManualAddressEntry,
isAccountMetadataLoading,
])

const chainAdapterManager = getChainAdapterManager()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type { TextPropTypes } from 'components/Text/Text'
import { useWallet } from 'hooks/useWallet/useWallet'
import { parseAddressInputWithChainId } from 'lib/address/address'
import { middleEllipsis } from 'lib/utils'
import { selectInputBuyAsset } from 'state/slices/selectors'
import { selectInputBuyAsset, selectIsAccountMetadataLoading } from 'state/slices/selectors'
import { tradeInput } from 'state/slices/tradeInputSlice/tradeInputSlice'
import { useAppDispatch, useAppSelector } from 'state/store'

Expand All @@ -42,6 +42,7 @@ export const RecipientAddress: React.FC<RecipientAddressProps> = ({
}) => {
const translate = useTranslate()
const dispatch = useAppDispatch()
const isAccountMetadataLoading = useAppSelector(selectIsAccountMetadataLoading)
const wallet = useWallet().state.wallet
const useReceiveAddressArgs = useMemo(
() => ({
Expand Down Expand Up @@ -154,7 +155,7 @@ export const RecipientAddress: React.FC<RecipientAddressProps> = ({

const handleFormSubmit = useMemo(() => handleSubmit(onSubmit), [handleSubmit, onSubmit])

if (!receiveAddress || shouldForceManualAddressEntry) return null
if (!receiveAddress || shouldForceManualAddressEntry || isAccountMetadataLoading) return null

return isRecipientAddressEditing ? (
<form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export type SellAssetInputProps = {
onAccountIdChange: AccountDropdownProps['onChange']
labelPostFix?: TradeAssetInputProps['labelPostFix']
percentOptions: number[]
isReadOnly?: TradeAssetInputProps['isReadOnly']
isLoading?: boolean
}

export const SellAssetInput = memo(
Expand All @@ -33,6 +35,7 @@ export const SellAssetInput = memo(
label,
onAccountIdChange,
percentOptions,
isLoading,
...rest
}: SellAssetInputProps) => {
const sellAmountCryptoPrecision = useAppSelector(selectInputSellAmountCryptoPrecision)
Expand Down Expand Up @@ -99,8 +102,8 @@ export const SellAssetInput = memo(
isSendMaxDisabled={false}
onChange={handleSellAssetInputChange}
percentOptions={percentOptions}
showInputSkeleton={false}
showFiatSkeleton={false}
showInputSkeleton={isLoading}
showFiatSkeleton={isLoading}
label={label}
formControlProps={formControlProps}
onAccountIdChange={onAccountIdChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ export const TradeInputBody = ({

// disable switching assets if the buy asset isn't supported
const shouldDisableSwitchAssets = useMemo(() => {
return !walletSupportsBuyAssetChain
}, [walletSupportsBuyAssetChain])
return !walletSupportsBuyAssetChain || isLoading
}, [walletSupportsBuyAssetChain, isLoading])

return (
<Stack spacing={0}>
Expand All @@ -162,6 +162,7 @@ export const TradeInputBody = ({
onAccountIdChange={setSellAssetAccountId}
labelPostFix={sellTradeAssetSelect}
percentOptions={percentOptions}
isLoading={isLoading}
/>
<Flex alignItems='center' justifyContent='center' my={-2}>
<Divider />
Expand Down

0 comments on commit 0c069b5

Please sign in to comment.