import { atom , useRecoilCallback } from 'recoil' ; import { ContextMenuCommand } from '../types/ContextMenuCommand' ; export const contextMenuIsOpeningState = atom ( { key : 'contextMenuIsOpeningState' , default : false } ) ; export const contextMenuPositionState = atom ( { key : 'contextMenuPositionState' , default : { x : 0 , y : 0 } } ) ; export const contextMenuCommandsState = atom ( { key : 'contextMenuCommandsState' , default : [ ] as Array < ContextMenuCommand | 'hr' > } ) ; export function useOpenContextMenuAction ( ) { return useRecoilCallback ( async ( { getPromise , set } , { x , y , commands : addCommands } ) => { const commands = await getPromise ( contextMenuCommandsState ) ; const newCommands = [ ] ; if ( commands . length !== 0 ) { newCommands . push ( ... commands ) ; newCommands . push ( 'hr' ) ; } newCommands . push ( ... addCommands ) ; set ( contextMenuIsOpeningState , true ) ; set ( contextMenuPositionState , { x , y } ) ; set ( contextMenuCommandsState , newCommands ) ; } , [ ] ) ; } export function useResetContextMenuAction ( ) { return useRecoilCallback ( async ( { reset } ) => { reset ( contextMenuIsOpeningState ) ; reset ( contextMenuPositionState ) ; reset ( contextMenuCommandsState ) ; } ) ; }