mango-ui-v3/components/SwapSettingsModal.tsx

85 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-01-02 21:05:54 -08:00
import { useEffect, useState } from 'react'
2021-12-29 14:12:43 -08:00
import Modal from './Modal'
2022-01-02 21:05:54 -08:00
import { useTranslation } from 'next-i18next'
2021-12-29 14:12:43 -08:00
import Button from './Button'
2022-01-02 04:20:42 -08:00
import ButtonGroup from './ButtonGroup'
import Input from './Input'
2022-01-02 21:05:54 -08:00
import { LinkButton } from './Button'
const slippagePresets = ['0.1', '0.5', '1', '2']
2021-12-29 14:12:43 -08:00
const SwapSettingsModal = ({
isOpen,
onClose,
slippage,
setSlippage,
}: {
isOpen: boolean
onClose?: () => void
slippage: number
setSlippage: (x) => void
}) => {
2022-01-02 21:05:54 -08:00
const { t } = useTranslation('common')
2021-12-29 14:12:43 -08:00
const [tempSlippage, setTempSlippage] = useState(slippage)
2022-01-02 21:05:54 -08:00
const [inputValue, setInputValue] = useState(
tempSlippage ? tempSlippage.toString() : ''
)
const [showCustomSlippageForm, setShowCustomSlippageForm] = useState(false)
2021-12-29 14:12:43 -08:00
const handleSetTempSlippage = (s) => {
setTempSlippage(s)
setInputValue('')
}
const handleSave = () => {
setSlippage(inputValue ? parseFloat(inputValue) : tempSlippage)
onClose()
}
2022-01-02 21:05:54 -08:00
useEffect(() => {
if (!slippagePresets.includes(tempSlippage.toString())) {
setShowCustomSlippageForm(true)
}
}, [])
2021-12-29 14:12:43 -08:00
return (
<Modal isOpen={isOpen} onClose={onClose} hideClose>
<Modal.Header>
2022-01-02 04:20:42 -08:00
<h2 className="font-bold text-th-fgd-1 text-lg">Slippage Settings</h2>
2021-12-29 14:12:43 -08:00
</Modal.Header>
2022-01-02 21:05:54 -08:00
<div className="flex justify-between mb-2">
<div className="text-th-fgd-1 text-xs">Slippage</div>
<LinkButton
className="font-normal text-th-fgd-3 text-xs"
onClick={() => setShowCustomSlippageForm(!showCustomSlippageForm)}
>
{showCustomSlippageForm ? t('presets') : t('custom')}
</LinkButton>
</div>
{showCustomSlippageForm ? (
2022-01-02 04:20:42 -08:00
<Input
2021-12-29 14:12:43 -08:00
type="text"
2022-01-02 04:20:42 -08:00
className="w-full bg-th-bkg-1 focus:outline-none rounded"
2021-12-29 14:12:43 -08:00
placeholder="0.00"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
2022-01-02 04:20:42 -08:00
suffix="%"
2021-12-29 14:12:43 -08:00
/>
2022-01-02 21:05:54 -08:00
) : (
<ButtonGroup
activeValue={tempSlippage.toString()}
className="h-10"
onChange={(v) => handleSetTempSlippage(v)}
unit="%"
values={slippagePresets}
/>
)}
<Button className="mt-6 w-full" onClick={handleSave}>
2022-01-02 04:20:42 -08:00
Save
</Button>
2021-12-29 14:12:43 -08:00
</Modal>
)
}
export default SwapSettingsModal