1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00
joplin/packages/app-desktop/gui/WindowCommandsAndDialogs/utils/useSyncDialogState.ts
Henry Heino 4a88d6ff7a
Desktop: Multiple window support (#11181)
Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
2024-11-08 15:32:05 +00:00

43 lines
1.6 KiB
TypeScript

import { useEffect, useRef } from 'react';
import { DialogState } from '../types';
import { Dispatch } from 'redux';
// Syncs whether dialogs are open/closed with the global reducer state.
const useSyncDialogState = (dialogState: DialogState, dispatch: Dispatch) => {
const lastDialogStateRef = useRef(dialogState);
useEffect(() => {
const prevState = lastDialogStateRef.current;
const state = dialogState;
if (state.notePropertiesDialogOptions !== prevState.notePropertiesDialogOptions) {
dispatch({
type: state.notePropertiesDialogOptions && state.notePropertiesDialogOptions.visible ? 'VISIBLE_DIALOGS_ADD' : 'VISIBLE_DIALOGS_REMOVE',
name: 'noteProperties',
});
}
if (state.noteContentPropertiesDialogOptions !== prevState.noteContentPropertiesDialogOptions) {
dispatch({
type: state.noteContentPropertiesDialogOptions && state.noteContentPropertiesDialogOptions.visible ? 'VISIBLE_DIALOGS_ADD' : 'VISIBLE_DIALOGS_REMOVE',
name: 'noteContentProperties',
});
}
if (state.shareNoteDialogOptions !== prevState.shareNoteDialogOptions) {
dispatch({
type: state.shareNoteDialogOptions && state.shareNoteDialogOptions.visible ? 'VISIBLE_DIALOGS_ADD' : 'VISIBLE_DIALOGS_REMOVE',
name: 'shareNote',
});
}
if (state.shareFolderDialogOptions !== prevState.shareFolderDialogOptions) {
dispatch({
type: state.shareFolderDialogOptions && state.shareFolderDialogOptions.visible ? 'VISIBLE_DIALOGS_ADD' : 'VISIBLE_DIALOGS_REMOVE',
name: 'shareFolder',
});
}
lastDialogStateRef.current = dialogState;
}, [dialogState, dispatch]);
};
export default useSyncDialogState;