BlueWallet/components/addresses/AddressItem.tsx

255 lines
6.8 KiB
TypeScript
Raw Normal View History

import React, { useContext, useRef } from 'react';
2021-04-19 06:24:04 -04:00
import { StyleSheet, Text, View } from 'react-native';
2023-10-23 21:28:44 -04:00
import { useNavigation } from '@react-navigation/native';
2021-02-04 04:05:37 -03:00
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';
2023-10-23 21:28:44 -04:00
import { useTheme } from '../themes';
import { BitcoinUnit } from '../../models/bitcoinUnits';
import { BlueStorageContext } from '../../blue_modules/storage-context';
import { AbstractWallet } from '../../class';
import Biometric from '../../class/biometrics';
const confirm = require('../../helpers/confirm');
interface AddressItemProps {
// todo: fix `any` after addresses.js is converted to the church of holy typescript
item: any;
balanceUnit: BitcoinUnit;
walletID: string;
allowSignVerifyMessage: boolean;
}
const AddressItem = ({ item, balanceUnit, walletID, allowSignVerifyMessage }: AddressItemProps) => {
const { wallets } = useContext(BlueStorageContext);
2021-02-04 04:05:37 -03:00
const { colors } = useTheme();
2021-07-06 06:37:15 -03:00
const hasTransactions = item.transactions > 0;
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-07-06 06:37:15 -03:00
address: {
color: hasTransactions ? colors.darkGray : colors.buttonTextColor,
},
2021-02-04 04:05:37 -03:00
});
2021-07-06 06:37:15 -03:00
const { navigate } = useNavigation();
const menuRef = useRef();
2021-07-06 06:37:15 -03:00
const navigateToReceive = () => {
// @ts-ignore wtf
menuRef.current?.dismissMenu();
// @ts-ignore wtf
2021-07-06 06:37:15 -03:00
navigate('ReceiveDetailsRoot', {
screen: 'ReceiveDetails',
params: {
walletID,
address: item.address,
},
});
};
const navigateToSignVerify = () => {
// @ts-ignore wtf
menuRef.current?.dismissMenu();
// @ts-ignore wtf
2021-07-06 06:37:15 -03:00
navigate('SignVerifyRoot', {
screen: 'SignVerify',
params: {
walletID,
address: item.address,
},
});
};
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));
};
const handleCopyPrivkeyPress = () => {
const wallet = wallets.find((w: AbstractWallet) => w.getID() === walletID);
if (!wallet) {
alert('Internal error: cant find wallet');
return;
}
try {
const wif = wallet._getWIFbyAddress(item.address);
if (!wif) {
alert('Internal error: cant get WIF from the wallet');
return;
}
Clipboard.setString(wif);
} catch (error: any) {
alert(error.message);
}
};
const onToolTipPress = async (id: string) => {
if (id === AddressItem.actionKeys.CopyToClipboard) {
2021-08-16 00:43:04 -04:00
handleCopyPress();
} else if (id === AddressItem.actionKeys.Share) {
2021-08-16 00:43:04 -04:00
handleSharePress();
} else if (id === AddressItem.actionKeys.SignVerify) {
2021-08-16 00:43:04 -04:00
navigateToSignVerify();
} else if (id === AddressItem.actionKeys.ExportPrivateKey) {
if (await confirm(loc.addresses.sensitive_private_key)) {
if (await Biometric.isBiometricUseCapableAndEnabled()) {
if (!(await Biometric.unlockWithBiometrics())) {
return;
}
}
handleCopyPrivkeyPress();
}
2021-08-16 00:43:04 -04:00
}
};
2021-07-06 06:37:15 -03:00
const getAvailableActions = () => {
const actions = [
{
id: AddressItem.actionKeys.CopyToClipboard,
2021-07-06 06:37:15 -03:00
text: loc.transactions.details_copy,
icon: AddressItem.actionIcons.Clipboard,
2021-07-06 06:37:15 -03:00
},
{
id: AddressItem.actionKeys.Share,
2021-07-06 06:37:15 -03:00
text: loc.receive.details_share,
icon: AddressItem.actionIcons.Share,
2021-07-06 06:37:15 -03:00
},
];
if (allowSignVerifyMessage) {
actions.push({
id: AddressItem.actionKeys.SignVerify,
2021-07-06 06:37:15 -03:00
text: loc.addresses.sign_title,
icon: AddressItem.actionIcons.Signature,
2021-07-06 06:37:15 -03:00
});
}
if (allowSignVerifyMessage) {
actions.push({
id: AddressItem.actionKeys.ExportPrivateKey,
text: loc.addresses.copy_private_key,
icon: AddressItem.actionIcons.ExportPrivateKey,
});
}
2021-07-06 06:37:15 -03:00
return actions;
};
2021-02-04 04:05:37 -03:00
const render = () => {
return (
<TooltipMenu
title={item.address}
ref={menuRef}
actions={getAvailableActions()}
onPressMenuItem={onToolTipPress}
previewQRCode
previewValue={item.address}
onPress={navigateToReceive}
>
2023-07-25 15:31:09 +01:00
<ListItem key={item.key} containerStyle={stylesHook.container}>
2021-04-19 06:24:04 -04:00
<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>
2021-07-06 06:37:15 -03:00
<View style={styles.subtitle}>
<Text style={[stylesHook.list, styles.balance, stylesHook.balance]}>{balance}</Text>
</View>
2021-04-19 06:24:04 -04:00
</ListItem.Content>
<View>
2021-07-06 06:37:15 -03:00
<AddressTypeBadge isInternal={item.isInternal} hasTransactions={hasTransactions} />
<Text style={[stylesHook.list, styles.balance, stylesHook.balance]}>
{loc.addresses.transactions}: {item.transactions}
</Text>
</View>
2021-04-19 06:24:04 -04:00
</ListItem>
2021-08-16 00:43:04 -04:00
</TooltipMenu>
2021-02-04 04:05:37 -03:00
);
};
return render();
};
AddressItem.actionKeys = {
Share: 'share',
CopyToClipboard: 'copyToClipboard',
SignVerify: 'signVerify',
ExportPrivateKey: 'exportPrivateKey',
};
AddressItem.actionIcons = {
Signature: {
iconType: 'SYSTEM',
iconValue: 'signature',
},
Share: {
iconType: 'SYSTEM',
iconValue: 'square.and.arrow.up',
},
Clipboard: {
iconType: 'SYSTEM',
2021-08-26 21:31:36 -04:00
iconValue: 'doc.on.doc',
},
ExportPrivateKey: {
iconType: 'SYSTEM',
iconValue: 'key',
},
};
2021-04-19 06:24:04 -04:00
const styles = StyleSheet.create({
address: {
2021-07-06 06:37:15 -03:00
fontWeight: 'bold',
2021-04-19 06:24:04 -04:00
marginHorizontal: 40,
},
index: {
fontSize: 15,
},
balance: {
marginTop: 8,
marginLeft: 14,
},
2021-07-06 06:37:15 -03:00
subtitle: {
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between',
width: '100%',
},
2021-04-19 06:24:04 -04:00
});
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 };