import React, { useState } from 'react'; import { toast } from 'react-toastify'; import sortBy from 'lodash.sortby'; import { useAccountState } from 'src/context/AccountContext'; import { useGetChannelsQuery } from 'src/graphql/queries/__generated__/getChannels.generated'; import { GridWrapper } from 'src/components/gridWrapper/GridWrapper'; import { withApollo } from 'config/client'; import { CardWithTitle, Card, SubTitle, Sub4Title, ResponsiveLine, DarkSubTitle, NoWrapTitle, SingleLine, } from '../src/components/generic/Styled'; import { getErrorContent } from '../src/utils/error'; import { LoadingCard } from '../src/components/loading/LoadingCard'; import { getPercent } from '../src/utils/helpers'; import { Input } from '../src/components/input/Input'; import { BalanceCard } from '../src/views/balance/BalanceCard'; import { BalanceRoute } from '../src/views/balance/BalanceRoute'; import { Price } from '../src/components/price/Price'; import { useStatusState } from '../src/context/StatusContext'; import { Text } from '../src/components/typography/Styled'; const BalanceView = () => { const { minorVersion } = useStatusState(); const { auth } = useAccountState(); const [outgoing, setOutgoing] = useState<{ id: string } | null>(); const [incoming, setIncoming] = useState(); const [amount, setAmount] = useState(); const [maxFee, setMaxFee] = useState(); const [blocked, setBlocked] = useState(false); const { loading, data } = useGetChannelsQuery({ skip: !auth, variables: { auth, active: true }, onError: error => toast.error(getErrorContent(error)), }); if (minorVersion < 9) { return ( Channel Balancing Channel balancing is only available for nodes with LND versions 0.9.0-beta and up. If you want to use this feature please update your node. ); } if (loading || !data || !data.getChannels) { return ; } const handleReset = (type: string) => { switch (type) { case 'outgoing': setOutgoing(undefined); setIncoming(undefined); break; case 'incoming': setIncoming(undefined); break; case 'all': setMaxFee(undefined); setAmount(undefined); setOutgoing(undefined); setIncoming(undefined); setBlocked(false); break; default: break; } }; const renderChannels = (isOutgoing?: boolean) => { const channels = sortBy(data.getChannels, channel => getPercent(channel.remote_balance, channel.local_balance) ); const finalChannels = isOutgoing ? channels : channels.reverse(); return finalChannels.map((channel: any, index: number) => { if (!isOutgoing && outgoing && outgoing.id === channel.id) { return null; } const callback = isOutgoing ? !outgoing && { callback: () => setOutgoing(channel) } : outgoing && !incoming && { callback: () => setIncoming(channel) }; return ( ); }); }; const renderIncoming = () => { if (!outgoing) return null; return ( <> Incoming Channel {incoming ? ( handleReset('incoming'), }} /> ) : ( renderChannels() )} ); }; const renderOutgoing = () => { return ( <> Outgoing Channel {outgoing ? ( handleReset('outgoing'), }} /> ) : ( renderChannels(true) )} ); }; return ( Channel Balancing {renderOutgoing()} {renderIncoming()} Amount {!blocked && ( { setAmount(Number(e.target.value)); }} withMargin={'0 0 8px'} /> )} Max Fee {!blocked && ( { setMaxFee(Number(e.target.value)); }} withMargin={'0 0 24px'} /> )} {incoming && outgoing && amount && ( setBlocked(true)} callback={() => handleReset('all')} /> )} ); }; const Wrapped = () => ( ); export default withApollo(Wrapped);