import React, { useState, useEffect, useContext } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import { I18nContext } from '../../../contexts/i18n'; import ButtonGroup from '../../../components/ui/button-group'; import Button from '../../../components/ui/button'; import InfoTooltip from '../../../components/ui/info-tooltip'; import ToggleButton from '../../../components/ui/toggle-button'; import Box from '../../../components/ui/box'; import Typography from '../../../components/ui/typography'; import { TYPOGRAPHY, FONT_WEIGHT, ALIGN_ITEMS, DISPLAY, } from '../../../helpers/constants/design-system'; import { smartTransactionsErrorMessages } from '../swaps.util'; export default function SlippageButtons({ onSelect, maxAllowedSlippage, currentSlippage, smartTransactionsEnabled, smartTransactionsOptInStatus, setSmartTransactionsOptInStatus, currentSmartTransactionsError, }) { const t = useContext(I18nContext); const [customValue, setCustomValue] = useState(() => { if ( typeof currentSlippage === 'number' && currentSlippage !== 2 && currentSlippage !== 3 ) { return currentSlippage.toString(); } return ''; }); const [enteringCustomValue, setEnteringCustomValue] = useState(false); const [activeButtonIndex, setActiveButtonIndex] = useState(() => { if (currentSlippage === 3) { return 1; } else if (currentSlippage === 2) { return 0; } else if (typeof currentSlippage === 'number') { return 2; } return 1; // Choose activeButtonIndex = 1 for 3% slippage by default. }); const [open, setOpen] = useState(() => { return currentSlippage !== 3; // Only open Advanced Options by default if it's not default 3% slippage. }); const [inputRef, setInputRef] = useState(null); let errorText = ''; if (customValue) { // customValue is a string, e.g. '0' if (Number(customValue) < 0) { errorText = t('swapSlippageNegative'); } else if (Number(customValue) > 0 && Number(customValue) <= 1) { // We will not show this warning for 0% slippage, because we will only // return non-slippage quotes from off-chain makers. errorText = t('swapLowSlippageError'); } else if ( Number(customValue) >= 5 && Number(customValue) <= maxAllowedSlippage ) { errorText = t('swapHighSlippageWarning'); } else if (Number(customValue) > maxAllowedSlippage) { errorText = t('swapsExcessiveSlippageWarning'); } } const customValueText = customValue || t('swapCustom'); useEffect(() => { if ( inputRef && enteringCustomValue && window.document.activeElement !== inputRef ) { inputRef.focus(); } }, [inputRef, enteringCustomValue]); return (
{open && ( <>
{t('swapsMaxSlippage')}
{smartTransactionsEnabled && ( {t('smartTransaction')} {currentSmartTransactionsError ? ( ) : ( )} { setSmartTransactionsOptInStatus(!value); }} offLabel={t('off')} onLabel={t('on')} disabled={Boolean(currentSmartTransactionsError)} /> )} )} {errorText && (
{errorText}
)}
); } SlippageButtons.propTypes = { onSelect: PropTypes.func.isRequired, maxAllowedSlippage: PropTypes.number.isRequired, currentSlippage: PropTypes.number, smartTransactionsEnabled: PropTypes.bool.isRequired, smartTransactionsOptInStatus: PropTypes.object, setSmartTransactionsOptInStatus: PropTypes.func, currentSmartTransactionsError: PropTypes.string, };