2020-10-06 20:28:38 +02: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-12-07 16:12:55 +01:00
|
|
|
export default function SlippageButtons({ onSelect, maxAllowedSlippage }) {
|
2020-10-06 20:28:38 +02:00
|
|
|
const t = useContext(I18nContext)
|
|
|
|
const [open, setOpen] = useState(false)
|
|
|
|
const [customValue, setCustomValue] = useState('')
|
|
|
|
const [enteringCustomValue, setEnteringCustomValue] = useState(false)
|
|
|
|
const [activeButtonIndex, setActiveButtonIndex] = useState(1)
|
|
|
|
const [inputRef, setInputRef] = useState(null)
|
|
|
|
|
|
|
|
let errorText = ''
|
2020-12-07 16:12:55 +01:00
|
|
|
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')
|
|
|
|
}
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const customValueText = customValue || t('swapCustom')
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
inputRef &&
|
|
|
|
enteringCustomValue &&
|
|
|
|
window.document.activeElement !== inputRef
|
|
|
|
) {
|
2020-10-06 20:28:38 +02:00
|
|
|
inputRef.focus()
|
|
|
|
}
|
|
|
|
}, [inputRef, enteringCustomValue])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="slippage-buttons">
|
2020-10-30 15:35:47 +01:00
|
|
|
<button
|
2020-10-06 20:28:38 +02:00
|
|
|
onClick={() => setOpen(!open)}
|
|
|
|
className={classnames('slippage-buttons__header', {
|
|
|
|
'slippage-buttons__header--open': open,
|
|
|
|
})}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
<div className="slippage-buttons__header-text">
|
|
|
|
{t('swapsAdvancedOptions')}
|
|
|
|
</div>
|
|
|
|
{open ? (
|
|
|
|
<i className="fa fa-angle-up" />
|
|
|
|
) : (
|
|
|
|
<i className="fa fa-angle-down" />
|
|
|
|
)}
|
2020-10-30 15:35:47 +01:00
|
|
|
</button>
|
2020-10-06 20:28:38 +02:00
|
|
|
<div className="slippage-buttons__content">
|
|
|
|
{open && (
|
|
|
|
<div className="slippage-buttons__dropdown-content">
|
|
|
|
<div className="slippage-buttons__buttons-prefix">
|
2020-11-03 00:41:28 +01:00
|
|
|
<div className="slippage-buttons__prefix-text">
|
|
|
|
{t('swapsMaxSlippage')}
|
|
|
|
</div>
|
2020-10-06 20:28:38 +02:00
|
|
|
<InfoTooltip
|
|
|
|
position="top"
|
|
|
|
contentText={t('swapAdvancedSlippageInfo')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<ButtonGroup
|
2020-11-03 00:41:28 +01:00
|
|
|
defaultActiveButtonIndex={
|
|
|
|
activeButtonIndex === 2 && !customValue ? 1 : activeButtonIndex
|
|
|
|
}
|
2020-10-06 20:28:38 +02:00
|
|
|
variant="radiogroup"
|
|
|
|
newActiveButtonIndex={activeButtonIndex}
|
2020-11-03 00:41:28 +01:00
|
|
|
className={classnames(
|
|
|
|
'button-group',
|
|
|
|
'slippage-buttons__button-group',
|
|
|
|
)}
|
2020-10-06 20:28:38 +02:00
|
|
|
>
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
setCustomValue('')
|
|
|
|
setEnteringCustomValue(false)
|
|
|
|
setActiveButtonIndex(0)
|
|
|
|
onSelect(1)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
1%
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
2020-10-28 14:58:31 +01:00
|
|
|
setCustomValue('')
|
2020-10-06 20:28:38 +02:00
|
|
|
setEnteringCustomValue(false)
|
|
|
|
setActiveButtonIndex(1)
|
|
|
|
onSelect(2)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
2%
|
|
|
|
</Button>
|
|
|
|
<Button
|
2020-11-03 00:41:28 +01:00
|
|
|
className={classnames(
|
|
|
|
'slippage-buttons__button-group-custom-button',
|
|
|
|
{
|
|
|
|
'radio-button--danger': errorText,
|
|
|
|
},
|
|
|
|
)}
|
2020-10-06 20:28:38 +02:00
|
|
|
onClick={() => {
|
|
|
|
setActiveButtonIndex(2)
|
|
|
|
setEnteringCustomValue(true)
|
|
|
|
}}
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{enteringCustomValue ? (
|
|
|
|
<div
|
|
|
|
className={classnames('slippage-buttons__custom-input', {
|
|
|
|
'slippage-buttons__custom-input--danger': errorText,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
onChange={(event) => {
|
|
|
|
setCustomValue(event.target.value)
|
2020-11-19 16:26:55 +01:00
|
|
|
onSelect(Number(event.target.value))
|
2020-11-03 00:41:28 +01:00
|
|
|
}}
|
|
|
|
type="number"
|
|
|
|
step="0.1"
|
|
|
|
ref={setInputRef}
|
|
|
|
onBlur={() => {
|
|
|
|
setEnteringCustomValue(false)
|
|
|
|
if (customValue === '0') {
|
|
|
|
setCustomValue('')
|
|
|
|
setActiveButtonIndex(1)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
value={customValue || ''}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
customValueText
|
2020-10-06 20:28:38 +02:00
|
|
|
)}
|
2020-11-03 00:41:28 +01:00
|
|
|
{(customValue || enteringCustomValue) && (
|
2020-10-06 20:28:38 +02:00
|
|
|
<div className="slippage-buttons__percentage-suffix">%</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
)}
|
2020-10-06 20:28:38 +02:00
|
|
|
</Button>
|
|
|
|
</ButtonGroup>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
SlippageButtons.propTypes = {
|
|
|
|
onSelect: PropTypes.func.isRequired,
|
2020-12-07 16:12:55 +01:00
|
|
|
maxAllowedSlippage: PropTypes.number.isRequired,
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|