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

127 lines
4.0 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react';
import {
Box,
Icon,
Row,
Text,
Button,
Col,
} from '@tlon/indigo-react';
2021-04-12 19:24:27 +03:00
import Send from './send.js'
2021-04-20 08:02:56 +03:00
import CurrencyPicker from './currencyPicker.js'
import { currencyToSats, satsToCurrency } from '../../lib/util.js';
2021-04-12 19:24:27 +03:00
export default class Balance extends Component {
constructor(props) {
super(props);
this.state = {
sending: false,
2021-04-20 05:45:34 +03:00
copied: false,
}
2021-04-20 05:45:34 +03:00
this.copyAddress = this.copyAddress.bind(this);
}
2021-04-20 05:45:34 +03:00
copyAddress() {
let address = this.props.state.address;
function listener(e) {
e.clipboardData.setData('text/plain', address);
e.preventDefault();
}
document.addEventListener('copy', listener);
document.execCommand('copy');
document.removeEventListener('copy', listener);
this.props.api.btcWalletCommand({'gen-new-address': null});
this.setState({copied: true});
setTimeout(() => {
this.setState({copied: false});
}, 2000);
}
render() {
2021-04-16 17:11:03 +03:00
const sats = (this.props.state.balance || 0);
2021-04-20 08:02:56 +03:00
const denomination = this.props.state.denomination;
const value = satsToCurrency(sats, denomination, this.props.state.currencyRates);
2021-04-16 17:11:03 +03:00
const sendDisabled = (sats === 0);
2021-04-20 05:45:34 +03:00
const addressText = (this.props.state.address === null) ? '' :
this.props.state.address.slice(0, 6) + '...' +
this.props.state.address.slice(-6);
2021-04-20 08:02:56 +03:00
const conversion = this.props.state.currencyRates[denomination].last;
return (
2021-04-15 15:38:51 +03:00
<>
2021-04-12 19:24:27 +03:00
{this.state.sending ?
<Send
api={api}
psbt={this.props.state.psbt}
2021-04-20 05:45:34 +03:00
shipWallets={this.props.state.shipWallets}
2021-04-12 19:24:27 +03:00
value={value}
2021-04-20 08:02:56 +03:00
denomination={denomination}
2021-04-12 19:24:27 +03:00
sats={sats}
2021-04-20 08:02:56 +03:00
conversion={conversion}
2021-04-12 19:24:27 +03:00
stopSending={() => {this.setState({sending: false})}}
/> :
2021-04-15 15:38:51 +03:00
<Col
height="400px"
width='100%'
backgroundColor="white"
borderRadius="32px"
2021-04-16 17:11:03 +03:00
justifyContent="space-between"
2021-04-15 15:38:51 +03:00
mb={5}
p={5}
>
2021-04-12 19:24:27 +03:00
<Row justifyContent="space-between">
<Text color="orange" fontSize={1}>Balance</Text>
2021-04-20 05:45:34 +03:00
<Text color="lighterGray" fontSize="14px" mono>{addressText}</Text>
2021-04-20 08:02:56 +03:00
<CurrencyPicker
denomination={denomination}
currencies={this.props.state.currencyRates}
/>
2021-04-12 19:24:27 +03:00
</Row>
2021-04-20 05:45:34 +03:00
<Col justifyContent="center" alignItems="center">
2021-04-12 19:24:27 +03:00
<Text fontSize="52px" color="orange">{value}</Text>
<Text fontSize={1} color="orange">{sats} sats</Text>
</Col>
<Row flexDirection="row-reverse">
<Button children="Send"
2021-04-16 17:11:03 +03:00
disabled={sendDisabled}
2021-04-12 19:24:27 +03:00
fontSize={1}
fontWeight="bold"
2021-04-16 17:11:03 +03:00
color={sendDisabled ? "lighterGray" : "white"}
backgroundColor={sendDisabled ? "veryLightGray" : "orange"}
style={{cursor: sendDisabled ? "default" : "pointer" }}
2021-04-12 19:24:27 +03:00
borderColor="none"
borderRadius="24px"
py="24px"
px="24px"
onClick={() => this.setState({sending: true})}
/>
2021-04-20 05:45:34 +03:00
<Button children={(this.state.copied) ? "Address Copied!" : "Copy Address"}
mr={3}
disabled={this.state.copied}
2021-04-12 19:24:27 +03:00
fontSize={1}
fontWeight="bold"
2021-04-20 05:45:34 +03:00
color={(this.state.copied) ? "green" : "orange"}
backgroundColor={(this.state.copied) ? "veryLightGreen" : "midOrange" }
style={{cursor: (this.state.copied) ? "default" : "pointer"}}
2021-04-12 19:24:27 +03:00
borderColor="none"
borderRadius="24px"
py="24px"
px="24px"
2021-04-20 05:45:34 +03:00
onClick={this.copyAddress}
2021-04-12 19:24:27 +03:00
/>
</Row>
2021-04-15 15:38:51 +03:00
</Col>
2021-04-12 19:24:27 +03:00
}
2021-04-15 15:38:51 +03:00
</>
);
}
}