From 63f8f9a20e7ba7602a78c5d70f9a3009ebd81489 Mon Sep 17 00:00:00 2001 From: David Sanchez Soler Date: Wed, 9 Mar 2022 15:24:04 +0100 Subject: [PATCH] Reorder use_list_artifact hook params and move custom getInstance functions inside a useCallback --- .../artifacts/use_list_artifact.test.tsx | 4 +-- .../hooks/artifacts/use_list_artifact.tsx | 13 ++++---- .../flyout/policy_artifacts_flyout.tsx | 19 +++++++---- .../layout/policy_artifacts_layout.tsx | 6 ++-- .../artifacts/list/policy_artifacts_list.tsx | 16 ++++++---- .../pages/policy/view/tabs/policy_tabs.tsx | 32 +++++++++++++++---- 6 files changed, 60 insertions(+), 30 deletions(-) diff --git a/x-pack/plugins/security_solution/public/management/hooks/artifacts/use_list_artifact.test.tsx b/x-pack/plugins/security_solution/public/management/hooks/artifacts/use_list_artifact.test.tsx index e7f389735056e..e41660daa5c59 100644 --- a/x-pack/plugins/security_solution/public/management/hooks/artifacts/use_list_artifact.test.tsx +++ b/x-pack/plugins/security_solution/public/management/hooks/artifacts/use_list_artifact.test.tsx @@ -56,7 +56,7 @@ describe('List artifact hook', () => { result = await renderQuery( () => - useListArtifact(instance, searchableFields, options, { + useListArtifact(instance, options, searchableFields, { onSuccess: onSuccessMock, retry: false, }), @@ -92,7 +92,7 @@ describe('List artifact hook', () => { result = await renderQuery( () => - useListArtifact(instance, searchableFields, options, { + useListArtifact(instance, options, searchableFields, { onError: onErrorMock, retry: false, }), diff --git a/x-pack/plugins/security_solution/public/management/hooks/artifacts/use_list_artifact.tsx b/x-pack/plugins/security_solution/public/management/hooks/artifacts/use_list_artifact.tsx index ed103e20d729f..04dabaa4e5119 100644 --- a/x-pack/plugins/security_solution/public/management/hooks/artifacts/use_list_artifact.tsx +++ b/x-pack/plugins/security_solution/public/management/hooks/artifacts/use_list_artifact.tsx @@ -7,6 +7,8 @@ import { FoundExceptionListItemSchema } from '@kbn/securitysolution-io-ts-list-types'; import { HttpFetchError } from 'kibana/public'; import { QueryObserverResult, useQuery, UseQueryOptions } from 'react-query'; +import { DEFAULT_EXCEPTION_LIST_ITEM_SEARCHABLE_FIELDS } from '../../../../common/endpoint/service/artifacts/constants'; +import { MaybeImmutable } from '../../../../common/endpoint/types'; import { MANAGEMENT_DEFAULT_PAGE, MANAGEMENT_DEFAULT_PAGE_SIZE } from '../../common/constants'; import { parsePoliciesAndFilterToKql, parseQueryFilterToKQL } from '../../common/utils'; import { ExceptionsListApiClient } from '../../services/exceptions_list/exceptions_list_api_client'; @@ -15,23 +17,22 @@ const DEFAULT_OPTIONS = Object.freeze({}); export function useListArtifact( exceptionListApiClient: ExceptionsListApiClient, - searcheableFields: string[], - options: { + options: Partial<{ filter?: string; page?: number; perPage?: number; policies?: string[]; excludedPolicies?: string[]; - } = { + }> = { filter: '', page: MANAGEMENT_DEFAULT_PAGE + 1, perPage: MANAGEMENT_DEFAULT_PAGE_SIZE, policies: [], excludedPolicies: [], }, - customQueryOptions: UseQueryOptions< - FoundExceptionListItemSchema, - HttpFetchError + searcheableFields: MaybeImmutable = DEFAULT_EXCEPTION_LIST_ITEM_SEARCHABLE_FIELDS, + customQueryOptions: Partial< + UseQueryOptions > = DEFAULT_OPTIONS, customQueryIds: string[] = [] ): QueryObserverResult { diff --git a/x-pack/plugins/security_solution/public/management/pages/policy/view/artifacts/flyout/policy_artifacts_flyout.tsx b/x-pack/plugins/security_solution/public/management/pages/policy/view/artifacts/flyout/policy_artifacts_flyout.tsx index 6465ac17eabfc..e78aba6ee687c 100644 --- a/x-pack/plugins/security_solution/public/management/pages/policy/view/artifacts/flyout/policy_artifacts_flyout.tsx +++ b/x-pack/plugins/security_solution/public/management/pages/policy/view/artifacts/flyout/policy_artifacts_flyout.tsx @@ -66,18 +66,23 @@ export const PolicyArtifactsFlyout = React.memo( data: artifacts, isLoading: isLoadingArtifacts, isRefetching: isRefetchingArtifacts, - } = useListArtifact(apiClient, searcheableFields, { - perPage: MAX_ALLOWED_RESULTS, - filter: currentFilter, - excludedPolicies: [policyItem.id, 'all'], - }); + } = useListArtifact( + apiClient, + { + perPage: MAX_ALLOWED_RESULTS, + filter: currentFilter, + excludedPolicies: [policyItem.id, 'all'], + }, + searcheableFields + ); const { data: allNotAssigned, isLoading: isLoadingAllNotAssigned } = useListArtifact( apiClient, - searcheableFields, + { excludedPolicies: [policyItem.id, 'all'], - } + }, + searcheableFields ); const handleOnSearch = useCallback((query) => { diff --git a/x-pack/plugins/security_solution/public/management/pages/policy/view/artifacts/layout/policy_artifacts_layout.tsx b/x-pack/plugins/security_solution/public/management/pages/policy/view/artifacts/layout/policy_artifacts_layout.tsx index fbfa94c7ad21a..a1cd2a5e4193c 100644 --- a/x-pack/plugins/security_solution/public/management/pages/policy/view/artifacts/layout/policy_artifacts_layout.tsx +++ b/x-pack/plugins/security_solution/public/management/pages/policy/view/artifacts/layout/policy_artifacts_layout.tsx @@ -84,17 +84,17 @@ export const PolicyArtifactsLayout = React.memo( const { data: allAssigned, isLoading: isLoadingAllAssigned } = useListArtifact( exceptionsListApiClient, - [...searcheableFields], { policies: policyItem ? [policyItem.id, 'all'] : [], - } + }, + searcheableFields ); const { data: allArtifacts, isLoading: isLoadingAllArtifacts, isRefetching: isRefetchingAllArtifacts, - } = useListArtifact(exceptionsListApiClient, [...searcheableFields], {}, {}, ['allExisting']); + } = useListArtifact(exceptionsListApiClient, {}, searcheableFields, {}, ['allExisting']); const handleOnClickAssignButton = useCallback(() => { navigateCallback({ show: 'list' }); diff --git a/x-pack/plugins/security_solution/public/management/pages/policy/view/artifacts/list/policy_artifacts_list.tsx b/x-pack/plugins/security_solution/public/management/pages/policy/view/artifacts/list/policy_artifacts_list.tsx index 98f74fff6081a..2b3094ba9bc93 100644 --- a/x-pack/plugins/security_solution/public/management/pages/policy/view/artifacts/list/policy_artifacts_list.tsx +++ b/x-pack/plugins/security_solution/public/management/pages/policy/view/artifacts/list/policy_artifacts_list.tsx @@ -73,12 +73,16 @@ export const PolicyArtifactsList = React.memo( data: artifacts, isLoading: isLoadingArtifacts, isRefetching: isRefetchingArtifacts, - } = useListArtifact(apiClient, searcheableFields, { - page: urlPagination.page, - perPage: urlPagination.pageSize, - filter: urlParams.filter as string, - policies: [policy.id, 'all'], - }); + } = useListArtifact( + apiClient, + { + page: urlPagination.page, + perPage: urlPagination.pageSize, + filter: urlParams.filter as string, + policies: [policy.id, 'all'], + }, + searcheableFields + ); const pagination: Pagination = useMemo( () => ({ diff --git a/x-pack/plugins/security_solution/public/management/pages/policy/view/tabs/policy_tabs.tsx b/x-pack/plugins/security_solution/public/management/pages/policy/view/tabs/policy_tabs.tsx index 42072537b33ee..6f8e0f9bf7e3f 100644 --- a/x-pack/plugins/security_solution/public/management/pages/policy/view/tabs/policy_tabs.tsx +++ b/x-pack/plugins/security_solution/public/management/pages/policy/view/tabs/policy_tabs.tsx @@ -89,6 +89,21 @@ export const PolicyTabs = React.memo(() => { } }, [canSeeHostIsolationExceptions, history, isInHostIsolationExceptionsTab, policyId]); + const getTrustedAppsApiClientInstance = useCallback( + () => TrustedAppsApiClient.getInstance(http), + [http] + ); + + const getEventFiltersApiClientInstance = useCallback( + () => EventFiltersApiClient.getInstance(http), + [http] + ); + + const getHostIsolationExceptionsApiClientInstance = useCallback( + () => HostIsolationExceptionsApiClient.getInstance(http), + [http] + ); + const tabs: Record = useMemo(() => { const trustedAppsLabels = { ...POLICY_ARTIFACT_TRUSTED_APPS_LABELS, @@ -147,7 +162,7 @@ export const PolicyTabs = React.memo(() => { TrustedAppsApiClient.getInstance(http)} + getExceptionsListApiClient={getTrustedAppsApiClientInstance} searcheableFields={TRUSTED_APPS_SEARCHABLE_FIELDS} getArtifactPath={getTrustedAppsListPath} getPolicyArtifactsPath={getPolicyDetailsArtifactsListPath} @@ -166,7 +181,7 @@ export const PolicyTabs = React.memo(() => { EventFiltersApiClient.getInstance(http)} + getExceptionsListApiClient={getEventFiltersApiClientInstance} searcheableFields={EVENT_FILTERS_SEARCHABLE_FIELDS} getArtifactPath={getEventFiltersListPath} getPolicyArtifactsPath={getPolicyEventFiltersPath} @@ -189,9 +204,7 @@ export const PolicyTabs = React.memo(() => { - HostIsolationExceptionsApiClient.getInstance(http) - } + getExceptionsListApiClient={getHostIsolationExceptionsApiClientInstance} searcheableFields={HOST_ISOLATION_EXCEPTIONS_SEARCHABLE_FIELDS} getArtifactPath={getHostIsolationExceptionsListPath} getPolicyArtifactsPath={getPolicyHostIsolationExceptionsPath} @@ -202,7 +215,14 @@ export const PolicyTabs = React.memo(() => { } : undefined, }; - }, [canSeeHostIsolationExceptions, http, policyItem, privileges.canIsolateHost]); + }, [ + canSeeHostIsolationExceptions, + getEventFiltersApiClientInstance, + getHostIsolationExceptionsApiClientInstance, + getTrustedAppsApiClientInstance, + policyItem, + privileges.canIsolateHost, + ]); // convert tabs object into an array EuiTabbedContent can understand const tabsList: PolicyTab[] = useMemo(