Update useMenuElements.ios.ts

This commit is contained in:
Marcos Rodriguez Velez 2025-03-08 19:09:41 -04:00
parent 1cada11c50
commit 9ec0ef51e4

View file

@ -21,9 +21,6 @@ const handlerRegistry = new Map<string, MenuActionHandler>();
// Store subscription references for proper cleanup // Store subscription references for proper cleanup
let subscriptions: { remove: () => void }[] = []; let subscriptions: { remove: () => void }[] = [];
// For debugging purposes - track active screen
let activeScreen: string | null = null;
// Create a more robust emitter with error handling // Create a more robust emitter with error handling
try { try {
if (Platform.OS === 'ios' && MenuElementsEmitter) { if (Platform.OS === 'ios' && MenuElementsEmitter) {
@ -48,8 +45,8 @@ function safeNavigate(routeName: string, params?: Record<string, any>): void {
navigationRef.dispatch( navigationRef.dispatch(
CommonActions.navigate({ CommonActions.navigate({
name: routeName, name: routeName,
params params,
}) }),
); );
} }
} catch (error) { } catch (error) {
@ -64,7 +61,7 @@ function cleanupListeners(): void {
try { try {
subscription.remove(); subscription.remove();
} catch (e) { } catch (e) {
console.warn("[MenuElements] Error removing subscription:", e); console.warn('[MenuElements] Error removing subscription:', e);
} }
}); });
subscriptions = []; subscriptions = [];
@ -95,8 +92,6 @@ function initializeListeners(): void {
const currentRoute = navigationRef.current?.getCurrentRoute(); const currentRoute = navigationRef.current?.getCurrentRoute();
if (!currentRoute) return; if (!currentRoute) return;
activeScreen = currentRoute.name;
const screenName = currentRoute.name; const screenName = currentRoute.name;
const params = (currentRoute.params as { walletID?: string }) || {}; const params = (currentRoute.params as { walletID?: string }) || {};
const walletID = params.walletID; const walletID = params.walletID;
@ -110,18 +105,16 @@ function initializeListeners(): void {
if (typeof handler === 'function') { if (typeof handler === 'function') {
handler(); handler();
} }
} },
}; };
if (eventEmitter) {
try { try {
subscriptions.push(eventEmitter.addListener('openSettings', globalActions.navigateToSettings)); subscriptions.push(eventEmitter.addListener('openSettings', globalActions.navigateToSettings));
subscriptions.push(eventEmitter.addListener('addWalletMenuAction', globalActions.navigateToAddWallet)); subscriptions.push(eventEmitter.addListener('addWalletMenuAction', globalActions.navigateToAddWallet));
subscriptions.push(eventEmitter.addListener('importWalletMenuAction', globalActions.navigateToImportWallet)); subscriptions.push(eventEmitter.addListener('importWalletMenuAction', globalActions.navigateToImportWallet));
subscriptions.push(eventEmitter.addListener('reloadTransactionsMenuAction', globalActions.executeReloadTransactions)); subscriptions.push(eventEmitter.addListener('reloadTransactionsMenuAction', globalActions.executeReloadTransactions));
} catch (error) { } catch (error) {
console.error("[MenuElements] Error setting up event listeners:", error); console.error('[MenuElements] Error setting up event listeners:', error);
}
} }
listenersInitialized = true; listenersInitialized = true;
@ -139,12 +132,7 @@ const useMenuElements = (): MenuElementsHook => {
useEffect(() => { useEffect(() => {
initializeListeners(); initializeListeners();
const unsubscribe = navigationRef.addListener('state', () => { const unsubscribe = navigationRef.addListener('state', () => {});
const currentRoute = navigationRef.current?.getCurrentRoute();
if (currentRoute) {
activeScreen = currentRoute.name;
}
});
return () => { return () => {
unsubscribe(); unsubscribe();