mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-03-13 11:09:20 +01:00
Update useMenuElements.ios.ts
This commit is contained in:
parent
1cada11c50
commit
9ec0ef51e4
1 changed files with 28 additions and 40 deletions
|
@ -21,9 +21,6 @@ const handlerRegistry = new Map<string, MenuActionHandler>();
|
|||
// Store subscription references for proper cleanup
|
||||
let subscriptions: { remove: () => void }[] = [];
|
||||
|
||||
// For debugging purposes - track active screen
|
||||
let activeScreen: string | null = null;
|
||||
|
||||
// Create a more robust emitter with error handling
|
||||
try {
|
||||
if (Platform.OS === 'ios' && MenuElementsEmitter) {
|
||||
|
@ -48,8 +45,8 @@ function safeNavigate(routeName: string, params?: Record<string, any>): void {
|
|||
navigationRef.dispatch(
|
||||
CommonActions.navigate({
|
||||
name: routeName,
|
||||
params
|
||||
})
|
||||
params,
|
||||
}),
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
|
@ -64,7 +61,7 @@ function cleanupListeners(): void {
|
|||
try {
|
||||
subscription.remove();
|
||||
} catch (e) {
|
||||
console.warn("[MenuElements] Error removing subscription:", e);
|
||||
console.warn('[MenuElements] Error removing subscription:', e);
|
||||
}
|
||||
});
|
||||
subscriptions = [];
|
||||
|
@ -82,48 +79,44 @@ function initializeListeners(): void {
|
|||
navigateToSettings: (): void => {
|
||||
safeNavigate('Settings');
|
||||
},
|
||||
|
||||
|
||||
navigateToAddWallet: (): void => {
|
||||
safeNavigate('AddWalletRoot');
|
||||
},
|
||||
|
||||
|
||||
navigateToImportWallet: (): void => {
|
||||
safeNavigate('AddWalletRoot', { screen: 'ImportWallet' });
|
||||
},
|
||||
|
||||
|
||||
executeReloadTransactions: (): void => {
|
||||
const currentRoute = navigationRef.current?.getCurrentRoute();
|
||||
if (!currentRoute) return;
|
||||
|
||||
activeScreen = currentRoute.name;
|
||||
|
||||
const screenName = currentRoute.name;
|
||||
const params = (currentRoute.params as { walletID?: string }) || {};
|
||||
const walletID = params.walletID;
|
||||
|
||||
|
||||
const specificKey = walletID ? `${screenName}-${walletID}` : null;
|
||||
|
||||
|
||||
const specificHandler = specificKey ? handlerRegistry.get(specificKey) : undefined;
|
||||
const genericHandler = handlerRegistry.get(screenName);
|
||||
const handler = specificHandler || genericHandler;
|
||||
|
||||
|
||||
if (typeof handler === 'function') {
|
||||
handler();
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
if (eventEmitter) {
|
||||
try {
|
||||
subscriptions.push(eventEmitter.addListener('openSettings', globalActions.navigateToSettings));
|
||||
subscriptions.push(eventEmitter.addListener('addWalletMenuAction', globalActions.navigateToAddWallet));
|
||||
subscriptions.push(eventEmitter.addListener('importWalletMenuAction', globalActions.navigateToImportWallet));
|
||||
subscriptions.push(eventEmitter.addListener('reloadTransactionsMenuAction', globalActions.executeReloadTransactions));
|
||||
} catch (error) {
|
||||
console.error("[MenuElements] Error setting up event listeners:", error);
|
||||
}
|
||||
try {
|
||||
subscriptions.push(eventEmitter.addListener('openSettings', globalActions.navigateToSettings));
|
||||
subscriptions.push(eventEmitter.addListener('addWalletMenuAction', globalActions.navigateToAddWallet));
|
||||
subscriptions.push(eventEmitter.addListener('importWalletMenuAction', globalActions.navigateToImportWallet));
|
||||
subscriptions.push(eventEmitter.addListener('reloadTransactionsMenuAction', globalActions.executeReloadTransactions));
|
||||
} catch (error) {
|
||||
console.error('[MenuElements] Error setting up event listeners:', error);
|
||||
}
|
||||
|
||||
|
||||
listenersInitialized = true;
|
||||
}
|
||||
|
||||
|
@ -138,39 +131,34 @@ const mountedComponents = new Set<string>();
|
|||
const useMenuElements = (): MenuElementsHook => {
|
||||
useEffect(() => {
|
||||
initializeListeners();
|
||||
|
||||
const unsubscribe = navigationRef.addListener('state', () => {
|
||||
const currentRoute = navigationRef.current?.getCurrentRoute();
|
||||
if (currentRoute) {
|
||||
activeScreen = currentRoute.name;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
const unsubscribe = navigationRef.addListener('state', () => {});
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
}, []);
|
||||
|
||||
|
||||
const registerTransactionsHandler = useCallback((handler: MenuActionHandler, screenKey?: string): boolean => {
|
||||
if (typeof handler !== 'function') return false;
|
||||
|
||||
|
||||
const key = screenKey || navigationRef.current?.getCurrentRoute()?.name;
|
||||
if (!key) return false;
|
||||
|
||||
|
||||
mountedComponents.add(key);
|
||||
|
||||
|
||||
handlerRegistry.set(key, handler);
|
||||
|
||||
|
||||
return true;
|
||||
}, []);
|
||||
|
||||
|
||||
const unregisterTransactionsHandler = useCallback((screenKey: string): void => {
|
||||
if (!screenKey) return;
|
||||
|
||||
handlerRegistry.delete(screenKey);
|
||||
mountedComponents.delete(screenKey);
|
||||
}, []);
|
||||
|
||||
|
||||
return {
|
||||
registerTransactionsHandler,
|
||||
unregisterTransactionsHandler,
|
||||
|
|
Loading…
Add table
Reference in a new issue