You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
5.2 KiB
169 lines
5.2 KiB
4 years ago
|
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';
|
||
4 years ago
|
|
||
4 years ago
|
export default function SlippageButtons({
|
||
|
onSelect,
|
||
|
maxAllowedSlippage,
|
||
|
currentSlippage,
|
||
|
}) {
|
||
4 years ago
|
const t = useContext(I18nContext);
|
||
4 years ago
|
const [customValue, setCustomValue] = useState(() => {
|
||
|
if (currentSlippage && currentSlippage !== 2 && currentSlippage !== 3) {
|
||
|
return currentSlippage;
|
||
|
}
|
||
|
return '';
|
||
|
});
|
||
4 years ago
|
const [enteringCustomValue, setEnteringCustomValue] = useState(false);
|
||
4 years ago
|
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.
|
||
|
});
|
||
4 years ago
|
const [inputRef, setInputRef] = useState(null);
|
||
4 years ago
|
|
||
4 years ago
|
let errorText = '';
|
||
4 years ago
|
if (customValue) {
|
||
|
if (Number(customValue) <= 0) {
|
||
4 years ago
|
errorText = t('swapSlippageTooLow');
|
||
4 years ago
|
} else if (Number(customValue) < 0.5) {
|
||
4 years ago
|
errorText = t('swapLowSlippageError');
|
||
4 years ago
|
} else if (
|
||
|
Number(customValue) >= 5 &&
|
||
|
Number(customValue) <= maxAllowedSlippage
|
||
|
) {
|
||
4 years ago
|
errorText = t('swapHighSlippageWarning');
|
||
4 years ago
|
} else if (Number(customValue) > maxAllowedSlippage) {
|
||
4 years ago
|
errorText = t('swapsExcessiveSlippageWarning');
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
const customValueText = customValue || t('swapCustom');
|
||
4 years ago
|
|
||
|
useEffect(() => {
|
||
4 years ago
|
if (
|
||
|
inputRef &&
|
||
|
enteringCustomValue &&
|
||
|
window.document.activeElement !== inputRef
|
||
|
) {
|
||
4 years ago
|
inputRef.focus();
|
||
4 years ago
|
}
|
||
4 years ago
|
}, [inputRef, enteringCustomValue]);
|
||
4 years ago
|
|
||
|
return (
|
||
|
<div className="slippage-buttons">
|
||
4 years ago
|
<div className="slippage-buttons__header">
|
||
4 years ago
|
<div className="slippage-buttons__header-text">
|
||
|
{t('swapsAdvancedOptions')}
|
||
|
</div>
|
||
4 years ago
|
</div>
|
||
4 years ago
|
<div className="slippage-buttons__content">
|
||
4 years ago
|
<div className="slippage-buttons__dropdown-content">
|
||
|
<div className="slippage-buttons__buttons-prefix">
|
||
|
<div className="slippage-buttons__prefix-text">
|
||
|
{t('swapsMaxSlippage')}
|
||
4 years ago
|
</div>
|
||
4 years ago
|
<InfoTooltip
|
||
|
position="top"
|
||
|
contentText={t('swapAdvancedSlippageInfo')}
|
||
|
/>
|
||
|
</div>
|
||
|
<ButtonGroup
|
||
|
defaultActiveButtonIndex={
|
||
|
activeButtonIndex === 2 && !customValue ? 1 : activeButtonIndex
|
||
|
}
|
||
|
variant="radiogroup"
|
||
|
newActiveButtonIndex={activeButtonIndex}
|
||
|
className={classnames(
|
||
|
'button-group',
|
||
|
'slippage-buttons__button-group',
|
||
|
)}
|
||
|
>
|
||
|
<Button
|
||
|
onClick={() => {
|
||
|
setCustomValue('');
|
||
|
setEnteringCustomValue(false);
|
||
|
setActiveButtonIndex(0);
|
||
|
onSelect(2);
|
||
|
}}
|
||
|
>
|
||
|
2%
|
||
|
</Button>
|
||
|
<Button
|
||
|
onClick={() => {
|
||
|
setCustomValue('');
|
||
|
setEnteringCustomValue(false);
|
||
|
setActiveButtonIndex(1);
|
||
|
onSelect(3);
|
||
|
}}
|
||
|
>
|
||
|
3%
|
||
|
</Button>
|
||
|
<Button
|
||
4 years ago
|
className={classnames(
|
||
4 years ago
|
'slippage-buttons__button-group-custom-button',
|
||
|
{
|
||
|
'radio-button--danger': errorText,
|
||
|
},
|
||
4 years ago
|
)}
|
||
4 years ago
|
onClick={() => {
|
||
|
setActiveButtonIndex(2);
|
||
|
setEnteringCustomValue(true);
|
||
|
}}
|
||
4 years ago
|
>
|
||
4 years ago
|
{enteringCustomValue ? (
|
||
|
<div
|
||
|
className={classnames('slippage-buttons__custom-input', {
|
||
|
'slippage-buttons__custom-input--danger': errorText,
|
||
|
})}
|
||
|
>
|
||
|
<input
|
||
|
onChange={(event) => {
|
||
|
setCustomValue(event.target.value);
|
||
|
onSelect(Number(event.target.value));
|
||
|
}}
|
||
|
type="number"
|
||
|
step="0.1"
|
||
|
ref={setInputRef}
|
||
|
onBlur={() => {
|
||
|
setEnteringCustomValue(false);
|
||
|
if (customValue === '0') {
|
||
|
setCustomValue('');
|
||
|
setActiveButtonIndex(1);
|
||
|
}
|
||
|
}}
|
||
|
value={customValue || ''}
|
||
|
/>
|
||
|
</div>
|
||
|
) : (
|
||
|
customValueText
|
||
|
)}
|
||
|
{(customValue || enteringCustomValue) && (
|
||
|
<div className="slippage-buttons__percentage-suffix">%</div>
|
||
|
)}
|
||
|
</Button>
|
||
|
</ButtonGroup>
|
||
|
</div>
|
||
4 years ago
|
{errorText && (
|
||
4 years ago
|
<div className="slippage-buttons__error-text">{errorText}</div>
|
||
4 years ago
|
)}
|
||
|
</div>
|
||
|
</div>
|
||
4 years ago
|
);
|
||
4 years ago
|
}
|
||
|
|
||
|
SlippageButtons.propTypes = {
|
||
|
onSelect: PropTypes.func.isRequired,
|
||
4 years ago
|
maxAllowedSlippage: PropTypes.number.isRequired,
|
||
4 years ago
|
currentSlippage: PropTypes.number,
|
||
4 years ago
|
};
|