BlueWallet/screen/wallets/selectWallet.js

213 lines
7 KiB
JavaScript
Raw Normal View History

import React, { useContext, useEffect, useState } from 'react';
2023-10-20 13:47:37 -04:00
import { View, ActivityIndicator, Image, Text, TouchableOpacity, I18nManager, FlatList, StyleSheet } from 'react-native';
2018-12-23 19:44:31 -05:00
import LinearGradient from 'react-native-linear-gradient';
2020-12-04 13:39:47 +00:00
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
2023-10-23 21:28:44 -04:00
import { useRoute, useNavigation, useNavigationState } from '@react-navigation/native';
2020-12-25 19:09:53 +03:00
import { SafeBlueArea, BlueText, BlueSpacing20, BluePrivateBalance } from '../../BlueComponents';
import navigationStyle from '../../components/navigationStyle';
import WalletGradient from '../../class/wallet-gradient';
2020-07-20 16:38:46 +03:00
import loc, { formatBalance, transactionTimeToReadable } from '../../loc';
2021-09-09 12:00:11 +01:00
import { LightningLdkWallet, MultisigHDWallet, LightningCustodianWallet } from '../../class';
import { BlueStorageContext } from '../../blue_modules/storage-context';
2023-10-23 21:28:44 -04:00
import { useTheme } from '../../components/themes';
2018-12-23 19:44:31 -05:00
2021-01-26 19:04:11 -05:00
const SelectWallet = () => {
2021-04-15 20:46:10 +03:00
const { chainType, onWalletSelect, availableWallets, noWalletExplanationText } = useRoute().params;
2020-01-01 23:32:35 -06:00
const [isLoading, setIsLoading] = useState(true);
2023-11-11 07:33:50 -04:00
const { pop, navigate, setOptions, getParent } = useNavigation();
const { wallets } = useContext(BlueStorageContext);
2022-02-21 15:23:16 -05:00
const { colors, closeImage } = useTheme();
const isModal = useNavigationState(state => state.routes.length) === 1;
let data = chainType
? wallets.filter(item => item.chain === chainType && item.allowSend())
: wallets.filter(item => item.allowSend()) || [];
if (availableWallets && availableWallets.length > 0) {
// availableWallets if provided, overrides chainType argument and `allowSend()` check
data = availableWallets;
}
2020-07-15 13:32:59 -04:00
const styles = StyleSheet.create({
loading: {
flex: 1,
justifyContent: 'center',
alignContent: 'center',
paddingTop: 20,
backgroundColor: colors.background,
2020-07-15 13:32:59 -04:00
},
itemRoot: {
backgroundColor: 'transparent',
padding: 10,
marginVertical: 17,
},
gradient: {
padding: 15,
borderRadius: 10,
minHeight: 164,
elevation: 5,
},
image: {
width: 99,
height: 94,
position: 'absolute',
bottom: 0,
2021-03-27 06:47:01 -04:00
right: 0,
2020-07-15 13:32:59 -04:00
},
transparentText: {
backgroundColor: 'transparent',
},
label: {
backgroundColor: 'transparent',
fontSize: 19,
color: '#fff',
2021-03-27 06:34:26 -04:00
writingDirection: I18nManager.isRTL ? 'rtl' : 'ltr',
2020-07-15 13:32:59 -04:00
},
2021-03-27 06:34:26 -04:00
2020-07-15 13:32:59 -04:00
balance: {
backgroundColor: 'transparent',
fontWeight: 'bold',
fontSize: 36,
2021-03-27 06:34:26 -04:00
writingDirection: I18nManager.isRTL ? 'rtl' : 'ltr',
2020-07-15 13:32:59 -04:00
color: '#fff',
},
latestTxLabel: {
backgroundColor: 'transparent',
fontSize: 13,
color: '#fff',
2021-03-27 06:34:26 -04:00
writingDirection: I18nManager.isRTL ? 'rtl' : 'ltr',
2020-07-15 13:32:59 -04:00
},
latestTxValue: {
backgroundColor: 'transparent',
fontWeight: 'bold',
fontSize: 16,
2021-03-27 06:34:26 -04:00
writingDirection: I18nManager.isRTL ? 'rtl' : 'ltr',
2020-07-15 13:32:59 -04:00
color: '#fff',
},
noWallets: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingTop: 20,
},
center: {
textAlign: 'center',
},
});
2020-01-01 23:32:35 -06:00
useEffect(() => {
console.log('SelectWallet - useEffect');
2020-01-01 23:32:35 -06:00
setIsLoading(false);
2020-03-18 11:56:59 +00:00
}, []);
2018-12-23 19:44:31 -05:00
2023-10-20 13:47:37 -04:00
useEffect(() => {
if (isLoading || data.length === 0) {
setOptions({ statusBarStyle: 'light' });
} else {
setOptions({ statusBarStyle: 'auto' });
}
}, [isLoading, data.length, setOptions]);
2022-02-21 15:23:16 -05:00
useEffect(() => {
setOptions(
isModal
? {
// eslint-disable-next-line react/no-unstable-nested-components
2022-02-21 15:23:16 -05:00
headerLeft: () => (
<TouchableOpacity
accessibilityRole="button"
style={styles.button}
onPress={() => {
2023-11-11 07:33:50 -04:00
getParent().pop();
2022-02-21 15:23:16 -05:00
}}
testID="NavigationCloseButton"
>
<Image source={closeImage} />
</TouchableOpacity>
),
}
: {},
);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [closeImage, isModal, styles.button]);
2020-01-01 23:32:35 -06:00
const renderItem = ({ item }) => {
2018-12-23 19:44:31 -05:00
return (
<TouchableOpacity
onPress={() => {
2019-05-03 13:36:11 +01:00
ReactNativeHapticFeedback.trigger('selection', { ignoreAndroidSystemSettings: false });
onWalletSelect(item, { navigation: { pop, navigate } });
2018-12-23 19:44:31 -05:00
}}
accessibilityRole="button"
2018-12-23 19:44:31 -05:00
>
<View shadowOpacity={40 / 100} shadowOffset={{ width: 0, height: 0 }} shadowRadius={5} style={styles.itemRoot}>
<LinearGradient shadowColor="#000000" colors={WalletGradient.gradientsFor(item.type)} style={styles.gradient}>
2018-12-23 19:44:31 -05:00
<Image
2020-10-05 22:25:14 +01:00
source={(() => {
switch (item.type) {
2021-09-09 12:00:11 +01:00
case LightningLdkWallet.type:
2020-10-05 22:25:14 +01:00
case LightningCustodianWallet.type:
2021-03-27 06:34:26 -04:00
return I18nManager.isRTL ? require('../../img/lnd-shape-rtl.png') : require('../../img/lnd-shape.png');
2020-10-05 22:25:14 +01:00
case MultisigHDWallet.type:
2021-03-27 06:34:26 -04:00
return I18nManager.isRTL ? require('../../img/vault-shape-rtl.png') : require('../../img/vault-shape.png');
2020-10-05 22:25:14 +01:00
default:
2021-03-27 06:34:26 -04:00
return I18nManager.isRTL ? require('../../img/btc-shape-rtl.png') : require('../../img/btc-shape.png');
2020-10-05 22:25:14 +01:00
}
})()}
style={styles.image}
2018-12-23 19:44:31 -05:00
/>
<Text style={styles.transparentText} />
<Text numberOfLines={1} style={styles.label}>
2018-12-23 19:44:31 -05:00
{item.getLabel()}
</Text>
2019-08-03 17:29:15 -04:00
{item.hideBalance ? (
<BluePrivateBalance />
) : (
<Text numberOfLines={1} adjustsFontSizeToFit style={styles.balance}>
2020-07-20 16:38:46 +03:00
{formatBalance(Number(item.getBalance()), item.getPreferredBalanceUnit(), true)}
2019-08-03 17:29:15 -04:00
</Text>
)}
<Text style={styles.transparentText} />
<Text numberOfLines={1} style={styles.latestTxLabel}>
2020-07-20 16:38:46 +03:00
{loc.wallets.list_latest_transaction}
2018-12-23 19:44:31 -05:00
</Text>
<Text numberOfLines={1} style={styles.latestTxValue}>
2020-07-20 16:38:46 +03:00
{transactionTimeToReadable(item.getLatestTransactionTime())}
2018-12-23 19:44:31 -05:00
</Text>
</LinearGradient>
</View>
</TouchableOpacity>
);
};
2020-01-01 23:32:35 -06:00
if (isLoading) {
return (
<View style={styles.loading}>
2020-01-01 23:32:35 -06:00
<ActivityIndicator />
</View>
);
} else if (data.length <= 0) {
return (
<SafeBlueArea>
<View style={styles.noWallets}>
2020-07-20 16:38:46 +03:00
<BlueText style={styles.center}>{loc.wallets.select_no_bitcoin}</BlueText>
2020-01-01 23:32:35 -06:00
<BlueSpacing20 />
2021-04-15 20:46:10 +03:00
<BlueText style={styles.center}>{noWalletExplanationText || loc.wallets.select_no_bitcoin_exp}</BlueText>
2018-12-23 19:44:31 -05:00
</View>
2020-01-01 23:32:35 -06:00
</SafeBlueArea>
);
} else {
2018-12-23 19:44:31 -05:00
return (
<SafeBlueArea>
2020-01-01 23:32:35 -06:00
<FlatList extraData={data} data={data} renderItem={renderItem} keyExtractor={(_item, index) => `${index}`} />
2018-12-23 19:44:31 -05:00
</SafeBlueArea>
);
}
};
2020-01-01 23:32:35 -06:00
2021-09-14 01:36:00 -04:00
SelectWallet.navigationOptions = navigationStyle({}, opts => ({ ...opts, headerTitle: loc.wallets.select_wallet }));
2020-01-01 23:32:35 -06:00
export default SelectWallet;