Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const Table = memo(({ dataView, id, packages, query, ruleResponse }: Tabl
gridStyle={GRID_STYLE}
id={id}
query={query}
ref={refetchRef}
renderActionsCell={ActionsCell}
renderCellValue={CellValue}
rowHeightsOptions={ROW_HEIGHTS_OPTIONS}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const Table = memo(
id={id}
onLoaded={onLoaded}
query={query}
ref={refetchRef}
renderActionsCell={ActionsCell}
renderCellValue={CellValue}
rowHeightsOptions={ROW_HEIGHTS_OPTIONS}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { renderHook } from '@testing-library/react';
import { useSetAlertTags } from './use_set_alert_tags';
import { useAppToasts } from '../../../hooks/use_app_toasts';

jest.mock('../../../hooks/use_app_toasts');

describe('useSetAlertTags', () => {
it('should return a function', () => {
(useAppToasts as jest.Mock).mockReturnValue({
addSuccess: jest.fn(),
addError: jest.fn(),
});

const { result } = renderHook(() => useSetAlertTags());

expect(typeof result.current).toEqual('function');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* 2.0.
*/

import type { CoreStart } from '@kbn/core/public';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { useCallback, useEffect, useRef } from 'react';
import type { AlertTags } from '../../../../../common/api/detection_engine';
import { useAppToasts } from '../../../hooks/use_app_toasts';
Expand All @@ -33,9 +31,9 @@ export type ReturnSetAlertTags = SetAlertTagsFunc | null;
* @throws An error if response is not OK
*/
export const useSetAlertTags = (): ReturnSetAlertTags => {
const { http } = useKibana<CoreStart>().services;
const { addSuccess, addError } = useAppToasts();
const setAlertTagsRef = useRef<SetAlertTagsFunc | null>(null);

const abortCtrl = useRef<AbortController>(new AbortController());

const onUpdateSuccess = useCallback(
(updated: number = 0) => addSuccess(i18n.UPDATE_ALERT_TAGS_SUCCESS_TOAST(updated)),
Expand All @@ -49,33 +47,28 @@ export const useSetAlertTags = (): ReturnSetAlertTags => {
[addError]
);

useEffect(() => {
let ignore = false;
const abortCtrl = new AbortController();

const onSetAlertTags: SetAlertTagsFunc = async (tags, ids, onSuccess, setTableLoading) => {
const onSetAlertTags: SetAlertTagsFunc = useCallback(
async (tags, ids, onSuccess, setTableLoading) => {
try {
setTableLoading(true);
const response = await setAlertTags({ tags, ids, signal: abortCtrl.signal });
if (!ignore) {
onSuccess();
setTableLoading(false);
onUpdateSuccess(response.updated);
}
const response = await setAlertTags({ tags, ids, signal: abortCtrl.current.signal });
onSuccess();
setTableLoading(false);
onUpdateSuccess(response.updated);
} catch (error) {
if (!ignore) {
setTableLoading(false);
onUpdateFailure(error);
}
setTableLoading(false);
onUpdateFailure(error);
}
};
},
[onUpdateFailure, onUpdateSuccess]
);

setAlertTagsRef.current = onSetAlertTags;
useEffect(() => {
const currentAbortCtrl = abortCtrl.current;
return (): void => {
ignore = true;
abortCtrl.abort();
currentAbortCtrl.abort();
};
}, [http, onUpdateFailure, onUpdateSuccess]);
}, [abortCtrl]);

return setAlertTagsRef.current;
return onSetAlertTags;
};