BlueWallet/screen/wallets/exportMultisigCoordinationSetup.js

132 lines
4 KiB
JavaScript
Raw Normal View History

import React, { useCallback, useContext, useRef, useState } from 'react';
2023-10-20 10:25:40 -04:00
import { ActivityIndicator, InteractionManager, ScrollView, StyleSheet, View } from 'react-native';
2024-03-24 10:52:10 -04:00
import { useFocusEffect, useRoute } from '@react-navigation/native';
import { BlueSpacing20, BlueText } from '../../BlueComponents';
2020-12-25 19:09:53 +03:00
import navigationStyle from '../../components/navigationStyle';
import { DynamicQRCode } from '../../components/DynamicQRCode';
2020-10-05 22:25:14 +01:00
import loc from '../../loc';
import { SquareButton } from '../../components/SquareButton';
import { BlueStorageContext } from '../../blue_modules/storage-context';
2023-10-23 21:28:44 -04:00
import { useTheme } from '../../components/themes';
import SafeArea from '../../components/SafeArea';
2024-02-20 13:40:16 -04:00
import usePrivacy from '../../hooks/usePrivacy';
2020-10-05 22:25:14 +01:00
const fs = require('../../blue_modules/fs');
const ExportMultisigCoordinationSetup = () => {
const walletId = useRoute().params.walletId;
const { wallets } = useContext(BlueStorageContext);
const wallet = wallets.find(w => w.getID() === walletId);
const qrCodeContents = useRef();
2021-09-11 13:46:01 -04:00
const dynamicQRCode = useRef();
2020-10-05 22:25:14 +01:00
const [isLoading, setIsLoading] = useState(true);
const [isShareButtonTapped, setIsShareButtonTapped] = useState(false);
2020-10-05 22:25:14 +01:00
const { colors } = useTheme();
2024-02-20 13:40:16 -04:00
const { enableBlur, disableBlur } = usePrivacy();
const stylesHook = StyleSheet.create({
2020-10-05 22:25:14 +01:00
loading: {
backgroundColor: colors.elevated,
},
root: {
backgroundColor: colors.elevated,
},
type: { color: colors.foregroundColor },
secret: { color: colors.foregroundColor },
exportButton: {
backgroundColor: colors.buttonDisabledBackgroundColor,
},
});
2020-10-05 22:25:14 +01:00
const exportTxtFile = async () => {
setIsShareButtonTapped(true);
2021-09-11 13:46:01 -04:00
dynamicQRCode.current?.stopAutoMove();
setTimeout(() => {
fs.writeFileAndExport(wallet.getLabel() + '.txt', wallet.getXpub()).finally(() => {
setIsShareButtonTapped(false);
2021-09-11 13:46:01 -04:00
dynamicQRCode.current?.startAutoMove();
});
}, 10);
2020-10-05 22:25:14 +01:00
};
useFocusEffect(
useCallback(() => {
2024-02-20 13:40:16 -04:00
enableBlur();
2020-10-05 22:25:14 +01:00
const task = InteractionManager.runAfterInteractions(async () => {
if (wallet) {
qrCodeContents.current = Buffer.from(wallet.getXpub(), 'ascii').toString('hex');
2020-10-05 22:25:14 +01:00
setIsLoading(false);
}
});
return () => {
task.cancel();
2024-02-20 13:40:16 -04:00
disableBlur();
2020-10-05 22:25:14 +01:00
};
2024-03-24 10:52:10 -04:00
}, [disableBlur, enableBlur, wallet]),
2020-10-05 22:25:14 +01:00
);
return isLoading ? (
<View style={[styles.loading, stylesHook.loading]}>
2020-10-05 22:25:14 +01:00
<ActivityIndicator />
</View>
) : (
<SafeArea style={stylesHook.root}>
2020-10-05 22:25:14 +01:00
<ScrollView contentContainerStyle={styles.scrollViewContent}>
<View>
<BlueText style={[styles.type, stylesHook.type]}>{wallet.getLabel()}</BlueText>
2020-10-05 22:25:14 +01:00
</View>
<BlueSpacing20 />
2021-09-11 13:46:01 -04:00
<DynamicQRCode value={qrCodeContents.current} ref={dynamicQRCode} />
<BlueSpacing20 />
{isShareButtonTapped ? (
<ActivityIndicator />
) : (
<SquareButton style={[styles.exportButton, stylesHook.exportButton]} onPress={exportTxtFile} title={loc.multisig.share} />
)}
2020-10-05 22:25:14 +01:00
<BlueSpacing20 />
<BlueText style={[styles.secret, stylesHook.secret]}>{wallet.getXpub()}</BlueText>
2020-10-05 22:25:14 +01:00
</ScrollView>
</SafeArea>
2020-10-05 22:25:14 +01:00
);
};
const styles = StyleSheet.create({
loading: {
flex: 1,
justifyContent: 'center',
2020-10-05 22:25:14 +01:00
},
scrollViewContent: {
alignItems: 'center',
justifyContent: 'center',
flexGrow: 1,
},
type: {
fontSize: 17,
fontWeight: '700',
},
secret: {
alignItems: 'center',
paddingHorizontal: 16,
fontSize: 16,
lineHeight: 24,
},
exportButton: {
height: 48,
borderRadius: 8,
flex: 1,
justifyContent: 'center',
paddingHorizontal: 16,
width: '80%',
maxWidth: 300,
},
2020-10-05 22:25:14 +01:00
});
2021-02-15 11:03:54 +03:00
ExportMultisigCoordinationSetup.navigationOptions = navigationStyle(
{
closeButton: true,
2023-11-11 07:33:50 -04:00
headerBackVisible: false,
2023-10-20 10:25:40 -04:00
statusBarStyle: 'light',
2021-02-15 11:03:54 +03:00
},
opts => ({ ...opts, title: loc.multisig.export_coordination_setup }),
);
2020-10-05 22:25:14 +01:00
export default ExportMultisigCoordinationSetup;