import { Box, Heading } from '@biom3/react'; import { useContext, useEffect, useMemo, useState, } from 'react'; import { useTranslation } from 'react-i18next'; import { FundingStepType, SalePaymentTypes, TokenBalance, TransactionRequirement, } from '@imtbl/checkout-sdk'; import { } from '../../../context/view-context/SaleViewContextTypes'; import { calculateCryptoToFiat, tokenValueFormat } from '../../../lib/utils'; import { HeaderNavigation } from '../../../components/Header/HeaderNavigation'; import { SimpleLayout } from '../../../components/SimpleLayout/SimpleLayout'; import { EventTargetContext } from '../../../context/event-target-context/EventTargetContext'; import { sendSaleWidgetCloseEvent } from '../SaleWidgetEvents'; import { SelectCoinDropdown } from './SelectCoinDropdown'; import { CoinsDrawer } from './CoinsDrawer'; import { FundingBalance } from '../types'; import { OrderItems } from './OrderItems'; import { useSaleEvent } from '../hooks/useSaleEvents'; import { getFundingBalanceFeeBreakDown, getFundingBalanceTotalFees, } from '../functions/fundingBalanceFees'; import { FeesDisplay, OrderFees } from './OrderFees'; import { useSaleContext } from '../context/SaleContextProvider'; type OrderReviewProps = { collectionName: string; fundingBalances: FundingBalance[]; conversions: Map; loadingBalances: boolean; transactionRequirement?: TransactionRequirement; onBackButtonClick: () => void; onProceedToBuy: (fundingBalance: FundingBalance) => void; onPayWithCard?: (paymentType: SalePaymentTypes) => void; gasFees?: TokenBalance; }; export function OrderReview({ fundingBalances, conversions, collectionName, loadingBalances, transactionRequirement, onBackButtonClick, onPayWithCard, onProceedToBuy, gasFees, }: OrderReviewProps) { const { eventTargetState: { eventTarget }, } = useContext(EventTargetContext); const { t } = useTranslation(); const { provider, items, orderQuote, disabledPaymentTypes, selectedCurrency, config: { theme, environment }, } = useSaleContext(); const { sendSelectedPaymentToken } = useSaleEvent(); const [showCoinsDrawer, setShowCoinsDrawer] = useState(false); const [selectedCurrencyIndex, setSelectedCurrencyIndex] = useState(0); const [transactionFees, setTransactionFees] = useState({ token: undefined, amount: '', fiatAmount: '', formattedFees: [], }); const isFreeMint = selectedCurrency?.name ? orderQuote.totalAmount[selectedCurrency.name].amount === 0 : false; const openDrawer = () => { setShowCoinsDrawer(true); }; const closeDrawer = () => { setShowCoinsDrawer(false); }; const onSelect = (selectedIndex: number) => { setSelectedCurrencyIndex(selectedIndex); sendSelectedPaymentToken( fundingBalances[selectedIndex], conversions, ); // checkoutPrimarySalePaymentTokenSelected }; const fundingBalance = useMemo( () => fundingBalances[selectedCurrencyIndex], [fundingBalances, selectedCurrencyIndex, provider], ); useEffect(() => { if (!conversions?.size || !provider) { return; } if (fundingBalance.type !== FundingStepType.SWAP) { return; } const totalFees = getFundingBalanceTotalFees(fundingBalance); if (!totalFees) { return; } const entries = Object.entries(totalFees); if (!entries.length) { return; } const [[, fee]] = entries; if (!fee || !fee.token) { return; } setTransactionFees({ token: fee.token, amount: fee.formattedAmount, fiatAmount: calculateCryptoToFiat( fee.formattedAmount, fee.token.symbol, conversions, ), formattedFees: getFundingBalanceFeeBreakDown( fundingBalance, conversions, t, ), }); }, [fundingBalance, conversions, provider]); useEffect(() => { if (fundingBalance.type === FundingStepType.SWAP) { return; } if (!gasFees || gasFees.balance <= 0) { return; } const fiatAmount = calculateCryptoToFiat( gasFees.formattedBalance, gasFees.token.symbol, conversions, ); setTransactionFees({ token: gasFees.token, amount: gasFees.formattedBalance, fiatAmount, formattedFees: [ { label: t('drawers.feesBreakdown.fees.gasFeeMove.label'), fiatAmount: `≈ ${t('drawers.feesBreakdown.fees.fiatPricePrefix')}${fiatAmount}`, amount: `${tokenValueFormat(gasFees.formattedBalance)}`, prefix: '~ ', token: gasFees.token, }, ], }); }, [gasFees, fundingBalance, conversions]); const multiple = items.length > 1; const withFees = transactionFees.formattedFees.length > 0; return ( sendSaleWidgetCloseEvent(eventTarget)} onBackButtonClick={onBackButtonClick} title={collectionName} /> )} bodyStyleOverrides={{ display: 'flex', flexDirection: 'column', justifyContent: 'space-between', height: '0', }} > {t('views.ORDER_SUMMARY.orderReview.heading')} {!multiple && withFees && ( )} {multiple && withFees && ( )} 1} loading={loadingBalances} priceDisplay={items.length > 1} /> ); }