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
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

32 lines
993 B
TypeScript

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;