55 * 2.0.
66 */
77
8- import type { CoreStart } from '@kbn/core/public' ;
9- import { useKibana } from '@kbn/kibana-react-plugin/public' ;
108import { useCallback , useEffect , useRef } from 'react' ;
119import type { AlertAssignees } from '../../../../../common/api/detection_engine' ;
1210import { useAppToasts } from '../../../hooks/use_app_toasts' ;
@@ -28,14 +26,13 @@ export type ReturnSetAlertAssignees = SetAlertAssigneesFunc | null;
2826 * @param ids alert ids that will be used to create the update query.
2927 * @param onSuccess a callback function that will be called on successful api response
3028 * @param setTableLoading a function that sets the alert table in a loading state for bulk actions
31-
3229 *
3330 * @throws An error if response is not OK
3431 */
3532export const useSetAlertAssignees = ( ) : ReturnSetAlertAssignees => {
36- const { http } = useKibana < CoreStart > ( ) . services ;
3733 const { addSuccess, addError } = useAppToasts ( ) ;
38- const setAlertAssigneesRef = useRef < SetAlertAssigneesFunc | null > ( null ) ;
34+
35+ const abortCtrl = useRef < AbortController > ( new AbortController ( ) ) ;
3936
4037 const onUpdateSuccess = useCallback (
4138 ( updated : number = 0 ) => addSuccess ( i18n . UPDATE_ALERT_ASSIGNEES_SUCCESS_TOAST ( updated ) ) ,
@@ -49,38 +46,32 @@ export const useSetAlertAssignees = (): ReturnSetAlertAssignees => {
4946 [ addError ]
5047 ) ;
5148
52- useEffect ( ( ) => {
53- let ignore = false ;
54- const abortCtrl = new AbortController ( ) ;
55-
56- const onSetAlertAssignees : SetAlertAssigneesFunc = async (
57- assignees ,
58- ids ,
59- onSuccess ,
60- setTableLoading
61- ) => {
49+ const onSetAlertAssignees : SetAlertAssigneesFunc = useCallback (
50+ async ( assignees , ids , onSuccess , setTableLoading ) => {
6251 try {
6352 setTableLoading ( true ) ;
64- const response = await setAlertAssignees ( { assignees, ids, signal : abortCtrl . signal } ) ;
65- if ( ! ignore ) {
66- onSuccess ( ) ;
67- setTableLoading ( false ) ;
68- onUpdateSuccess ( response . updated ) ;
69- }
53+ const response = await setAlertAssignees ( {
54+ assignees,
55+ ids,
56+ signal : abortCtrl . current . signal ,
57+ } ) ;
58+ onSuccess ( ) ;
59+ setTableLoading ( false ) ;
60+ onUpdateSuccess ( response . updated ) ;
7061 } catch ( error ) {
71- if ( ! ignore ) {
72- setTableLoading ( false ) ;
73- onUpdateFailure ( error ) ;
74- }
62+ setTableLoading ( false ) ;
63+ onUpdateFailure ( error ) ;
7564 }
76- } ;
65+ } ,
66+ [ onUpdateFailure , onUpdateSuccess ]
67+ ) ;
7768
78- setAlertAssigneesRef . current = onSetAlertAssignees ;
69+ useEffect ( ( ) => {
70+ const currentAbortCtrl = abortCtrl . current ;
7971 return ( ) : void => {
80- ignore = true ;
81- abortCtrl . abort ( ) ;
72+ currentAbortCtrl . abort ( ) ;
8273 } ;
83- } , [ http , onUpdateFailure , onUpdateSuccess ] ) ;
74+ } , [ abortCtrl ] ) ;
8475
85- return setAlertAssigneesRef . current ;
76+ return onSetAlertAssignees ;
8677} ;
0 commit comments