From 9ec0ef51e423cd1ef6ee1ad88ceab06ac70ccb46 Mon Sep 17 00:00:00 2001 From: Marcos Rodriguez Velez Date: Sat, 8 Mar 2025 19:09:41 -0400 Subject: [PATCH] Update useMenuElements.ios.ts --- hooks/useMenuElements.ios.ts | 68 +++++++++++++++--------------------- 1 file changed, 28 insertions(+), 40 deletions(-) diff --git a/hooks/useMenuElements.ios.ts b/hooks/useMenuElements.ios.ts index 6404b5737..96bf7610c 100644 --- a/hooks/useMenuElements.ios.ts +++ b/hooks/useMenuElements.ios.ts @@ -21,9 +21,6 @@ const handlerRegistry = new Map(); // 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): 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(); 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,