2024-02-24 06:27:17 -05:00
|
|
|
import { NavigationProp, RouteProp, useFocusEffect, useNavigation, useRoute } from '@react-navigation/native';
|
2024-02-06 20:10:33 -04:00
|
|
|
import React, { useCallback, useContext, useEffect, useRef, useState } from 'react';
|
2024-02-24 06:27:17 -05:00
|
|
|
import { ActivityIndicator, InteractionManager, View } from 'react-native';
|
2023-12-27 13:52:11 +07:00
|
|
|
import Share from 'react-native-share';
|
2024-02-24 06:27:17 -05:00
|
|
|
import { BlueCopyTextToClipboard, BlueSpacing20, BlueText } from '../../BlueComponents';
|
2020-10-24 13:20:59 -04:00
|
|
|
import { BlueStorageContext } from '../../blue_modules/storage-context';
|
2024-02-24 06:27:17 -05:00
|
|
|
import { AbstractWallet } from '../../class';
|
|
|
|
import Biometric from '../../class/biometrics';
|
2023-11-15 04:40:22 -04:00
|
|
|
import Button from '../../components/Button';
|
2024-02-24 06:27:17 -05:00
|
|
|
import QRCodeComponent from '../../components/QRCodeComponent';
|
2023-12-27 13:52:11 +07:00
|
|
|
import SafeArea from '../../components/SafeArea';
|
2024-02-24 06:27:17 -05:00
|
|
|
import HandoffComponent from '../../components/handoff';
|
2024-02-20 13:40:16 -04:00
|
|
|
import usePrivacy from '../../hooks/usePrivacy';
|
2024-02-24 06:27:17 -05:00
|
|
|
import loc from '../../loc';
|
|
|
|
import { styles, useDynamicStyles } from './xpub.styles';
|
2018-12-27 16:37:12 +01:00
|
|
|
|
2024-02-06 20:10:33 -04:00
|
|
|
type WalletXpubRouteProp = RouteProp<{ params: { walletID: string; xpub: string } }, 'params'>;
|
|
|
|
export type RootStackParamList = {
|
|
|
|
WalletXpub: {
|
|
|
|
walletID: string;
|
|
|
|
xpub: string;
|
|
|
|
};
|
|
|
|
};
|
2020-05-24 12:17:26 +03:00
|
|
|
|
2024-02-06 20:10:33 -04:00
|
|
|
const WalletXpub: React.FC = () => {
|
2021-06-17 17:24:00 -04:00
|
|
|
const { wallets } = useContext(BlueStorageContext);
|
2024-02-06 20:10:33 -04:00
|
|
|
const route = useRoute<WalletXpubRouteProp>();
|
|
|
|
const { walletID, xpub } = route.params;
|
|
|
|
const wallet = wallets.find((w: AbstractWallet) => w.getID() === walletID);
|
|
|
|
const [isLoading, setIsLoading] = useState<boolean>(true);
|
|
|
|
const [xPubText, setXPubText] = useState<string | undefined>(undefined);
|
|
|
|
const navigation = useNavigation<NavigationProp<RootStackParamList, 'WalletXpub'>>();
|
|
|
|
const stylesHook = useDynamicStyles(); // This now includes the theme implicitly
|
|
|
|
const [qrCodeSize, setQRCodeSize] = useState<number>(90);
|
|
|
|
const lastWalletIdRef = useRef<string | undefined>();
|
2024-02-20 13:40:16 -04:00
|
|
|
const { enableBlur, disableBlur } = usePrivacy();
|
2018-12-27 16:37:12 +01:00
|
|
|
|
2020-08-04 19:50:32 -04:00
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
2024-02-06 20:10:33 -04:00
|
|
|
// Skip execution if walletID hasn't changed
|
|
|
|
if (lastWalletIdRef.current === walletID) {
|
|
|
|
return;
|
|
|
|
}
|
2024-02-20 13:40:16 -04:00
|
|
|
enableBlur();
|
2020-08-04 19:50:32 -04:00
|
|
|
const task = InteractionManager.runAfterInteractions(async () => {
|
|
|
|
if (wallet) {
|
|
|
|
const isBiometricsEnabled = await Biometric.isBiometricUseCapableAndEnabled();
|
2018-12-27 16:37:12 +01:00
|
|
|
|
2020-08-04 19:50:32 -04:00
|
|
|
if (isBiometricsEnabled) {
|
|
|
|
if (!(await Biometric.unlockWithBiometrics())) {
|
2024-02-06 20:10:33 -04:00
|
|
|
return navigation.goBack();
|
2020-08-04 19:50:32 -04:00
|
|
|
}
|
|
|
|
}
|
2024-02-06 20:10:33 -04:00
|
|
|
const walletXpub = wallet.getXpub();
|
|
|
|
if (xpub !== walletXpub) {
|
|
|
|
navigation.setParams({ xpub: walletXpub });
|
|
|
|
}
|
|
|
|
|
2020-08-04 19:50:32 -04:00
|
|
|
setIsLoading(false);
|
2021-09-25 12:05:43 -04:00
|
|
|
} else if (xpub) {
|
2021-09-24 23:36:08 -04:00
|
|
|
setIsLoading(false);
|
2020-08-04 19:50:32 -04:00
|
|
|
}
|
|
|
|
});
|
2024-02-06 20:10:33 -04:00
|
|
|
lastWalletIdRef.current = walletID;
|
2020-08-04 19:50:32 -04:00
|
|
|
return () => {
|
|
|
|
task.cancel();
|
2024-02-20 13:40:16 -04:00
|
|
|
disableBlur();
|
2020-08-04 19:50:32 -04:00
|
|
|
};
|
2024-02-20 13:40:16 -04:00
|
|
|
}, [walletID, enableBlur, wallet, xpub, navigation, disableBlur]),
|
2020-08-04 19:50:32 -04:00
|
|
|
);
|
2018-12-27 16:37:12 +01:00
|
|
|
|
2021-09-27 20:35:37 -04:00
|
|
|
useEffect(() => {
|
|
|
|
setXPubText(xpub);
|
|
|
|
}, [xpub]);
|
|
|
|
|
2024-02-06 20:10:33 -04:00
|
|
|
const onLayout = (e: { nativeEvent: { layout: { width: any; height?: any } } }) => {
|
2021-09-13 13:43:26 -04:00
|
|
|
const { height, width } = e.nativeEvent.layout;
|
|
|
|
setQRCodeSize(height > width ? width - 40 : e.nativeEvent.layout.width / 1.8);
|
|
|
|
};
|
|
|
|
|
2024-02-06 20:20:02 -04:00
|
|
|
const handleShareButtonPressed = useCallback(() => {
|
|
|
|
Share.open({ message: xpub }).catch(console.log);
|
|
|
|
}, [xpub]);
|
2022-09-18 12:41:51 -04:00
|
|
|
|
2024-02-06 20:20:02 -04:00
|
|
|
return (
|
2023-12-27 13:52:11 +07:00
|
|
|
<SafeArea style={[styles.root, stylesHook.root]} onLayout={onLayout}>
|
2024-02-06 20:20:02 -04:00
|
|
|
{isLoading ? (
|
|
|
|
<ActivityIndicator />
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<View style={styles.container}>
|
|
|
|
{wallet && (
|
|
|
|
<>
|
|
|
|
<View>
|
|
|
|
<BlueText>{wallet.typeReadable}</BlueText>
|
|
|
|
</View>
|
|
|
|
<BlueSpacing20 />
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<QRCodeComponent value={xpub} size={qrCodeSize} />
|
2018-12-27 16:37:12 +01:00
|
|
|
|
2024-02-06 20:20:02 -04:00
|
|
|
<BlueSpacing20 />
|
|
|
|
<BlueCopyTextToClipboard text={xPubText} />
|
|
|
|
</View>
|
|
|
|
<HandoffComponent title={loc.wallets.xpub_title} type={HandoffComponent.activityTypes.Xpub} userInfo={{ xpub: xPubText }} />
|
|
|
|
<View style={styles.share}>
|
|
|
|
<Button onPress={handleShareButtonPressed} title={loc.receive.details_share} />
|
|
|
|
</View>
|
|
|
|
</>
|
|
|
|
)}
|
2023-12-27 13:52:11 +07:00
|
|
|
</SafeArea>
|
2020-08-04 19:50:32 -04:00
|
|
|
);
|
2018-12-27 16:37:12 +01:00
|
|
|
};
|
2020-07-15 13:32:59 -04:00
|
|
|
|
2020-08-04 19:50:32 -04:00
|
|
|
export default WalletXpub;
|