Skip to content

Commit 5add3ce

Browse files
committed
[Security Solution][Case] Improve hooks (#89580)
# Conflicts: # x-pack/plugins/security_solution/public/cases/containers/use_bulk_update_case.tsx
1 parent 20beb97 commit 5add3ce

24 files changed

Lines changed: 696 additions & 608 deletions

x-pack/plugins/security_solution/public/cases/components/connectors/jira/use_get_fields_by_issue_type.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,28 @@ export const useGetFieldsByIssueType = ({
3535
}: Props): UseGetFieldsByIssueType => {
3636
const [isLoading, setIsLoading] = useState(true);
3737
const [fields, setFields] = useState<Fields>({});
38+
const didCancel = useRef(false);
3839
const abortCtrl = useRef(new AbortController());
3940

4041
useEffect(() => {
41-
let didCancel = false;
4242
const fetchData = async () => {
4343
if (!connector || !issueType) {
4444
setIsLoading(false);
4545
return;
4646
}
4747

48-
abortCtrl.current = new AbortController();
49-
setIsLoading(true);
5048
try {
49+
abortCtrl.current = new AbortController();
50+
setIsLoading(true);
51+
5152
const res = await getFieldsByIssueType({
5253
http,
5354
signal: abortCtrl.current.signal,
5455
connectorId: connector.id,
5556
id: issueType,
5657
});
5758

58-
if (!didCancel) {
59+
if (!didCancel.current) {
5960
setIsLoading(false);
6061
setFields(res.data ?? {});
6162
if (res.status && res.status === 'error') {
@@ -66,22 +67,24 @@ export const useGetFieldsByIssueType = ({
6667
}
6768
}
6869
} catch (error) {
69-
if (!didCancel) {
70+
if (!didCancel.current) {
7071
setIsLoading(false);
71-
toastNotifications.addDanger({
72-
title: i18n.FIELDS_API_ERROR,
73-
text: error.message,
74-
});
72+
if (error.name !== 'AbortError') {
73+
toastNotifications.addDanger({
74+
title: i18n.FIELDS_API_ERROR,
75+
text: error.message,
76+
});
77+
}
7578
}
7679
}
7780
};
7881

82+
didCancel.current = false;
7983
abortCtrl.current.abort();
8084
fetchData();
8185

8286
return () => {
83-
didCancel = true;
84-
setIsLoading(false);
87+
didCancel.current = true;
8588
abortCtrl.current.abort();
8689
};
8790
}, [http, connector, issueType, toastNotifications]);

x-pack/plugins/security_solution/public/cases/components/connectors/jira/use_get_issue_types.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,27 @@ export const useGetIssueTypes = ({
3535
}: Props): UseGetIssueTypes => {
3636
const [isLoading, setIsLoading] = useState(true);
3737
const [issueTypes, setIssueTypes] = useState<IssueTypes>([]);
38+
const didCancel = useRef(false);
3839
const abortCtrl = useRef(new AbortController());
3940

4041
useEffect(() => {
41-
let didCancel = false;
4242
const fetchData = async () => {
4343
if (!connector) {
4444
setIsLoading(false);
4545
return;
4646
}
4747

48-
abortCtrl.current = new AbortController();
49-
setIsLoading(true);
50-
5148
try {
49+
abortCtrl.current = new AbortController();
50+
setIsLoading(true);
51+
5252
const res = await getIssueTypes({
5353
http,
5454
signal: abortCtrl.current.signal,
5555
connectorId: connector.id,
5656
});
5757

58-
if (!didCancel) {
58+
if (!didCancel.current) {
5959
setIsLoading(false);
6060
const asOptions = (res.data ?? []).map((type) => ({
6161
text: type.name ?? '',
@@ -71,25 +71,29 @@ export const useGetIssueTypes = ({
7171
}
7272
}
7373
} catch (error) {
74-
if (!didCancel) {
74+
if (!didCancel.current) {
7575
setIsLoading(false);
76-
toastNotifications.addDanger({
77-
title: i18n.ISSUE_TYPES_API_ERROR,
78-
text: error.message,
79-
});
76+
if (error.name !== 'AbortError') {
77+
toastNotifications.addDanger({
78+
title: i18n.ISSUE_TYPES_API_ERROR,
79+
text: error.message,
80+
});
81+
}
8082
}
8183
}
8284
};
8385

86+
didCancel.current = false;
8487
abortCtrl.current.abort();
8588
fetchData();
8689

8790
return () => {
88-
didCancel = true;
89-
setIsLoading(false);
91+
didCancel.current = true;
9092
abortCtrl.current.abort();
9193
};
92-
}, [http, connector, toastNotifications, handleIssueType]);
94+
// handleIssueType unmounts the component at init causing the request to be aborted
95+
// eslint-disable-next-line react-hooks/exhaustive-deps
96+
}, [http, connector, toastNotifications]);
9397

9498
return {
9599
issueTypes,

x-pack/plugins/security_solution/public/cases/components/connectors/jira/use_get_issues.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,28 @@ export const useGetIssues = ({
3636
}: Props): UseGetIssues => {
3737
const [isLoading, setIsLoading] = useState(false);
3838
const [issues, setIssues] = useState<Issues>([]);
39+
const didCancel = useRef(false);
3940
const abortCtrl = useRef(new AbortController());
4041

4142
useEffect(() => {
42-
let didCancel = false;
4343
const fetchData = debounce(500, async () => {
4444
if (!actionConnector || isEmpty(query)) {
4545
setIsLoading(false);
4646
return;
4747
}
4848

49-
abortCtrl.current = new AbortController();
50-
setIsLoading(true);
51-
5249
try {
50+
abortCtrl.current = new AbortController();
51+
setIsLoading(true);
52+
5353
const res = await getIssues({
5454
http,
5555
signal: abortCtrl.current.signal,
5656
connectorId: actionConnector.id,
5757
title: query ?? '',
5858
});
5959

60-
if (!didCancel) {
60+
if (!didCancel.current) {
6161
setIsLoading(false);
6262
setIssues(res.data ?? []);
6363
if (res.status && res.status === 'error') {
@@ -68,22 +68,24 @@ export const useGetIssues = ({
6868
}
6969
}
7070
} catch (error) {
71-
if (!didCancel) {
71+
if (!didCancel.current) {
7272
setIsLoading(false);
73-
toastNotifications.addDanger({
74-
title: i18n.ISSUES_API_ERROR,
75-
text: error.message,
76-
});
73+
if (error.name !== 'AbortError') {
74+
toastNotifications.addDanger({
75+
title: i18n.ISSUES_API_ERROR,
76+
text: error.message,
77+
});
78+
}
7779
}
7880
}
7981
});
8082

83+
didCancel.current = false;
8184
abortCtrl.current.abort();
8285
fetchData();
8386

8487
return () => {
85-
didCancel = true;
86-
setIsLoading(false);
88+
didCancel.current = true;
8789
abortCtrl.current.abort();
8890
};
8991
}, [http, actionConnector, toastNotifications, query]);

x-pack/plugins/security_solution/public/cases/components/connectors/jira/use_get_single_issue.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ export const useGetSingleIssue = ({
3535
}: Props): UseGetSingleIssue => {
3636
const [isLoading, setIsLoading] = useState(false);
3737
const [issue, setIssue] = useState<Issue | null>(null);
38+
const didCancel = useRef(false);
3839
const abortCtrl = useRef(new AbortController());
3940

4041
useEffect(() => {
41-
let didCancel = false;
4242
const fetchData = async () => {
4343
if (!actionConnector || !id) {
4444
setIsLoading(false);
@@ -55,7 +55,7 @@ export const useGetSingleIssue = ({
5555
id,
5656
});
5757

58-
if (!didCancel) {
58+
if (!didCancel.current) {
5959
setIsLoading(false);
6060
setIssue(res.data ?? null);
6161
if (res.status && res.status === 'error') {
@@ -66,22 +66,24 @@ export const useGetSingleIssue = ({
6666
}
6767
}
6868
} catch (error) {
69-
if (!didCancel) {
69+
if (!didCancel.current) {
7070
setIsLoading(false);
71-
toastNotifications.addDanger({
72-
title: i18n.GET_ISSUE_API_ERROR(id),
73-
text: error.message,
74-
});
71+
if (error.name !== 'AbortError') {
72+
toastNotifications.addDanger({
73+
title: i18n.GET_ISSUE_API_ERROR(id),
74+
text: error.message,
75+
});
76+
}
7577
}
7678
}
7779
};
7880

81+
didCancel.current = false;
7982
abortCtrl.current.abort();
8083
fetchData();
8184

8285
return () => {
83-
didCancel = true;
84-
setIsLoading(false);
86+
didCancel.current = true;
8587
abortCtrl.current.abort();
8688
};
8789
}, [http, actionConnector, id, toastNotifications]);

x-pack/plugins/security_solution/public/cases/components/connectors/resilient/use_get_incident_types.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,27 @@ export const useGetIncidentTypes = ({
3434
}: Props): UseGetIncidentTypes => {
3535
const [isLoading, setIsLoading] = useState(true);
3636
const [incidentTypes, setIncidentTypes] = useState<IncidentTypes>([]);
37+
const didCancel = useRef(false);
3738
const abortCtrl = useRef(new AbortController());
3839

3940
useEffect(() => {
40-
let didCancel = false;
4141
const fetchData = async () => {
4242
if (!connector) {
4343
setIsLoading(false);
4444
return;
4545
}
4646

47-
abortCtrl.current = new AbortController();
48-
setIsLoading(true);
49-
5047
try {
48+
abortCtrl.current = new AbortController();
49+
setIsLoading(true);
50+
5151
const res = await getIncidentTypes({
5252
http,
5353
signal: abortCtrl.current.signal,
5454
connectorId: connector.id,
5555
});
5656

57-
if (!didCancel) {
57+
if (!didCancel.current) {
5858
setIsLoading(false);
5959
setIncidentTypes(res.data ?? []);
6060
if (res.status && res.status === 'error') {
@@ -65,22 +65,24 @@ export const useGetIncidentTypes = ({
6565
}
6666
}
6767
} catch (error) {
68-
if (!didCancel) {
68+
if (!didCancel.current) {
6969
setIsLoading(false);
70-
toastNotifications.addDanger({
71-
title: i18n.INCIDENT_TYPES_API_ERROR,
72-
text: error.message,
73-
});
70+
if (error.name !== 'AbortError') {
71+
toastNotifications.addDanger({
72+
title: i18n.INCIDENT_TYPES_API_ERROR,
73+
text: error.message,
74+
});
75+
}
7476
}
7577
}
7678
};
7779

80+
didCancel.current = false;
7881
abortCtrl.current.abort();
7982
fetchData();
8083

8184
return () => {
82-
didCancel = true;
83-
setIsLoading(false);
85+
didCancel.current = true;
8486
abortCtrl.current.abort();
8587
};
8688
}, [http, connector, toastNotifications]);

x-pack/plugins/security_solution/public/cases/components/connectors/resilient/use_get_severity.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
import { useState, useEffect, useRef } from 'react';
99
import { HttpSetup, ToastsApi } from 'kibana/public';
10+
import { ActionConnector } from '../../../containers/types';
1011
import { getSeverity } from './api';
1112
import * as i18n from './translations';
12-
import { ActionConnector } from '../../../containers/types';
1313

1414
type Severity = Array<{ id: number; name: string }>;
1515

@@ -31,26 +31,26 @@ export const useGetSeverity = ({ http, toastNotifications, connector }: Props):
3131
const [isLoading, setIsLoading] = useState(true);
3232
const [severity, setSeverity] = useState<Severity>([]);
3333
const abortCtrl = useRef(new AbortController());
34+
const didCancel = useRef(false);
3435

3536
useEffect(() => {
36-
let didCancel = false;
3737
const fetchData = async () => {
3838
if (!connector) {
3939
setIsLoading(false);
4040
return;
4141
}
4242

43-
abortCtrl.current = new AbortController();
44-
setIsLoading(true);
45-
4643
try {
44+
abortCtrl.current = new AbortController();
45+
setIsLoading(true);
46+
4747
const res = await getSeverity({
4848
http,
4949
signal: abortCtrl.current.signal,
5050
connectorId: connector.id,
5151
});
5252

53-
if (!didCancel) {
53+
if (!didCancel.current) {
5454
setIsLoading(false);
5555
setSeverity(res.data ?? []);
5656

@@ -62,22 +62,24 @@ export const useGetSeverity = ({ http, toastNotifications, connector }: Props):
6262
}
6363
}
6464
} catch (error) {
65-
if (!didCancel) {
65+
if (!didCancel.current) {
6666
setIsLoading(false);
67-
toastNotifications.addDanger({
68-
title: i18n.SEVERITY_API_ERROR,
69-
text: error.message,
70-
});
67+
if (error.name !== 'AbortError') {
68+
toastNotifications.addDanger({
69+
title: i18n.SEVERITY_API_ERROR,
70+
text: error.message,
71+
});
72+
}
7173
}
7274
}
7375
};
7476

77+
didCancel.current = false;
7578
abortCtrl.current.abort();
7679
fetchData();
7780

7881
return () => {
79-
didCancel = true;
80-
setIsLoading(false);
82+
didCancel.current = true;
8183
abortCtrl.current.abort();
8284
};
8385
}, [http, connector, toastNotifications]);

0 commit comments

Comments
 (0)