You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			30 lines
		
	
	
		
			950 B
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			30 lines
		
	
	
		
			950 B
		
	
	
	
		
			TypeScript
		
	
| import { useEffect } from 'react';
 | |
| import { useDispatch, useSelector } from 'react-redux';
 | |
| import { setIsAppFocused } from '../state/ducks/section';
 | |
| import { getIsAppFocused } from '../state/selectors/section';
 | |
| import { ipcRenderer } from 'electron';
 | |
| 
 | |
| /**
 | |
|  * This custom hook should be called on the top of the app only once.
 | |
|  * It sets up a listener for events from main_node.ts and update the global redux state with the focused state.
 | |
|  */
 | |
| export function useAppIsFocused() {
 | |
|   const dispatch = useDispatch();
 | |
|   const isFocusedFromStore = useSelector(getIsAppFocused);
 | |
| 
 | |
|   const ipcCallback = (_event: unknown, isFocused: unknown) => {
 | |
|     if (isFocusedFromStore !== isFocused) {
 | |
|       dispatch(setIsAppFocused(Boolean(isFocused)));
 | |
|     }
 | |
|   };
 | |
| 
 | |
|   useEffect(() => {
 | |
|     ipcRenderer.on('set-window-focus', ipcCallback);
 | |
|     return () => {
 | |
|       ipcRenderer.removeListener('set-window-focus', ipcCallback);
 | |
|     };
 | |
|   });
 | |
| 
 | |
|   return isFocusedFromStore;
 | |
| }
 |