BlueWallet/screen/wallets/xpub.tsx

109 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-02-24 06:27:17 -05:00
import { NavigationProp, RouteProp, useFocusEffect, useNavigation, useRoute } from '@react-navigation/native';
import React, { useCallback, useEffect, useRef, useState } from 'react';
2024-02-24 06:27:17 -05:00
import { ActivityIndicator, InteractionManager, View } from 'react-native';
import Share from 'react-native-share';
2024-05-20 10:54:13 +01:00
import { BlueSpacing20, BlueText } from '../../BlueComponents';
2023-11-15 04:40:22 -04:00
import Button from '../../components/Button';
2024-05-20 10:54:13 +01:00
import CopyTextToClipboard from '../../components/CopyTextToClipboard';
import HandOffComponent from '../../components/HandOffComponent';
2024-02-24 06:27:17 -05:00
import QRCodeComponent from '../../components/QRCodeComponent';
import SafeArea from '../../components/SafeArea';
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';
import { useStorage } from '../../hooks/context/useStorage';
2024-06-04 15:02:12 -04:00
import { HandOffActivityType } from '../../components/types';
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;
};
};
2024-02-06 20:10:33 -04:00
const WalletXpub: React.FC = () => {
const { wallets } = useStorage();
2024-02-06 20:10:33 -04:00
const route = useRoute<WalletXpubRouteProp>();
const { walletID, xpub } = route.params;
2024-03-15 23:05:15 +03:00
const wallet = wallets.find(w => w.getID() === walletID);
2024-02-06 20:10:33 -04:00
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();
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) {
2024-02-06 20:10:33 -04:00
const walletXpub = wallet.getXpub();
if (xpub !== walletXpub) {
2024-03-15 23:05:15 +03:00
navigation.setParams({ xpub: walletXpub || undefined });
2024-02-06 20:10:33 -04:00
}
2020-08-04 19:50:32 -04:00
setIsLoading(false);
2021-09-25 12:05:43 -04:00
} else if (xpub) {
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
);
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 (
<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} />
2024-02-06 20:20:02 -04:00
<BlueSpacing20 />
{xPubText && <CopyTextToClipboard text={xPubText} />}
2024-02-06 20:20:02 -04:00
</View>
2024-06-04 15:02:12 -04:00
<HandOffComponent title={loc.wallets.xpub_title} type={HandOffActivityType.Xpub} userInfo={{ xpub: xPubText }} />
2024-02-06 20:20:02 -04:00
<View style={styles.share}>
<Button onPress={handleShareButtonPressed} title={loc.receive.details_share} />
</View>
</>
)}
</SafeArea>
2020-08-04 19:50:32 -04:00
);
};
2020-07-15 13:32:59 -04:00
2020-08-04 19:50:32 -04:00
export default WalletXpub;