2022-03-29 16:09:44 +00:00
|
|
|
import Clipboard from '@react-native-clipboard/clipboard';
|
2022-08-17 13:51:40 +00:00
|
|
|
import React, { useEffect, useLayoutEffect, useState } from 'react';
|
|
|
|
import { ScrollView, StyleSheet, Text, View } from 'react-native';
|
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
import { StackActions, useNavigation } from '@react-navigation/native';
|
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2022-01-13 18:37:14 +00:00
|
|
|
import { encryptionSetBanner } from '../actions/encryption';
|
|
|
|
import Button from '../containers/Button';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2020-09-11 14:31:38 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
|
|
|
import { LISTENER } from '../containers/Toast';
|
|
|
|
import I18n from '../i18n';
|
2022-08-17 13:51:40 +00:00
|
|
|
import { E2E_RANDOM_PASSWORD_KEY } from '../lib/constants';
|
|
|
|
import { useAppSelector } from '../lib/hooks';
|
2022-06-06 14:17:51 +00:00
|
|
|
import EventEmitter from '../lib/methods/helpers/events';
|
|
|
|
import { events, logEvent } from '../lib/methods/helpers/log';
|
|
|
|
import scrollPersistTaps from '../lib/methods/helpers/scrollPersistTaps';
|
2022-08-17 13:51:40 +00:00
|
|
|
import UserPreferences from '../lib/methods/userPreferences';
|
|
|
|
import { E2ESaveYourPasswordStackParamList } from '../stacks/types';
|
|
|
|
import { useTheme } from '../theme';
|
2022-01-13 18:37:14 +00:00
|
|
|
import sharedStyles from './Styles';
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
padding: 44,
|
|
|
|
paddingTop: 32
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
marginVertical: 68,
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
warning: {
|
|
|
|
fontSize: 14,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
passwordText: {
|
|
|
|
marginBottom: 8,
|
|
|
|
...sharedStyles.textAlignCenter
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
fontSize: 24,
|
|
|
|
marginBottom: 24,
|
|
|
|
...sharedStyles.textBold
|
|
|
|
},
|
|
|
|
copyButton: {
|
|
|
|
width: 72,
|
|
|
|
height: 32
|
|
|
|
},
|
|
|
|
info: {
|
|
|
|
fontSize: 14,
|
|
|
|
marginBottom: 64,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-08-17 13:51:40 +00:00
|
|
|
const E2ESaveYourPasswordView = () => {
|
|
|
|
const server = useAppSelector(state => state.server.server);
|
|
|
|
const navigation = useNavigation<StackNavigationProp<E2ESaveYourPasswordStackParamList, 'E2ESaveYourPasswordView'>>();
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const [password, setPassword] = useState('');
|
|
|
|
const { colors } = useTheme();
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Save_Your_E2E_Password'),
|
|
|
|
headerLeft: () => <HeaderButton.CloseModal testID='e2e-save-your-password-view-close' />
|
|
|
|
});
|
|
|
|
}, [navigation]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const init = () => {
|
2022-03-09 19:41:26 +00:00
|
|
|
const password = UserPreferences.getString(`${server}-${E2E_RANDOM_PASSWORD_KEY}`);
|
2022-08-17 13:51:40 +00:00
|
|
|
if (password) setPassword(password);
|
|
|
|
};
|
|
|
|
init();
|
|
|
|
}, []);
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2022-08-17 13:51:40 +00:00
|
|
|
const onSaved = () => {
|
2020-09-11 14:31:38 +00:00
|
|
|
logEvent(events.E2E_SAVE_PW_SAVED);
|
2022-03-09 19:41:26 +00:00
|
|
|
UserPreferences.removeItem(`${server}-${E2E_RANDOM_PASSWORD_KEY}`);
|
2022-01-13 18:37:14 +00:00
|
|
|
dispatch(encryptionSetBanner());
|
2022-08-17 13:51:40 +00:00
|
|
|
navigation.dispatch(StackActions.pop());
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2022-08-17 13:51:40 +00:00
|
|
|
const onCopy = () => {
|
2020-09-11 14:31:38 +00:00
|
|
|
logEvent(events.E2E_SAVE_PW_COPY);
|
2022-05-20 02:42:55 +00:00
|
|
|
if (password) {
|
|
|
|
Clipboard.setString(password);
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('Copied_to_clipboard') });
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2022-08-17 13:51:40 +00:00
|
|
|
const onHowItWorks = () => {
|
2020-09-11 14:31:38 +00:00
|
|
|
logEvent(events.E2E_SAVE_PW_HOW_IT_WORKS);
|
|
|
|
navigation.navigate('E2EHowItWorksView');
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2022-08-17 13:51:40 +00:00
|
|
|
return (
|
|
|
|
<SafeAreaView style={{ backgroundColor: colors.backgroundColor }} testID='e2e-save-password-view'>
|
|
|
|
<StatusBar />
|
|
|
|
<ScrollView {...scrollPersistTaps} style={sharedStyles.container} contentContainerStyle={sharedStyles.containerScrollView}>
|
|
|
|
<View style={[styles.container, { backgroundColor: colors.backgroundColor }]}>
|
|
|
|
<Text style={[styles.warning, { color: colors.dangerColor }]}>{I18n.t('Save_Your_Encryption_Password_warning')}</Text>
|
|
|
|
<View style={styles.content}>
|
|
|
|
<Text style={[styles.passwordText, { color: colors.bodyText }]}>{I18n.t('Your_password_is')}</Text>
|
|
|
|
<Text style={[styles.password, { color: colors.bodyText }]}>{password}</Text>
|
2020-09-11 14:31:38 +00:00
|
|
|
<Button
|
2022-08-17 13:51:40 +00:00
|
|
|
onPress={onCopy}
|
|
|
|
style={[styles.copyButton, { backgroundColor: colors.auxiliaryBackground }]}
|
|
|
|
title={I18n.t('Copy')}
|
2020-09-11 14:31:38 +00:00
|
|
|
type='secondary'
|
2022-08-17 13:51:40 +00:00
|
|
|
fontSize={12}
|
2020-09-11 14:31:38 +00:00
|
|
|
/>
|
|
|
|
</View>
|
2022-08-17 13:51:40 +00:00
|
|
|
<Text style={[styles.info, { color: colors.bodyText }]}>{I18n.t('Save_Your_Encryption_Password_info')}</Text>
|
|
|
|
<Button
|
|
|
|
onPress={onHowItWorks}
|
|
|
|
style={{ backgroundColor: colors.auxiliaryBackground }}
|
|
|
|
title={I18n.t('How_It_Works')}
|
|
|
|
type='secondary'
|
|
|
|
testID='e2e-save-password-view-how-it-works'
|
|
|
|
/>
|
|
|
|
<Button onPress={onSaved} title={I18n.t('I_Saved_My_E2E_Password')} testID='e2e-save-password-view-saved-password' />
|
|
|
|
</View>
|
|
|
|
</ScrollView>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default E2ESaveYourPasswordView;
|