BlueWallet/screen/wallets/WalletExport.tsx

158 lines
5.5 KiB
TypeScript
Raw Normal View History

2024-07-23 22:06:05 -04:00
import { useFocusEffect, useNavigation, useRoute, RouteProp } from '@react-navigation/native';
2024-11-03 18:18:07 -04:00
import React, { useCallback, useEffect, useState } from 'react';
import { ActivityIndicator, InteractionManager, ScrollView, StyleSheet, View, LayoutChangeEvent } from 'react-native';
2024-05-20 10:54:13 +01:00
import { BlueCard, BlueSpacing20, BlueText } from '../../BlueComponents';
import { LegacyWallet, LightningCustodianWallet, SegwitBech32Wallet, SegwitP2SHWallet, WatchOnlyWallet } from '../../class';
import CopyTextToClipboard from '../../components/CopyTextToClipboard';
import HandOffComponent from '../../components/HandOffComponent';
2024-05-20 10:54:13 +01:00
import QRCodeComponent from '../../components/QRCodeComponent';
import { useTheme } from '../../components/themes';
2024-10-14 01:46:57 -04:00
import { disallowScreenshot } from 'react-native-screen-capture';
2024-05-20 10:54:13 +01:00
import loc from '../../loc';
2024-05-31 13:18:01 -04:00
import { useStorage } from '../../hooks/context/useStorage';
2024-06-04 15:02:12 -04:00
import { HandOffActivityType } from '../../components/types';
2024-07-23 22:06:05 -04:00
import { WalletExportStackParamList } from '../../navigation/WalletExportStack';
2024-11-03 18:18:07 -04:00
import useAppState from '../../hooks/useAppState';
2024-11-08 22:38:05 -04:00
import { useSettings } from '../../hooks/context/useSettings';
2018-01-30 22:42:38 +00:00
2024-07-23 22:06:05 -04:00
type RouteProps = RouteProp<WalletExportStackParamList, 'WalletExport'>;
const WalletExport: React.FC = () => {
const { wallets, saveToDisk } = useStorage();
2024-07-23 22:06:05 -04:00
const { walletID } = useRoute<RouteProps>().params;
2020-08-04 20:03:00 -04:00
const [isLoading, setIsLoading] = useState(true);
const { goBack } = useNavigation();
2024-11-08 22:38:05 -04:00
const { isPrivacyBlurEnabled } = useSettings();
2020-08-04 20:03:00 -04:00
const { colors } = useTheme();
const wallet = wallets.find(w => w.getID() === walletID);
2021-09-13 13:43:26 -04:00
const [qrCodeSize, setQRCodeSize] = useState(90);
2024-11-03 18:18:07 -04:00
const { currentAppState, previousAppState } = useAppState();
2024-11-05 16:07:28 -04:00
const stylesHook = StyleSheet.create({
root: {
backgroundColor: colors.elevated,
},
type: { color: colors.foregroundColor },
secret: { color: colors.foregroundColor },
warning: { color: colors.failedColor },
});
useEffect(() => {
2024-11-05 16:07:28 -04:00
if (!isLoading && previousAppState === 'active' && currentAppState !== 'active') {
const timer = setTimeout(() => goBack(), 500);
return () => clearTimeout(timer);
2024-11-03 18:18:07 -04:00
}
}, [currentAppState, previousAppState, goBack, isLoading]);
2018-01-30 22:42:38 +00:00
2020-08-04 20:03:00 -04:00
useFocusEffect(
useCallback(() => {
2024-11-08 22:38:05 -04:00
disallowScreenshot(isPrivacyBlurEnabled);
2020-08-04 20:03:00 -04:00
const task = InteractionManager.runAfterInteractions(async () => {
if (wallet) {
if (!wallet.getUserHasSavedExport()) {
wallet.setUserHasSavedExport(true);
saveToDisk();
}
2020-08-04 20:03:00 -04:00
setIsLoading(false);
}
});
return () => {
2024-10-14 01:46:57 -04:00
disallowScreenshot(false);
2024-11-08 22:38:05 -04:00
task.cancel();
2020-08-04 20:03:00 -04:00
};
2024-11-08 22:38:05 -04:00
}, [isPrivacyBlurEnabled, wallet, saveToDisk]),
2020-08-04 20:03:00 -04:00
);
2024-11-05 16:07:28 -04:00
const secrets: string[] = (() => {
try {
const secret = wallet?.getSecret();
if (!secret) return [];
return typeof secret === 'string' ? [secret] : Array.isArray(secret) ? secret : [];
} catch (error) {
console.error('Failed to get wallet secret:', error);
return [];
}
})();
2024-07-23 22:06:05 -04:00
const onLayout = (e: LayoutChangeEvent) => {
2021-09-13 13:43:26 -04:00
const { height, width } = e.nativeEvent.layout;
2024-07-23 22:06:05 -04:00
setQRCodeSize(height > width ? width - 40 : width / 1.8);
2021-09-13 13:43:26 -04:00
};
return (
2024-11-03 18:18:07 -04:00
<ScrollView
automaticallyAdjustContentInsets
contentInsetAdjustmentBehavior="automatic"
style={stylesHook.root}
contentContainerStyle={styles.scrollViewContent}
onLayout={onLayout}
testID="WalletExportScroll"
centerContent={isLoading}
>
{isLoading ? (
<ActivityIndicator />
) : (
wallet && (
<>
<View>
<BlueText style={[styles.type, stylesHook.type]}>{wallet.typeReadable}</BlueText>
</View>
2024-11-03 18:18:07 -04:00
{[LegacyWallet.type, SegwitBech32Wallet.type, SegwitP2SHWallet.type].includes(wallet.type) && (
<BlueCard>
<BlueText>{wallet.getAddress()}</BlueText>
</BlueCard>
)}
2024-11-03 18:18:07 -04:00
<BlueSpacing20 />
{secrets.map(secret => (
<React.Fragment key={secret}>
<QRCodeComponent isMenuAvailable={false} value={secret} size={qrCodeSize} logoSize={70} />
{wallet.type !== WatchOnlyWallet.type && (
<>
<BlueSpacing20 />
<BlueText style={stylesHook.warning}>{loc.wallets.warning_do_not_disclose}</BlueText>
</>
)}
<BlueSpacing20 />
{wallet.type === LightningCustodianWallet.type || wallet.type === WatchOnlyWallet.type ? (
<CopyTextToClipboard text={secret} />
) : (
<BlueText style={[styles.secret, styles.secretWritingDirection, stylesHook.secret]} testID="Secret">
{secret}
</BlueText>
)}
{wallet.type === WatchOnlyWallet.type && (
<HandOffComponent title={loc.wallets.xpub_title} type={HandOffActivityType.Xpub} userInfo={{ xpub: secret }} />
)}
</React.Fragment>
))}
</>
)
)}
</ScrollView>
2020-08-04 20:03:00 -04:00
);
2018-03-18 02:48:23 +00:00
};
2020-08-04 20:03:00 -04:00
const styles = StyleSheet.create({
scrollViewContent: {
alignItems: 'center',
justifyContent: 'center',
flexGrow: 1,
},
type: {
fontSize: 17,
fontWeight: '700',
},
secret: {
alignSelf: 'stretch',
textAlign: 'center',
paddingHorizontal: 16,
fontSize: 16,
lineHeight: 24,
},
secretWritingDirection: {
writingDirection: 'ltr',
},
});
2020-08-04 20:03:00 -04:00
export default WalletExport;