import { useCallback, useContext } from 'react' import { Chain } from 'types' import { Web3Context } from 'providers/Web3Provider' import { Tooltip } from '@/components/ui/Tooltip' import { useBridgeState } from '@/state/slices/bridge/hooks' import { switchNetwork } from '@/utils/actions/switchNetwork' import { useValidations } from '@/hooks/useValidations' import { DEFAULT_NATIVE_FEE_VALIDATION_MESSAGE, useNativeFeeValidation, } from '@/hooks/useNativeFeeValidation' interface BridgeButtonProps { originChain: Chain isValidQuote: boolean handleApprove: () => any handleBridge: () => any isApprovalPending: boolean isBridgePending: boolean isBridgePaused: boolean } export const BridgeButton = ({ originChain, isValidQuote, handleApprove, handleBridge, isApprovalPending, isBridgePending, isBridgePaused, }: BridgeButtonProps) => { const web3Context = useContext(Web3Context) const { connectedAddress, provider } = web3Context.web3Provider const { originChainId } = useBridgeState() const { hasEnoughBalance, isInputValid, onSelectedChain, isApproved } = useValidations() const { hasEnoughNativeBalanceForQuoteFee, nativeFeeValidationMessage } = useNativeFeeValidation() const handleSwitchNetwork = useCallback(async () => { switchNetwork(originChainId, provider) }, [originChainId, provider]) const buttonClassName = ` p-2 text-lg font-sans font-medium tracking-wide rounded-md w-full border border-solid border-[--synapse-button-border] hover:border-[--synapse-focus] disabled:hover:border-[--synapse-button-border] active:opacity-70 disabled:opacity-40 text-[--synapse-button-text] cursor-pointer disabled:cursor-not-allowed ` const buttonStyle = { background: 'var(--synapse-button-bg)', } const tooltipPositionStyle = '-top-8' let insufficientNativeBalanceLabel = 'Approve & Sign' if (isApproved) { insufficientNativeBalanceLabel = 'Send' } else if (isApprovalPending) { insufficientNativeBalanceLabel = 'Approve in Wallet' } if (isBridgePaused) { return ( ) } if (!provider || !connectedAddress) { return ( ) } if (!originChainId) { return ( ) } if (!onSelectedChain) { return ( ) } if (!hasEnoughBalance) { return ( ) } if (!isInputValid) { return ( ) } if (!isValidQuote) { return ( ) } if (!hasEnoughNativeBalanceForQuoteFee) { return ( ) } return (
{isApproved ? ( ) : ( )}
) }