BlueWallet/components/TransactionsNavigationHeader.tsx

375 lines
12 KiB
TypeScript
Raw Normal View History

2024-04-18 03:05:48 +02:00
import React, { useState, useEffect, useRef, useCallback, useMemo } from 'react';
2024-03-31 18:33:25 +02:00
import { Image, Text, TouchableOpacity, View, I18nManager, StyleSheet, LayoutAnimation } from 'react-native';
2023-04-02 02:16:00 +02:00
import Clipboard from '@react-native-clipboard/clipboard';
import LinearGradient from 'react-native-linear-gradient';
2024-03-15 21:05:15 +01:00
import { HDSegwitBech32Wallet, LightningCustodianWallet, LightningLdkWallet, MultisigHDWallet } from '../class';
2023-04-02 02:16:00 +02:00
import { BitcoinUnit } from '../models/bitcoinUnits';
import WalletGradient from '../class/wallet-gradient';
2024-03-31 18:33:25 +02:00
import loc, { formatBalance, formatBalanceWithoutSuffix } from '../loc';
2023-04-02 02:16:00 +02:00
import ToolTipMenu from './TooltipMenu';
import { FiatUnit } from '../models/fiatUnit';
2024-03-15 21:05:15 +01:00
import { TWallet } from '../class/wallets/types';
2024-03-31 18:33:25 +02:00
import { BlurredBalanceView } from './BlurredBalanceView';
2024-04-18 03:05:48 +02:00
import { useSettings } from './Context/SettingsContext';
2023-04-02 02:16:00 +02:00
interface TransactionsNavigationHeaderProps {
2024-03-15 21:05:15 +01:00
wallet: TWallet;
2023-04-02 02:16:00 +02:00
onWalletUnitChange?: (wallet: any) => void;
navigation: {
navigate: (route: string, params?: any) => void;
goBack: () => void;
};
2024-04-29 17:51:02 +02:00
onManageFundsPressed?: (id?: string) => void;
2024-03-31 18:58:23 +02:00
onWalletBalanceVisibilityChange?: (isShouldBeVisible: boolean) => void;
2023-04-02 02:16:00 +02:00
actionKeys: {
CopyToClipboard: 'copyToClipboard';
WalletBalanceVisibility: 'walletBalanceVisibility';
Refill: 'refill';
RefillWithExternalWallet: 'qrcode';
};
}
const TransactionsNavigationHeader: React.FC<TransactionsNavigationHeaderProps> = ({
// @ts-ignore: Ugh
wallet: initialWallet,
// @ts-ignore: Ugh
onWalletUnitChange,
// @ts-ignore: Ugh
navigation,
// @ts-ignore: Ugh
onManageFundsPressed,
2024-03-31 18:58:23 +02:00
// @ts-ignore: Ugh
onWalletBalanceVisibilityChange,
2023-04-02 02:16:00 +02:00
}) => {
const [wallet, setWallet] = useState(initialWallet);
const [allowOnchainAddress, setAllowOnchainAddress] = useState(false);
2024-04-18 03:05:48 +02:00
const { preferredFiatCurrency } = useSettings();
2023-04-02 02:16:00 +02:00
const menuRef = useRef(null);
const verifyIfWalletAllowsOnchainAddress = useCallback(() => {
if (wallet.type === LightningCustodianWallet.type) {
wallet
.allowOnchainAddress()
.then((value: boolean) => setAllowOnchainAddress(value))
2023-12-25 22:22:49 +01:00
.catch((e: Error) => {
2023-04-02 02:16:00 +02:00
console.log('This Lndhub wallet does not have an onchain address API.');
setAllowOnchainAddress(false);
});
}
}, [wallet]);
useEffect(() => {
setWallet(initialWallet);
}, [initialWallet]);
2023-04-02 02:16:00 +02:00
useEffect(() => {
verifyIfWalletAllowsOnchainAddress();
}, [wallet, verifyIfWalletAllowsOnchainAddress]);
const handleCopyPress = () => {
2024-01-28 16:11:08 +01:00
const value = formatBalance(wallet.getBalance(), wallet.getPreferredBalanceUnit());
if (value) {
Clipboard.setString(value);
}
2023-04-02 02:16:00 +02:00
};
2024-03-31 18:58:23 +02:00
const handleBalanceVisibility = () => {
onWalletBalanceVisibilityChange?.(!wallet.hideBalance);
2023-04-02 02:16:00 +02:00
};
2024-03-15 21:05:15 +01:00
const updateWalletWithNewUnit = (w: TWallet, newPreferredUnit: BitcoinUnit) => {
w.preferredBalanceUnit = newPreferredUnit;
return w;
2023-04-02 02:16:00 +02:00
};
const changeWalletBalanceUnit = () => {
// @ts-ignore: Ugh
menuRef.current?.dismissMenu();
let newWalletPreferredUnit = wallet.getPreferredBalanceUnit();
if (newWalletPreferredUnit === BitcoinUnit.BTC) {
newWalletPreferredUnit = BitcoinUnit.SATS;
} else if (newWalletPreferredUnit === BitcoinUnit.SATS) {
newWalletPreferredUnit = BitcoinUnit.LOCAL_CURRENCY;
} else {
newWalletPreferredUnit = BitcoinUnit.BTC;
}
2024-03-31 18:33:25 +02:00
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
2023-04-02 02:16:00 +02:00
const updatedWallet = updateWalletWithNewUnit(wallet, newWalletPreferredUnit);
setWallet(updatedWallet);
onWalletUnitChange?.(updatedWallet);
};
2024-04-29 17:51:02 +02:00
const handleManageFundsPressed = (actionKeyID?: string) => {
2024-04-29 17:44:20 +02:00
if (onManageFundsPressed) {
2024-04-29 17:51:02 +02:00
if (actionKeyID) {
onManageFundsPressed(actionKeyID);
2024-04-30 00:54:19 +02:00
}
}
2023-04-02 02:16:00 +02:00
};
2023-04-21 16:30:39 +02:00
const handleOnPaymentCodeButtonPressed = () => {
navigation.navigate('PaymentCodeRoot', {
screen: 'PaymentCode',
params: { paymentCode: (wallet as HDSegwitBech32Wallet).getBIP47PaymentCode() },
2023-04-21 16:30:39 +02:00
});
};
2023-04-02 02:16:00 +02:00
const onPressMenuItem = (id: string) => {
if (id === 'walletBalanceVisibility') {
handleBalanceVisibility();
} else if (id === 'copyToClipboard') {
handleCopyPress();
}
};
const balance = useMemo(() => {
2023-04-03 20:54:15 +02:00
const hideBalance = wallet.hideBalance;
const balanceUnit = wallet.getPreferredBalanceUnit();
2024-03-31 18:33:25 +02:00
const balanceFormatted =
balanceUnit === BitcoinUnit.LOCAL_CURRENCY
? formatBalance(wallet.getBalance(), balanceUnit, true)
: formatBalanceWithoutSuffix(wallet.getBalance(), balanceUnit, true);
return !hideBalance && balanceFormatted;
2023-04-03 20:54:15 +02:00
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [wallet.hideBalance, wallet.getPreferredBalanceUnit()]);
2023-04-02 02:16:00 +02:00
return (
<LinearGradient
colors={WalletGradient.gradientsFor(wallet.type)}
style={styles.lineaderGradient}
// @ts-ignore: Ugh
{...WalletGradient.linearGradientProps(wallet.type)}
>
<Image
source={(() => {
switch (wallet.type) {
case LightningLdkWallet.type:
case LightningCustodianWallet.type:
return I18nManager.isRTL ? require('../img/lnd-shape-rtl.png') : require('../img/lnd-shape.png');
case MultisigHDWallet.type:
return I18nManager.isRTL ? require('../img/vault-shape-rtl.png') : require('../img/vault-shape.png');
default:
return I18nManager.isRTL ? require('../img/btc-shape-rtl.png') : require('../img/btc-shape.png');
}
})()}
style={styles.chainIcon}
/>
<Text testID="WalletLabel" numberOfLines={1} style={styles.walletLabel} selectable>
2023-04-02 02:16:00 +02:00
{wallet.getLabel()}
</Text>
2024-03-31 18:33:25 +02:00
<View style={styles.walletBalanceAndUnitContainer}>
<ToolTipMenu
isMenuPrimaryAction
isButton
enableAndroidRipple={false}
ref={menuRef}
buttonStyle={styles.walletBalance}
onPressMenuItem={onPressMenuItem}
actions={
wallet.hideBalance
? [
{
id: 'walletBalanceVisibility',
text: loc.transactions.details_balance_show,
icon: {
iconType: 'SYSTEM',
iconValue: 'eye',
},
2023-04-02 02:16:00 +02:00
},
2024-03-31 18:33:25 +02:00
]
: [
{
id: 'walletBalanceVisibility',
text: loc.transactions.details_balance_hide,
icon: {
iconType: 'SYSTEM',
iconValue: 'eye.slash',
},
2023-04-02 02:16:00 +02:00
},
2024-03-31 18:33:25 +02:00
{
id: 'copyToClipboard',
text: loc.transactions.details_copy,
icon: {
iconType: 'SYSTEM',
iconValue: 'doc.on.doc',
},
2023-04-02 02:16:00 +02:00
},
2024-03-31 18:33:25 +02:00
]
}
>
<View style={styles.walletBalance}>
{wallet.hideBalance ? (
<BlurredBalanceView />
) : (
2024-04-04 16:06:15 +02:00
<View>
2024-03-31 18:33:25 +02:00
<Text
testID="WalletBalance"
// @ts-ignore: Ugh
key={balance} // force component recreation on balance change. To fix right-to-left languages, like Farsi
numberOfLines={1}
2024-04-18 19:53:17 +02:00
minimumFontScale={0.5}
2024-03-31 18:33:25 +02:00
adjustsFontSizeToFit
style={styles.walletBalanceText}
>
{balance}
</Text>
2024-04-04 16:06:15 +02:00
</View>
2024-03-31 18:33:25 +02:00
)}
</View>
</ToolTipMenu>
<TouchableOpacity style={styles.walletPreferredUnitView} onPress={changeWalletBalanceUnit}>
<Text style={styles.walletPreferredUnitText}>
{wallet.getPreferredBalanceUnit() === BitcoinUnit.LOCAL_CURRENCY
? preferredFiatCurrency?.endPointKey ?? FiatUnit.USD
: wallet.getPreferredBalanceUnit()}
</Text>
</TouchableOpacity>
</View>
2023-04-02 02:16:00 +02:00
{wallet.type === LightningCustodianWallet.type && allowOnchainAddress && (
<ToolTipMenu
isMenuPrimaryAction
isButton
onPressMenuItem={handleManageFundsPressed}
actions={[
{
id: actionKeys.Refill,
text: loc.lnd.refill,
icon: actionIcons.Refill,
},
{
id: actionKeys.RefillWithExternalWallet,
text: loc.lnd.refill_external,
icon: actionIcons.RefillWithExternalWallet,
},
]}
buttonStyle={styles.manageFundsButton}
>
<Text style={styles.manageFundsButtonText}>{loc.lnd.title}</Text>
</ToolTipMenu>
)}
2023-04-21 16:30:39 +02:00
{wallet.allowBIP47() && wallet.isBIP47Enabled() && (
<TouchableOpacity style={styles.manageFundsButton} accessibilityRole="button" onPress={handleOnPaymentCodeButtonPressed}>
<Text style={styles.manageFundsButtonText}>{loc.bip47.payment_code}</Text>
2023-04-21 16:30:39 +02:00
</TouchableOpacity>
)}
{wallet.type === LightningLdkWallet.type && (
<TouchableOpacity
style={styles.manageFundsButton}
accessibilityRole="button"
accessibilityLabel={loc.lnd.title}
2024-04-29 17:51:02 +02:00
onPress={() => handleManageFundsPressed()}
>
<Text style={styles.manageFundsButtonText}>{loc.lnd.title}</Text>
2023-04-21 16:30:39 +02:00
</TouchableOpacity>
)}
2023-04-02 02:16:00 +02:00
{wallet.type === MultisigHDWallet.type && (
2024-04-29 17:51:02 +02:00
<TouchableOpacity style={styles.manageFundsButton} accessibilityRole="button" onPress={() => handleManageFundsPressed()}>
<Text style={styles.manageFundsButtonText}>{loc.multisig.manage_keys}</Text>
2023-04-02 02:16:00 +02:00
</TouchableOpacity>
)}
</LinearGradient>
);
};
const styles = StyleSheet.create({
lineaderGradient: {
padding: 15,
minHeight: 140,
justifyContent: 'center',
},
chainIcon: {
width: 99,
height: 94,
position: 'absolute',
bottom: 0,
right: 0,
},
walletLabel: {
backgroundColor: 'transparent',
fontSize: 19,
color: '#fff',
writingDirection: I18nManager.isRTL ? 'rtl' : 'ltr',
2024-03-31 18:33:25 +02:00
marginBottom: 10,
2023-04-02 02:16:00 +02:00
},
walletBalance: {
2024-03-31 18:33:25 +02:00
flexShrink: 1,
marginRight: 6,
2023-04-02 02:16:00 +02:00
},
manageFundsButton: {
marginTop: 14,
marginBottom: 10,
backgroundColor: 'rgba(255,255,255,0.2)',
borderRadius: 9,
minHeight: 39,
alignSelf: 'flex-start',
justifyContent: 'center',
alignItems: 'center',
},
manageFundsButtonText: {
fontWeight: '500',
fontSize: 14,
color: '#FFFFFF',
padding: 12,
},
2024-03-31 18:33:25 +02:00
walletBalanceAndUnitContainer: {
flexDirection: 'row',
alignItems: 'center',
paddingRight: 10, // Ensure there's some padding to the right
},
walletBalanceText: {
color: '#fff',
fontWeight: 'bold',
fontSize: 36,
flexShrink: 1, // Allow the text to shrink if there's not enough space
},
walletPreferredUnitView: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(255, 255, 255, 0.25)',
borderRadius: 8,
minHeight: 35,
minWidth: 65,
},
walletPreferredUnitText: {
color: '#fff',
fontWeight: '600',
},
2023-04-02 02:16:00 +02:00
});
export const actionKeys = {
CopyToClipboard: 'copyToClipboard',
WalletBalanceVisibility: 'walletBalanceVisibility',
Refill: 'refill',
2024-04-29 17:44:20 +02:00
RefillWithExternalWallet: 'refillWithExternalWallet',
2023-04-02 02:16:00 +02:00
};
export const actionIcons = {
Eye: {
iconType: 'SYSTEM',
iconValue: 'eye',
},
EyeSlash: {
iconType: 'SYSTEM',
iconValue: 'eye.slash',
},
Clipboard: {
iconType: 'SYSTEM',
iconValue: 'doc.on.doc',
},
Refill: {
iconType: 'SYSTEM',
iconValue: 'goforward.plus',
},
RefillWithExternalWallet: {
iconType: 'SYSTEM',
iconValue: 'qrcode',
},
};
export default TransactionsNavigationHeader;