mirror of
https://github.com/lux-tel/session-desktop.git
synced 2026-10-30 20:33:24 +00:00
31 lines
1006 B
TypeScript
31 lines
1006 B
TypeScript
import { ipcRenderer } from 'electron';
|
|
import { useEffect } from 'react';
|
|
import { useSelector } from 'react-redux';
|
|
import { getAppDispatch } from '../state/dispatch';
|
|
import { getIsAppFocused } from '../state/selectors/section';
|
|
import { sectionActions } from '../state/ducks/section';
|
|
|
|
/**
|
|
* 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 = getAppDispatch();
|
|
const isFocusedFromStore = useSelector(getIsAppFocused);
|
|
|
|
const ipcCallback = (_event: unknown, isFocused: unknown) => {
|
|
if (isFocusedFromStore !== isFocused) {
|
|
dispatch(sectionActions.setIsAppFocused(Boolean(isFocused)));
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
ipcRenderer.on('set-window-focus', ipcCallback);
|
|
return () => {
|
|
ipcRenderer.removeListener('set-window-focus', ipcCallback);
|
|
};
|
|
});
|
|
|
|
return isFocusedFromStore;
|
|
}
|