Skip to content

Commit

Permalink
fix ui bugs in send flows
Browse files Browse the repository at this point in the history
  • Loading branch information
onghwan committed Nov 16, 2024
1 parent e1ae2a0 commit 068fdd1
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 106 deletions.
10 changes: 5 additions & 5 deletions packages/core-mobile/app/contexts/SendContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ interface SendContextState {
setError: Dispatch<string | undefined>
isSending: boolean
setIsSending: Dispatch<boolean>
isValidating: boolean
setIsValidating: Dispatch<boolean>
canValidate: boolean
setCanValidate: Dispatch<boolean>
isValid: boolean
}

Expand All @@ -53,7 +53,7 @@ export const SendContextProvider = ({
const { data: networkFee } = useNetworkFee(activeNetwork)
const [error, setError] = useState<string | undefined>()
const [isSending, setIsSending] = useState(false)
const [isValidating, setIsValidating] = useState(false)
const [canValidate, setCanValidate] = useState(false)

const [defaultMaxFeePerGas, setDefaultMaxFeePerGas] = useState<bigint>(0n)

Expand Down Expand Up @@ -88,8 +88,8 @@ export const SendContextProvider = ({
setError,
isSending,
setIsSending,
isValidating,
setIsValidating,
canValidate,
setCanValidate,
isValid: error === undefined
}
return <SendContext.Provider value={state}>{children}</SendContext.Provider>
Expand Down
10 changes: 7 additions & 3 deletions packages/core-mobile/app/screens/nft/send/NftSend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function NftSend({
error,
isSending,
isValid,
isValidating
setCanValidate
} = useSendContext()
const { activeNetwork } = useNetworks()
const activeAccount = useSelector(selectActiveAccount)
Expand Down Expand Up @@ -87,7 +87,7 @@ export default function NftSend({
})

const canSubmit =
!isValidating && !isSending && isValid && !!toAddress && error === undefined
!isSending && isValid && !!toAddress && error === undefined && touched

const {
saveRecentContact,
Expand Down Expand Up @@ -143,7 +143,11 @@ export default function NftSend({
if (touched === false && toAddress) {
setTouched(true)
}
}, [toAddress, token, touched])
}, [toAddress, touched, setCanValidate])

useEffect(() => {
setCanValidate(touched)
}, [touched, setCanValidate])

const onContactSelected = (
item: Contact | Account,
Expand Down
20 changes: 3 additions & 17 deletions packages/core-mobile/app/screens/send/components/SendAVM.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react'
import React from 'react'
import { useSendContext } from 'contexts/SendContext'
import { Network } from '@avalabs/core-chains-sdk'
import { TokenWithBalanceAVM } from '@avalabs/vm-module-types'
Expand Down Expand Up @@ -26,17 +26,8 @@ const SendAVM = ({
onSuccess: (txHash: string) => void
onFailure: (txError: unknown) => void
}): JSX.Element => {
const {
setToAddress,
token,
setToken,
maxAmount,
error,
isValid,
isValidating,
isSending,
maxFee
} = useSendContext()
const { setToAddress, token, maxAmount, error, isValid, isSending, maxFee } =
useSendContext()
const activeAccount = useSelector(selectActiveAccount)

const fromAddress = activeAccount?.addressAVM ?? ''
Expand All @@ -49,10 +40,6 @@ const SendAVM = ({
account
})

useEffect(() => {
setToken(nativeToken)
}, [nativeToken, setToken])

const handleSend = async (): Promise<void> => {
if (token === undefined) {
return
Expand All @@ -79,7 +66,6 @@ const SendAVM = ({
error={error}
isValid={isValid}
isSending={isSending}
isValidating={isValidating}
onOpenQRScanner={onOpenQRScanner}
onOpenAddressBook={onOpenAddressBook}
onSelectContact={handleSelectContact}
Expand Down
20 changes: 3 additions & 17 deletions packages/core-mobile/app/screens/send/components/SendPVM.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react'
import React from 'react'
import { useSendContext } from 'contexts/SendContext'
import { Network } from '@avalabs/core-chains-sdk'
import { TokenWithBalancePVM } from '@avalabs/vm-module-types'
Expand Down Expand Up @@ -26,17 +26,8 @@ const SendPVM = ({
onSuccess: (txHash: string) => void
onFailure: (txError: unknown) => void
}): JSX.Element => {
const {
setToAddress,
token,
setToken,
maxAmount,
error,
isValid,
isValidating,
isSending,
maxFee
} = useSendContext()
const { setToAddress, token, maxAmount, error, isValid, isSending, maxFee } =
useSendContext()
const activeAccount = useSelector(selectActiveAccount)

const fromAddress = activeAccount?.addressPVM ?? ''
Expand All @@ -49,10 +40,6 @@ const SendPVM = ({
account
})

useEffect(() => {
setToken(nativeToken)
}, [nativeToken, setToken])

const handleSend = async (): Promise<void> => {
if (token === undefined) {
return
Expand All @@ -79,7 +66,6 @@ const SendPVM = ({
error={error}
isValid={isValid}
isSending={isSending}
isValidating={isValidating}
onOpenQRScanner={onOpenQRScanner}
onOpenAddressBook={onOpenAddressBook}
onSelectContact={handleSelectContact}
Expand Down
19 changes: 14 additions & 5 deletions packages/core-mobile/app/screens/send/components/SendTokenForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const SendTokenForm = ({
addressPlaceholder,
error,
isValid,
isValidating,
isSending,
onOpenQRScanner,
onOpenAddressBook,
Expand All @@ -37,15 +36,21 @@ const SendTokenForm = ({
addressPlaceholder: string
error: string | undefined
isValid: boolean
isValidating: boolean
isSending: boolean
onOpenQRScanner: () => void
onOpenAddressBook: () => void
onSelectContact: (item: Contact | CorePrimaryAccount) => void
onSend: () => void
}): JSX.Element => {
const { setToken, token, setAmount, amount, toAddress, setToAddress } =
useSendContext()
const {
setToken,
token,
setAmount,
amount,
toAddress,
setToAddress,
setCanValidate
} = useSendContext()
const [isAddressTouched, setIsAddressTouched] = useState(false)
const [isTokenTouched, setIsTokenTouched] = useState(false)
const [isAmountTouched, setIsAmountTouched] = useState(false)
Expand Down Expand Up @@ -108,7 +113,11 @@ const SendTokenForm = ({
[isAddressTouched, isTokenTouched, isAmountTouched]
)

const canSubmit = !isValidating && !isSending && isValid
useEffect(() => {
setCanValidate(isAllFieldsTouched)
}, [isAllFieldsTouched, setCanValidate])

const canSubmit = !isSending && isValid && isAllFieldsTouched

return (
<ScrollView contentContainerStyle={{ flexGrow: 1 }}>
Expand Down
28 changes: 9 additions & 19 deletions packages/core-mobile/app/screens/send/hooks/useAVMSend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const useAVMSend: SendAdapterAVM = ({
const {
setMaxAmount,
setError,
setIsValidating,
setIsSending,
amount,
token,
toAddress
toAddress,
canValidate
} = useSendContext()

const send = useCallback(async () => {
Expand Down Expand Up @@ -56,9 +56,6 @@ const useAVMSend: SendAdapterAVM = ({
)

const validate = useCallback(async () => {
setIsValidating(true)
setError(undefined)

try {
validateAVMSend({
amount: amount?.bn,
Expand All @@ -67,25 +64,18 @@ const useAVMSend: SendAdapterAVM = ({
token: token as TokenWithBalanceAVM,
onCalculateMaxAmount: setMaxAmount
})

setError(undefined)
} catch (err) {
handleError(err)
} finally {
setIsValidating(false)
}
}, [
maxFee,
setMaxAmount,
setError,
handleError,
setIsValidating,
toAddress,
amount,
token
])
}, [maxFee, setMaxAmount, setError, handleError, toAddress, amount, token])

useEffect(() => {
validate()
}, [validate])
if (canValidate) {
validate()
}
}, [validate, canValidate])

return {
send
Expand Down
18 changes: 8 additions & 10 deletions packages/core-mobile/app/screens/send/hooks/useBTCSend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ const useBTCSend: SendAdapterBTC = ({
const {
setMaxAmount,
setError,
setIsValidating,
setIsSending,
toAddress,
token,
amount
amount,
canValidate
} = useSendContext()
const provider = useBitcoinProvider(!!network.isTestnet)

Expand Down Expand Up @@ -63,9 +63,6 @@ const useBTCSend: SendAdapterBTC = ({
return
}

setIsValidating(true)
setError(undefined)

try {
const maxAmountValue = BigInt(
Math.max(
Expand All @@ -88,13 +85,13 @@ const useBTCSend: SendAdapterBTC = ({
maxFee,
isMainnet
})

setError(undefined)
} catch (e) {
if (e instanceof Error) {
setError(e.message)
Logger.error('failed to validate send', e)
}
} finally {
setIsValidating(false)
}
}, [
fromAddress,
Expand All @@ -104,7 +101,6 @@ const useBTCSend: SendAdapterBTC = ({
isMainnet,
setMaxAmount,
setError,
setIsValidating,
toAddress,
token,
amount
Expand All @@ -131,8 +127,10 @@ const useBTCSend: SendAdapterBTC = ({
}, [isMainnet, maxFee, fromAddress, request, setIsSending, toAddress, amount])

useEffect(() => {
validate()
}, [validate])
if (canValidate) {
validate()
}
}, [validate, canValidate])

return {
send
Expand Down
18 changes: 8 additions & 10 deletions packages/core-mobile/app/screens/send/hooks/useEVMSend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ const useEVMSend: SendAdapterEVM = ({
setMaxAmount,
setError,
setIsSending,
setIsValidating,
token,
toAddress,
amount
amount,
canValidate
} = useSendContext()
const provider = useEVMProvider(network)

Expand Down Expand Up @@ -78,9 +78,6 @@ const useEVMSend: SendAdapterEVM = ({
)

const validate = useCallback(async () => {
setIsValidating(true)
setError(undefined)

try {
validateBasicInputs(token, toAddress, maxFee)

Expand Down Expand Up @@ -120,18 +117,17 @@ const useEVMSend: SendAdapterEVM = ({
}

validateGasLimit(gasLimit)

setError(undefined)
} catch (err) {
handleError(err)
} finally {
setIsValidating(false)
}
}, [
nativeToken,
fromAddress,
provider,
handleError,
setError,
setIsValidating,
maxFee,
setMaxAmount,
token,
Expand All @@ -140,8 +136,10 @@ const useEVMSend: SendAdapterEVM = ({
])

useEffect(() => {
validate()
}, [validate])
if (canValidate) {
validate()
}
}, [validate, canValidate])

return {
send
Expand Down
Loading

0 comments on commit 068fdd1

Please sign in to comment.