BlueWallet/hooks/useExtendedNavigation.ts

100 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-03-24 15:52:10 +01:00
import { useNavigation, NavigationProp, ParamListBase } from '@react-navigation/native';
import { navigationRef } from '../NavigationService';
2024-03-24 21:29:58 +01:00
import { presentWalletExportReminder } from '../helpers/presentWalletExportReminder';
2024-06-04 03:27:21 +02:00
import { unlockWithBiometrics, useBiometrics } from './useBiometrics';
2024-05-31 19:18:01 +02:00
import { useStorage } from './context/useStorage';
2024-03-24 15:52:10 +01:00
2024-03-24 21:41:16 +01: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-05-22 15:56:10 +02:00
export const useExtendedNavigation = <T extends NavigationProp<ParamListBase>>(): T => {
const originalNavigation = useNavigation<T>();
2024-05-18 00:34:39 +02:00
const { wallets, saveToDisk } = useStorage();
2024-06-04 03:27:21 +02:00
const { isBiometricUseEnabled } = useBiometrics();
2024-03-24 15:52:10 +01:00
2024-03-24 21:29:58 +01:00
const enhancedNavigate: NavigationProp<ParamListBase>['navigate'] = (screenOrOptions: any, params?: any) => {
2024-03-24 15:52:10 +01:00
let screenName: string;
if (typeof screenOrOptions === 'string') {
screenName = screenOrOptions;
} else if (typeof screenOrOptions === 'object' && 'name' in screenOrOptions) {
screenName = screenOrOptions.name;
2024-03-24 21:29:58 +01:00
params = screenOrOptions.params; // Assign params from object if present
2024-03-24 15:52:10 +01:00
} else {
throw new Error('Invalid navigation options');
}
2024-03-24 21:41:16 +01:00
const isRequiresBiometrics = requiresBiometrics.includes(screenName);
const isRequiresWalletExportIsSaved = requiresWalletExportIsSaved.includes(screenName);
2024-03-24 15:52:10 +01:00
const proceedWithNavigation = () => {
2024-03-24 21:41:16 +01:00
console.log('Proceeding with navigation to', screenName);
2024-03-24 15:52:10 +01:00
if (navigationRef.current?.isReady()) {
typeof screenOrOptions === 'string'
? originalNavigation.navigate(screenOrOptions, params)
2024-03-24 21:29:58 +01:00
: originalNavigation.navigate(screenName, params); // Fixed to use screenName and params
2024-03-24 15:52:10 +01:00
}
};
2024-03-24 21:29:58 +01:00
(async () => {
2024-03-24 21:41:16 +01:00
if (isRequiresBiometrics) {
2024-05-18 00:34:39 +02:00
const isBiometricsEnabled = await isBiometricUseEnabled();
2024-03-24 15:52:10 +01:00
if (isBiometricsEnabled) {
2024-05-18 00:34:39 +02:00
const isAuthenticated = await unlockWithBiometrics();
2024-03-24 21:29:58 +01:00
if (isAuthenticated) {
proceedWithNavigation();
2024-05-18 18:48:03 +02:00
return;
2024-03-24 21:29:58 +01:00
} else {
console.error('Biometric authentication failed');
// Decide if navigation should proceed or not after failed authentication
2024-04-06 22:02:14 +02:00
return; // Prevent proceeding with the original navigation if bio fails
2024-03-24 21:29:58 +01:00
}
}
}
2024-03-24 21:41:16 +01:00
if (isRequiresWalletExportIsSaved) {
2024-03-24 21:29:58 +01:00
console.log('Checking if wallet export is saved');
2024-03-24 21:41:16 +01: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 21:29:58 +01:00
if (!walletID) {
2024-03-24 15:52:10 +01:00
proceedWithNavigation();
2024-03-24 21:29:58 +01:00
return;
2024-03-24 15:52:10 +01:00
}
2024-03-24 21:29:58 +01:00
const wallet = wallets.find(w => w.getID() === walletID);
if (wallet && !wallet.getUserHasSavedExport()) {
2024-03-24 21:43:12 +01: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 21:43:12 +01:00
}
2024-03-24 21:29:58 +01:00
return; // Prevent proceeding with the original navigation if the reminder is shown
}
}
2024-03-24 15:52:10 +01:00
proceedWithNavigation();
2024-03-24 21:29:58 +01:00
})();
2024-03-24 15:52:10 +01:00
};
return {
...originalNavigation,
navigate: enhancedNavigate,
};
};
2024-05-22 15:56:10 +02:00
// Usage example:
// type NavigationProps = NativeStackNavigationProp<SendDetailsStackParamList, 'SendDetails'>;
// const navigation = useExtendedNavigation<NavigationProps>();