|
| 1 | +import { type FC } from 'react' |
| 2 | +import { Button, Flex } from '../primitives/index.js' |
| 3 | +import type { ChainVM, RelayChain } from '@relayprotocol/relay-sdk' |
| 4 | +import type { PublicClient } from 'viem' |
| 5 | + |
| 6 | +type PercentageButtonsProps = { |
| 7 | + balance: bigint |
| 8 | + onPercentageClick: (amount: bigint, label: string, feeBuffer?: bigint) => void |
| 9 | + getFeeBufferAmount?: ( |
| 10 | + vmType: ChainVM | undefined | null, |
| 11 | + chainId: number | undefined | null, |
| 12 | + balance: bigint, |
| 13 | + publicClient: PublicClient | null |
| 14 | + ) => Promise<bigint> |
| 15 | + fromChain?: RelayChain |
| 16 | + publicClient?: PublicClient |
| 17 | + isFromNative?: boolean |
| 18 | + variant?: 'desktop' | 'mobile' |
| 19 | +} |
| 20 | + |
| 21 | +export const PercentageButtons: FC<PercentageButtonsProps> = ({ |
| 22 | + balance, |
| 23 | + onPercentageClick, |
| 24 | + getFeeBufferAmount, |
| 25 | + fromChain, |
| 26 | + publicClient, |
| 27 | + isFromNative, |
| 28 | + variant = 'desktop' |
| 29 | +}) => { |
| 30 | + const isMobile = variant === 'mobile' |
| 31 | + |
| 32 | + const buttonStyles = { |
| 33 | + fontSize: isMobile ? 14 : 12, |
| 34 | + fontWeight: '500', |
| 35 | + px: '1', |
| 36 | + py: isMobile ? '6px' : '1', |
| 37 | + minHeight: isMobile ? 'auto' : '23px', |
| 38 | + lineHeight: '100%', |
| 39 | + backgroundColor: 'widget-selector-background', |
| 40 | + border: 'none', |
| 41 | + borderRadius: isMobile ? '6px' : '12px', |
| 42 | + flex: isMobile ? '1' : 'none', |
| 43 | + justifyContent: 'center', |
| 44 | + _hover: { |
| 45 | + backgroundColor: 'widget-selector-hover-background' |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + const handleMaxClick = async () => { |
| 50 | + if (!balance || !fromChain) return |
| 51 | + |
| 52 | + let feeBufferAmount: bigint = 0n |
| 53 | + if (isFromNative && getFeeBufferAmount) { |
| 54 | + feeBufferAmount = await getFeeBufferAmount( |
| 55 | + fromChain.vmType, |
| 56 | + fromChain.id, |
| 57 | + balance, |
| 58 | + publicClient ?? null |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + const finalMaxAmount = |
| 63 | + isFromNative && feeBufferAmount > 0n |
| 64 | + ? balance > feeBufferAmount |
| 65 | + ? balance - feeBufferAmount |
| 66 | + : 0n |
| 67 | + : balance |
| 68 | + |
| 69 | + onPercentageClick( |
| 70 | + finalMaxAmount, |
| 71 | + 'max', |
| 72 | + isFromNative ? feeBufferAmount : 0n |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + const handleMaxMouseEnter = () => { |
| 77 | + if ( |
| 78 | + fromChain?.vmType === 'evm' && |
| 79 | + publicClient && |
| 80 | + balance && |
| 81 | + getFeeBufferAmount |
| 82 | + ) { |
| 83 | + getFeeBufferAmount(fromChain.vmType, fromChain.id, balance, publicClient) |
| 84 | + } else if ( |
| 85 | + fromChain?.vmType === 'svm' && |
| 86 | + fromChain.id && |
| 87 | + getFeeBufferAmount |
| 88 | + ) { |
| 89 | + getFeeBufferAmount(fromChain.vmType, fromChain.id, 0n, null) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return ( |
| 94 | + <Flex |
| 95 | + css={{ |
| 96 | + gap: '1', |
| 97 | + width: isMobile ? '100%' : 'auto', |
| 98 | + mb: isMobile ? '1' : '0' |
| 99 | + }} |
| 100 | + > |
| 101 | + <Button |
| 102 | + aria-label="20%" |
| 103 | + css={buttonStyles} |
| 104 | + color="white" |
| 105 | + onClick={() => { |
| 106 | + const percentageBuffer = (balance * 20n) / 100n |
| 107 | + onPercentageClick(percentageBuffer, '20%') |
| 108 | + }} |
| 109 | + > |
| 110 | + 20% |
| 111 | + </Button> |
| 112 | + |
| 113 | + <Button |
| 114 | + aria-label="50%" |
| 115 | + css={buttonStyles} |
| 116 | + color="white" |
| 117 | + onClick={() => { |
| 118 | + const percentageBuffer = (balance * 50n) / 100n |
| 119 | + onPercentageClick(percentageBuffer, '50%') |
| 120 | + }} |
| 121 | + > |
| 122 | + 50% |
| 123 | + </Button> |
| 124 | + |
| 125 | + <Button |
| 126 | + aria-label="MAX" |
| 127 | + css={buttonStyles} |
| 128 | + color="white" |
| 129 | + onMouseEnter={handleMaxMouseEnter} |
| 130 | + onClick={handleMaxClick} |
| 131 | + > |
| 132 | + MAX |
| 133 | + </Button> |
| 134 | + </Flex> |
| 135 | + ) |
| 136 | +} |
0 commit comments