BlueWallet/screen/PlausibleDeniability.tsx

97 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-02-24 12:27:17 +01:00
import { useNavigation } from '@react-navigation/native';
2024-05-20 11:54:13 +02:00
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
2024-05-31 19:18:01 +02:00
import React, { useReducer } from 'react';
import { ScrollView } from 'react-native';
2024-02-24 12:27:17 +01:00
import triggerHapticFeedback, { HapticFeedbackTypes } from '../blue_modules/hapticFeedback';
2024-05-20 11:54:13 +02:00
import { BlueCard, BlueLoading, BlueSpacing20, BlueText } from '../BlueComponents';
import presentAlert from '../components/Alert';
2023-11-15 09:40:22 +01:00
import Button from '../components/Button';
import prompt from '../helpers/prompt';
2024-02-24 12:27:17 +01:00
import loc from '../loc';
2024-05-31 19:18:01 +02:00
import { useStorage } from '../hooks/context/useStorage';
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 = () => {
const { cachedPassword, isPasswordInUse, createFakeStorage, resetWallets } = useStorage();
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.create_password, loc.plausibledeniability.retype_password);
2020-11-22 09:04:04 +01:00
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);
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;