Skip to content

Commit

Permalink
Reorder use_list_artifact hook params and move custom getInstance fun…
Browse files Browse the repository at this point in the history
…ctions inside a useCallback
  • Loading branch information
dasansol92 committed Mar 9, 2022
1 parent ed6c258 commit 63f8f9a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('List artifact hook', () => {

result = await renderQuery(
() =>
useListArtifact(instance, searchableFields, options, {
useListArtifact(instance, options, searchableFields, {
onSuccess: onSuccessMock,
retry: false,
}),
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('List artifact hook', () => {

result = await renderQuery(
() =>
useListArtifact(instance, searchableFields, options, {
useListArtifact(instance, options, searchableFields, {
onError: onErrorMock,
retry: false,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<string[]> = DEFAULT_EXCEPTION_LIST_ITEM_SEARCHABLE_FIELDS,
customQueryOptions: Partial<
UseQueryOptions<FoundExceptionListItemSchema, HttpFetchError>
> = DEFAULT_OPTIONS,
customQueryIds: string[] = []
): QueryObserverResult<FoundExceptionListItemSchema, HttpFetchError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,23 @@ export const PolicyArtifactsFlyout = React.memo<PolicyArtifactsFlyoutProps>(
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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ export const PolicyArtifactsLayout = React.memo<PolicyArtifactsLayoutProps>(

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' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ export const PolicyArtifactsList = React.memo<PolicyArtifactsListProps>(
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(
() => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PolicyTabKeys, PolicyTab | undefined> = useMemo(() => {
const trustedAppsLabels = {
...POLICY_ARTIFACT_TRUSTED_APPS_LABELS,
Expand Down Expand Up @@ -147,7 +162,7 @@ export const PolicyTabs = React.memo(() => {
<PolicyArtifactsLayout
policyItem={policyItem}
labels={trustedAppsLabels}
getExceptionsListApiClient={() => TrustedAppsApiClient.getInstance(http)}
getExceptionsListApiClient={getTrustedAppsApiClientInstance}
searcheableFields={TRUSTED_APPS_SEARCHABLE_FIELDS}
getArtifactPath={getTrustedAppsListPath}
getPolicyArtifactsPath={getPolicyDetailsArtifactsListPath}
Expand All @@ -166,7 +181,7 @@ export const PolicyTabs = React.memo(() => {
<PolicyArtifactsLayout
policyItem={policyItem}
labels={eventFiltersLabels}
getExceptionsListApiClient={() => EventFiltersApiClient.getInstance(http)}
getExceptionsListApiClient={getEventFiltersApiClientInstance}
searcheableFields={EVENT_FILTERS_SEARCHABLE_FIELDS}
getArtifactPath={getEventFiltersListPath}
getPolicyArtifactsPath={getPolicyEventFiltersPath}
Expand All @@ -189,9 +204,7 @@ export const PolicyTabs = React.memo(() => {
<PolicyArtifactsLayout
policyItem={policyItem}
labels={hostIsolationExceptionsLabels}
getExceptionsListApiClient={() =>
HostIsolationExceptionsApiClient.getInstance(http)
}
getExceptionsListApiClient={getHostIsolationExceptionsApiClientInstance}
searcheableFields={HOST_ISOLATION_EXCEPTIONS_SEARCHABLE_FIELDS}
getArtifactPath={getHostIsolationExceptionsListPath}
getPolicyArtifactsPath={getPolicyHostIsolationExceptionsPath}
Expand All @@ -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(
Expand Down

0 comments on commit 63f8f9a

Please sign in to comment.