From cff4d928b655f4ce5b307e598d5e56d36cbd32d5 Mon Sep 17 00:00:00 2001 From: Evan Gray Date: Thu, 21 Oct 2021 17:11:03 -0400 Subject: [PATCH] bridge_ui: max button for number inputs Change-Id: I4fdbb2b3191a012cbdf02df4ad6292c78eed5bb7 --- .../src/components/Migration/EvmWorkflow.tsx | 27 ++++++++-------- .../components/Migration/SolanaWorkflow.tsx | 14 ++++++--- bridge_ui/src/components/NumberTextField.tsx | 31 +++++++++++++++++++ bridge_ui/src/components/Transfer/Source.tsx | 12 +++++-- 4 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 bridge_ui/src/components/NumberTextField.tsx diff --git a/bridge_ui/src/components/Migration/EvmWorkflow.tsx b/bridge_ui/src/components/Migration/EvmWorkflow.tsx index 06a4ac56..0ec0a01a 100644 --- a/bridge_ui/src/components/Migration/EvmWorkflow.tsx +++ b/bridge_ui/src/components/Migration/EvmWorkflow.tsx @@ -1,10 +1,5 @@ import { ChainId } from "@certusone/wormhole-sdk"; -import { - CircularProgress, - makeStyles, - TextField, - Typography, -} from "@material-ui/core"; +import { CircularProgress, makeStyles, Typography } from "@material-ui/core"; import { Alert } from "@material-ui/lab"; import { parseUnits } from "ethers/lib/utils"; import { useSnackbar } from "notistack"; @@ -14,6 +9,7 @@ import useEthereumMigratorInformation from "../../hooks/useEthereumMigratorInfor import useIsWalletReady from "../../hooks/useIsWalletReady"; import ButtonWithLoader from "../ButtonWithLoader"; import EthereumSignerKey from "../EthereumSignerKey"; +import NumberTextField from "../NumberTextField"; import ShowTx from "../ShowTx"; import SmartAddress from "../SmartAddress"; @@ -49,6 +45,7 @@ export default function EvmWorkflow({ signerAddress, toggleRefresh ); + const fromWalletBalance = poolInfo.data?.fromWalletBalance; const [migrationAmount, setMigrationAmount] = useState(""); const [migrationIsProcessing, setMigrationIsProcessing] = useState(false); @@ -69,9 +66,9 @@ export default function EvmWorkflow({ const hasRequisiteData = poolInfo.data; const amountGreaterThanZero = fromParse(migrationAmount) > BigInt(0); const sufficientFromTokens = - poolInfo.data?.fromWalletBalance && + fromWalletBalance && migrationAmount && - fromParse(migrationAmount) <= fromParse(poolInfo.data.fromWalletBalance); + fromParse(migrationAmount) <= fromParse(fromWalletBalance); const sufficientPoolBalance = poolInfo.data?.toPoolBalance && migrationAmount && @@ -106,6 +103,11 @@ export default function EvmWorkflow({ (event) => setMigrationAmount(event.target.value), [setMigrationAmount] ); + const handleMaxClick = useCallback(() => { + if (fromWalletBalance) { + setMigrationAmount(fromWalletBalance); + } + }, [fromWalletBalance]); const migrateTokens = useCallback(async () => { if (!poolInfo.data) { @@ -170,8 +172,7 @@ export default function EvmWorkflow({
This action will convert - {fromTokenPretty}{" "} - {`(Balance: ${poolInfo.data?.fromWalletBalance || ""})`} + {fromTokenPretty} {`(Balance: ${fromWalletBalance || ""})`}
to @@ -190,14 +191,14 @@ export default function EvmWorkflow({ <> {explainerContent}
- + onMaxClick={fromWalletBalance ? handleMaxClick : undefined} + /> {!transaction && ( setMigrationAmount(event.target.value), [setMigrationAmount] ); + const handleMaxClick = useCallback(() => { + if (fromTokenAccountBalance) { + setMigrationAmount(fromTokenAccountBalance); + } + }, [fromTokenAccountBalance]); const getMetadata = (address: string) => { const tokenMapItem = solanaTokenMap.data?.find( @@ -459,14 +465,14 @@ export default function Workflow({ ) : null}
- + onMaxClick={fromTokenAccountBalance ? handleMaxClick : undefined} + /> {!transaction && ( void } +) { + return ( + + + + ) : undefined, + ...(props?.InputProps || {}), + }} + > + ); +} diff --git a/bridge_ui/src/components/Transfer/Source.tsx b/bridge_ui/src/components/Transfer/Source.tsx index feea71f4..f1b1c902 100644 --- a/bridge_ui/src/components/Transfer/Source.tsx +++ b/bridge_ui/src/components/Transfer/Source.tsx @@ -4,7 +4,7 @@ import { CHAIN_ID_SOLANA, } from "@certusone/wormhole-sdk"; import { getAddress } from "@ethersproject/address"; -import { Button, makeStyles, TextField } from "@material-ui/core"; +import { Button, makeStyles } from "@material-ui/core"; import { useCallback } from "react"; import { useDispatch, useSelector } from "react-redux"; import { useHistory } from "react-router"; @@ -33,6 +33,7 @@ import ButtonWithLoader from "../ButtonWithLoader"; import ChainSelect from "../ChainSelect"; import KeyAndBalance from "../KeyAndBalance"; import LowBalanceWarning from "../LowBalanceWarning"; +import NumberTextField from "../NumberTextField"; import StepDescription from "../StepDescription"; import { TokenSelector } from "../TokenSelectors/SourceTokenSelector"; import TokenWarning from "./TokenWarning"; @@ -95,6 +96,11 @@ function Source() { }, [dispatch] ); + const handleMaxClick = useCallback(() => { + if (uiAmountString) { + dispatch(setAmount(uiAmountString)); + } + }, [dispatch, uiAmountString]); const handleNextClick = useCallback(() => { dispatch(incrementStep()); }, [dispatch]); @@ -136,15 +142,15 @@ function Source() { /> {hasParsedTokenAccount ? ( - ) : null}