BlueWallet/hooks/useExtendedNavigation.ts

98 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-03-24 10:52:10 -04:00
import { useNavigation, NavigationProp, ParamListBase } from '@react-navigation/native';
import Biometric from '../class/biometrics';
import { navigationRef } from '../NavigationService';
2024-03-24 16:29:58 -04:00
import { BlueStorageContext } from '../blue_modules/storage-context';
import { useContext } from 'react';
import { presentWalletExportReminder } from '../helpers/presentWalletExportReminder';
2024-03-24 10:52:10 -04:00
2024-03-24 16:41:16 -04:00
// List of screens that require biometrics
const requiresBiometrics = ['WalletExportRoot', 'WalletXpubRoot', 'ViewEditMultisigCosignersRoot', 'ExportMultisigCoordinationSetupRoot'];
// List of screens that require wallet export to be saved
const requiresWalletExportIsSaved = ['ReceiveDetailsRoot', 'WalletAddresses'];
2024-03-24 10:52:10 -04:00
export const useExtendedNavigation = (): NavigationProp<ParamListBase> => {
const originalNavigation = useNavigation<NavigationProp<ParamListBase>>();
2024-03-24 16:29:58 -04:00
const { wallets, saveToDisk } = useContext(BlueStorageContext);
2024-03-24 10:52:10 -04:00
2024-03-24 16:29:58 -04:00
const enhancedNavigate: NavigationProp<ParamListBase>['navigate'] = (screenOrOptions: any, params?: any) => {
2024-03-24 10:52:10 -04:00
let screenName: string;
if (typeof screenOrOptions === 'string') {
screenName = screenOrOptions;
} else if (typeof screenOrOptions === 'object' && 'name' in screenOrOptions) {
screenName = screenOrOptions.name;
2024-03-24 16:29:58 -04:00
params = screenOrOptions.params; // Assign params from object if present
2024-03-24 10:52:10 -04:00
} else {
throw new Error('Invalid navigation options');
}
2024-03-24 16:41:16 -04:00
const isRequiresBiometrics = requiresBiometrics.includes(screenName);
const isRequiresWalletExportIsSaved = requiresWalletExportIsSaved.includes(screenName);
2024-03-24 10:52:10 -04:00
const proceedWithNavigation = () => {
2024-03-24 16:41:16 -04:00
console.log('Proceeding with navigation to', screenName);
2024-03-24 10:52:10 -04:00
if (navigationRef.current?.isReady()) {
typeof screenOrOptions === 'string'
? originalNavigation.navigate(screenOrOptions, params)
2024-03-24 16:29:58 -04:00
: originalNavigation.navigate(screenName, params); // Fixed to use screenName and params
2024-03-24 10:52:10 -04:00
}
};
2024-03-24 16:29:58 -04:00
(async () => {
2024-03-24 16:41:16 -04:00
if (isRequiresBiometrics) {
2024-03-24 16:29:58 -04:00
const isBiometricsEnabled = await Biometric.isBiometricUseEnabled();
2024-03-24 10:52:10 -04:00
if (isBiometricsEnabled) {
2024-03-24 16:29:58 -04:00
const isAuthenticated = await Biometric.unlockWithBiometrics();
if (isAuthenticated) {
proceedWithNavigation();
return; // Ensure the function exits if this path is taken
} else {
console.error('Biometric authentication failed');
// Decide if navigation should proceed or not after failed authentication
2024-04-06 16:02:14 -04:00
return; // Prevent proceeding with the original navigation if bio fails
2024-03-24 16:29:58 -04:00
}
}
}
2024-03-24 16:41:16 -04:00
if (isRequiresWalletExportIsSaved) {
2024-03-24 16:29:58 -04:00
console.log('Checking if wallet export is saved');
2024-03-24 16:41:16 -04:00
let walletID: string | undefined;
if (params && params.walletID) {
walletID = params.walletID;
} else if (params && params.params && params.params.walletID) {
walletID = params.params.walletID;
}
2024-03-24 16:29:58 -04:00
if (!walletID) {
2024-03-24 10:52:10 -04:00
proceedWithNavigation();
2024-03-24 16:29:58 -04:00
return;
2024-03-24 10:52:10 -04:00
}
2024-03-24 16:29:58 -04:00
const wallet = wallets.find(w => w.getID() === walletID);
if (wallet && !wallet.getUserHasSavedExport()) {
2024-03-24 16:43:12 -04:00
try {
await presentWalletExportReminder();
wallet.setUserHasSavedExport(true);
await saveToDisk(); // Assuming saveToDisk() returns a Promise.
proceedWithNavigation();
} catch (error) {
if (error) {
originalNavigation.navigate('WalletExportRoot', {
screen: 'WalletExport',
params: { walletID },
});
}
2024-03-24 16:43:12 -04:00
}
2024-03-24 16:29:58 -04:00
return; // Prevent proceeding with the original navigation if the reminder is shown
}
}
2024-03-24 10:52:10 -04:00
proceedWithNavigation();
2024-03-24 16:29:58 -04:00
})();
2024-03-24 10:52:10 -04:00
};
return {
...originalNavigation,
navigate: enhancedNavigate,
};
};