|
| 1 | +import { truncateNumbers } from '@kwenta/sdk/utils' |
| 2 | +import { FC, useCallback, useMemo } from 'react' |
| 3 | +import { useTranslation } from 'react-i18next' |
| 4 | +import styled from 'styled-components' |
| 5 | + |
| 6 | +import Button from 'components/Button' |
| 7 | +import ErrorView from 'components/ErrorView' |
| 8 | +import { FlexDivRowCentered } from 'components/layout/flex' |
| 9 | +import { StakingCard } from 'sections/dashboard/Stake/card' |
| 10 | +import { useAppDispatch, useAppSelector } from 'state/hooks' |
| 11 | +import { approveKwentaToken, redeemToken } from 'state/staking/actions' |
| 12 | +import { |
| 13 | + selectIsVeKwentaTokenApproved, |
| 14 | + selectIsVKwentaTokenApproved, |
| 15 | + selectVeKwentaBalance, |
| 16 | + selectVKwentaBalance, |
| 17 | +} from 'state/staking/selectors' |
| 18 | +import { selectDisableRedeemEscrowKwenta } from 'state/stakingMigration/selectors' |
| 19 | +import { numericValueCSS } from 'styles/common' |
| 20 | + |
| 21 | +type RedeemInputCardProps = { |
| 22 | + inputLabel: string |
| 23 | + isVKwenta: boolean |
| 24 | +} |
| 25 | + |
| 26 | +const RedeemInputCard: FC<RedeemInputCardProps> = ({ inputLabel, isVKwenta }) => { |
| 27 | + const { t } = useTranslation() |
| 28 | + const dispatch = useAppDispatch() |
| 29 | + |
| 30 | + const vKwentaBalance = useAppSelector(selectVKwentaBalance) |
| 31 | + const veKwentaBalance = useAppSelector(selectVeKwentaBalance) |
| 32 | + const isVKwentaApproved = useAppSelector(selectIsVKwentaTokenApproved) |
| 33 | + const isVeKwentaApproved = useAppSelector(selectIsVeKwentaTokenApproved) |
| 34 | + const VeKwentaDisableRedeem = useAppSelector(selectDisableRedeemEscrowKwenta) |
| 35 | + |
| 36 | + const isApproved = useMemo( |
| 37 | + () => (isVKwenta ? isVKwentaApproved : isVeKwentaApproved), |
| 38 | + [isVKwenta, isVKwentaApproved, isVeKwentaApproved] |
| 39 | + ) |
| 40 | + |
| 41 | + const balance = useMemo( |
| 42 | + () => (isVKwenta ? vKwentaBalance : veKwentaBalance), |
| 43 | + [isVKwenta, vKwentaBalance, veKwentaBalance] |
| 44 | + ) |
| 45 | + |
| 46 | + const DisableRedeem = useMemo( |
| 47 | + () => (isVKwenta ? false : VeKwentaDisableRedeem), |
| 48 | + [isVKwenta, VeKwentaDisableRedeem] |
| 49 | + ) |
| 50 | + |
| 51 | + const buttonLabel = useMemo(() => { |
| 52 | + return isApproved |
| 53 | + ? t('dashboard.stake.tabs.stake-table.redeem') |
| 54 | + : t('dashboard.stake.tabs.stake-table.approve') |
| 55 | + }, [isApproved, t]) |
| 56 | + |
| 57 | + const submitRedeem = useCallback(() => { |
| 58 | + const token = isVKwenta ? 'vKwenta' : 'veKwenta' |
| 59 | + |
| 60 | + if (!isApproved) { |
| 61 | + dispatch(approveKwentaToken(token)) |
| 62 | + } else { |
| 63 | + dispatch(redeemToken(token)) |
| 64 | + } |
| 65 | + }, [dispatch, isApproved, isVKwenta]) |
| 66 | + |
| 67 | + return ( |
| 68 | + <> |
| 69 | + <StakingInputCardContainer> |
| 70 | + <div> |
| 71 | + <StakeInputHeader> |
| 72 | + <div>{inputLabel}</div> |
| 73 | + <StyledFlexDivRowCentered> |
| 74 | + <div>{t('dashboard.stake.tabs.stake-table.balance')}</div> |
| 75 | + <div className="max">{truncateNumbers(balance, 4)}</div> |
| 76 | + </StyledFlexDivRowCentered> |
| 77 | + </StakeInputHeader> |
| 78 | + </div> |
| 79 | + {DisableRedeem ? ( |
| 80 | + <ErrorView |
| 81 | + messageType="warn" |
| 82 | + message={t('dashboard.stake.tabs.redeem.warning')} |
| 83 | + containerStyle={{ |
| 84 | + margin: '0', |
| 85 | + marginTop: '25px', |
| 86 | + padding: '10px 0', |
| 87 | + }} |
| 88 | + /> |
| 89 | + ) : ( |
| 90 | + <Button |
| 91 | + fullWidth |
| 92 | + variant="flat" |
| 93 | + size="small" |
| 94 | + disabled={balance.eq(0) || DisableRedeem} |
| 95 | + onClick={submitRedeem} |
| 96 | + > |
| 97 | + {buttonLabel} |
| 98 | + </Button> |
| 99 | + )} |
| 100 | + </StakingInputCardContainer> |
| 101 | + </> |
| 102 | + ) |
| 103 | +} |
| 104 | + |
| 105 | +const StyledFlexDivRowCentered = styled(FlexDivRowCentered)` |
| 106 | + column-gap: 5px; |
| 107 | +` |
| 108 | + |
| 109 | +const StakingInputCardContainer = styled(StakingCard)` |
| 110 | + min-height: 125px; |
| 111 | + max-height: 250px; |
| 112 | + display: flex; |
| 113 | + flex-direction: column; |
| 114 | + justify-content: space-between; |
| 115 | +` |
| 116 | + |
| 117 | +const StakeInputHeader = styled.div` |
| 118 | + display: flex; |
| 119 | + justify-content: space-between; |
| 120 | + align-items: center; |
| 121 | + margin-bottom: 10px; |
| 122 | + color: ${(props) => props.theme.colors.selectedTheme.title}; |
| 123 | + font-size: 14px; |
| 124 | + .max { |
| 125 | + color: ${(props) => props.theme.colors.selectedTheme.button.text.primary}; |
| 126 | + ${numericValueCSS}; |
| 127 | + } |
| 128 | +` |
| 129 | + |
| 130 | +export default RedeemInputCard |
0 commit comments