BlueWallet/components/addresses/AddressItem.js

116 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-04-19 06:24:04 -04:00
import React, { useRef } from 'react';
import { StyleSheet, Text, View } from 'react-native';
2021-02-04 04:05:37 -03:00
import { useTheme } from '@react-navigation/native';
import { ListItem } from 'react-native-elements';
import PropTypes from 'prop-types';
import { AddressTypeBadge } from './AddressTypeBadge';
2021-04-19 06:24:04 -04:00
import loc, { formatBalance } from '../../loc';
import TooltipMenu from '../TooltipMenu';
import Clipboard from '@react-native-clipboard/clipboard';
import Share from 'react-native-share';
2021-02-04 04:05:37 -03:00
2021-04-17 13:43:39 -04:00
const AddressItem = ({ item, balanceUnit, onPress }) => {
2021-02-04 04:05:37 -03:00
const { colors } = useTheme();
2021-04-19 06:24:04 -04:00
const tooltip = useRef();
const listItem = useRef();
2021-02-04 04:05:37 -03:00
2021-04-19 06:24:04 -04:00
const stylesHook = StyleSheet.create({
2021-02-04 04:05:37 -03:00
container: {
borderBottomColor: colors.lightBorder,
backgroundColor: colors.elevated,
},
list: {
color: colors.buttonTextColor,
},
2021-04-16 15:14:06 +02:00
index: {
color: colors.alternativeTextColor,
},
balance: {
color: colors.alternativeTextColor,
},
2021-02-04 04:05:37 -03:00
});
2021-04-19 06:24:04 -04:00
const showToolTipMenu = () => {
tooltip.current.showMenu();
};
2021-02-04 04:05:37 -03:00
const balance = formatBalance(item.balance, balanceUnit, true);
2021-04-19 06:24:04 -04:00
const handleCopyPress = () => {
Clipboard.setString(item.address);
};
const handleSharePress = () => {
Share.open({ message: item.address }).catch(error => console.log(error));
};
2021-02-04 04:05:37 -03:00
const render = () => {
return (
2021-04-19 06:24:04 -04:00
<View>
<TooltipMenu
ref={tooltip}
anchorRef={listItem}
actions={[
{
id: 'copyToClipboard',
text: loc.transactions.details_copy,
onPress: handleCopyPress,
},
{
id: 'share',
text: loc.receive.details_share,
onPress: handleSharePress,
},
]}
/>
<ListItem
ref={listItem}
key={`${item.key}`}
button
onPress={onPress}
containerStyle={stylesHook.container}
onLongPress={showToolTipMenu}
>
<ListItem.Content style={stylesHook.list}>
<ListItem.Title style={stylesHook.list} numberOfLines={1} ellipsizeMode="middle">
<Text style={[styles.index, stylesHook.index]}>{item.index + 1}</Text>{' '}
<Text style={[stylesHook.address, styles.address]}>{item.address}</Text>
</ListItem.Title>
<ListItem.Subtitle style={[stylesHook.list, styles.balance, stylesHook.balance]}>{balance}</ListItem.Subtitle>
</ListItem.Content>
<AddressTypeBadge isInternal={item.isInternal} />
</ListItem>
</View>
2021-02-04 04:05:37 -03:00
);
};
return render();
};
2021-04-19 06:24:04 -04:00
const styles = StyleSheet.create({
address: {
fontWeight: '600',
marginHorizontal: 40,
},
index: {
fontSize: 15,
},
balance: {
marginTop: 8,
marginLeft: 14,
},
});
2021-02-04 04:05:37 -03:00
AddressItem.propTypes = {
item: PropTypes.shape({
key: PropTypes.string,
index: PropTypes.number,
address: PropTypes.string,
isInternal: PropTypes.bool,
transactions: PropTypes.number,
balance: PropTypes.number,
}),
balanceUnit: PropTypes.string,
};
export { AddressItem };