|
| 1 | +import { Trans } from '@lingui/macro'; |
| 2 | +import { Box, Button, ButtonProps } from '@mui/material'; |
| 3 | +import { ReactNode } from 'react'; |
| 4 | + |
| 5 | +import { useFunSupplyATokenIcon } from './useFunSupplyATokenIcon'; |
| 6 | +import { useSupplyButtonAction } from './useSupplyButtonAction'; |
| 7 | + |
| 8 | +export type FunSupplyButtonProps = Omit<ButtonProps, 'onClick' | 'children'> & { |
| 9 | + /** Reserve underlying address (matched case-insensitively against the allowlist). */ |
| 10 | + underlyingAsset: string; |
| 11 | + /** Reserve display name — forwarded to the native supply modal fallback. */ |
| 12 | + name: string; |
| 13 | + /** Patched display symbol of the underlying (drives the funkit checkout title). */ |
| 14 | + symbol: string; |
| 15 | + /** |
| 16 | + * Symbol used to generate the ringed aToken icon. Often equals `symbol`, but |
| 17 | + * can differ (e.g. wrapped tokens), so it's passed explicitly. |
| 18 | + */ |
| 19 | + iconSymbol: string; |
| 20 | + /** Aave's `supplyAPY` — a 0–1 fraction. */ |
| 21 | + supplyAPY: string | number; |
| 22 | + /** Collateral flag shown in the funkit checkout (`collateralizationEnabled`). */ |
| 23 | + collateralEnabled: boolean; |
| 24 | + /** Analytics funnel for the native supply modal fallback. Defaults to `'dashboard'`. */ |
| 25 | + funnel?: string; |
| 26 | + /** The native supply modal's reserve-page flag (`openSupply`'s 5th arg). Defaults to `false`. */ |
| 27 | + isReserve?: boolean; |
| 28 | + /** Button label. Defaults to a translated "Supply". */ |
| 29 | + children?: ReactNode; |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * The Supply button, everywhere. It owns the funkit branch so individual call |
| 34 | + * sites can't forget it: renders the hidden ringed-aToken icon generator and |
| 35 | + * routes the click through `useSupplyButtonAction` (funkit checkout for the |
| 36 | + * allowlisted Core-mainnet assets, native Aave supply modal otherwise). Every |
| 37 | + * MUI `Button` prop (`sx`, `variant`, `disabled`, `fullWidth`, `data-cy`, …) |
| 38 | + * passes straight through, so it drops into any layout. |
| 39 | + * |
| 40 | + * Adding a new Supply entry point? Render this instead of calling `openSupply` |
| 41 | + * directly — keeping the funkit branch in one place is the whole point (ENG-4228). |
| 42 | + */ |
| 43 | +export function FunSupplyButton({ |
| 44 | + underlyingAsset, |
| 45 | + name, |
| 46 | + symbol, |
| 47 | + iconSymbol, |
| 48 | + supplyAPY, |
| 49 | + collateralEnabled, |
| 50 | + funnel, |
| 51 | + isReserve, |
| 52 | + children, |
| 53 | + ...buttonProps |
| 54 | +}: FunSupplyButtonProps) { |
| 55 | + const handleSupplyClick = useSupplyButtonAction({ funnel, isReserve }); |
| 56 | + const { aTokenBase64, generator } = useFunSupplyATokenIcon(underlyingAsset, iconSymbol); |
| 57 | + |
| 58 | + return ( |
| 59 | + <> |
| 60 | + {/* Hidden ringed-aToken icon generator (fun-routed rows only). Wrapped out |
| 61 | + of flow so it never participates as a flex/grid item in the host layout |
| 62 | + — it's a 0×0 element and would otherwise take a slot / introduce a gap. */} |
| 63 | + {generator && ( |
| 64 | + <Box component="span" sx={{ position: 'absolute', width: 0, height: 0 }}> |
| 65 | + {generator} |
| 66 | + </Box> |
| 67 | + )} |
| 68 | + <Button |
| 69 | + variant="contained" |
| 70 | + {...buttonProps} |
| 71 | + onClick={() => |
| 72 | + handleSupplyClick({ |
| 73 | + underlyingAsset, |
| 74 | + name, |
| 75 | + symbol, |
| 76 | + aTokenBase64, |
| 77 | + supplyAPY, |
| 78 | + collateralEnabled, |
| 79 | + }) |
| 80 | + } |
| 81 | + > |
| 82 | + {children ?? <Trans>Supply</Trans>} |
| 83 | + </Button> |
| 84 | + </> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +export default FunSupplyButton; |
0 commit comments