BlueWallet/hooks/useExtendedNavigation.ts

119 lines
4.6 KiB
TypeScript
Raw Normal View History

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