From c9e957a44e89a6ce881d55657ff34619e56b205a Mon Sep 17 00:00:00 2001 From: Sven Grossmann Date: Thu, 29 Sep 2022 14:51:20 +0200 Subject: [PATCH] Logs: Center `show context` modal on click (#55989) * added center implementation * fixed passing `scrollElement` to wrong component * rather destructure than direct ref * removed unnecessary `undefined` --- public/app/features/explore/Explore.tsx | 1 + public/app/features/explore/Logs.tsx | 3 +++ public/app/features/explore/LogsContainer.tsx | 3 +++ .../app/features/logs/components/LogRow.tsx | 3 +++ .../logs/components/LogRowMessage.tsx | 22 +++++++++++++++++-- .../app/features/logs/components/LogRows.tsx | 4 ++++ 6 files changed, 34 insertions(+), 2 deletions(-) diff --git a/public/app/features/explore/Explore.tsx b/public/app/features/explore/Explore.tsx index f4c3b7784fc..cceefbd44dd 100644 --- a/public/app/features/explore/Explore.tsx +++ b/public/app/features/explore/Explore.tsx @@ -275,6 +275,7 @@ export class Explore extends React.PureComponent { onClickFilterOutLabel={this.onClickFilterOutLabel} onStartScanning={this.onStartScanning} onStopScanning={this.onStopScanning} + scrollElement={this.scrollElement} /> ); } diff --git a/public/app/features/explore/Logs.tsx b/public/app/features/explore/Logs.tsx index 2bd7b3308d3..261b2f8e9c6 100644 --- a/public/app/features/explore/Logs.tsx +++ b/public/app/features/explore/Logs.tsx @@ -66,6 +66,7 @@ interface Props extends Themeable2 { datasourceType?: string; logsVolumeEnabled: boolean; logsVolumeData: DataQueryResponse | undefined; + scrollElement?: HTMLDivElement; onSetLogsVolumeEnabled: (enabled: boolean) => void; loadLogsVolumeData: (exploreId: ExploreId) => void; showContextToggle?: (row?: LogRowModel) => boolean; @@ -318,6 +319,7 @@ class UnthemedLogs extends PureComponent { clearCache, addResultsToCache, exploreId, + scrollElement, } = this.props; const { @@ -477,6 +479,7 @@ class UnthemedLogs extends PureComponent { onClickShowDetectedField={this.showDetectedField} onClickHideDetectedField={this.hideDetectedField} app={CoreApp.Explore} + scrollElement={scrollElement} /> void; onClickFilterOutLabel?: (key: string, value: string) => void; onStartScanning: () => void; @@ -97,6 +98,7 @@ class LogsContainer extends PureComponent { exploreId, addResultsToCache, clearCache, + scrollElement, } = this.props; if (!logRows) { @@ -152,6 +154,7 @@ class LogsContainer extends PureComponent { getFieldLinks={this.getFieldLinks} addResultsToCache={() => addResultsToCache(exploreId)} clearCache={() => clearCache(exploreId)} + scrollElement={scrollElement} /> diff --git a/public/app/features/logs/components/LogRow.tsx b/public/app/features/logs/components/LogRow.tsx index 370c35756e2..f256b7a5352 100644 --- a/public/app/features/logs/components/LogRow.tsx +++ b/public/app/features/logs/components/LogRow.tsx @@ -43,6 +43,7 @@ interface Props extends Themeable2 { logsSortOrder?: LogsSortOrder | null; forceEscape?: boolean; showDetectedFields?: string[]; + scrollElement?: HTMLDivElement; showRowMenu?: boolean; app?: CoreApp; getRows: () => LogRowModel[]; @@ -148,6 +149,7 @@ class UnThemedLogRow extends PureComponent { forceEscape, onLogRowHover, app, + scrollElement, } = this.props; const { showDetails, showContext } = this.state; const style = getLogRowStyles(theme, row.logLevel); @@ -219,6 +221,7 @@ class UnThemedLogRow extends PureComponent { prettifyLogMessage={prettifyLogMessage} onToggleContext={this.toggleContext} app={app} + scrollElement={scrollElement} logsSortOrder={logsSortOrder} /> )} diff --git a/public/app/features/logs/components/LogRowMessage.tsx b/public/app/features/logs/components/LogRowMessage.tsx index 59ffe09e263..a29c2f9c850 100644 --- a/public/app/features/logs/components/LogRowMessage.tsx +++ b/public/app/features/logs/components/LogRowMessage.tsx @@ -24,6 +24,7 @@ interface Props extends Themeable2 { context?: LogRowContextRows; showRowMenu?: boolean; app?: CoreApp; + scrollElement?: HTMLDivElement; showContextToggle?: (row?: LogRowModel) => boolean; getRows: () => LogRowModel[]; onToggleContext: () => void; @@ -112,11 +113,24 @@ const restructureLog = memoizeOne((line: string, prettifyLogMessage: boolean): s }); class UnThemedLogRowMessage extends PureComponent { + logRowRef: React.RefObject = React.createRef(); + onContextToggle = (e: React.SyntheticEvent) => { e.stopPropagation(); this.props.onToggleContext(); }; + onShowContextClick = (e: React.SyntheticEvent) => { + const { scrollElement } = this.props; + this.onContextToggle(e); + if (scrollElement && this.logRowRef.current) { + scrollElement.scroll({ + behavior: 'smooth', + top: scrollElement.scrollTop + this.logRowRef.current.getBoundingClientRect().top - window.innerHeight / 2, + }); + } + }; + render() { const { row, @@ -144,7 +158,11 @@ class UnThemedLogRowMessage extends PureComponent { return ( // When context is open, the position has to be NOT relative. // Setting the postion as inline-style to overwrite the more sepecific style definition from `style.logsRowMessage`. - +
@@ -171,7 +189,7 @@ class UnThemedLogRowMessage extends PureComponent { e.stopPropagation()}> {shouldShowContextToggle && ( - + )} diff --git a/public/app/features/logs/components/LogRows.tsx b/public/app/features/logs/components/LogRows.tsx index 0c5c130b9d6..f9725dd4887 100644 --- a/public/app/features/logs/components/LogRows.tsx +++ b/public/app/features/logs/components/LogRows.tsx @@ -35,6 +35,7 @@ export interface Props extends Themeable2 { forceEscape?: boolean; showDetectedFields?: string[]; app?: CoreApp; + scrollElement?: HTMLDivElement; showContextToggle?: (row?: LogRowModel) => boolean; onClickFilterLabel?: (key: string, value: string) => void; onClickFilterOutLabel?: (key: string, value: string) => void; @@ -124,6 +125,7 @@ class UnThemedLogRows extends PureComponent { forceEscape, onLogRowHover, app, + scrollElement, } = this.props; const { renderAll, contextIsOpen } = this.state; const { logsRowsTable } = getLogRowStyles(theme); @@ -173,6 +175,7 @@ class UnThemedLogRows extends PureComponent { toggleContextIsOpen={this.toggleContextIsOpen} onLogRowHover={onLogRowHover} app={app} + scrollElement={scrollElement} /> ))} {hasData && @@ -203,6 +206,7 @@ class UnThemedLogRows extends PureComponent { toggleContextIsOpen={this.toggleContextIsOpen} onLogRowHover={onLogRowHover} app={app} + scrollElement={scrollElement} /> ))} {hasData && !renderAll && (