import React, { useEffect, useState } from 'react';
import { Platform, View, Keyboard, StyleSheet, Switch, TouchableWithoutFeedback } from 'react-native';
import { useNavigation, useRoute } from '@react-navigation/native';
import {
BlueButtonLink,
BlueDoneAndDismissKeyboardInputAccessory,
BlueFormLabel,
BlueFormMultiInput,
BlueSpacing20,
BlueText,
} from '../../BlueComponents';
import loc from '../../loc';
import { requestCameraAuthorization } from '../../helpers/scan-qr';
import { useTheme } from '../../components/themes';
import Button from '../../components/Button';
import SafeArea from '../../components/SafeArea';
import usePrivacy from '../../hooks/usePrivacy';
import { useSettings } from '../../components/Context/SettingsContext';
const WalletsImport = () => {
const navigation = useNavigation();
const { colors } = useTheme();
const route = useRoute();
const label = route?.params?.label ?? '';
const triggerImport = route?.params?.triggerImport ?? false;
const { isAdvancedModeEnabled } = useSettings();
const [importText, setImportText] = useState(label);
const [isToolbarVisibleForAndroid, setIsToolbarVisibleForAndroid] = useState(false);
const [, setSpeedBackdoor] = useState(0);
const [searchAccounts, setSearchAccounts] = useState(false);
const [askPassphrase, setAskPassphrase] = useState(false);
const { enableBlur, disableBlur } = usePrivacy();
const styles = StyleSheet.create({
root: {
paddingTop: 10,
backgroundColor: colors.elevated,
},
center: {
flex: 1,
marginHorizontal: 16,
backgroundColor: colors.elevated,
},
row: {
flexDirection: 'row',
alignItems: 'center',
marginHorizontal: 16,
marginTop: 10,
justifyContent: 'space-between',
},
});
const onBlur = () => {
const valueWithSingleWhitespace = importText.replace(/^\s+|\s+$|\s+(?=\s)/g, '');
setImportText(valueWithSingleWhitespace);
return valueWithSingleWhitespace;
};
useEffect(() => {
enableBlur();
Keyboard.addListener(Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow', () => setIsToolbarVisibleForAndroid(true));
Keyboard.addListener(Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide', () => setIsToolbarVisibleForAndroid(false));
return () => {
Keyboard.removeAllListeners(Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow');
Keyboard.removeAllListeners(Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide');
disableBlur();
};
}, [disableBlur, enableBlur]);
useEffect(() => {
if (triggerImport) importButtonPressed();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const importButtonPressed = () => {
const textToImport = onBlur();
if (textToImport.trim().length === 0) {
return;
}
importMnemonic(textToImport);
};
const importMnemonic = text => {
navigation.navigate('ImportWalletDiscovery', { importText: text, askPassphrase, searchAccounts });
};
const onBarScanned = value => {
if (value && value.data) value = value.data + ''; // no objects here, only strings
setImportText(value);
setTimeout(() => importMnemonic(value), 500);
};
const importScan = () => {
requestCameraAuthorization().then(() =>
navigation.navigate('ScanQRCodeRoot', {
screen: 'ScanQRCode',
params: {
launchedBy: route.name,
onBarScanned,
showFileImportButton: true,
},
}),
);
};
const speedBackdoorTap = () => {
setSpeedBackdoor(v => {
v += 1;
if (v < 5) return v;
navigation.navigate('ImportWallet');
return 0;
});
};
const renderOptionsAndImportButton = (
<>
{isAdvancedModeEnabled && (
<>
{loc.wallets.import_passphrase}
{loc.wallets.import_search_accounts}
>
)}
<>
>
>
);
return (
{loc.wallets.import_explanation}
{Platform.select({ android: !isToolbarVisibleForAndroid && renderOptionsAndImportButton, default: renderOptionsAndImportButton })}
{Platform.select({
ios: (
{
setImportText('');
}}
onPasteTapped={text => {
setImportText(text);
Keyboard.dismiss();
}}
/>
),
android: isToolbarVisibleForAndroid && (
{
setImportText('');
Keyboard.dismiss();
}}
onPasteTapped={text => {
setImportText(text);
Keyboard.dismiss();
}}
/>
),
})}
);
};
export default WalletsImport;