2024-05-20 11:54:13 +02:00
|
|
|
import { CommonActions } from '@react-navigation/native';
|
2024-05-31 19:18:01 +02:00
|
|
|
import { useCallback, useEffect } from 'react';
|
2024-04-21 03:46:48 +02:00
|
|
|
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
|
2024-05-20 11:54:13 +02:00
|
|
|
import * as NavigationService from '../NavigationService';
|
2024-05-31 19:18:01 +02:00
|
|
|
import { useStorage } from '../hooks/context/useStorage';
|
2024-04-21 03:46:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Component for iPadOS and macOS menu items with keyboard shortcuts.
|
|
|
|
EventEmitter on the native side should receive a payload and rebuild menus.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const eventEmitter = Platform.OS === 'ios' || Platform.OS === 'macos' ? new NativeEventEmitter(NativeModules.EventEmitter) : undefined;
|
|
|
|
const MenuElements = () => {
|
2024-05-31 17:52:29 +02:00
|
|
|
const { walletsInitialized, reloadTransactionsMenuActionFunction } = useStorage();
|
2024-04-21 03:46:48 +02:00
|
|
|
|
|
|
|
// BlueWallet -> Settings
|
|
|
|
const openSettings = useCallback(() => {
|
|
|
|
dispatchNavigate('Settings');
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// File -> Add Wallet
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
const reloadTransactionsMenuElementsFunction = useCallback(() => {
|
2024-04-22 02:20:28 +02:00
|
|
|
if (reloadTransactionsMenuActionFunction && typeof reloadTransactionsMenuActionFunction === 'function') {
|
|
|
|
reloadTransactionsMenuActionFunction();
|
|
|
|
}
|
2024-09-04 02:02:29 +02:00
|
|
|
}, [reloadTransactionsMenuActionFunction]);
|
2024-04-21 03:46:48 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2024-09-04 02:02:29 +02:00
|
|
|
console.debug('MenuElements: useEffect');
|
2024-04-21 03:46:48 +02:00
|
|
|
if (walletsInitialized) {
|
|
|
|
eventEmitter?.addListener('openSettings', openSettings);
|
|
|
|
eventEmitter?.addListener('addWalletMenuAction', addWalletMenuAction);
|
|
|
|
eventEmitter?.addListener('importWalletMenuAction', importWalletMenuAction);
|
|
|
|
eventEmitter?.addListener('reloadTransactionsMenuAction', reloadTransactionsMenuElementsFunction);
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
eventEmitter?.removeAllListeners('openSettings');
|
|
|
|
eventEmitter?.removeAllListeners('addWalletMenuAction');
|
|
|
|
eventEmitter?.removeAllListeners('importWalletMenuAction');
|
|
|
|
eventEmitter?.removeAllListeners('reloadTransactionsMenuAction');
|
|
|
|
};
|
2024-09-04 02:02:29 +02:00
|
|
|
}, [addWalletMenuAction, importWalletMenuAction, openSettings, reloadTransactionsMenuElementsFunction, walletsInitialized]);
|
2024-04-21 03:46:48 +02:00
|
|
|
|
2024-05-18 04:51:58 +02:00
|
|
|
return null;
|
2024-04-21 03:46:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default MenuElements;
|