BlueWallet/screen/PlausibleDeniability.tsx

104 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-02-08 16:40:09 +01:00
import React, { useContext, useReducer } from 'react';
import { ScrollView } from 'react-native';
import { useNavigation } from '@react-navigation/native';
2020-12-25 17:09:53 +01:00
import navigationStyle from '../components/navigationStyle';
import { BlueLoading, BlueCard, BlueText, BlueSpacing20 } from '../BlueComponents';
2020-07-20 15:38:46 +02:00
import loc from '../loc';
import { BlueStorageContext } from '../blue_modules/storage-context';
import presentAlert from '../components/Alert';
2023-11-15 09:40:22 +01:00
import Button from '../components/Button';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../blue_modules/hapticFeedback';
2024-02-08 16:40:09 +01:00
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
2022-09-05 20:34:02 +02:00
const prompt = require('../helpers/prompt');
2018-04-01 01:16:42 +02:00
2024-02-08 16:40:09 +01:00
// Action Types
const SET_LOADING = 'SET_LOADING';
// Defining State and Action Types
type State = {
isLoading: boolean;
};
type Action = { type: typeof SET_LOADING; payload: boolean };
// Initial State
const initialState: State = {
isLoading: false,
};
// Reducer Function
function reducer(state: State, action: Action): State {
switch (action.type) {
case SET_LOADING:
return { ...state, isLoading: action.payload };
default:
return state;
}
}
// Component
const PlausibleDeniability: React.FC = () => {
2020-11-22 09:04:04 +01:00
const { cachedPassword, isPasswordInUse, createFakeStorage, resetWallets } = useContext(BlueStorageContext);
2024-02-08 16:40:09 +01:00
const [state, dispatch] = useReducer(reducer, initialState);
const navigation = useNavigation<NativeStackNavigationProp<Record<string, object | undefined>>>();
2020-11-22 09:04:04 +01:00
const handleOnCreateFakeStorageButtonPressed = async () => {
2024-02-08 16:40:09 +01:00
dispatch({ type: SET_LOADING, payload: true });
2020-11-22 09:04:04 +01:00
try {
const p1 = await prompt(loc.plausibledeniability.create_password, loc.plausibledeniability.create_password_explanation);
const isProvidedPasswordInUse = p1 === cachedPassword || (await isPasswordInUse(p1));
if (isProvidedPasswordInUse) {
2024-02-08 16:40:09 +01:00
dispatch({ type: SET_LOADING, payload: false });
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
return presentAlert({ message: loc.plausibledeniability.password_should_not_match });
2020-11-22 09:04:04 +01:00
}
if (!p1) {
2024-02-08 16:40:09 +01:00
dispatch({ type: SET_LOADING, payload: false });
2020-11-22 09:04:04 +01:00
return;
}
const p2 = await prompt(loc.plausibledeniability.retype_password);
if (p1 !== p2) {
2024-02-08 16:40:09 +01:00
dispatch({ type: SET_LOADING, payload: false });
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
return presentAlert({ message: loc.plausibledeniability.passwords_do_not_match });
2020-11-22 09:04:04 +01:00
}
2018-04-01 01:16:42 +02:00
2020-11-22 09:04:04 +01:00
await createFakeStorage(p1);
await resetWallets();
triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess);
presentAlert({ message: loc.plausibledeniability.success });
2024-02-08 16:40:09 +01:00
navigation.popToTop();
2020-11-22 09:04:04 +01:00
} catch {
2024-02-08 16:40:09 +01:00
dispatch({ type: SET_LOADING, payload: false });
2018-04-01 01:16:42 +02:00
}
2020-11-22 09:04:04 +01:00
};
2018-04-01 01:16:42 +02:00
2024-02-09 02:54:46 +01:00
return (
<ScrollView centerContent={state.isLoading} automaticallyAdjustContentInsets contentInsetAdjustmentBehavior="automatic">
{state.isLoading ? (
<BlueLoading />
) : (
<BlueCard>
2020-11-22 09:04:04 +01:00
<BlueText>{loc.plausibledeniability.help}</BlueText>
<BlueText />
<BlueText>{loc.plausibledeniability.help2}</BlueText>
<BlueSpacing20 />
2023-11-15 09:40:22 +01:00
<Button
2020-11-22 09:04:04 +01:00
testID="CreateFakeStorageButton"
title={loc.plausibledeniability.create_fake_storage}
onPress={handleOnCreateFakeStorageButtonPressed}
/>
2024-02-09 02:54:46 +01:00
</BlueCard>
)}
</ScrollView>
2020-11-22 09:04:04 +01:00
);
2018-04-01 01:16:42 +02:00
};
2020-07-15 19:32:59 +02:00
2020-11-22 09:04:04 +01:00
export default PlausibleDeniability;
2024-02-08 16:40:09 +01:00
// @ts-ignore: Fix later
2020-12-25 17:09:53 +01:00
PlausibleDeniability.navigationOptions = navigationStyle({
2020-07-15 19:32:59 +02:00
title: loc.plausibledeniability.title,
});