BlueWallet/screen/wallets/import.js

158 lines
4.8 KiB
JavaScript
Raw Normal View History

/* global alert */
2019-12-27 03:21:07 +01:00
import React, { useEffect, useState } from 'react';
import { Platform, Dimensions, View, Keyboard, StatusBar, StyleSheet } from 'react-native';
import {
BlueFormMultiInput,
BlueButtonLink,
BlueFormLabel,
2019-08-29 06:18:32 +02:00
BlueDoneAndDismissKeyboardInputAccessory,
BlueButton,
SafeBlueArea,
BlueSpacing20,
BlueNavigationStyle,
} from '../../BlueComponents';
2018-12-13 17:50:18 +01:00
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
import Privacy from '../../Privacy';
2020-07-15 19:32:59 +02:00
import { useNavigation, useRoute, useTheme } from '@react-navigation/native';
import WalletImport from '../../class/wallet-import';
2020-07-20 15:38:46 +02:00
import loc from '../../loc';
const { width } = Dimensions.get('window');
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 [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: {
flex: 1,
paddingTop: 40,
backgroundColor: colors.elevated,
},
center: {
flex: 1,
alignItems: 'center',
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();
};
}, []);
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
* @param additionalProperties key-values passed from outside. Used only to set up `masterFingerprint` property for watch-only wallet
*/
2020-01-01 04:31:04 +01:00
const importMnemonic = (importText, additionalProperties) => {
try {
2020-01-01 04:31:04 +01:00
WalletImport.processImportText(importText, additionalProperties);
2020-05-27 13:12:17 +02:00
navigation.dangerouslyGetParent().pop();
2019-12-27 03:21:07 +01:00
} catch (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
* @param additionalProperties key-values passed from outside. Used only to set up `masterFingerprint` property for watch-only wallet
*/
2020-01-01 04:31:04 +01:00
const onBarScanned = (value, additionalProperties) => {
2019-12-27 03:21:07 +01:00
setImportText(value);
2020-01-01 04:31:04 +01:00
importMnemonic(value, additionalProperties);
2019-12-27 03:21:07 +01:00
};
2020-06-01 00:27:26 +02:00
const importScan = () => {
navigation.navigate('ScanQRCodeRoot', {
screen: 'ScanQRCode',
params: {
launchedBy: route.name,
onBarScanned: onBarScanned,
2020-06-01 00:36:00 +02:00
showFileImportButton: true,
2020-06-01 00:27:26 +02:00
},
});
};
2019-12-27 03:21:07 +01:00
return (
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={styles.root}>
2020-07-15 19:32:59 +02:00
<StatusBar barStyle="default" />
<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}
contextMenuHidden
onChangeText={setImportText}
inputAccessoryViewID={BlueDoneAndDismissKeyboardInputAccessory.InputAccessoryViewID}
/>
<BlueSpacing20 />
<View style={styles.center}>
2020-04-27 09:11:33 +02:00
<BlueButton
testID="DoImport"
2020-04-27 09:11:33 +02:00
disabled={importText.trim().length === 0}
2020-07-20 15:38:46 +02:00
title={loc.wallets.import_do_import}
2020-04-27 09:11:33 +02:00
buttonStyle={{
width: width / 1.5,
}}
onPress={importButtonPressed}
/>
<BlueSpacing20 />
2020-07-20 15:38:46 +02:00
<BlueButtonLink title={loc.wallets.import_scan_qr} onPress={importScan} />
2020-04-27 09:11:33 +02:00
</View>
{Platform.select({
ios: (
<BlueDoneAndDismissKeyboardInputAccessory
onClearTapped={() => {
setImportText('');
Keyboard.dismiss();
}}
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>
);
};
2020-07-15 19:32:59 +02:00
WalletsImport.navigationOptions = ({ navigation, route }) => ({
2019-12-27 03:21:07 +01:00
...BlueNavigationStyle(),
2020-07-20 15:38:46 +02:00
title: loc.wallets.import_title,
2020-07-15 19:32:59 +02:00
});
2019-12-27 03:21:07 +01:00
export default WalletsImport;