2021-02-04 19:15:23 +01:00
|
|
|
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';
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-04-08 20:58:19 +02:00
|
|
|
export default function SlippageButtons({
|
|
|
|
onSelect,
|
|
|
|
maxAllowedSlippage,
|
|
|
|
currentSlippage,
|
|
|
|
}) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const t = useContext(I18nContext);
|
2021-04-08 20:58:19 +02:00
|
|
|
const [customValue, setCustomValue] = useState(() => {
|
2021-05-05 19:17:38 +02:00
|
|
|
if (
|
|
|
|
typeof currentSlippage === 'number' &&
|
|
|
|
currentSlippage !== 2 &&
|
|
|
|
currentSlippage !== 3
|
|
|
|
) {
|
|
|
|
return currentSlippage.toString();
|
2021-04-08 20:58:19 +02:00
|
|
|
}
|
|
|
|
return '';
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
const [enteringCustomValue, setEnteringCustomValue] = useState(false);
|
2021-04-08 20:58:19 +02:00
|
|
|
const [activeButtonIndex, setActiveButtonIndex] = useState(() => {
|
|
|
|
if (currentSlippage === 3) {
|
|
|
|
return 1;
|
|
|
|
} else if (currentSlippage === 2) {
|
|
|
|
return 0;
|
2021-05-05 19:17:38 +02:00
|
|
|
} else if (typeof currentSlippage === 'number') {
|
2021-04-08 20:58:19 +02:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 1; // Choose activeButtonIndex = 1 for 3% slippage by default.
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
const [inputRef, setInputRef] = useState(null);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let errorText = '';
|
2020-12-07 16:12:55 +01:00
|
|
|
if (customValue) {
|
2021-05-05 19:17:38 +02:00
|
|
|
// 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.
|
2021-02-04 19:15:23 +01:00
|
|
|
errorText = t('swapLowSlippageError');
|
2020-12-07 16:12:55 +01:00
|
|
|
} else if (
|
|
|
|
Number(customValue) >= 5 &&
|
|
|
|
Number(customValue) <= maxAllowedSlippage
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
errorText = t('swapHighSlippageWarning');
|
2020-12-07 16:12:55 +01:00
|
|
|
} else if (Number(customValue) > maxAllowedSlippage) {
|
2021-02-04 19:15:23 +01:00
|
|
|
errorText = t('swapsExcessiveSlippageWarning');
|
2020-12-07 16:12:55 +01:00
|
|
|
}
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const customValueText = customValue || t('swapCustom');
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
inputRef &&
|
|
|
|
enteringCustomValue &&
|
|
|
|
window.document.activeElement !== inputRef
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
inputRef.focus();
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
}, [inputRef, enteringCustomValue]);
|
2020-10-06 20:28:38 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="slippage-buttons">
|
2021-04-08 20:58:19 +02:00
|
|
|
<div className="slippage-buttons__header">
|
2020-11-03 00:41:28 +01:00
|
|
|
<div className="slippage-buttons__header-text">
|
|
|
|
{t('swapsAdvancedOptions')}
|
|
|
|
</div>
|
2021-04-08 20:58:19 +02:00
|
|
|
</div>
|
2020-10-06 20:28:38 +02:00
|
|
|
<div className="slippage-buttons__content">
|
2021-04-08 20:58:19 +02:00
|
|
|
<div className="slippage-buttons__dropdown-content">
|
|
|
|
<div className="slippage-buttons__buttons-prefix">
|
|
|
|
<div className="slippage-buttons__prefix-text">
|
|
|
|
{t('swapsMaxSlippage')}
|
2020-10-06 20:28:38 +02:00
|
|
|
</div>
|
2021-04-08 20:58:19 +02:00
|
|
|
<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
|
2020-11-03 00:41:28 +01:00
|
|
|
className={classnames(
|
2021-04-08 20:58:19 +02:00
|
|
|
'slippage-buttons__button-group-custom-button',
|
|
|
|
{
|
|
|
|
'radio-button--danger': errorText,
|
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
)}
|
2021-04-08 20:58:19 +02:00
|
|
|
onClick={() => {
|
|
|
|
setActiveButtonIndex(2);
|
|
|
|
setEnteringCustomValue(true);
|
|
|
|
}}
|
2020-10-06 20:28:38 +02:00
|
|
|
>
|
2021-04-08 20:58:19 +02:00
|
|
|
{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);
|
|
|
|
}}
|
|
|
|
value={customValue || ''}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
customValueText
|
|
|
|
)}
|
|
|
|
{(customValue || enteringCustomValue) && (
|
|
|
|
<div className="slippage-buttons__percentage-suffix">%</div>
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
</ButtonGroup>
|
|
|
|
</div>
|
2020-10-06 20:28:38 +02:00
|
|
|
{errorText && (
|
2020-11-03 00:41:28 +01:00
|
|
|
<div className="slippage-buttons__error-text">{errorText}</div>
|
2020-10-06 20:28:38 +02:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SlippageButtons.propTypes = {
|
|
|
|
onSelect: PropTypes.func.isRequired,
|
2020-12-07 16:12:55 +01:00
|
|
|
maxAllowedSlippage: PropTypes.number.isRequired,
|
2021-04-08 20:58:19 +02:00
|
|
|
currentSlippage: PropTypes.number,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|