-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Security Solution] Refactor useSelector #75297
Changes from 6 commits
6e840b7
186d4e6
a1d4462
865367a
1ea3502
437c6ef
ce0c6bc
bfdf64c
c46389d
73ead59
a0f0700
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that we don't have to import |
||
import { setInsertTimeline } from '../../../timelines/store/timeline/actions'; | ||
import { timelineSelectors } from '../../../timelines/store/timeline'; | ||
|
||
|
@@ -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) | ||
); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
// eslint-disable-next-line no-restricted-imports | ||
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 |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import React, { memo, useMemo, HTMLAttributes } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { | ||
htmlIdGenerator, | ||
|
@@ -17,8 +16,10 @@ import { | |
import styled from 'styled-components'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import { EuiDescriptionListProps } from '@elastic/eui/src/components/description_list/description_list'; | ||
|
||
import * as selectors from '../../store/selectors'; | ||
import * as event from '../../../../common/endpoint/models/event'; | ||
import { useShallowEqualSelector } from '../../../common/hooks/use_selector'; | ||
import { CrumbInfo, formatDate, StyledBreadcrumbs } from './panel_content_utilities'; | ||
import { | ||
processPath, | ||
|
@@ -51,7 +52,7 @@ export const ProcessDetails = memo(function ProcessDetails({ | |
}) { | ||
const processName = event.eventName(processEvent); | ||
const entityId = event.entityId(processEvent); | ||
const isProcessTerminated = useSelector(selectors.isProcessTerminated)(entityId); | ||
const isProcessTerminated = useShallowEqualSelector(selectors.isProcessTerminated)(entityId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated to this PR but: |
||
const processInfoEntry: EuiDescriptionListProps['listItems'] = useMemo(() => { | ||
const eventTime = event.eventTimestamp(processEvent); | ||
const dateTime = eventTime === undefined ? null : formatDate(eventTime); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you'll still get the error when you create your own selector correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should add a rule here. Many teams work in this code base, for that reason I think we should all use the exact set of lint rules defined by Kibana core.
The Resolver code base, for example, has (AFAIK) no reason to use shallow comparison for selectors as it was designed with the expectation that strict comparison would be used.