fix: fixed Borrow infinite render (#488)

* fix: fixed Borrow infinite render

* feat: added a Repay not available info

* tidy: updated wording
This commit is contained in:
Linkie Link 2023-09-19 17:02:56 +02:00 committed by GitHub
parent 9ced4ab6c9
commit ca5e424700
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 46 deletions

View File

@ -8,7 +8,7 @@ import Card from 'components/Card'
import DisplayCurrency from 'components/DisplayCurrency' import DisplayCurrency from 'components/DisplayCurrency'
import Divider from 'components/Divider' import Divider from 'components/Divider'
import { FormattedNumber } from 'components/FormattedNumber' import { FormattedNumber } from 'components/FormattedNumber'
import { ArrowRight } from 'components/Icons' import { ArrowRight, InfoCircle } from 'components/Icons'
import Modal from 'components/Modal' import Modal from 'components/Modal'
import Switch from 'components/Switch' import Switch from 'components/Switch'
import Text from 'components/Text' import Text from 'components/Text'
@ -28,6 +28,11 @@ import { formatPercent, formatValue } from 'utils/formatters'
import { BN } from 'utils/helpers' import { BN } from 'utils/helpers'
import { getDebtAmountWithInterest } from 'utils/tokens' import { getDebtAmountWithInterest } from 'utils/tokens'
interface Props {
account: Account
modal: BorrowModal
}
function getDebtAmount(modal: BorrowModal) { function getDebtAmount(modal: BorrowModal) {
return BN((modal.marketData as BorrowMarketTableData)?.debt ?? 0).toString() return BN((modal.marketData as BorrowMarketTableData)?.debt ?? 0).toString()
} }
@ -37,9 +42,21 @@ function getAssetLogo(modal: BorrowModal) {
return <AssetImage asset={modal.asset} size={24} /> return <AssetImage asset={modal.asset} size={24} />
} }
interface Props { function RepayNotAvailable(props: { asset: Asset }) {
account: Account return (
modal: BorrowModal <Card className='mt-6'>
<div className='flex items-start p-4'>
<InfoCircle width={26} className='mr-2' />
<div className='flex flex-col gap-1'>
<Text size='sm'>No funds for repay</Text>
<Text
size='xs'
className='text-white/40'
>{`Unfortunately you don't have any ${props.asset.symbol} in your Credit Account to repay the debt.`}</Text>
</div>
</div>
</Card>
)
} }
export default function BorrowModalController() { export default function BorrowModalController() {
@ -69,11 +86,24 @@ function BorrowModal(props: Props) {
const { computeMaxBorrowAmount } = useHealthComputer(account) const { computeMaxBorrowAmount } = useHealthComputer(account)
const totalDebt = BN(getDebtAmount(modal)) const totalDebt = BN(getDebtAmount(modal))
const [depositBalance, lendBalance] = useMemo(
() => [
account.deposits.find(byDenom(asset.denom))?.amount ?? BN_ZERO,
account.lends.find(byDenom(asset.denom))?.amount ?? BN_ZERO,
],
[account, asset.denom],
)
const totalDebtRepayAmount = useMemo( const totalDebtRepayAmount = useMemo(
() => getDebtAmountWithInterest(totalDebt, Number(apr)), () => getDebtAmountWithInterest(totalDebt, Number(apr)),
[totalDebt, apr], [totalDebt, apr],
) )
const maxRepayAmount = useMemo(() => {
const maxBalance = depositBalance.plus(lendBalance)
return isRepay ? BigNumber.min(maxBalance, totalDebtRepayAmount) : BN_ZERO
}, [depositBalance, lendBalance, isRepay, totalDebtRepayAmount])
function resetState() { function resetState() {
setAmount(BN_ZERO) setAmount(BN_ZERO)
} }
@ -131,26 +161,20 @@ function BorrowModal(props: Props) {
useEffect(() => { useEffect(() => {
if (!account || isRepay) return if (!account || isRepay) return
if (maxBorrow !== max) setMax(maxBorrow) if (maxBorrow.isEqualTo(max)) return
setMax(maxBorrow)
}, [account, isRepay, maxBorrow, max]) }, [account, isRepay, maxBorrow, max])
useEffect(() => { useEffect(() => {
if (!account) return if (!isRepay) return
if (isRepay) { if (maxRepayAmount.isEqualTo(max)) return
const depositBalance = account.deposits.find(byDenom(asset.denom))?.amount ?? BN_ZERO
const lendBalance = account.lends.find(byDenom(asset.denom))?.amount ?? BN_ZERO
const maxBalance = depositBalance.plus(lendBalance)
const maxRepayAmount = BigNumber.min(maxBalance, totalDebtRepayAmount)
setMax(maxRepayAmount) setMax(maxRepayAmount)
return }, [isRepay, max, maxRepayAmount])
}
}, [account, asset.denom, isRepay, totalDebtRepayAmount])
useEffect(() => { useEffect(() => {
if (amount.isGreaterThan(max)) { if (amount.isLessThanOrEqualTo(max)) return
handleChange(max) handleChange(max)
setAmount(max) setAmount(max)
}
}, [amount, max, handleChange]) }, [amount, max, handleChange])
useEffect(() => { useEffect(() => {
@ -223,9 +247,11 @@ function BorrowModal(props: Props) {
onChange={handleChange} onChange={handleChange}
amount={amount} amount={amount}
max={max} max={max}
disabled={max.isZero()}
className='w-full' className='w-full'
maxText='Max' maxText='Max'
/> />
{isRepay && maxRepayAmount.isZero() && <RepayNotAvailable asset={asset} />}
{!isRepay && ( {!isRepay && (
<> <>
<Divider className='my-6' /> <Divider className='my-6' />

View File

@ -128,6 +128,7 @@ export default function Slider(props: Props) {
disabled={props.disabled} disabled={props.disabled}
/> />
</div> </div>
{!props.disabled && (
<div onMouseEnter={handleShowTooltip} onMouseLeave={handleHideTooltip}> <div onMouseEnter={handleShowTooltip} onMouseLeave={handleHideTooltip}>
<DraggableElement <DraggableElement
nodeRef={nodeRef} nodeRef={nodeRef}
@ -154,6 +155,7 @@ export default function Slider(props: Props) {
</div> </div>
</DraggableElement> </DraggableElement>
</div> </div>
)}
</div> </div>
) )
} }
@ -170,7 +172,9 @@ function Mark(props: MarkProps) {
<button <button
onClick={() => props.onClick(props.value)} onClick={() => props.onClick(props.value)}
className={`z-20 h-3 w-3 rotate-45 rounded-xs border-[1px] border-white/20 hover:border-[2px] hover:border-white ${ className={`z-20 h-3 w-3 rotate-45 rounded-xs border-[1px] border-white/20 hover:border-[2px] hover:border-white ${
props.sliderValue >= props.value ? 'bg-martian-red hover:border-white' : 'bg-grey-medium' props.sliderValue >= props.value && !props.disabled
? 'bg-martian-red hover:border-white'
: 'bg-grey-medium'
}`} }`}
disabled={props.disabled} disabled={props.disabled}
></button> ></button>