Skip to content

Commit

Permalink
fetch session id only when vis inabled
Browse files Browse the repository at this point in the history
  • Loading branch information
angorayc committed Jan 7, 2023
1 parent 82b4ddb commit d2023fa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export const MatrixHistogramComponent: React.FC<MatrixHistogramComponentProps> =
const { searchSessionId, refetchByRestartingSession } = useRefetchByRestartingSession({
inputId: InputsModelId.global,
queryId: id,
skip: !isChartEmbeddablesEnabled,
});

const matrixHistogramRequest = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { useCallback, useRef, useEffect, useMemo } from 'react';
import { useCallback, useRef, useMemo } from 'react';
import { useDispatch } from 'react-redux';
import { useDeepEqualSelector } from '../../hooks/use_selector';
import { useKibana } from '../../lib/kibana';
Expand All @@ -23,21 +23,28 @@ interface UseRefetchByRestartingSessionProps {
export const useRefetchByRestartingSession = ({
inputId,
queryId,
skip,
}: UseRefetchByRestartingSessionProps): {
searchSessionId: string;
searchSessionId: string | undefined;
refetchByRestartingSession: Refetch;
} => {
const dispatch = useDispatch();
const { data } = useKibana().services;

const session = useRef(data.search.session);
const searchSessionId = useMemo(() => session.current.start(), [session]);

const getGlobalQuery = inputsSelectors.globalQueryByIdSelector();
const getTimelineQuery = inputsSelectors.timelineQueryByIdSelector();
const { selectedInspectIndex } = useDeepEqualSelector((state) =>
inputId === InputsModelId.global
? getGlobalQuery(state, queryId)
: getTimelineQuery(state, queryId)
const { selectedInspectIndex, searchSessionId: existingSearchSessionId } = useDeepEqualSelector(
(state) =>
inputId === InputsModelId.global
? getGlobalQuery(state, queryId)
: getTimelineQuery(state, queryId)
);

const searchSessionId = useMemo(
() => (skip ? undefined : existingSearchSessionId ?? session.current.start()),
[existingSearchSessionId, skip]
);

const refetchByRestartingSession = useCallback(() => {
Expand All @@ -51,16 +58,10 @@ export const useRefetchByRestartingSession = ({
* like most of our components, it refetches when receiving a new search
* session ID.
**/
searchSessionId: session.current.start(),
searchSessionId: skip ? undefined : session.current.start(),
})
);
}, [dispatch, queryId, selectedInspectIndex]);

useEffect(() => {
return () => {
data.search.session.clear();
};
});
}, [dispatch, queryId, selectedInspectIndex, skip]);

return { searchSessionId, refetchByRestartingSession };
};

0 comments on commit d2023fa

Please sign in to comment.