BlueWallet/components/addresses/AddressItem.tsx

260 lines
7.1 KiB
TypeScript
Raw Normal View History

2024-05-26 22:03:35 -04:00
import React, { useMemo, useRef } from 'react';
2024-05-20 10:54:13 +01:00
import Clipboard from '@react-native-clipboard/clipboard';
import { useNavigation } from '@react-navigation/native';
2021-04-19 06:24:04 -04:00
import { StyleSheet, Text, View } from 'react-native';
2024-06-12 12:46:44 -04:00
import { ListItem } from '@rneui/themed';
2021-04-19 06:24:04 -04:00
import Share from 'react-native-share';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
import confirm from '../../helpers/confirm';
2024-06-03 21:27:21 -04:00
import { unlockWithBiometrics, useBiometrics } from '../../hooks/useBiometrics';
2024-05-20 10:54:13 +01:00
import loc, { formatBalance } from '../../loc';
import { BitcoinUnit } from '../../models/bitcoinUnits';
import presentAlert from '../Alert';
import QRCodeComponent from '../QRCodeComponent';
import { useTheme } from '../themes';
import TooltipMenu from '../TooltipMenu';
2024-05-26 22:03:35 -04:00
import { Action, ToolTipMenuProps } from '../types';
2024-05-20 10:54:13 +01:00
import { AddressTypeBadge } from './AddressTypeBadge';
2024-05-26 22:03:35 -04:00
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import { DetailViewStackParamList } from '../../navigation/DetailViewStackParamList';
import { useStorage } from '../../hooks/context/useStorage';
interface AddressItemProps {
// todo: fix `any` after addresses.js is converted to the church of holy typescript
item: any;
balanceUnit: BitcoinUnit;
walletID: string;
allowSignVerifyMessage: boolean;
}
2024-05-26 22:03:35 -04:00
type NavigationProps = NativeStackNavigationProp<DetailViewStackParamList>;
const AddressItem = ({ item, balanceUnit, walletID, allowSignVerifyMessage }: AddressItemProps) => {
2024-05-18 12:48:03 -04:00
const { wallets } = useStorage();
2021-02-04 04:05:37 -03:00
const { colors } = useTheme();
2024-06-03 21:27:21 -04:00
const { isBiometricUseCapableAndEnabled } = useBiometrics();
2021-02-04 04:05:37 -03:00
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
});
2024-05-26 22:03:35 -04:00
const { navigate } = useNavigation<NavigationProps>();
const menuRef = useRef<ToolTipMenuProps>();
const dismissMenu = () => {
if (menuRef.current?.dismissMenu) {
menuRef.current.dismissMenu();
}
};
2021-07-06 06:37:15 -03:00
const navigateToReceive = () => {
2024-05-26 22:03:35 -04:00
dismissMenu();
2021-07-06 06:37:15 -03:00
navigate('ReceiveDetailsRoot', {
screen: 'ReceiveDetails',
params: {
walletID,
address: item.address,
},
});
};
const navigateToSignVerify = () => {
2024-05-26 22:03:35 -04:00
dismissMenu();
2021-07-06 06:37:15 -03:00
navigate('SignVerifyRoot', {
screen: 'SignVerify',
params: {
walletID,
address: item.address,
},
});
};
2024-05-18 12:48:03 -04:00
const menuActions = useMemo(() => getAvailableActions({ allowSignVerifyMessage }), [allowSignVerifyMessage]);
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 = () => {
2024-03-15 23:05:15 +03:00
const wallet = wallets.find(w => w.getID() === walletID);
if (!wallet) {
presentAlert({ message: 'Internal error: cant find wallet' });
return;
}
try {
const wif = wallet._getWIFbyAddress(item.address);
if (!wif) {
presentAlert({ message: 'Internal error: cant get WIF from the wallet' });
return;
}
triggerHapticFeedback(HapticFeedbackTypes.Selection);
Clipboard.setString(wif);
} catch (error: any) {
presentAlert({ message: error.message });
}
};
const onToolTipPress = async (id: string) => {
2024-05-26 22:03:35 -04:00
if (id === actionKeys.CopyToClipboard) {
2021-08-16 00:43:04 -04:00
handleCopyPress();
2024-05-26 22:03:35 -04:00
} else if (id === actionKeys.Share) {
2021-08-16 00:43:04 -04:00
handleSharePress();
2024-05-26 22:03:35 -04:00
} else if (id === actionKeys.SignVerify) {
2021-08-16 00:43:04 -04:00
navigateToSignVerify();
2024-05-26 22:03:35 -04:00
} else if (id === actionKeys.ExportPrivateKey) {
if (await confirm(loc.addresses.sensitive_private_key)) {
2024-05-17 18:34:39 -04:00
if (await isBiometricUseCapableAndEnabled()) {
if (!(await unlockWithBiometrics())) {
return;
}
}
handleCopyPrivkeyPress();
}
2021-08-16 00:43:04 -04:00
}
};
2024-03-03 09:56:22 -04:00
const renderPreview = () => {
return <QRCodeComponent value={item.address} isMenuAvailable={false} />;
};
2021-02-04 04:05:37 -03:00
const render = () => {
return (
<TooltipMenu
title={item.address}
ref={menuRef}
2024-05-18 12:48:03 -04:00
actions={menuActions}
onPressMenuItem={onToolTipPress}
2024-03-03 09:56:22 -04:00
renderPreview={renderPreview}
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();
};
2024-05-26 22:03:35 -04:00
const actionKeys = {
Share: 'share',
CopyToClipboard: 'copyToClipboard',
SignVerify: 'signVerify',
ExportPrivateKey: 'exportPrivateKey',
};
2024-05-26 22:03:35 -04:00
const 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
});
2024-05-18 12:48:03 -04:00
const getAvailableActions = ({ allowSignVerifyMessage }: { allowSignVerifyMessage: boolean }): Action[] | Action[][] => {
const actions = [
{
2024-05-26 22:03:35 -04:00
id: actionKeys.CopyToClipboard,
2024-05-18 12:48:03 -04:00
text: loc.transactions.details_copy,
2024-05-26 22:03:35 -04:00
icon: actionIcons.Clipboard,
2024-05-18 12:48:03 -04:00
},
{
2024-05-26 22:03:35 -04:00
id: actionKeys.Share,
2024-05-18 12:48:03 -04:00
text: loc.receive.details_share,
2024-05-26 22:03:35 -04:00
icon: actionIcons.Share,
2024-05-18 12:48:03 -04:00
},
];
if (allowSignVerifyMessage) {
actions.push({
2024-05-26 22:03:35 -04:00
id: actionKeys.SignVerify,
2024-05-18 12:48:03 -04:00
text: loc.addresses.sign_title,
2024-05-26 22:03:35 -04:00
icon: actionIcons.Signature,
2024-05-18 12:48:03 -04:00
});
}
if (allowSignVerifyMessage) {
actions.push({
2024-05-26 22:03:35 -04:00
id: actionKeys.ExportPrivateKey,
2024-05-18 12:48:03 -04:00
text: loc.addresses.copy_private_key,
2024-05-26 22:03:35 -04:00
icon: actionIcons.ExportPrivateKey,
2024-05-18 12:48:03 -04:00
});
}
return actions;
2021-02-04 04:05:37 -03:00
};
2024-05-18 12:48:03 -04:00
2021-02-04 04:05:37 -03:00
export { AddressItem };