2024-08-02 20:51:22 -04:00
|
|
|
import React, { useReducer, useRef } from 'react';
|
2021-03-22 07:54:17 -04:00
|
|
|
import { ScrollView } from 'react-native';
|
2024-02-24 06:27:17 -05:00
|
|
|
import triggerHapticFeedback, { HapticFeedbackTypes } from '../blue_modules/hapticFeedback';
|
2024-05-20 10:54:13 +01:00
|
|
|
import { BlueCard, BlueLoading, BlueSpacing20, BlueText } from '../BlueComponents';
|
2024-02-07 15:24:24 -04:00
|
|
|
import presentAlert from '../components/Alert';
|
2023-11-15 04:40:22 -04:00
|
|
|
import Button from '../components/Button';
|
2024-02-24 06:27:17 -05:00
|
|
|
import loc from '../loc';
|
2024-05-31 13:18:01 -04:00
|
|
|
import { useStorage } from '../hooks/context/useStorage';
|
2024-08-10 18:54:23 -04:00
|
|
|
import PromptPasswordConfirmationModal, {
|
|
|
|
PromptPasswordConfirmationModalHandle,
|
|
|
|
MODAL_TYPES,
|
|
|
|
} from '../components/PromptPasswordConfirmationModal';
|
2024-08-14 14:03:52 -04:00
|
|
|
import { useExtendedNavigation } from '../hooks/useExtendedNavigation';
|
2018-04-01 00:16:42 +01:00
|
|
|
|
2024-02-08 11:40:09 -04:00
|
|
|
// Action Types
|
|
|
|
const SET_LOADING = 'SET_LOADING';
|
2024-08-02 20:51:22 -04:00
|
|
|
const SET_MODAL_TYPE = 'SET_MODAL_TYPE';
|
2024-02-08 11:40:09 -04:00
|
|
|
|
|
|
|
// Defining State and Action Types
|
|
|
|
type State = {
|
|
|
|
isLoading: boolean;
|
2024-08-02 20:51:22 -04:00
|
|
|
modalType: keyof typeof MODAL_TYPES;
|
2024-02-08 11:40:09 -04:00
|
|
|
};
|
|
|
|
|
2024-08-10 18:54:23 -04:00
|
|
|
type Action = { type: typeof SET_LOADING; payload: boolean } | { type: typeof SET_MODAL_TYPE; payload: keyof typeof MODAL_TYPES };
|
2024-02-08 11:40:09 -04:00
|
|
|
|
|
|
|
// Initial State
|
|
|
|
const initialState: State = {
|
|
|
|
isLoading: false,
|
2024-08-10 18:54:23 -04:00
|
|
|
modalType: MODAL_TYPES.CREATE_FAKE_STORAGE,
|
2024-02-08 11:40:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Reducer Function
|
|
|
|
function reducer(state: State, action: Action): State {
|
|
|
|
switch (action.type) {
|
|
|
|
case SET_LOADING:
|
|
|
|
return { ...state, isLoading: action.payload };
|
2024-08-02 20:51:22 -04:00
|
|
|
case SET_MODAL_TYPE:
|
|
|
|
return { ...state, modalType: action.payload };
|
2024-02-08 11:40:09 -04:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Component
|
|
|
|
const PlausibleDeniability: React.FC = () => {
|
2024-05-31 11:52:29 -04:00
|
|
|
const { cachedPassword, isPasswordInUse, createFakeStorage, resetWallets } = useStorage();
|
2024-02-08 11:40:09 -04:00
|
|
|
const [state, dispatch] = useReducer(reducer, initialState);
|
2024-08-14 15:51:48 -04:00
|
|
|
const { navigate } = useExtendedNavigation();
|
2024-08-02 20:51:22 -04:00
|
|
|
const promptRef = useRef<PromptPasswordConfirmationModalHandle>(null);
|
2020-10-24 13:20:59 -04:00
|
|
|
|
2020-11-22 03:04:04 -05:00
|
|
|
const handleOnCreateFakeStorageButtonPressed = async () => {
|
2024-08-11 14:41:45 -04:00
|
|
|
dispatch({ type: SET_LOADING, payload: true });
|
2024-08-10 18:54:23 -04:00
|
|
|
dispatch({ type: SET_MODAL_TYPE, payload: MODAL_TYPES.CREATE_FAKE_STORAGE });
|
2024-08-02 20:51:22 -04:00
|
|
|
promptRef.current?.present();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleConfirmationSuccess = async (password: string) => {
|
|
|
|
let success = false;
|
|
|
|
const isProvidedPasswordInUse = password === cachedPassword || (await isPasswordInUse(password));
|
|
|
|
if (isProvidedPasswordInUse) {
|
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
|
|
|
|
presentAlert({ message: loc.plausibledeniability.password_should_not_match });
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-01 00:16:42 +01:00
|
|
|
|
2024-08-02 20:51:22 -04:00
|
|
|
try {
|
|
|
|
await createFakeStorage(password);
|
2024-03-31 20:59:14 +01:00
|
|
|
resetWallets();
|
2023-12-29 07:52:12 -04:00
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess);
|
2024-08-10 18:54:23 -04:00
|
|
|
|
2024-08-02 20:51:22 -04:00
|
|
|
// Set the modal type to SUCCESS to show the success animation instead of the alert
|
|
|
|
dispatch({ type: SET_MODAL_TYPE, payload: MODAL_TYPES.SUCCESS });
|
|
|
|
|
|
|
|
success = true;
|
2024-08-11 14:41:45 -04:00
|
|
|
setTimeout(() => {
|
2024-08-14 14:03:52 -04:00
|
|
|
navigate('WalletsList');
|
2024-08-13 17:34:09 -04:00
|
|
|
}, 3000);
|
2020-11-22 03:04:04 -05:00
|
|
|
} catch {
|
2024-08-02 20:51:22 -04:00
|
|
|
success = false;
|
2024-08-11 14:41:45 -04:00
|
|
|
dispatch({ type: SET_LOADING, payload: false });
|
2024-08-02 20:51:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleConfirmationFailure = () => {
|
|
|
|
dispatch({ type: SET_LOADING, payload: false });
|
|
|
|
};
|
|
|
|
|
2024-02-08 21:54:46 -04:00
|
|
|
return (
|
|
|
|
<ScrollView centerContent={state.isLoading} automaticallyAdjustContentInsets contentInsetAdjustmentBehavior="automatic">
|
|
|
|
{state.isLoading ? (
|
|
|
|
<BlueLoading />
|
|
|
|
) : (
|
|
|
|
<BlueCard>
|
2020-11-22 03:04:04 -05:00
|
|
|
<BlueText>{loc.plausibledeniability.help}</BlueText>
|
|
|
|
<BlueText />
|
|
|
|
<BlueText>{loc.plausibledeniability.help2}</BlueText>
|
|
|
|
<BlueSpacing20 />
|
2023-11-15 04:40:22 -04:00
|
|
|
<Button
|
2020-11-22 03:04:04 -05:00
|
|
|
testID="CreateFakeStorageButton"
|
|
|
|
title={loc.plausibledeniability.create_fake_storage}
|
|
|
|
onPress={handleOnCreateFakeStorageButtonPressed}
|
2024-08-11 14:41:45 -04:00
|
|
|
disabled={state.isLoading}
|
2020-11-22 03:04:04 -05:00
|
|
|
/>
|
2024-02-08 21:54:46 -04:00
|
|
|
</BlueCard>
|
|
|
|
)}
|
2024-08-02 20:51:22 -04:00
|
|
|
<PromptPasswordConfirmationModal
|
|
|
|
ref={promptRef}
|
|
|
|
modalType={state.modalType}
|
|
|
|
onConfirmationSuccess={handleConfirmationSuccess}
|
|
|
|
onConfirmationFailure={handleConfirmationFailure}
|
|
|
|
/>
|
2024-02-08 21:54:46 -04:00
|
|
|
</ScrollView>
|
2020-11-22 03:04:04 -05:00
|
|
|
);
|
2018-04-01 00:16:42 +01:00
|
|
|
};
|
2020-07-15 13:32:59 -04:00
|
|
|
|
2024-08-10 18:54:23 -04:00
|
|
|
export default PlausibleDeniability;
|