import { useCallback , useState } from 'react' ; import { i18n } from '@kbn/i18n' ; import { HttpSetup , MountPoint } from 'kibana/public' ; import { useKibana } from '../../../../utils/kibana_react' ; import { Case , SubCase } from '../../../../../../cases/common' ; import { TypedLensByValueInput } from '../../../../../../lens/public' ; import { AddToCaseProps } from '../header/add_to_case_action' ; import { observabilityFeatureId } from '../../../../../common' ; import { CasesDeepLinkId } from '../../../../../../cases/public' ; async function addToCase ( http , theCase , attributes , timeRange ? ) { const apiPath = `/api/cases/${ theCase ?. id }/comments` ; const vizPayload = { attributes , timeRange , } ; const payload = { comment : `!{lens${ JSON . stringify ( vizPayload ) }}` , type : 'user' , owner : observabilityFeatureId , } ; return http . post ( apiPath , { body : JSON . stringify ( payload ) } ) ; } export const useAddToCase = ( { lensAttributes , getToastText , timeRange , } ) => { const [ isSaving , setIsSaving ] = useState ( false ) ; const [ isCasesOpen , setIsCasesOpen ] = useState ( false ) ; const { http , application : { navigateToApp } , notifications : { toasts } , } = useKibana ( ) . services ; const onCaseClicked = useCallback ( ( theCase ? ) => { if ( theCase && lensAttributes ) { setIsCasesOpen ( false ) ; setIsSaving ( true ) ; addToCase ( http , theCase , lensAttributes , timeRange ) . then ( ( ) => { setIsSaving ( false ) ; toasts . addSuccess ( { title : i18n . translate ( 'xpack.observability.expView.heading.addToCase.notification' , { defaultMessage : 'Successfully added visualization to the case: {caseTitle}' , values : { caseTitle : theCase . title } , } ) , text : getToastText ( theCase ) , } , { toastLifeTimeMs : 10000 , } ) ; } , ( error ) => { toasts . addError ( error , { title : i18n . translate ( 'xpack.observability.expView.heading.addToCase.notification.error' , { defaultMessage : 'Failed to add visualization to the selected case.' , } ) , } ) ; } ) ; } else { navigateToApp ( observabilityFeatureId , { deepLinkId : CasesDeepLinkId . casesCreate , openInNewTab : true , } ) ; } } , [ getToastText , http , lensAttributes , navigateToApp , timeRange , toasts ] ) ; return { onCaseClicked , isSaving , isCasesOpen , setIsCasesOpen , } ; } ;