shrub/pkg/btc-wallet/src/js/components/lib/balance.js

141 lines
4.0 KiB
JavaScript
Raw Normal View History

2021-07-01 00:17:31 +03:00
import React, { useState } from 'react';
import { Row, Text, Button, Col } from '@tlon/indigo-react';
import Send from './send.js';
import CurrencyPicker from './currencyPicker.js';
import { satsToCurrency } from '../../lib/util.js';
2021-07-01 00:17:31 +03:00
import { useSettings } from '../../hooks/useSettings.js';
import { api } from '../../api';
2021-07-01 00:17:31 +03:00
const Balance = () => {
const {
address,
confirmedBalance: sats,
unconfirmedBalance: unconfirmedSats,
denomination,
currencyRates,
setPsbt,
setFee,
setError,
} = useSettings();
const [sending, setSending] = useState(false);
const [copiedButton, setCopiedButton] = useState(false);
const [copiedString, setCopiedString] = useState(false);
2021-07-01 00:17:31 +03:00
const copyAddress = (arg) => {
navigator.clipboard.writeText(address);
2021-07-01 00:17:31 +03:00
api.btcWalletCommand({ 'gen-new-address': null });
2021-05-21 20:32:15 +03:00
if (arg === 'button') {
2021-07-01 00:17:31 +03:00
setCopiedButton(true);
setTimeout(() => {
2021-07-01 00:17:31 +03:00
setCopiedButton(false);
}, 2000);
2021-05-21 20:32:15 +03:00
} else if (arg === 'string') {
2021-07-01 00:17:31 +03:00
setCopiedString(true);
setTimeout(() => {
2021-07-01 00:17:31 +03:00
setCopiedString(false);
}, 2000);
2021-05-21 20:32:15 +03:00
}
2021-07-01 00:17:31 +03:00
};
2021-04-20 05:45:34 +03:00
2021-07-01 00:17:31 +03:00
const unconfirmedString = unconfirmedSats ? ` (${unconfirmedSats}) ` : '';
2021-07-01 00:17:31 +03:00
const value = satsToCurrency(sats, denomination, currencyRates);
const sendDisabled = sats === 0;
const addressText =
address === null ? '' : address.slice(0, 6) + '...' + address.slice(-6);
2021-07-01 00:17:31 +03:00
const conversion = currencyRates[denomination]?.last;
2021-07-01 00:17:31 +03:00
return (
<>
{sending ? (
<Send
value={value}
conversion={conversion}
stopSending={() => {
setSending(false);
setPsbt('');
setFee(0);
setError('');
}}
/>
) : (
<Col
height="400px"
width="100%"
backgroundColor="white"
borderRadius="48px"
justifyContent="space-between"
mb={5}
p={5}
>
<Row justifyContent="space-between">
<Text color="orange" fontSize={1}>
Balance
</Text>
<Text
color="lightGray"
fontSize="14px"
mono
style={{ cursor: 'pointer' }}
onClick={() => copyAddress('string')}
>
{copiedString ? 'copied' : addressText}
</Text>
<CurrencyPicker />
</Row>
<Col justifyContent="center" alignItems="center">
<Text
fontSize="40px"
color="orange"
style={{ whiteSpace: 'nowrap' }}
>
{value}
</Text>
<Text
fontSize={1}
color="orange"
>{`${sats}${unconfirmedString} sats`}</Text>
2021-04-15 15:38:51 +03:00
</Col>
2021-07-01 00:17:31 +03:00
<Row flexDirection="row-reverse">
<Button
disabled={sendDisabled}
fontSize={1}
fontWeight="bold"
color={sendDisabled ? 'lighterGray' : 'white'}
backgroundColor={sendDisabled ? 'veryLightGray' : 'orange'}
style={{ cursor: sendDisabled ? 'default' : 'pointer' }}
borderColor="none"
borderRadius="24px"
height="48px"
onClick={() => setSending(true)}
>
Send
</Button>
<Button
mr={3}
disabled={copiedButton}
fontSize={1}
fontWeight="bold"
color={copiedButton ? 'green' : 'orange'}
backgroundColor={copiedButton ? 'veryLightGreen' : 'midOrange'}
style={{
cursor: copiedButton ? 'default' : 'pointer',
}}
borderColor="none"
borderRadius="24px"
height="48px"
onClick={() => copyAddress('button')}
>
{copiedButton ? 'Address Copied!' : 'Copy Address'}
</Button>
</Row>
</Col>
)}
</>
);
};
export default Balance;