From e0619e8aa7e95570013573445d9340f1d37dfaba Mon Sep 17 00:00:00 2001 From: Sven Grossmann Date: Wed, 28 Jun 2023 14:55:22 +0200 Subject: [PATCH] LogContext: Add button to scroll to center (#70821) add scroll to center button --- .../log-context/LogContextButtons.test.tsx | 12 +++++++-- .../log-context/LogContextButtons.tsx | 26 ++++++++++++++++--- .../log-context/LogRowContextModal.tsx | 16 +++++++++--- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/public/app/features/logs/components/log-context/LogContextButtons.test.tsx b/public/app/features/logs/components/log-context/LogContextButtons.test.tsx index 2d3ac411445..41e8cc4ef6d 100644 --- a/public/app/features/logs/components/log-context/LogContextButtons.test.tsx +++ b/public/app/features/logs/components/log-context/LogContextButtons.test.tsx @@ -7,7 +7,7 @@ import { LogContextButtons } from './LogContextButtons'; describe('LogContextButtons', () => { it('should call onChangeWrapLines when the checkbox is used, case 1', async () => { const onChangeWrapLines = jest.fn(); - render(); + render(); const wrapLinesBox = screen.getByRole('checkbox', { name: 'Wrap lines', }); @@ -18,7 +18,7 @@ describe('LogContextButtons', () => { it('should call onChangeWrapLines when the checkbox is used, case 2', async () => { const onChangeWrapLines = jest.fn(); - render(); + render(); const wrapLinesBox = screen.getByRole('checkbox', { name: 'Wrap lines', }); @@ -26,4 +26,12 @@ describe('LogContextButtons', () => { expect(onChangeWrapLines).toHaveBeenCalledTimes(1); expect(onChangeWrapLines).toHaveBeenCalledWith(false); }); + + it('should call onScrollCenterClick when the button is clicked', async () => { + const onScrollCenterClick = jest.fn(); + render(); + const scrollButton = screen.getByRole('button'); + await userEvent.click(scrollButton); + expect(onScrollCenterClick).toHaveBeenCalledTimes(1); + }); }); diff --git a/public/app/features/logs/components/log-context/LogContextButtons.tsx b/public/app/features/logs/components/log-context/LogContextButtons.tsx index a1d578353a6..01e24518c02 100644 --- a/public/app/features/logs/components/log-context/LogContextButtons.tsx +++ b/public/app/features/logs/components/log-context/LogContextButtons.tsx @@ -1,15 +1,28 @@ +import { css } from '@emotion/css'; import React, { useCallback } from 'react'; +import { GrafanaTheme2 } from '@grafana/data'; import { reportInteraction } from '@grafana/runtime'; -import { InlineSwitch } from '@grafana/ui'; +import { Button, InlineSwitch, useStyles2 } from '@grafana/ui'; export type Props = { wrapLines?: boolean; onChangeWrapLines: (wrapLines: boolean) => void; + onScrollCenterClick: () => void; }; +function getStyles(theme: GrafanaTheme2) { + return { + buttons: css({ + display: 'flex', + gap: theme.spacing(1), + }), + }; +} + export const LogContextButtons = (props: Props) => { - const { wrapLines, onChangeWrapLines } = props; + const styles = useStyles2(getStyles); + const { wrapLines, onChangeWrapLines, onScrollCenterClick } = props; const internalOnChangeWrapLines = useCallback( (event: React.FormEvent) => { const state = event.currentTarget.checked; @@ -21,5 +34,12 @@ export const LogContextButtons = (props: Props) => { [onChangeWrapLines] ); - return ; + return ( +
+ + +
+ ); }; diff --git a/public/app/features/logs/components/log-context/LogRowContextModal.tsx b/public/app/features/logs/components/log-context/LogRowContextModal.tsx index 191e81913be..648e78665f7 100644 --- a/public/app/features/logs/components/log-context/LogRowContextModal.tsx +++ b/public/app/features/logs/components/log-context/LogRowContextModal.tsx @@ -362,6 +362,11 @@ export const LogRowContextModal: React.FunctionComponent { + preEntryElement.current?.scrollIntoView({ block: 'center' }); + entryElement.current?.scrollIntoView({ block: 'center' }); + }, [preEntryElement, entryElement]); + useLayoutEffect(() => { const scrollE = scrollElement.current; if (scrollE == null) { @@ -373,8 +378,7 @@ export const LogRowContextModal: React.FunctionComponent
- +