import { useCallback } from 'react' ; import * as React from 'react' ; import { FormikProps } from 'formik' ; import { useSelectedAsset } from '@app/common/hooks/use-selected-asset' ; import { microStxToStx } from '@app/common/stacks-utils' ; import { removeCommas } from '@app/common/token-utils' ; import { TransactionFormValues } from '@app/common/transactions/transaction-utils' ; import { useCurrentAccountAvailableStxBalance } from '@app/store/accounts/account.hooks' ; export function useSendAmountFieldActions ( { setFieldValue , } ) { const availableStxBalance = useCurrentAccountAvailableStxBalance ( ) ; const { selectedAsset , balance } = useSelectedAsset ( ) ; const isStx = selectedAsset ?. type === 'stx' ; const handleSetSendMax = useCallback ( ( fee ) => { if ( ! selectedAsset || ! balance ) return ; if ( isStx && fee ) { const stx = microStxToStx ( availableStxBalance || 0 ) . minus ( fee ) ; if ( stx . isLessThanOrEqualTo ( 0 ) ) return ; return setFieldValue ( 'amount' , stx . toNumber ( ) ) ; } else { if ( balance ) setFieldValue ( 'amount' , removeCommas ( balance ) ) ; } } , [ selectedAsset , balance , isStx , setFieldValue , availableStxBalance ] ) ; const handleOnKeyDown = useCallback ( ( event ) => { const hasDecimals = typeof selectedAsset ?. meta ?. decimals === 'number' && selectedAsset ?. meta . decimals !== 0 ; const { key } = event ; const value = event . currentTarget . value ; if ( selectedAsset ?. type !== 'stx' && ( ( key === '0' && value . length === 0 && ! hasDecimals ) || ( key === '0' && value [ 0 ] === '0' && value [ 1 ] !== '.' ) ) ) return event . preventDefault ( ) ; if ( key === '.' ) { if ( ! hasDecimals && selectedAsset ?. type !== 'stx' ) return event . preventDefault ( ) ; const hasPeriod = value . includes ( '.' ) ; if ( hasPeriod && key === '.' ) { return event . preventDefault ( ) ; } } } , [ selectedAsset ] ) ; return { handleSetSendMax , handleOnKeyDown , } ; }