import { useQuery } from 'react-query' ; import { PLUGIN_ID } from '../../common' ; import { useKibana } from '../common/lib/kibana' ; import { pagePathGetters } from '../common/page_paths' ; import { useErrorToast } from '../common/hooks/use_error_toast' ; import { SAVED_QUERY_ID } from './constants' ; interface UseSavedQueryProps { savedQueryId : string ; } export const useSavedQuery = ( { savedQueryId } ) => { const { application : { navigateToApp } , http , } = useKibana ( ) . services ; const setErrorToast = useErrorToast ( ) ; return useQuery ( [ SAVED_QUERY_ID , { savedQueryId } ] , ( ) => http . get < any > ( `/internal/osquery/saved_query/${ savedQueryId }` ) , { keepPreviousData : true , refetchOnWindowFocus : false , onSuccess : ( data ) => { if ( data . error ) { setErrorToast ( data . error , { title : data . error . error , toastMessage : data . error . message , } ) ; navigateToApp ( PLUGIN_ID , { path : pagePathGetters . saved_queries ( ) } ) ; } } , onError : ( error ) => { setErrorToast ( error , { title : error . body . error , toastMessage : error . body . message , } ) ; } , } ) ; } ;