import { useTranslation } from "@plexswap/localization"; import { displayBalance } from "@plexswap/utils/displayBalance"; import { trimTrailZero } from "@plexswap/utils/trimTrailZero"; import BigNumber from "bignumber.js"; import { useMemo } from "react"; import { styled } from "styled-components"; import { Balance } from "../../components/Balance"; import { Flex } from "../../components/Box"; import { Button } from "../../components/Button"; import { Input, InputProps } from "../../components/Input"; import { Link } from "../../components/Link"; import { Text } from "../../components/Text"; interface ModalInputProps { max: string; maxAmount?: BigNumber; symbol: string; onSelectMax?: () => void; onPercentInput?: (percent: number) => void; onChange: (e: React.FormEvent) => void; placeholder?: string; value: string; valueUSDPrice?: BigNumber; addLiquidityUrl?: string; inputTitle?: string; decimals?: number; needEnable?: boolean; } const StyledTokenInput = styled.div` display: flex; flex-direction: column; background-color: ${({ theme }) => theme.colors.input}; border-radius: 16px; box-shadow: ${({ theme, isWarning }) => (isWarning ? theme.colors.warning : theme.shadows.inset)}; color: ${({ theme }) => theme.colors.text}; padding: 8px 16px 8px 0; width: 100%; `; const StyledInput = styled(Input)` box-shadow: none; width: 50%; margin: 0 8px; padding: 0 8px; border: none; ${({ theme }) => theme.mediaQueries.sm} { width: auto; } `; const StyledErrorMessage = styled(Text)` position: absolute; bottom: -22px; a { display: inline; } `; const ModalInput: React.FC> = ({ max, maxAmount, symbol, onChange, onSelectMax, onPercentInput, value, valueUSDPrice, addLiquidityUrl, inputTitle, decimals = 18, needEnable, }) => { const { t } = useTranslation(); const isBalanceZero = max === "0" || !max; const percentAmount = useMemo( () => ({ 25: maxAmount ? trimTrailZero(maxAmount.dividedBy(100).multipliedBy(25).toNumber().toFixed(decimals)) : undefined, 50: maxAmount ? trimTrailZero(maxAmount.dividedBy(100).multipliedBy(50).toNumber().toFixed(decimals)) : undefined, 75: maxAmount ? trimTrailZero(maxAmount.dividedBy(100).multipliedBy(75).toNumber().toFixed(decimals)) : undefined, }), [maxAmount, decimals] ); const isAtPercentMax = maxAmount && value === maxAmount.toString(); return (
{inputTitle} {t("Balance: %balance%", { balance: displayBalance({ balance: max as `${number}`, decimals, isBalanceZero, }), })} {symbol} {valueUSDPrice && ( )} {maxAmount?.isGreaterThan(0) && onPercentInput && [25, 50, 75].map((percent) => { let currentPercentAmount; if (percent === 25) currentPercentAmount = percentAmount[25]; else if (percent === 50) currentPercentAmount = percentAmount[50]; else if (percent === 75) currentPercentAmount = percentAmount[75]; const isAtCurrentPercent = maxAmount && value === currentPercentAmount; return ( ); })} {maxAmount?.isGreaterThan(0) && ( <> )} {isBalanceZero && ( {t("No tokens to stake")}:{" "} {t("Add %symbol%", { symbol })} )} {needEnable && ( {t('Insufficient token allowance. Click "Enable" to approve.')} )}
); }; export default ModalInput;