BlueWallet/screen/wallets/import.js

172 lines
5.3 KiB
JavaScript
Raw Normal View History

/* global alert */
2019-12-27 03:21:07 +01:00
import React, { useEffect, useState } from 'react';
import { Platform, View, Keyboard, StatusBar, StyleSheet } from 'react-native';
2020-12-25 17:09:53 +01:00
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import { useNavigation, useRoute, useTheme } from '@react-navigation/native';
import {
BlueFormMultiInput,
BlueButtonLink,
BlueFormLabel,
2019-08-29 06:18:32 +02:00
BlueDoneAndDismissKeyboardInputAccessory,
BlueButton,
SafeBlueArea,
BlueSpacing20,
} from '../../BlueComponents';
2020-12-25 17:09:53 +01:00
import navigationStyle from '../../components/navigationStyle';
2021-01-19 04:44:55 +01:00
import Privacy from '../../blue_modules/Privacy';
import WalletImport from '../../class/wallet-import';
2020-07-20 15:38:46 +02:00
import loc from '../../loc';
2021-05-29 07:57:40 +02:00
import { isDesktop, isMacCatalina } from '../../blue_modules/environment';
2020-12-15 05:11:05 +01:00
const fs = require('../../blue_modules/fs');
2019-12-27 03:21:07 +01:00
const WalletsImport = () => {
const [isToolbarVisibleForAndroid, setIsToolbarVisibleForAndroid] = useState(false);
2020-05-28 11:53:14 +02:00
const route = useRoute();
const label = (route.params && route.params.label) || '';
const triggerImport = (route.params && route.params.triggerImport) || false;
2020-05-28 11:53:14 +02:00
const [importText, setImportText] = useState(label);
2020-05-27 13:12:17 +02:00
const navigation = useNavigation();
2020-07-15 19:32:59 +02:00
const { colors } = useTheme();
const styles = StyleSheet.create({
root: {
paddingTop: 40,
backgroundColor: colors.elevated,
},
center: {
flex: 1,
marginHorizontal: 16,
2020-07-15 19:32:59 +02:00
backgroundColor: colors.elevated,
},
});
2019-12-27 03:21:07 +01:00
useEffect(() => {
Privacy.enableBlur();
2020-04-27 09:11:33 +02:00
Keyboard.addListener(Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow', () => setIsToolbarVisibleForAndroid(true));
Keyboard.addListener(Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide', () => setIsToolbarVisibleForAndroid(false));
return () => {
Keyboard.removeListener(Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide');
Keyboard.removeListener(Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow');
Privacy.disableBlur();
};
}, []);
useEffect(() => {
if (triggerImport) importButtonPressed();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
2019-12-27 03:21:07 +01:00
const importButtonPressed = () => {
if (importText.trim().length === 0) {
return;
}
2019-12-27 03:21:07 +01:00
importMnemonic(importText);
};
2020-02-25 15:42:11 +01:00
/**
*
* @param importText
*/
const importMnemonic = async importText => {
if (WalletImport.isCurrentlyImportingWallet()) {
return;
}
WalletImport.addPlaceholderWallet(importText);
navigation.dangerouslyGetParent().pop();
await new Promise(resolve => setTimeout(resolve, 500)); // giving some time to animations
try {
await WalletImport.processImportText(importText);
WalletImport.removePlaceholderWallet();
2019-12-27 03:21:07 +01:00
} catch (error) {
WalletImport.removePlaceholderWallet();
WalletImport.addPlaceholderWallet(importText, true);
console.log(error);
2020-07-20 15:38:46 +02:00
alert(loc.wallets.import_error);
2019-12-27 03:21:07 +01:00
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
}
2019-12-27 03:21:07 +01:00
};
2020-02-25 15:42:11 +01:00
/**
*
* @param value
*/
const onBarScanned = value => {
if (value && value.data) value = value.data + ''; // no objects here, only strings
2019-12-27 03:21:07 +01:00
setImportText(value);
setTimeout(() => importMnemonic(value), 500);
2019-12-27 03:21:07 +01:00
};
2020-06-01 00:27:26 +02:00
const importScan = () => {
2021-02-17 22:49:10 +01:00
if (isMacCatalina) {
2020-12-15 05:11:05 +01:00
fs.showActionSheet().then(onBarScanned);
2020-08-23 07:07:50 +02:00
} else {
navigation.navigate('ScanQRCodeRoot', {
screen: 'ScanQRCode',
params: {
launchedBy: route.name,
onBarScanned: onBarScanned,
showFileImportButton: true,
},
});
}
};
2019-12-27 03:21:07 +01:00
return (
<SafeBlueArea style={styles.root}>
<StatusBar barStyle="light-content" />
2020-07-15 19:32:59 +02:00
<BlueSpacing20 />
2020-07-20 15:38:46 +02:00
<BlueFormLabel>{loc.wallets.import_explanation}</BlueFormLabel>
2020-04-27 09:11:33 +02:00
<BlueSpacing20 />
<BlueFormMultiInput
testID="MnemonicInput"
2020-04-27 09:11:33 +02:00
value={importText}
2021-05-29 07:57:40 +02:00
contextMenuHidden={!isDesktop}
2020-04-27 09:11:33 +02:00
onChangeText={setImportText}
inputAccessoryViewID={BlueDoneAndDismissKeyboardInputAccessory.InputAccessoryViewID}
/>
<BlueSpacing20 />
<View style={styles.center}>
<>
<BlueButton
testID="DoImport"
disabled={importText.trim().length === 0}
title={loc.wallets.import_do_import}
onPress={importButtonPressed}
/>
<BlueSpacing20 />
<BlueButtonLink title={loc.wallets.import_scan_qr} onPress={importScan} testID="ScanImport" />
</>
2020-04-27 09:11:33 +02:00
</View>
{Platform.select({
ios: (
<BlueDoneAndDismissKeyboardInputAccessory
onClearTapped={() => {
setImportText('');
}}
onPasteTapped={text => {
setImportText(text);
Keyboard.dismiss();
}}
/>
2020-04-27 09:11:33 +02:00
),
android: isToolbarVisibleForAndroid && (
<BlueDoneAndDismissKeyboardInputAccessory
onClearTapped={() => {
setImportText('');
Keyboard.dismiss();
}}
onPasteTapped={text => {
setImportText(text);
Keyboard.dismiss();
}}
/>
2020-04-27 09:11:33 +02:00
),
})}
2019-12-27 03:21:07 +01:00
</SafeBlueArea>
);
};
2021-02-15 09:03:54 +01:00
WalletsImport.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.wallets.import_title }));
2019-12-27 03:21:07 +01:00
export default WalletsImport;