import { observable } from 'mobx' ; import { injectable } from '@cloudbeaver/core-di' ; export interface DialogOptions { persistent ? : boolean ; } export interface DialogComponentProps < TPayload , TResult = DialogueStateResult > { visible : boolean ; payload : TPayload ; options ? : DialogOptions ; resolveDialog : ( result ? : TResult ) => void ; rejectDialog : ( ) => void ; className ? : string ; } export type DialogComponent < TPayload , TResult = DialogueStateResult > = React . FC < DialogComponentProps < TPayload , TResult > > ; export enum DialogueStateResult { Resolved , Rejected } export interface DialogInternal < TResult > { component : DialogComponent < any , any > ; payload : any ; options ? : DialogOptions ; resolve : ( result : TResult | DialogueStateResult ) => void ; promise : Promise < TResult | DialogueStateResult > ; } @ injectable ( ) export class CommonDialogService { dialogs : Array < DialogInternal < any > > = observable ( [ ] , { deep : false } ) ; open < TPayload , TResult > ( component , payload , options ? ) { let _resolve ; let _reject ; const promise = new Promise < TResult | DialogueStateResult > ( ( resolve , reject ) => { _resolve = resolve ; _reject = reject ; } ) ; const dialogInternal = { component , payload , resolve : _resolve ! , options , promise , } ; this . dialogs . push ( dialogInternal ) ; return promise ; } rejectDialog ( promise ) { const dialog = this . dialogs . find ( internal => internal . promise === promise ) ; if ( dialog ) { dialog . resolve ( DialogueStateResult . Rejected ) ; this . removeDialog ( dialog ) ; } } resolveDialog < TResult > ( promise , result ? ) { const dialog = this . dialogs . find ( internal => internal . promise === promise ) ; if ( dialog ) { dialog . resolve ( result ?? DialogueStateResult . Resolved ) ; this . removeDialog ( dialog ) ; } } private removeDialog ( dialog ) { const index = this . dialogs . findIndex ( i => i === dialog ) ; if ( index !== - 1 ) { this . dialogs . splice ( index , 1 ) ; } } }