import { useEffect , useRef , useState } from 'react' ; import type { ExceptionListItemSchema , Pagination , UseExceptionListProps , FilterExceptionsOptions , } from '@kbn/securitysolution-io-ts-list-types' ; import { fetchExceptionListsItemsByListIds } from '@kbn/securitysolution-list-api' ; import { getIdsAndNamespaces } from '@kbn/securitysolution-list-utils' ; import { transformInput } from '../transforms' ; type Func = ( ) => void ; export type ReturnExceptionListAndItems = [ boolean , ExceptionListItemSchema [ ] , Pagination , Func | null ] ; param http param lists param onError param onSuccess param filterOptions param showDetectionsListsOnly param showEndpointListsOnly param matchFilters param pagination export const useExceptionListItems = ( { http , lists , pagination = { page : 1 , perPage : 20 , total : 0 , } , filterOptions , showDetectionsListsOnly , showEndpointListsOnly , matchFilters , onError , onSuccess , } ) : ReturnExceptionListAndItems => { const [ exceptionItems , setExceptionListItems ] = useState < ExceptionListItemSchema [ ] > ( [ ] ) ; const [ paginationInfo , setPagination ] = useState < Pagination > ( pagination ) ; const fetchExceptionListsItems = useRef < Func | null > ( null ) ; const [ loading , setLoading ] = useState ( true ) ; const { ids , namespaces } = getIdsAndNamespaces ( { lists , showDetection : showDetectionsListsOnly , showEndpoint : showEndpointListsOnly , } ) ; const filters = matchFilters && filterOptions . length > 0 ? ids . map ( ( ) => filterOptions [ 0 ] ) : filterOptions ; const idsAsString = ids . join ( ',' ) ; const namespacesAsString = namespaces . join ( ',' ) ; const filterAsString = filterOptions . map ( ( { filter } ) => filter ) . join ( ',' ) ; const filterTagsAsString = filterOptions . map ( ( { tags } ) => tags . join ( ',' ) ) . join ( ',' ) ; useEffect ( ( ) => { let isSubscribed = true ; const abortCtrl = new AbortController ( ) ; const fetchData = async ( ) : Promise < void > => { try { setLoading ( true ) ; if ( ids . length === 0 && isSubscribed ) { setPagination ( { page : 0 , perPage : pagination . perPage , total : 0 , } ) ; setExceptionListItems ( [ ] ) ; if ( onSuccess != null ) { onSuccess ( { exceptions : [ ] , pagination : { page : 0 , perPage : pagination . perPage , total : 0 , } , } ) ; } setLoading ( false ) ; } else { const { page , per_page : perPage , total , data , } = await fetchExceptionListsItemsByListIds ( { filterOptions : filters , http , listIds : ids , namespaceTypes : namespaces , pagination : { page : pagination . page , perPage : pagination . perPage , } , signal : abortCtrl . signal , } ) ; const transformedData = data . map ( ( item ) => transformInput ( item ) ) ; if ( isSubscribed ) { setPagination ( { page , perPage , total , } ) ; setExceptionListItems ( transformedData ) ; if ( onSuccess != null ) { onSuccess ( { exceptions : transformedData , pagination : { page , perPage , total , } , } ) ; } } } } catch ( error ) { if ( isSubscribed ) { setExceptionListItems ( [ ] ) ; setPagination ( { page : 1 , perPage : 20 , total : 0 , } ) ; if ( onError != null ) { onError ( error ) ; } } } if ( isSubscribed ) { setLoading ( false ) ; } } ; fetchData ( ) ; fetchExceptionListsItems . current = fetchData ; return ( ) : void => { isSubscribed = false ; abortCtrl . abort ( ) ; } ; } , [ http , idsAsString , namespacesAsString , setExceptionListItems , pagination . page , pagination . perPage , filterAsString , filterTagsAsString , ] ) ; return [ loading , exceptionItems , paginationInfo , fetchExceptionListsItems . current ] ; } ;