Skip to content

Commit

Permalink
Prevent unneeded re-render in discover timeline context
Browse files Browse the repository at this point in the history
  • Loading branch information
kqualters-elastic committed Oct 26, 2023
1 parent 217929a commit e76cfd3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type { DiscoverStateContainer } from '@kbn/discover-plugin/public';
import type { PropsWithChildren, FC } from 'react';
import React, { useCallback, useRef } from 'react';
import React, { useCallback, useRef, useMemo } from 'react';
import { DiscoverInTimelineContext } from './context';
import { useDiscoverInTimelineActions } from './use_discover_in_timeline_actions';

Expand All @@ -24,14 +24,16 @@ export const DiscoverInTimelineContextProvider: FC<DiscoverInTimelineContextProv
discoverStateContainer.current = stateContainer;
}, []);

const discoverInTimelineContext = useMemo(() => {
return {
discoverStateContainer,
setDiscoverStateContainer,
...actions,
};
}, [actions, setDiscoverStateContainer]);

return (
<DiscoverInTimelineContext.Provider
value={{
discoverStateContainer,
setDiscoverStateContainer,
...actions,
}}
>
<DiscoverInTimelineContext.Provider value={discoverInTimelineContext}>
{props.children}
</DiscoverInTimelineContext.Provider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { DiscoverStateContainer } from '@kbn/discover-plugin/public';
import type { SaveSavedSearchOptions } from '@kbn/saved-search-plugin/public';
import type { RefObject } from 'react';
import { useMemo, useCallback } from 'react';
import { useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import type { SavedSearch } from '@kbn/saved-search-plugin/common';
import type { DiscoverAppState } from '@kbn/discover-plugin/public/application/main/services/discover_app_state_container';
import type { TimeRange } from '@kbn/es-query';
Expand Down Expand Up @@ -51,11 +51,7 @@ export const useDiscoverInTimelineActions = (

const { dataViewId } = useSourcererDataView(SourcererScopeName.detections);

const getTimeline = useMemo(() => timelineSelectors.getTimelineByIdSelector(), []);
const timeline = useShallowEqualSelector(
(state) => getTimeline(state, TimelineId.active) ?? timelineDefaults
);
const { savedSearchId } = timeline;
const savedSearchId = useSelector(timelineSelectors.savedSearchId);

const queryClient = useQueryClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { createSelector } from 'reselect';
import type { State } from '../../../common/store/types';
import { TimelineId } from '../../../../common/types';

import type { TimelineModel } from './model';
import type { AutoSavedWarningMsg, InsertTimeline, TimelineById } from './types';
Expand All @@ -24,6 +25,10 @@ export const selectTimeline = (state: State, timelineId: string): TimelineModel
export const selectInsertTimeline = (state: State): InsertTimeline | null =>
state.timeline.insertTimeline;

export const savedSearchId = createSelector(selectTimelineById, (timeline) =>
timeline[TimelineId.active] ? timeline[TimelineId.active].savedSearchId : null
);

export const autoSaveMsgSelector = createSelector(selectAutoSaveMsg, (autoSaveMsg) => autoSaveMsg);

export const timelineByIdSelector = createSelector(
Expand Down

0 comments on commit e76cfd3

Please sign in to comment.