Skip to content

Commit

Permalink
Extract explore pages refetch logic into a hook
Browse files Browse the repository at this point in the history
  • Loading branch information
machadoum committed May 8, 2024
1 parent 2df045a commit 0aaea5c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 { TableId } from '@kbn/securitysolution-data-table';
import { useCallback, useMemo } from 'react';
import { useDeepEqualSelector } from '../../common/hooks/use_selector';
import { inputsSelectors } from '../../common/store';
import type { Refetch } from '../../common/types';

const useRefetchByQueryId = (QueryId: string) => {
const getGlobalQuery = useMemo(() => inputsSelectors.globalQueryByIdSelector(), []);
const { refetch } = useDeepEqualSelector((state) => getGlobalQuery(state, QueryId));
return refetch;
};

export const useRefetchOverviewPageRiskScore = (overviewRiskScoreQueryId: string) => {
const refetchOverviewRiskScore = useRefetchByQueryId(overviewRiskScoreQueryId);
const refetchAlertsRiskInputs = useRefetchByQueryId(TableId.alertsRiskInputs);

const refetchRiskScore = useCallback(() => {
if (refetchOverviewRiskScore) {
(refetchOverviewRiskScore as Refetch)();
}

if (refetchAlertsRiskInputs) {
(refetchAlertsRiskInputs as Refetch)();
}
}, [refetchAlertsRiskInputs, refetchOverviewRiskScore]);
return refetchRiskScore;
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import type { Filter } from '@kbn/es-query';
import { buildEsQuery } from '@kbn/es-query';
import { getEsQueryConfig } from '@kbn/data-plugin/common';
import { tableDefaults, dataTableSelectors, TableId } from '@kbn/securitysolution-data-table';
import type { Refetch } from '../../../../common/types';
import { useCalculateEntityRiskScore } from '../../../../entity_analytics/api/hooks/use_calculate_entity_risk_score';
import {
useAssetCriticalityData,
Expand Down Expand Up @@ -80,6 +79,7 @@ import { AlertCountByRuleByStatus } from '../../../../common/components/alert_co
import { useLicense } from '../../../../common/hooks/use_license';
import { ResponderActionButton } from '../../../../detections/components/endpoint_responder/responder_action_button';
import { useHasSecurityCapability } from '../../../../helper_hooks';
import { useRefetchOverviewPageRiskScore } from '../../../hooks/use_refetch_overview_page_risk_score';

const ES_HOST_FIELD = 'host.name';
const HostOverviewManage = manageQuery(HostOverview);
Expand Down Expand Up @@ -188,25 +188,7 @@ const HostDetailsComponent: React.FC<HostDetailsProps> = ({ detailName, hostDeta
const entity = useMemo(() => ({ type: 'host' as const, name: detailName }), [detailName]);
const privileges = useAssetCriticalityPrivileges(entity.name);

const getGlobalQuery = useMemo(() => inputsSelectors.globalQueryByIdSelector(), []);
const { refetch: refetchOverviewRiskScore } = useDeepEqualSelector((state) =>
getGlobalQuery(state, HOST_OVERVIEW_RISK_SCORE_QUERY_ID)
);

const { refetch: refetchAlertsRiskInputs } = useDeepEqualSelector((state) =>
getGlobalQuery(state, TableId.alertsRiskInputs)
);

const refetchRiskScore = useCallback(() => {
if (refetchOverviewRiskScore) {
(refetchOverviewRiskScore as Refetch)();
}

if (refetchAlertsRiskInputs) {
(refetchAlertsRiskInputs as Refetch)();
}
}, [refetchAlertsRiskInputs, refetchOverviewRiskScore]);

const refetchRiskScore = useRefetchOverviewPageRiskScore(HOST_OVERVIEW_RISK_SCORE_QUERY_ID);
const { calculateEntityRiskScore } = useCalculateEntityRiskScore(
RiskScoreEntity.host,
detailName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ import { hasMlUserPermissions } from '../../../../../common/machine_learning/has
import { useMlCapabilities } from '../../../../common/components/ml/hooks/use_ml_capabilities';
import { EmptyPrompt } from '../../../../common/components/empty_prompt';
import { useHasSecurityCapability } from '../../../../helper_hooks';
import type { Refetch } from '../../../../common/types';
import { useRefetchOverviewPageRiskScore } from '../../../hooks/use_refetch_overview_page_risk_score';

const QUERY_ID = 'UsersDetailsQueryId';
const ES_USER_FIELD = 'user.name';
Expand Down Expand Up @@ -186,25 +186,7 @@ const UsersDetailsComponent: React.FC<UsersDetailsProps> = ({
const entity = useMemo(() => ({ type: 'user' as const, name: detailName }), [detailName]);
const privileges = useAssetCriticalityPrivileges(entity.name);

const getGlobalQuery = useMemo(() => inputsSelectors.globalQueryByIdSelector(), []);
const { refetch: refetchOverviewRiskScore } = useDeepEqualSelector((state) =>
getGlobalQuery(state, USER_OVERVIEW_RISK_SCORE_QUERY_ID)
);

const { refetch: refetchAlertsRiskInputs } = useDeepEqualSelector((state) =>
getGlobalQuery(state, TableId.alertsRiskInputs)
);

const refetchRiskScore = useCallback(() => {
if (refetchOverviewRiskScore) {
(refetchOverviewRiskScore as Refetch)();
}

if (refetchAlertsRiskInputs) {
(refetchAlertsRiskInputs as Refetch)();
}
}, [refetchAlertsRiskInputs, refetchOverviewRiskScore]);

const refetchRiskScore = useRefetchOverviewPageRiskScore(USER_OVERVIEW_RISK_SCORE_QUERY_ID);
const { calculateEntityRiskScore } = useCalculateEntityRiskScore(
RiskScoreEntity.user,
detailName,
Expand Down

0 comments on commit 0aaea5c

Please sign in to comment.