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 = [];
@ -82,48 +79,44 @@ function initializeListeners(): void {
navigateToSettings: (): void => { navigateToSettings: (): void => {
safeNavigate('Settings'); safeNavigate('Settings');
}, },
navigateToAddWallet: (): void => { navigateToAddWallet: (): void => {
safeNavigate('AddWalletRoot'); safeNavigate('AddWalletRoot');
}, },
navigateToImportWallet: (): void => { navigateToImportWallet: (): void => {
safeNavigate('AddWalletRoot', { screen: 'ImportWallet' }); safeNavigate('AddWalletRoot', { screen: 'ImportWallet' });
}, },
executeReloadTransactions: (): void => { executeReloadTransactions: (): 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;
const specificKey = walletID ? `${screenName}-${walletID}` : null; const specificKey = walletID ? `${screenName}-${walletID}` : null;
const specificHandler = specificKey ? handlerRegistry.get(specificKey) : undefined; const specificHandler = specificKey ? handlerRegistry.get(specificKey) : undefined;
const genericHandler = handlerRegistry.get(screenName); const genericHandler = handlerRegistry.get(screenName);
const handler = specificHandler || genericHandler; const handler = specificHandler || genericHandler;
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;
} }
@ -138,39 +131,34 @@ const mountedComponents = new Set<string>();
const useMenuElements = (): MenuElementsHook => { 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();
}; };
}, []); }, []);
const registerTransactionsHandler = useCallback((handler: MenuActionHandler, screenKey?: string): boolean => { const registerTransactionsHandler = useCallback((handler: MenuActionHandler, screenKey?: string): boolean => {
if (typeof handler !== 'function') return false; if (typeof handler !== 'function') return false;
const key = screenKey || navigationRef.current?.getCurrentRoute()?.name; const key = screenKey || navigationRef.current?.getCurrentRoute()?.name;
if (!key) return false; if (!key) return false;
mountedComponents.add(key); mountedComponents.add(key);
handlerRegistry.set(key, handler); handlerRegistry.set(key, handler);
return true; return true;
}, []); }, []);
const unregisterTransactionsHandler = useCallback((screenKey: string): void => { const unregisterTransactionsHandler = useCallback((screenKey: string): void => {
if (!screenKey) return; if (!screenKey) return;
handlerRegistry.delete(screenKey); handlerRegistry.delete(screenKey);
mountedComponents.delete(screenKey); mountedComponents.delete(screenKey);
}, []); }, []);
return { return {
registerTransactionsHandler, registerTransactionsHandler,
unregisterTransactionsHandler, unregisterTransactionsHandler,