import cn from 'classnames'; import { Box, Text } from '@tlon/indigo-react'; import { AddressButton } from 'components/AddressButton'; import InlineEthereumTransaction from 'components/InlineEthereumTransaction'; import BridgeForm from 'form/BridgeForm'; import { convertToNumber } from 'form/formatters'; import FormError from 'form/FormError'; import { AddressInput, NumberInput } from 'form/Inputs'; import { buildNumberValidator, composeValidator } from 'form/validators'; import { Grid, Flex } from 'indigo-react'; import { pluralize } from 'lib/pluralize'; import { useCallback, useEffect, useMemo } from 'react'; import { useStarReleaseCache } from 'store/starRelease'; import { useWithdrawStars } from './useWithdrawStars'; interface WithdrawFormProps { afterSubmit: VoidFunction; } export const WithdrawForm = ({ afterSubmit }: WithdrawFormProps) => { const { bind, construct, unconstruct, inputsLocked, completed, reset, } = useWithdrawStars(); const { syncStarReleaseDetails, starReleaseDetails, }: any = useStarReleaseCache(); useEffect(() => { syncStarReleaseDetails(); }, [syncStarReleaseDetails]); const onValues = useCallback( ({ valid, values }) => { if (valid) { construct(values.address, convertToNumber(values.numStars)); } else { unconstruct(); } }, [construct, unconstruct] ); const canWithdraw = starReleaseDetails.map((b: any) => b.available); const total = starReleaseDetails.map((b: any) => b.total); const validate = useMemo( () => composeValidator( { numStars: buildNumberValidator(0, canWithdraw.getOrElse(0) + 1), }, (values, errors) => (inputsLocked ? false : errors) ), [canWithdraw, inputsLocked] ); const initialValues = {}; return ( <> {({ values, form }: any) => ( <> {!completed && ( <> 0, })}> {`${canWithdraw.value} of ${total.value} available`} {`${total.value - canWithdraw.value} are in lockup`} Use my address )} {completed && ( <> {values.numStars}{' '} {pluralize(values.numStars, 'star has', 'stars have')} been withdrawn )} { reset(); form.reset(); }} /> )} ); };