mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-21 09:38:01 +02:00
4a88d6ff7a
Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
43 lines
1.6 KiB
TypeScript
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;
|