import React, { useState, useCallback } from 'react'; import { Just, Nothing } from 'folktale/maybe'; import { Grid, H4, Text, ErrorText } from 'indigo-react'; import { reticketPointBetweenWallets } from 'lib/invite'; import { fromWei } from 'lib/txn'; import * as need from 'lib/need'; import useLifecycle from 'lib/useLifecycle'; import { useNetwork } from 'store/network'; import { useWallet } from 'store/wallet'; import { usePointCursor } from 'store/pointCursor'; import { useHistory } from 'store/history'; import { RestartButton, ForwardButton } from 'components/Buttons'; import WarningBox from 'components/WarningBox'; import LoadingBar from 'components/LoadingBar'; import Highlighted from 'components/Highlighted'; import { WALLET_TYPES } from 'lib/wallet'; // TODO: maybe we can merge PassportTransfer here? export default function ReticketExecute({ newWallet, setNewWallet }) { const { popTo, names, reset } = useHistory(); const { web3, contracts } = useNetwork(); const { wallet, setWalletType, resetWallet, setUrbitWallet } = useWallet(); const { pointCursor } = usePointCursor(); const [generalError, setGeneralError] = useState(); const [{ label, progress }, setState] = useState({ label: 'Starting...', progress: 0, }); const [needFunds, setNeedFunds] = useState(); const isDone = progress >= 1.0; // start reticketing transactions on mount useLifecycle(() => { (async () => { try { await reticketPointBetweenWallets({ fromWallet: need.wallet(wallet), toWallet: newWallet.value.wallet, point: need.point(pointCursor), web3: need.web3(web3), contracts: need.contracts(contracts), onUpdate: handleUpdate, }); } catch (err) { setGeneralError(err); } })(); }); const goToRestart = useCallback(() => { reset(); setNewWallet(Nothing()); }, [reset, setNewWallet]); const loginAndGoHome = useCallback(() => { // set wallet state resetWallet(); setWalletType(WALLET_TYPES.TICKET); setUrbitWallet(Just(newWallet.value.wallet)); // (implicit) pointCursor stays the same // rediect to point popTo(names.POINT); }, [resetWallet, setWalletType, setUrbitWallet, newWallet, popTo, names]); const handleUpdate = useCallback( ({ type, state, value }) => { switch (type) { case 'progress': return setState(state); case 'askFunding': return setNeedFunds(value); case 'gotFunding': return setNeedFunds(false); default: console.error(`Unknown update: ${type}`); } }, [setState, setNeedFunds] ); const renderAdditionalInfo = () => { if (generalError) { return ( <> {generalError.message.toString()} Restart ); } if (needFunds) { return ( The address {needFunds.address} needs at least{' '} {fromWei(needFunds.minBalance)} ETH and currently has{' '} {fromWei(needFunds.balance)} ETH. Waiting until the account has enough funds. ); } return ( Never give your Master Ticket to anyone ); }; return ( {label} {!isDone && ( <> This process can take up to 5 minutes to complete. Don't leave this page until the process is complete. {renderAdditionalInfo()} )} {isDone && ( <> Your changes are now reflected on-chain and you can use the new ticket to manage your point. Login with New Master Ticket )} ); }