From 1a335193de56ab81a32a091555a639e977de357c Mon Sep 17 00:00:00 2001 From: Carlos Crespo Date: Wed, 25 May 2022 10:27:20 +0200 Subject: [PATCH] move open flyout logic to a hook; fix flyout remaining open after moving to another page --- .../components/log_stream/log_stream.tsx | 47 +++-------------- .../log_entry_flyout/log_entry_flyout.tsx | 51 ++++++++++++++++++- 2 files changed, 57 insertions(+), 41 deletions(-) diff --git a/x-pack/plugins/infra/public/components/log_stream/log_stream.tsx b/x-pack/plugins/infra/public/components/log_stream/log_stream.tsx index eedc7c06e0aab..be33e52bd3687 100644 --- a/x-pack/plugins/infra/public/components/log_stream/log_stream.tsx +++ b/x-pack/plugins/infra/public/components/log_stream/log_stream.tsx @@ -8,14 +8,10 @@ import { buildEsQuery, Filter, Query } from '@kbn/es-query'; import { JsonValue } from '@kbn/utility-types'; import { noop } from 'lodash'; -import React, { useCallback, useEffect, useMemo, useRef } from 'react'; +import React, { useCallback, useEffect, useMemo } from 'react'; import { DataPublicPluginStart } from '@kbn/data-plugin/public'; import { euiStyled } from '@kbn/kibana-react-plugin/common'; -import { - createKibanaReactContext, - KibanaReactOverlays, - useKibana, -} from '@kbn/kibana-react-plugin/public'; +import { useKibana } from '@kbn/kibana-react-plugin/public'; import { LogEntryCursor } from '../../../common/log_entry'; import { defaultLogViewsStaticConfig } from '../../../common/log_views'; import { BuiltEsQuery, useLogStream } from '../../containers/logs/log_stream'; @@ -25,7 +21,7 @@ import { LogColumnRenderConfiguration } from '../../utils/log_column_render_conf import { useKibanaQuerySettings } from '../../utils/use_kibana_query_settings'; import { ScrollableLogTextStreamView } from '../logging/log_text_stream'; import { LogStreamErrorBoundary } from './log_stream_error_boundary'; -import { LogEntryFlyout } from '../logging/log_entry_flyout'; +import { useLogEntryFlyout } from '../logging/log_entry_flyout'; interface LogStreamPluginDeps { data: DataPublicPluginStart; @@ -101,15 +97,13 @@ export const LogStreamContent: React.FC = ({ columns, flyoutEnabled = false, }) => { - const flyoutRef = useRef>(); const customColumns = useMemo( () => (columns ? convertLogColumnDefinitionToLogSourceColumnDefinition(columns) : undefined), [columns] ); const { - services: { http, data, uiSettings, application }, - overlays: { openFlyout }, + services: { http, data }, } = useKibana(); if (http == null || data == null) { throw new Error( @@ -121,6 +115,8 @@ Read more at https://github.com/elastic/kibana/blob/main/src/plugins/kibana_reac ); } + const { openLogEntryFlyout } = useLogEntryFlyout(sourceId); + const kibanaQuerySettings = useKibanaQuerySettings(); const logViews = useMemo( @@ -217,35 +213,6 @@ Read more at https://github.com/elastic/kibana/blob/main/src/plugins/kibana_reac [isLoadingMore, fetchPreviousEntries, fetchNextEntries] ); - const handleCloseFlyout = () => { - if (flyoutRef.current) { - flyoutRef.current.close(); - } - }; - - const handleOpenLogFlyoutClick = useCallback( - (logEntryId) => { - const { Provider: KibanaReactContextProvider } = createKibanaReactContext({ - http, - data, - uiSettings, - application, - }); - - flyoutRef.current = openFlyout( - - - - ); - }, - [http, data, uiSettings, application, openFlyout, sourceId] - ); - return ( { + const flyoutRef = useRef(); + const { + services: { http, data, uiSettings, application }, + overlays: { openFlyout }, + } = useKibana<{ data: DataPublicPluginStart }>(); + + const closeLogEntryFlyout = useCallback(() => { + flyoutRef.current?.close(); + }, []); + + const openLogEntryFlyout = useCallback( + (logEntryId) => { + const { Provider: KibanaReactContextProvider } = createKibanaReactContext({ + http, + data, + uiSettings, + application, + }); + + flyoutRef.current = openFlyout( + + + + ); + }, + [http, data, uiSettings, application, openFlyout, sourceId, closeLogEntryFlyout] + ); + + useEffect(() => { + return () => { + closeLogEntryFlyout(); + }; + }, [closeLogEntryFlyout]); + + return { + openLogEntryFlyout, + closeLogEntryFlyout, + }; +}; + export const LogEntryFlyout = ({ logEntryId, onCloseFlyout,