diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_logs/constants.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_logs/constants.tsx index 4ee1618a3858..4e802fee3043 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_logs/constants.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_logs/constants.tsx @@ -36,6 +36,7 @@ export const DEFAULT_LOGS_STATE: AgentLogsState = { query: '', }; +export const STATE_STORAGE_KEY = '_q'; export const STATE_DATASET_FIELD = 'datasets'; export const AGENT_LOG_LEVELS = { diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_logs/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_logs/index.tsx index 0d888a88ec2c..707bc05904aa 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_logs/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agents/agent_details_page/components/agent_logs/index.tsx @@ -3,7 +3,7 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import React, { memo, useEffect, useState } from 'react'; +import React, { memo, useEffect, useState, useMemo } from 'react'; import { createStateContainer, syncState, @@ -12,38 +12,44 @@ import { PureTransition, getStateFromKbnUrl, } from '../../../../../../../../../../../src/plugins/kibana_utils/public'; -import { DEFAULT_LOGS_STATE } from './constants'; +import { DEFAULT_LOGS_STATE, STATE_STORAGE_KEY } from './constants'; import { AgentLogsUI, AgentLogsProps, AgentLogsState, AgentLogsUrlStateHelper } from './agent_logs'; -const stateStorageKey = '_q'; - -const stateContainer = createStateContainer< - AgentLogsState, - { - update: PureTransition]>; - } ->( - { - ...DEFAULT_LOGS_STATE, - ...getStateFromKbnUrl(stateStorageKey, window.location.href), - }, - { - update: (state) => (updatedState) => ({ ...state, ...updatedState }), - } -); - -const AgentLogsConnected = AgentLogsUrlStateHelper.connect((state) => ({ - state: state || DEFAULT_LOGS_STATE, -}))(AgentLogsUI); - export const AgentLogs: React.FunctionComponent> = memo( ({ agent }) => { + const stateContainer = useMemo( + () => + createStateContainer< + AgentLogsState, + { + update: PureTransition]>; + } + >( + { + ...DEFAULT_LOGS_STATE, + ...getStateFromKbnUrl(STATE_STORAGE_KEY, window.location.href), + }, + { + update: (state) => (updatedState) => ({ ...state, ...updatedState }), + } + ), + [] + ); + + const AgentLogsConnected = useMemo( + () => + AgentLogsUrlStateHelper.connect((state) => ({ + state: state || DEFAULT_LOGS_STATE, + }))(AgentLogsUI), + [] + ); + const [isSyncReady, setIsSyncReady] = useState(false); useEffect(() => { const stateStorage = createKbnUrlStateStorage(); const { start, stop } = syncState({ - storageKey: stateStorageKey, + storageKey: STATE_STORAGE_KEY, stateContainer: stateContainer as INullableBaseStateContainer, stateStorage, }); @@ -54,7 +60,7 @@ export const AgentLogs: React.FunctionComponent> = stop(); stateContainer.set(DEFAULT_LOGS_STATE); }; - }, []); + }, [stateContainer]); return (