Skip to content

Commit

Permalink
[Security Solution] Refactor useSelector (elastic#75297)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykkopycinski committed Sep 28, 2020
1 parent c39243c commit ad37f02
Show file tree
Hide file tree
Showing 33 changed files with 135 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
*/

import React, { useState, useCallback, useMemo } from 'react';
import { useDispatch } from 'react-redux';

import { useDispatch, useSelector } from 'react-redux';
import { useShallowEqualSelector } from '../../../common/hooks/use_selector';
import { APP_ID } from '../../../../common/constants';
import { SecurityPageName } from '../../../app/types';
import { useKibana } from '../../../common/lib/kibana';
import { getCaseDetailsUrl, getCreateCaseUrl } from '../../../common/components/link_to';
import { State } from '../../../common/store';
import { setInsertTimeline } from '../../../timelines/store/timeline/actions';
import { timelineSelectors } from '../../../timelines/store/timeline';

Expand All @@ -34,7 +34,7 @@ export const useAllCasesModal = ({
}: UseAllCasesModalProps): UseAllCasesModalReturnedValues => {
const dispatch = useDispatch();
const { navigateToApp } = useKibana().services.application;
const timeline = useSelector((state: State) =>
const timeline = useShallowEqualSelector((state) =>
timelineSelectors.selectTimeline(state, timelineId)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { isEqual } from 'lodash/fp';
import { useMemo } from 'react';
import { useSelector } from 'react-redux';

import { useDeepEqualSelector } from '../../hooks/use_selector';
import { makeMapStateToProps } from '../url_state/helpers';
import { getSearch } from './helpers';
import { SearchNavTab } from './types';

export const useGetUrlSearch = (tab: SearchNavTab) => {
const mapState = makeMapStateToProps();
const { urlState } = useSelector(mapState, isEqual);
const { urlState } = useDeepEqualSelector(mapState);
const urlSearch = useMemo(() => getSearch(tab, urlState), [tab, urlState]);
return urlSearch;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { set } from '@elastic/safer-lodash-set/fp';
import { keyBy, pick, isEmpty, isEqual, isUndefined } from 'lodash/fp';
import memoizeOne from 'memoize-one';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useDispatch, useSelector, shallowEqual } from 'react-redux';
import { useDispatch } from 'react-redux';
import { IIndexPattern } from 'src/plugins/data/public';

import { useKibana } from '../../lib/kibana';
Expand All @@ -20,11 +20,10 @@ import {
BrowserFields,
} from '../../../../common/search_strategy/index_fields';
import { AbortError } from '../../../../../../../src/plugins/data/common';
import { useShallowEqualSelector } from '../../../common/hooks/use_selector';
import * as i18n from './translations';
import { SourcererScopeName } from '../../store/sourcerer/model';
import { sourcererActions, sourcererSelectors } from '../../store/sourcerer';

import { State } from '../../store';
import { DocValueFields } from '../../../../common/search_strategy/common';

export { BrowserField, BrowserFields, DocValueFields };
Expand Down Expand Up @@ -201,9 +200,8 @@ export const useIndexFields = (sourcererScopeName: SourcererScopeName) => {
() => sourcererSelectors.getIndexNamesSelectedSelector(),
[]
);
const indexNames = useSelector<State, string[]>(
(state) => indexNamesSelectedSelector(state, sourcererScopeName),
shallowEqual
const indexNames = useShallowEqualSelector<string[]>((state) =>
indexNamesSelectedSelector(state, sourcererScopeName)
);

const setLoading = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/

import { useCallback, useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { SCROLLING_DISABLED_CLASS_NAME } from '../../../../common/constants';
import { useDispatch } from 'react-redux';

import { SCROLLING_DISABLED_CLASS_NAME } from '../../../../common/constants';
import { useShallowEqualSelector } from '../../hooks/use_selector';
import { inputsSelectors } from '../../store';
import { inputsActions } from '../../store/actions';

Expand All @@ -29,8 +30,10 @@ export const resetScroll = () => {

export const useFullScreen = () => {
const dispatch = useDispatch();
const globalFullScreen = useSelector(inputsSelectors.globalFullScreenSelector) ?? false;
const timelineFullScreen = useSelector(inputsSelectors.timelineFullScreenSelector) ?? false;
const globalFullScreen =
useShallowEqualSelector(inputsSelectors.globalFullScreenSelector) ?? false;
const timelineFullScreen =
useShallowEqualSelector(inputsSelectors.timelineFullScreenSelector) ?? false;

const setGlobalFullScreen = useCallback(
(fullScreen: boolean) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
*/

import { useCallback, useState, useEffect, useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useDispatch } from 'react-redux';

import { useShallowEqualSelector } from '../../hooks/use_selector';
import { inputsSelectors } from '../../store';
import { inputsActions } from '../../store/actions';
import { SetQuery, DeleteQuery } from './types';

export const useGlobalTime = (clearAllQuery: boolean = true) => {
const dispatch = useDispatch();
const { from, to } = useSelector(inputsSelectors.globalTimeRangeSelector);
const { from, to } = useShallowEqualSelector(inputsSelectors.globalTimeRangeSelector);
const [isInitializing, setIsInitializing] = useState(true);

const setQuery = useCallback(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { shallowEqual, useSelector } from 'react-redux';
import deepEqual from 'fast-deep-equal';
import { State } from '../store';

export type TypedUseSelectorHook = <TSelected, TState = State>(
selector: (state: TState) => TSelected,
equalityFn?: (left: TSelected, right: TSelected) => boolean
) => TSelected;

export const useShallowEqualSelector: TypedUseSelectorHook = (selector) =>
useSelector(selector, shallowEqual);

export const useDeepEqualSelector: TypedUseSelectorHook = (selector) =>
useSelector(selector, deepEqual);
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { noop } from 'lodash/fp';
import { useCallback, useEffect, useRef, useState } from 'react';
import { shallowEqual, useSelector } from 'react-redux';
import deepEqual from 'fast-deep-equal';

import {
Expand All @@ -26,7 +25,8 @@ import {
} from '../../../../common/search_strategy';
import { ESTermQuery } from '../../../../common/typed_json';

import { inputsModel, State } from '../../../common/store';
import { useShallowEqualSelector } from '../../../common/hooks/use_selector';
import { inputsModel } from '../../../common/store';
import { createFilter } from '../../../common/containers/helpers';
import { generateTablePaginationOptions } from '../../../common/components/paginated_table/helpers';
import { useKibana } from '../../../common/lib/kibana';
Expand Down Expand Up @@ -71,9 +71,8 @@ export const useAuthentications = ({
skip,
}: UseAuthentications): [boolean, AuthenticationArgs] => {
const getAuthenticationsSelector = hostsSelectors.authenticationsSelector();
const { activePage, limit } = useSelector(
(state: State) => getAuthenticationsSelector(state, type),
shallowEqual
const { activePage, limit } = useShallowEqualSelector((state) =>
getAuthenticationsSelector(state, type)
);
const { data, notifications } = useKibana().services;
const refetch = useRef<inputsModel.Refetch>(noop);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import deepEqual from 'fast-deep-equal';
import { noop } from 'lodash/fp';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useSelector } from 'react-redux';

import { inputsModel, State } from '../../../common/store';
import { createFilter } from '../../../common/containers/helpers';
import { useKibana } from '../../../common/lib/kibana';
import { useShallowEqualSelector } from '../../../common/hooks/use_selector';
import { hostsModel, hostsSelectors } from '../../store';
import { generateTablePaginationOptions } from '../../../common/components/paginated_table/helpers';
import {
Expand Down Expand Up @@ -69,7 +69,7 @@ export const useAllHost = ({
type,
}: UseAllHost): [boolean, HostsArgs] => {
const getHostsSelector = hostsSelectors.hostsSelector();
const { activePage, direction, limit, sortField } = useSelector((state: State) =>
const { activePage, direction, limit, sortField } = useShallowEqualSelector((state: State) =>
getHostsSelector(state, type)
);
const { data, notifications } = useKibana().services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import React, { useCallback, useMemo } from 'react';
import { useDispatch, useSelector, shallowEqual } from 'react-redux';
import { useDispatch } from 'react-redux';
import deepEqual from 'fast-deep-equal';

import { networkActions, networkModel, networkSelectors } from '../../store';
Expand All @@ -16,6 +16,7 @@ import {
NetworkDnsFields,
} from '../../../../common/search_strategy';
import { Criteria, ItemsPerRow, PaginatedTable } from '../../../common/components/paginated_table';
import { useShallowEqualSelector } from '../../../common/hooks/use_selector';

import { getNetworkDnsColumns } from './columns';
import { IsPtrIncluded } from './is_ptr_included';
Expand Down Expand Up @@ -59,10 +60,7 @@ const NetworkDnsTableComponent: React.FC<NetworkDnsTableProps> = ({
}) => {
const dispatch = useDispatch();
const getNetworkDnsSelector = networkSelectors.dnsSelector();
const { activePage, isPtrIncluded, limit, sort } = useSelector(
getNetworkDnsSelector,
shallowEqual
);
const { activePage, isPtrIncluded, limit, sort } = useShallowEqualSelector(getNetworkDnsSelector);
const updateLimitPagination = useCallback(
(newLimit) =>
dispatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

import React, { useCallback, useMemo } from 'react';
import { useDispatch, useSelector, shallowEqual } from 'react-redux';
import { useDispatch } from 'react-redux';

import { networkActions, networkModel, networkSelectors } from '../../store';
import { NetworkHttpEdges, NetworkHttpFields } from '../../../../common/search_strategy';
import { State } from '../../../common/store';
import { useShallowEqualSelector } from '../../../common/hooks/use_selector';
import { Criteria, ItemsPerRow, PaginatedTable } from '../../../common/components/paginated_table';

import { getNetworkHttpColumns } from './columns';
Expand Down Expand Up @@ -51,9 +51,8 @@ const NetworkHttpTableComponent: React.FC<NetworkHttpTableProps> = ({
}) => {
const dispatch = useDispatch();
const getNetworkHttpSelector = networkSelectors.httpSelector();
const { activePage, limit, sort } = useSelector(
(state: State) => getNetworkHttpSelector(state, type),
shallowEqual
const { activePage, limit, sort } = useShallowEqualSelector((state) =>
getNetworkHttpSelector(state, type)
);
const tableType =
type === networkModel.NetworkType.page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { last } from 'lodash/fp';
import React, { useCallback, useMemo } from 'react';
import { useDispatch, useSelector, shallowEqual } from 'react-redux';
import { useDispatch } from 'react-redux';
import deepEqual from 'fast-deep-equal';
import { IIndexPattern } from 'src/plugins/data/public';

Expand All @@ -18,7 +18,7 @@ import {
NetworkTopTablesFields,
SortField,
} from '../../../../common/search_strategy';
import { State } from '../../../common/store';
import { useShallowEqualSelector } from '../../../common/hooks/use_selector';

import { Criteria, ItemsPerRow, PaginatedTable } from '../../../common/components/paginated_table';

Expand Down Expand Up @@ -67,9 +67,8 @@ const NetworkTopCountriesTableComponent: React.FC<NetworkTopCountriesTableProps>
}) => {
const dispatch = useDispatch();
const getTopCountriesSelector = networkSelectors.topCountriesSelector();
const { activePage, limit, sort } = useSelector(
(state: State) => getTopCountriesSelector(state, type, flowTargeted),
shallowEqual
const { activePage, limit, sort } = useShallowEqualSelector((state) =>
getTopCountriesSelector(state, type, flowTargeted)
);

const headerTitle: string = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
import { last } from 'lodash/fp';
import React, { useCallback, useMemo } from 'react';
import { useDispatch, useSelector, shallowEqual } from 'react-redux';
import { useDispatch } from 'react-redux';
import deepEqual from 'fast-deep-equal';

import {
Expand All @@ -15,7 +15,7 @@ import {
NetworkTopNFlowEdges,
NetworkTopTablesFields,
} from '../../../../common/search_strategy';
import { State } from '../../../common/store';
import { useShallowEqualSelector } from '../../../common/hooks/use_selector';
import { Criteria, ItemsPerRow, PaginatedTable } from '../../../common/components/paginated_table';
import { networkActions, networkModel, networkSelectors } from '../../store';
import { getNFlowColumnsCurated } from './columns';
Expand Down Expand Up @@ -61,9 +61,8 @@ const NetworkTopNFlowTableComponent: React.FC<NetworkTopNFlowTableProps> = ({
}) => {
const dispatch = useDispatch();
const getTopNFlowSelector = networkSelectors.topNFlowSelector();
const { activePage, limit, sort } = useSelector(
(state: State) => getTopNFlowSelector(state, type, flowTargeted),
shallowEqual
const { activePage, limit, sort } = useShallowEqualSelector((state) =>
getTopNFlowSelector(state, type, flowTargeted)
);

const columns = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import React, { useCallback, useMemo } from 'react';
import { useDispatch, useSelector, shallowEqual } from 'react-redux';
import { useDispatch } from 'react-redux';
import deepEqual from 'fast-deep-equal';

import { networkActions, networkModel, networkSelectors } from '../../store';
Expand All @@ -15,7 +15,7 @@ import {
NetworkTlsFields,
SortField,
} from '../../../../common/search_strategy';
import { State } from '../../../common/store';
import { useShallowEqualSelector } from '../../../common/hooks/use_selector';
import {
Criteria,
ItemsPerRow,
Expand Down Expand Up @@ -63,9 +63,8 @@ const TlsTableComponent: React.FC<TlsTableProps> = ({
}) => {
const dispatch = useDispatch();
const getTlsSelector = networkSelectors.tlsSelector();
const { activePage, limit, sort } = useSelector(
(state: State) => getTlsSelector(state, type),
shallowEqual
const { activePage, limit, sort } = useShallowEqualSelector((state) =>
getTlsSelector(state, type)
);
const tableType: networkModel.TopTlsTableType =
type === networkModel.NetworkType.page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/

import React, { useCallback, useMemo } from 'react';
import { useDispatch, useSelector, shallowEqual } from 'react-redux';
import { useDispatch } from 'react-redux';
import deepEqual from 'fast-deep-equal';

import { useShallowEqualSelector } from '../../../common/hooks/use_selector';
import { assertUnreachable } from '../../../../common/utility_types';
import { networkActions, networkModel, networkSelectors } from '../../store';
import {
Expand Down Expand Up @@ -68,7 +69,7 @@ const UsersTableComponent: React.FC<UsersTableProps> = ({
}) => {
const dispatch = useDispatch();
const getUsersSelector = networkSelectors.usersSelector();
const { activePage, sort, limit } = useSelector(getUsersSelector, shallowEqual);
const { activePage, sort, limit } = useShallowEqualSelector(getUsersSelector);
const updateLimitPagination = useCallback(
(newLimit) =>
dispatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

import { noop } from 'lodash/fp';
import { useState, useEffect, useCallback, useRef } from 'react';
import { shallowEqual, useSelector } from 'react-redux';
import deepEqual from 'fast-deep-equal';

import { ESTermQuery } from '../../../../common/typed_json';
import { inputsModel, State } from '../../../common/store';
import { inputsModel } from '../../../common/store';
import { useShallowEqualSelector } from '../../../common/hooks/use_selector';
import { useKibana } from '../../../common/lib/kibana';
import { createFilter } from '../../../common/containers/helpers';
import { NetworkDnsEdges, PageInfoPaginated } from '../../../../common/search_strategy';
Expand Down Expand Up @@ -68,10 +68,7 @@ export const useNetworkDns = ({
type,
}: UseNetworkDns): [boolean, NetworkDnsArgs] => {
const getNetworkDnsSelector = networkSelectors.dnsSelector();
const { activePage, sort, isPtrIncluded, limit } = useSelector(
(state: State) => getNetworkDnsSelector(state),
shallowEqual
);
const { activePage, sort, isPtrIncluded, limit } = useShallowEqualSelector(getNetworkDnsSelector);
const { data, notifications } = useKibana().services;
const refetch = useRef<inputsModel.Refetch>(noop);
const abortCtrl = useRef(new AbortController());
Expand Down
Loading

0 comments on commit ad37f02

Please sign in to comment.