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'; export default function SlippageButtons({ onSelect, maxAllowedSlippage, currentSlippage, }) { const t = useContext(I18nContext); const [customValue, setCustomValue] = useState(() => { if (currentSlippage && currentSlippage !== 2 && currentSlippage !== 3) { return currentSlippage; } return ''; }); const [enteringCustomValue, setEnteringCustomValue] = useState(false); const [activeButtonIndex, setActiveButtonIndex] = useState(() => { if (currentSlippage === 3) { return 1; } else if (currentSlippage === 2) { return 0; } else if (currentSlippage) { return 2; } return 1; // Choose activeButtonIndex = 1 for 3% slippage by default. }); const [inputRef, setInputRef] = useState(null); let errorText = ''; if (customValue) { if (Number(customValue) <= 0) { errorText = t('swapSlippageTooLow'); } else if (Number(customValue) < 0.5) { 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 (