import { Dispatch , useEffect , useState } from 'react' ; import type { AddExceptionList , PersistHookProps , UpdateExceptionListSchema , } from '@kbn/securitysolution-io-ts-list-types' ; import { addExceptionList , updateExceptionList } from '@kbn/securitysolution-list-api' ; interface PersistReturnExceptionList { isLoading : boolean ; isSaved : boolean ; } export type ReturnPersistExceptionList = [ PersistReturnExceptionList , Dispatch < AddExceptionList | null > ] ; param http param onError export const usePersistExceptionList = ( { http , onError , } ) : ReturnPersistExceptionList => { const [ exceptionList , setExceptionList ] = useState < AddExceptionList | null > ( null ) ; const [ isSaved , setIsSaved ] = useState ( false ) ; const [ isLoading , setIsLoading ] = useState ( false ) ; const isUpdateExceptionList = ( item ) : item is UpdateExceptionListSchema => Boolean ( item && ( item as UpdateExceptionListSchema ) . id != null ) ; useEffect ( ( ) => { let isSubscribed = true ; const abortCtrl = new AbortController ( ) ; setIsSaved ( false ) ; const saveExceptionList = async ( ) : Promise < void > => { if ( exceptionList != null ) { try { setIsLoading ( true ) ; if ( isUpdateExceptionList ( exceptionList ) ) { await updateExceptionList ( { http , list : exceptionList , signal : abortCtrl . signal } ) ; } else { await addExceptionList ( { http , list : exceptionList , signal : abortCtrl . signal } ) ; } if ( isSubscribed ) { setIsSaved ( true ) ; } } catch ( error ) { if ( isSubscribed ) { onError ( error ) ; } } if ( isSubscribed ) { setIsLoading ( false ) ; } } } ; saveExceptionList ( ) ; return ( ) : void => { isSubscribed = false ; abortCtrl . abort ( ) ; } ; } , [ http , exceptionList , onError ] ) ; return [ { isLoading , isSaved } , setExceptionList ] ; } ;