1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-desktop/gui/WindowCommandsAndDialogs/utils/useWindowControl.ts

32 lines
993 B
TypeScript
Raw Normal View History

import * as React from 'react';
import { useMemo, useRef } from 'react';
import { DialogState } from '../types';
import { PrintCallback } from './usePrintToCallback';
export interface WindowControl {
setState: (update: Partial<DialogState>)=> void;
printTo: PrintCallback;
}
export type OnSetDialogState = React.Dispatch<React.SetStateAction<DialogState>>;
const useWindowControl = (setDialogState: OnSetDialogState, onPrint: PrintCallback) => {
// Use refs to avoid reloading the output where possible -- reloading the window control
// may mean reloading all main window commands.
const onPrintRef = useRef(onPrint);
onPrintRef.current = onPrint;
return useMemo((): WindowControl => {
return {
setState: (newPartialState: Partial<DialogState>) => {
setDialogState(oldState => ({
...oldState,
...newPartialState,
}));
},
printTo: (target, options) => onPrintRef.current(target, options),
};
}, [setDialogState]);
};
export default useWindowControl;