BlueWallet/components/MenuElements.tsx

69 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-01-18 20:28:06 -04:00
import React, { useCallback, useContext, useEffect } from 'react';
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
import * as NavigationService from '../NavigationService';
import { CommonActions } from '@react-navigation/native';
import { BlueStorageContext } from '../blue_modules/storage-context';
2024-01-19 11:28:03 -04:00
/*
Component for iPadOS and macOS menu items with keyboard shortcuts.
EventEmitter on the native side should receive a payload and rebuild menus.
*/
2024-01-18 20:28:06 -04:00
2024-01-19 11:28:03 -04:00
const eventEmitter = Platform.OS === 'ios' || Platform.OS === 'macos' ? new NativeEventEmitter(NativeModules.EventEmitter) : undefined;
2024-01-18 20:28:06 -04:00
const MenuElements = () => {
const { walletsInitialized, reloadTransactionsMenuActionFunction } = useContext(BlueStorageContext);
2024-01-18 20:28:06 -04:00
2024-01-19 11:28:03 -04:00
// BlueWallet -> Settings
2024-01-18 20:28:06 -04:00
const openSettings = useCallback(() => {
dispatchNavigate('Settings');
}, []);
2024-01-19 11:28:03 -04:00
// File -> Add Wallet
2024-01-18 20:28:06 -04:00
const addWalletMenuAction = useCallback(() => {
dispatchNavigate('AddWalletRoot');
}, []);
// File -> Add Wallet
const importWalletMenuAction = useCallback(() => {
dispatchNavigate('AddWalletRoot', 'ImportWallet');
}, []);
const dispatchNavigate = (routeName: string, screen?: string) => {
const action = screen
? CommonActions.navigate({
name: routeName,
params: { screen },
})
: CommonActions.navigate({
name: routeName,
});
NavigationService.dispatch(action);
2024-01-18 20:28:06 -04:00
};
const reloadTransactionsMenuElementsFunction = useCallback(() => {
reloadTransactionsMenuActionFunction();
2024-02-16 10:13:52 -04:00
}, [reloadTransactionsMenuActionFunction]);
2024-01-18 20:28:06 -04:00
useEffect(() => {
console.log('MenuElements: useEffect');
if (walletsInitialized) {
eventEmitter?.addListener('openSettings', openSettings);
eventEmitter?.addListener('addWalletMenuAction', addWalletMenuAction);
eventEmitter?.addListener('importWalletMenuAction', importWalletMenuAction);
eventEmitter?.addListener('reloadTransactionsMenuAction', reloadTransactionsMenuElementsFunction);
2024-01-18 20:28:06 -04:00
}
return () => {
eventEmitter?.removeAllListeners('openSettings');
eventEmitter?.removeAllListeners('addWalletMenuAction');
eventEmitter?.removeAllListeners('importWalletMenuAction');
eventEmitter?.removeAllListeners('reloadTransactionsMenuAction');
2024-01-18 20:28:06 -04:00
};
2024-02-15 18:35:21 -04:00
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [addWalletMenuAction, importWalletMenuAction, openSettings, reloadTransactionsMenuActionFunction, walletsInitialized]);
2024-01-18 20:28:06 -04:00
return <></>;
};
export default MenuElements;