// @flow import * as ICONS from 'constants/icons'; import * as PAGES from 'constants/pages'; import React from 'react'; import Button from 'component/button'; import { FormField } from 'component/common/form'; import { MINIMUM_PUBLISH_BID } from 'constants/claim'; import CreditAmount from 'component/common/credit-amount'; import I18nMessage from 'component/i18nMessage'; import classnames from 'classnames'; import usePersistedState from 'effects/use-persisted-state'; import WalletSpendableBalanceHelp from 'component/walletSpendableBalanceHelp'; const DEFAULT_TIP_AMOUNTS = [1, 5, 25, 100]; type Props = { balance: number, amount: number, onChange: (number) => void, }; function WalletTipAmountSelector(props: Props) { const { balance, amount, onChange } = props; const [useCustomTip, setUseCustomTip] = usePersistedState('comment-support:useCustomTip', false); const [tipError, setTipError] = React.useState(); React.useEffect(() => { const regexp = RegExp(/^(\d*([.]\d{0,8})?)$/); const validTipInput = regexp.test(String(amount)); let tipError; if (!amount) { tipError = __('Amount must be a number'); } else if (amount <= 0) { tipError = __('Amount must be a positive number'); } else if (amount < MINIMUM_PUBLISH_BID) { tipError = __('Amount must be higher'); } else if (!validTipInput) { tipError = __('Amount must have no more than 8 decimal places'); } else if (amount === balance) { tipError = __('Please decrease the amount to account for transaction fees'); } else if (amount > balance) { tipError = __('Not enough Credits'); } setTipError(tipError); }, [amount, balance, setTipError]); function handleCustomPriceChange(amount: number) { const tipAmount = parseFloat(amount); onChange(tipAmount); } return ( <>