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';
|
2021-03-22 12:54:17 +01:00
|
|
|
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';
|
2024-02-07 20:24:24 +01:00
|
|
|
import presentAlert from '../components/Alert';
|
2023-11-15 09:40:22 +01:00
|
|
|
import Button from '../components/Button';
|
2024-03-31 21:59:14 +02:00
|
|
|
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 = () => {
|
2024-05-31 17:52:29 +02:00
|
|
|
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-10-24 19:20:59 +02:00
|
|
|
|
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 });
|
2023-12-29 12:52:12 +01:00
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
|
2024-02-07 20:24:24 +01:00
|
|
|
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;
|
|
|
|
}
|
2024-03-31 21:59:14 +02:00
|
|
|
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 });
|
2023-12-29 12:52:12 +01:00
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
|
2024-02-07 20:24:24 +01:00
|
|
|
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);
|
2024-03-31 21:59:14 +02:00
|
|
|
resetWallets();
|
2023-12-29 12:52:12 +01:00
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess);
|
2024-02-07 20:24:24 +01:00
|
|
|
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;
|