import { EuiGlobalToastListToast as EuiToast } from '@elastic/eui' ; import React from 'react' ; import * as Rx from 'rxjs' ; import { ErrorToast } from './error_toast' ; import { MountPoint } from '../../types' ; import { mountReactNode } from '../../utils' ; import { IUiSettingsClient } from '../../ui_settings' ; import { OverlayStart } from '../../overlays' ; import { I18nStart } from '../../i18n' ; /**
 * Allowed fields for  ToastInput .
 *
 *  remarks public export type ToastInputFields = Pick < EuiToast , Exclude < keyof EuiToast , 'id' | 'text' | 'title' > > & { title ? : string | MountPoint ; text ? : string | MountPoint ; } ; export type Toast = ToastInputFields & { id : string ; } ; /**
 * Inputs for  IToasts APIs.
 *  public export type ToastInput = string | ToastInputFields ; /**
 * Options available for  IToasts APIs.
 *  public export interface ToastOptions { /**
     * How long should the toast remain on screen.
     */ toastLifeTimeMs ? : number ; } /**
 * Options available for  IToasts error APIs.
 *  public export interface ErrorToastOptions extends ToastOptions { /**
     * The title of the toast and the dialog when expanding the message.
     */ title : string ; /**
     * The message to be shown in the toast. If this is not specified the error's
     * message will be shown in the toast instead. Overwriting that message can
     * be used to provide more user-friendly toasts. If you specify this, the error
     * message will still be shown in the detailed error modal.
     */ toastMessage ? : string ; } const normalizeToast = ( toastOrTitle ) : ToastInputFields => { if ( typeof toastOrTitle === 'string' ) { return { title : toastOrTitle , } ; } return toastOrTitle ; } ; /**
 * Methods for adding and removing global toast messages. See  ToastsApi .
 *  public export type IToasts = Pick < ToastsApi , 'get$' | 'add' | 'remove' | 'addSuccess' | 'addWarning' | 'addDanger' | 'addError' | 'addInfo' > ; public export class ToastsApi implements IToasts { private toasts$ = new Rx . BehaviorSubject < Toast [ ] > ( [ ] ) ; private idCounter = 0 ; private uiSettings : IUiSettingsClient ; private overlays ? : OverlayStart ; private i18n ? : I18nStart ; constructor ( deps ) { this . uiSettings = deps . uiSettings ; } internal public start ( { overlays , i18n } ) { this . overlays = overlays ; this . i18n = i18n ; } /** Observable of the toast messages to show to the user. */ public get$ ( ) { return this . toasts$ . asObservable ( ) ; } param toastOrTitle - a  ToastInput returns a  Toast public add ( toastOrTitle ) { const toast = { id : String ( this . idCounter ++ ) , toastLifeTimeMs : this . uiSettings . get ( 'notifications:lifetime:info' ) , ... normalizeToast ( toastOrTitle ) , } ; this . toasts$ . next ( [ ... this . toasts$ . getValue ( ) , toast ] ) ; return toast ; } param toastOrId - a  Toast returned by  ToastsApi . add or its id
      public remove ( toastOrId ) { const toRemove = typeof toastOrId === 'string' ? toastOrId : toastOrId . id ; const list = this . toasts$ . getValue ( ) ; const listWithoutToast = list . filter ( ( t ) => t . id !== toRemove ) ; if ( listWithoutToast . length !== list . length ) { this . toasts$ . next ( listWithoutToast ) ; } } param toastOrTitle - a  ToastInput param options - a  ToastOptions returns a  Toast public addInfo ( toastOrTitle , options ? ) { return this . add ( { color : 'primary' , iconType : 'iInCircle' , ... normalizeToast ( toastOrTitle ) , ... options , } ) ; } param toastOrTitle - a  ToastInput param options - a  ToastOptions returns a  Toast public addSuccess ( toastOrTitle , options ? ) { return this . add ( { color : 'success' , iconType : 'check' , ... normalizeToast ( toastOrTitle ) , ... options , } ) ; } param toastOrTitle - a  ToastInput param options - a  ToastOptions returns a  Toast public addWarning ( toastOrTitle , options ? ) { return this . add ( { color : 'warning' , iconType : 'help' , toastLifeTimeMs : this . uiSettings . get ( 'notifications:lifetime:warning' ) , ... normalizeToast ( toastOrTitle ) , ... options , } ) ; } param toastOrTitle - a  ToastInput param options - a  ToastOptions returns a  Toast public addDanger ( toastOrTitle , options ? ) { return this . add ( { color : 'danger' , iconType : 'alert' , toastLifeTimeMs : this . uiSettings . get ( 'notifications:lifetime:warning' ) , ... normalizeToast ( toastOrTitle ) , ... options , } ) ; } param error param options -  ErrorToastOptions returns a  Toast public addError ( error , options ) { const message = options . toastMessage || error . message ; return this . add ( { color : 'danger' , iconType : 'alert' , toastLifeTimeMs : this . uiSettings . get ( 'notifications:lifetime:error' ) , text : mountReactNode ( < ErrorToast openModal = { this . openModal . bind ( this ) } error = { error } title = { options . title } toastMessage = { message } i18nContext = { ( ) => this . i18n ! . Context } / > ) , ... options , } ) ; } private openModal ( ... args ) { if ( ! this . overlays ) { throw new Error ( `Modal opened before ToastService was started.` ) ; } return this . overlays . openModal ( ... args ) ; } }