Skip to content

Commit 2c356fc

Browse files
Fix bug where user can't add an exception when "close alert" is checked (#72919) (#72991)
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 5ff15b2 commit 2c356fc

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

x-pack/plugins/security_solution/public/common/components/exceptions/use_add_exception.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ describe('useAddOrUpdateException', () => {
144144
await act(async () => {
145145
const { result, waitForNextUpdate } = render();
146146
await waitForNextUpdate();
147-
expect(result.current).toEqual([{ isLoading: false }, null]);
147+
expect(result.current).toEqual([{ isLoading: false }, result.current[1]]);
148148
});
149149
});
150150

x-pack/plugins/security_solution/public/common/components/exceptions/use_add_exception.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { useEffect, useRef, useState } from 'react';
7+
import { useEffect, useRef, useState, useCallback } from 'react';
88
import { HttpStart } from '../../../../../../../src/core/public';
99

1010
import {
@@ -60,7 +60,19 @@ export const useAddOrUpdateException = ({
6060
onSuccess,
6161
}: UseAddOrUpdateExceptionProps): ReturnUseAddOrUpdateException => {
6262
const [isLoading, setIsLoading] = useState(false);
63-
const addOrUpdateException = useRef<AddOrUpdateExceptionItemsFunc | null>(null);
63+
const addOrUpdateExceptionRef = useRef<AddOrUpdateExceptionItemsFunc | null>(null);
64+
const addOrUpdateException = useCallback<AddOrUpdateExceptionItemsFunc>(
65+
async (exceptionItemsToAddOrUpdate, alertIdToClose, bulkCloseIndex) => {
66+
if (addOrUpdateExceptionRef.current !== null) {
67+
addOrUpdateExceptionRef.current(
68+
exceptionItemsToAddOrUpdate,
69+
alertIdToClose,
70+
bulkCloseIndex
71+
);
72+
}
73+
},
74+
[]
75+
);
6476

6577
useEffect(() => {
6678
let isSubscribed = true;
@@ -114,6 +126,7 @@ export const useAddOrUpdateException = ({
114126
await updateAlertStatus({
115127
query: getUpdateAlertsQuery([alertIdToClose]),
116128
status: 'closed',
129+
signal: abortCtrl.signal,
117130
});
118131
}
119132

@@ -131,6 +144,7 @@ export const useAddOrUpdateException = ({
131144
query: filter,
132145
},
133146
status: 'closed',
147+
signal: abortCtrl.signal,
134148
});
135149
}
136150

@@ -148,12 +162,12 @@ export const useAddOrUpdateException = ({
148162
}
149163
};
150164

151-
addOrUpdateException.current = addOrUpdateExceptionItems;
165+
addOrUpdateExceptionRef.current = addOrUpdateExceptionItems;
152166
return (): void => {
153167
isSubscribed = false;
154168
abortCtrl.abort();
155169
};
156170
}, [http, onSuccess, onError]);
157171

158-
return [{ isLoading }, addOrUpdateException.current];
172+
return [{ isLoading }, addOrUpdateException];
159173
};

0 commit comments

Comments
 (0)