Skip to content

Commit d2cb862

Browse files
[Security Solution] Fix alerts table potentially not applying alert assignees
1 parent 7b3efa8 commit d2cb862

2 files changed

Lines changed: 47 additions & 31 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { renderHook } from '@testing-library/react';
9+
import { useAppToasts } from '../../../hooks/use_app_toasts';
10+
import { useSetAlertAssignees } from './use_set_alert_assignees';
11+
12+
jest.mock('../../../hooks/use_app_toasts');
13+
14+
describe('useSetAlertAssignees', () => {
15+
it('should return a function', () => {
16+
(useAppToasts as jest.Mock).mockReturnValue({
17+
addSuccess: jest.fn(),
18+
addError: jest.fn(),
19+
});
20+
21+
const { result } = renderHook(() => useSetAlertAssignees());
22+
23+
expect(typeof result.current).toEqual('function');
24+
});
25+
});

x-pack/solutions/security/plugins/security_solution/public/common/components/toolbar/bulk_actions/use_set_alert_assignees.tsx

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* 2.0.
66
*/
77

8-
import type { CoreStart } from '@kbn/core/public';
9-
import { useKibana } from '@kbn/kibana-react-plugin/public';
108
import { useCallback, useEffect, useRef } from 'react';
119
import type { AlertAssignees } from '../../../../../common/api/detection_engine';
1210
import { 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
*/
3532
export 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

Comments
 (0)