Skip to content

Commit

Permalink
Extract explore pages refetch logic into a hook [2]
Browse files Browse the repository at this point in the history
  • Loading branch information
machadoum committed May 8, 2024
1 parent 0aaea5c commit 335615b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@
*/

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;
};
import { useCallback } from 'react';
import type { Refetch } from '../../../common/types';
import { useRefetchQueryById } from './use_refetch_query_by_id';

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

const refetchRiskScore = useCallback(() => {
if (refetchOverviewRiskScore) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 { useMemo } from 'react';
import { useDeepEqualSelector } from '../../../common/hooks/use_selector';
import { inputsSelectors } from '../../../common/store';

export const useRefetchQueryById = (QueryId: string) => {
const getGlobalQuery = useMemo(() => inputsSelectors.globalQueryByIdSelector(), []);
const { refetch } = useDeepEqualSelector((state) => getGlobalQuery(state, QueryId));
return refetch;
};
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +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';
import { useRefetchOverviewPageRiskScore } from '../../../../entity_analytics/api/hooks/use_refetch_overview_page_risk_score';

const ES_HOST_FIELD = 'host.name';
const HostOverviewManage = manageQuery(HostOverview);
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 { useRefetchOverviewPageRiskScore } from '../../../hooks/use_refetch_overview_page_risk_score';
import { useRefetchOverviewPageRiskScore } from '../../../../entity_analytics/api/hooks/use_refetch_overview_page_risk_score';

const QUERY_ID = 'UsersDetailsQueryId';
const ES_USER_FIELD = 'user.name';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import React, { useCallback, useMemo } from 'react';
import type { FlyoutPanelProps } from '@kbn/expandable-flyout';
import { useExpandableFlyoutApi } from '@kbn/expandable-flyout';

import { useRefetchQueryById } from '../../../entity_analytics/api/hooks/use_refetch_query_by_id';
import { RISK_INPUTS_TAB_QUERY_ID } from '../../../entity_analytics/components/entity_details_flyout/tabs/risk_inputs/risk_inputs_tab';
import type { Refetch } from '../../../common/types';
import { inputsSelectors } from '../../../common/store';
import { useDeepEqualSelector } from '../../../common/hooks/use_selector';
import { useCalculateEntityRiskScore } from '../../../entity_analytics/api/hooks/use_calculate_entity_risk_score';
import { useKibana } from '../../../common/lib/kibana/kibana_react';
import { hostToCriteria } from '../../../common/components/ml/criteria/host_to_criteria';
Expand Down Expand Up @@ -73,11 +72,7 @@ export const HostPanel = ({ contextID, scopeId, hostName, isDraggable }: HostPan
const hostRiskData = hostRisk && hostRisk.length > 0 ? hostRisk[0] : undefined;
const isRiskScoreExist = !!hostRiskData?.host.risk;

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

const refetchRiskInputsTab = useRefetchQueryById(RISK_INPUTS_TAB_QUERY_ID);
const refetchRiskScore = useCallback(() => {
refetch();
(refetchRiskInputsTab as Refetch | null)?.();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import React, { useCallback, useMemo } from 'react';
import type { FlyoutPanelProps } from '@kbn/expandable-flyout';
import { useExpandableFlyoutApi } from '@kbn/expandable-flyout';
import { useRefetchQueryById } from '../../../entity_analytics/api/hooks/use_refetch_query_by_id';
import type { Refetch } from '../../../common/types';
import { useDeepEqualSelector } from '../../../common/hooks/use_selector';
import { RISK_INPUTS_TAB_QUERY_ID } from '../../../entity_analytics/components/entity_details_flyout/tabs/risk_inputs/risk_inputs_tab';
import { inputsSelectors } from '../../../common/store';
import { useCalculateEntityRiskScore } from '../../../entity_analytics/api/hooks/use_calculate_entity_risk_score';
import { useKibana } from '../../../common/lib/kibana/kibana_react';
import { useRiskScore } from '../../../entity_analytics/api/hooks/use_risk_score';
Expand Down Expand Up @@ -75,11 +74,7 @@ export const UserPanel = ({ contextID, scopeId, userName, isDraggable }: UserPan
const { data: userRisk } = riskScoreState;
const userRiskData = userRisk && userRisk.length > 0 ? userRisk[0] : undefined;

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

const refetchRiskInputsTab = useRefetchQueryById(RISK_INPUTS_TAB_QUERY_ID);
const refetchRiskScore = useCallback(() => {
refetch();
(refetchRiskInputsTab as Refetch | null)?.();
Expand Down

0 comments on commit 335615b

Please sign in to comment.