From a993ab78bf9f259dbc6b7d879f1c60cbddcb810c Mon Sep 17 00:00:00 2001 From: michalsmiarowski Date: Wed, 13 Dec 2023 17:10:45 +0100 Subject: [PATCH] Extract send bitcoins form to separate file Extracts `SendBitcoinsToDepositAddressForm` to a separate file to make the `MakeDeposit` file cleaner. --- .../tBTC/SendBitcoinsToDepositAddressForm.tsx | 100 +++++++++++++++++ src/components/tBTC/index.ts | 1 + src/pages/tBTC/Bridge/Minting/MakeDeposit.tsx | 105 +----------------- 3 files changed, 107 insertions(+), 99 deletions(-) create mode 100644 src/components/tBTC/SendBitcoinsToDepositAddressForm.tsx diff --git a/src/components/tBTC/SendBitcoinsToDepositAddressForm.tsx b/src/components/tBTC/SendBitcoinsToDepositAddressForm.tsx new file mode 100644 index 000000000..43f657deb --- /dev/null +++ b/src/components/tBTC/SendBitcoinsToDepositAddressForm.tsx @@ -0,0 +1,100 @@ +import { FC } from "react" +import { BodySm, Box, Button } from "@threshold-network/components" +import { Form, FormikTokenBalanceInput } from "../../components/Forms" +import { InlineTokenBalance } from "../../components/TokenBalance" +import { tBTCFillBlack } from "../../static/icons/tBTCFillBlack" +import { FormikErrors, useFormikContext, withFormik } from "formik" +import { getErrorsObj, validateAmountInRange } from "../../utils/forms" +import { MINT_BITCOIN_MIN_AMOUNT } from "../../utils/tBTC" + +/** + * Formik for Ledger Live App where we can send bitcoins without leaving our + * T dashboard. It should only be visible with `isEmbed` flag. + */ + +type SendBitcoinsToDepositAddressFormBaseProps = { + maxTokenAmount: string +} + +const SendBitcoinsToDepositAddressFormBase: FC< + SendBitcoinsToDepositAddressFormBaseProps +> = ({ maxTokenAmount }) => { + const { isSubmitting } = + useFormikContext() + + return ( +
+ + Amount + + Balance:{" "} + + + + } + placeholder="Amount of bitcoins you want to send to deposit address." + max={maxTokenAmount} + icon={tBTCFillBlack} + tokenDecimals={8} + /> + + + ) +} + +export type SendBitcoinsToDepositAddressFormValues = { + amount: string +} + +type SendBitcoinsToDepositAddressFormProps = { + onSubmitForm: (values: SendBitcoinsToDepositAddressFormValues) => void +} & SendBitcoinsToDepositAddressFormBaseProps + +export const SendBitcoinsToDepositAddressForm = withFormik< + SendBitcoinsToDepositAddressFormProps, + SendBitcoinsToDepositAddressFormValues +>({ + mapPropsToValues: () => ({ + amount: "", + }), + validate: async (values, props) => { + const errors: FormikErrors = {} + + errors.amount = validateAmountInRange( + values.amount, + props.maxTokenAmount, + MINT_BITCOIN_MIN_AMOUNT, + undefined, + 8, + 8 + ) + + return getErrorsObj(errors) + }, + handleSubmit: (values, { props }) => { + props.onSubmitForm(values) + }, + displayName: "SendBitcoinsToDepositAddressForm", + enableReinitialize: true, +})(SendBitcoinsToDepositAddressFormBase) diff --git a/src/components/tBTC/index.ts b/src/components/tBTC/index.ts index 8daf3ab33..b752f2dc5 100644 --- a/src/components/tBTC/index.ts +++ b/src/components/tBTC/index.ts @@ -4,3 +4,4 @@ export * from "./Stats" export * from "./tBTCText" export * from "./BridgeActivity" export * from "./BridgeProcessIndicator" +export * from "./SendBitcoinsToDepositAddressForm" diff --git a/src/pages/tBTC/Bridge/Minting/MakeDeposit.tsx b/src/pages/tBTC/Bridge/Minting/MakeDeposit.tsx index f003b442b..698e48c4f 100644 --- a/src/pages/tBTC/Bridge/Minting/MakeDeposit.tsx +++ b/src/pages/tBTC/Bridge/Minting/MakeDeposit.tsx @@ -1,7 +1,6 @@ import { Badge, BodyMd, - BodySm, Box, BoxLabel, Button, @@ -31,12 +30,10 @@ import { useRequestBitcoinAccount, useSendBitcoinTransaction, } from "../../../../hooks/ledger-live-app" -import { Form, FormikTokenBalanceInput } from "../../../../components/Forms" -import { InlineTokenBalance } from "../../../../components/TokenBalance" -import { tBTCFillBlack } from "../../../../static/icons/tBTCFillBlack" -import { FormikErrors, useFormikContext, withFormik } from "formik" -import { getErrorsObj, validateAmountInRange } from "../../../../utils/forms" -import { MINT_BITCOIN_MIN_AMOUNT } from "../../../../utils/tBTC" +import { + SendBitcoinsToDepositAddressForm, + SendBitcoinsToDepositAddressFormValues, +} from "../../../../components/tBTC" const AddressRow: FC< { address: string; text: string } & Pick @@ -138,6 +135,7 @@ const MakeDepositComponent: FC<{ const { btcDepositAddress, ethAddress, btcRecoveryAddress, updateState } = useTbtcState() + // ↓ Ledger Live App ↓ const { isEmbed } = useIsEmbed() const { requestAccount, account: ledgerBitcoinAccount } = useRequestBitcoinAccount() @@ -158,6 +156,7 @@ const MakeDepositComponent: FC<{ }, [btcDepositAddress, sendBitcoinTransaction] ) + // ↑ Ledger Live App ↑ return ( <> @@ -246,96 +245,4 @@ const MakeDepositComponent: FC<{ ) } -/** - * Formik for Ledger Live App where we can send bitcoins without leaving our - * T dashboard. It should only be visible with `isEmbed` flag. - */ - -type SendBitcoinsToDepositAddressFormBaseProps = { - maxTokenAmount: string -} - -const SendBitcoinsToDepositAddressFormBase: FC< - SendBitcoinsToDepositAddressFormBaseProps -> = ({ maxTokenAmount }) => { - const { isSubmitting } = - useFormikContext() - - return ( -
- - Amount - - Balance:{" "} - - - - } - placeholder="Amount of bitcoins you want to send to deposit address." - max={maxTokenAmount} - icon={tBTCFillBlack} - tokenDecimals={8} - /> - - - ) -} - -type SendBitcoinsToDepositAddressFormValues = { - amount: string -} - -type SendBitcoinsToDepositAddressFormProps = { - onSubmitForm: (values: SendBitcoinsToDepositAddressFormValues) => void -} & SendBitcoinsToDepositAddressFormBaseProps - -const SendBitcoinsToDepositAddressForm = withFormik< - SendBitcoinsToDepositAddressFormProps, - SendBitcoinsToDepositAddressFormValues ->({ - mapPropsToValues: () => ({ - amount: "", - }), - validate: async (values, props) => { - const errors: FormikErrors = {} - - errors.amount = validateAmountInRange( - values.amount, - props.maxTokenAmount, - MINT_BITCOIN_MIN_AMOUNT, - undefined, - 8, - 8 - ) - - return getErrorsObj(errors) - }, - handleSubmit: (values, { props }) => { - props.onSubmitForm(values) - }, - displayName: "SendBitcoinsToDepositAddressForm", - enableReinitialize: true, -})(SendBitcoinsToDepositAddressFormBase) - export const MakeDeposit = withOnlyConnectedWallet(MakeDepositComponent)