Skip to content

Commit

Permalink
[Security Solution] Use timelineId to tell when investigate in timeli…
Browse files Browse the repository at this point in the history
…ne is in a table (elastic#124310)

## Summary

Fixes elastic#124307 by preventing event tables from making event detail requests when in a timeline by skipping the data fetching hook when the action is in a timeline.
0 calls to the detection detail api when viewing just the table:
![image](https://user-images.githubusercontent.com/56408403/152084212-4b61fb5d-240b-4921-9842-61f3e505d7c7.png)
1 made when opening the detail flyout:
![image](https://user-images.githubusercontent.com/56408403/152084244-d94ac8f5-c336-458a-b1a2-3a2780e102f4.png)

### Checklist

- [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios

(cherry picked from commit a6c7d96)
  • Loading branch information
kqualters-elastic authored and kibanamachine committed Feb 2, 2022
1 parent 04eaaf3 commit 2482907
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface InvestigateInTimelineActionProps {
alertIds?: string[];
buttonType?: 'text' | 'icon';
onInvestigateInTimelineAlertClick?: () => void;
timelineId?: string;
}

const InvestigateInTimelineActionComponent: React.FC<InvestigateInTimelineActionProps> = ({
Expand All @@ -30,11 +31,13 @@ const InvestigateInTimelineActionComponent: React.FC<InvestigateInTimelineAction
ecsRowData,
buttonType,
onInvestigateInTimelineAlertClick,
timelineId,
}) => {
const { investigateInTimelineAlertClick } = useInvestigateInTimeline({
ecsRowData,
alertIds,
onInvestigateInTimelineAlertClick,
timelineId,
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ interface UseInvestigateInTimelineActionProps {
nonEcsRowData?: TimelineNonEcsData[];
alertIds?: string[] | null | undefined;
onInvestigateInTimelineAlertClick?: () => void;
timelineId?: string;
}

export const useInvestigateInTimeline = ({
ecsRowData,
alertIds,
onInvestigateInTimelineAlertClick,
timelineId,
}: UseInvestigateInTimelineActionProps) => {
const {
data: { search: searchStrategyClient, query },
Expand Down Expand Up @@ -78,7 +80,7 @@ export const useInvestigateInTimeline = ({
const showInvestigateInTimelineAction = alertIds != null;
const { isLoading: isFetchingAlertEcs, alertsEcsData } = useFetchEcsAlertsData({
alertIds,
skip: alertIds == null,
skip: alertIds == null || timelineId !== undefined,
});

const investigateInTimelineAlertClick = useCallback(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { useEffect, useState } from 'react';
import { useEffect, useState, useRef } from 'react';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { isEmpty } from 'lodash';

Expand All @@ -25,10 +25,11 @@ export const useFetchEcsAlertsData = ({
}): { isLoading: boolean | null; alertsEcsData: Ecs[] | null } => {
const [isLoading, setIsLoading] = useState<boolean | null>(null);
const [alertsEcsData, setAlertEcsData] = useState<Ecs[] | null>(null);
const abortCtrl = useRef(new AbortController());

useEffect(() => {
let isSubscribed = true;
const abortCtrl = new AbortController();
const controller = abortCtrl.current;

const fetchAlert = async () => {
try {
Expand Down Expand Up @@ -72,7 +73,7 @@ export const useFetchEcsAlertsData = ({

return (): void => {
isSubscribed = false;
abortCtrl.abort();
controller.abort();
};
}, [alertIds, onError, skip]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ const ActionsComponent: React.FC<ActionProps> = ({
key="investigate-in-timeline"
alertIds={alertIds}
ecsRowData={ecsData}
timelineId={timelineId}
/>
)}

Expand Down

0 comments on commit 2482907

Please sign in to comment.