DashboardPage: Remember scroll position when coming back panel edit / view panel (#47639)

* DashboardPage: Remember scroll position when coming back panel edit / view panel

* Use scollElement callback

* Fixed ts issue
This commit is contained in:
Torkel Ödegaard
2022-04-14 19:58:08 +02:00
committed by GitHub
parent 6a8f9b7ebc
commit a126c07e54

View File

@@ -4,7 +4,6 @@ import { connect, ConnectedProps } from 'react-redux';
import { locationService } from '@grafana/runtime'; import { locationService } from '@grafana/runtime';
import { selectors } from '@grafana/e2e-selectors'; import { selectors } from '@grafana/e2e-selectors';
import { CustomScrollbar, stylesFactory, Themeable2, withTheme2 } from '@grafana/ui'; import { CustomScrollbar, stylesFactory, Themeable2, withTheme2 } from '@grafana/ui';
import { createErrorNotification } from 'app/core/copy/appNotification'; import { createErrorNotification } from 'app/core/copy/appNotification';
import { Branding } from 'app/core/components/Branding/Branding'; import { Branding } from 'app/core/components/Branding/Branding';
import { DashboardGrid } from '../dashgrid/DashboardGrid'; import { DashboardGrid } from '../dashgrid/DashboardGrid';
@@ -78,6 +77,7 @@ export interface State {
showLoadingState: boolean; showLoadingState: boolean;
panelNotFound: boolean; panelNotFound: boolean;
editPanelAccessDenied: boolean; editPanelAccessDenied: boolean;
scrollElement?: HTMLDivElement;
} }
export class UnthemedDashboardPage extends PureComponent<Props, State> { export class UnthemedDashboardPage extends PureComponent<Props, State> {
@@ -224,14 +224,14 @@ export class UnthemedDashboardPage extends PureComponent<Props, State> {
} }
if (dashboard.canEditPanel(panel)) { if (dashboard.canEditPanel(panel)) {
return { ...state, editPanel: panel }; return { ...state, editPanel: panel, rememberScrollTop: state.scrollElement?.scrollTop };
} else { } else {
return { ...state, editPanelAccessDenied: true }; return { ...state, editPanelAccessDenied: true };
} }
} }
// Leaving edit mode // Leaving edit mode
else if (state.editPanel && !urlEditPanelId) { else if (state.editPanel && !urlEditPanelId) {
return { ...state, editPanel: null }; return { ...state, editPanel: null, updateScrollTop: state.rememberScrollTop };
} }
// Entering view mode // Entering view mode
@@ -245,11 +245,7 @@ export class UnthemedDashboardPage extends PureComponent<Props, State> {
// Should move this state out of dashboard in the future // Should move this state out of dashboard in the future
dashboard.initViewPanel(panel); dashboard.initViewPanel(panel);
return { return { ...state, viewPanel: panel, rememberScrollTop: state.scrollElement?.scrollTop, updateScrollTop: 0 };
...state,
viewPanel: panel,
updateScrollTop: 0,
};
} }
// Leaving view mode // Leaving view mode
else if (state.viewPanel && !urlViewPanelId) { else if (state.viewPanel && !urlViewPanelId) {
@@ -290,6 +286,10 @@ export class UnthemedDashboardPage extends PureComponent<Props, State> {
this.setState({ updateScrollTop: 0 }); this.setState({ updateScrollTop: 0 });
}; };
setScrollRef = (scrollElement: HTMLDivElement): void => {
this.setState({ scrollElement });
};
getInspectPanel() { getInspectPanel() {
const { dashboard, queryParams } = this.props; const { dashboard, queryParams } = this.props;
@@ -346,6 +346,7 @@ export class UnthemedDashboardPage extends PureComponent<Props, State> {
<div className={styles.dashboardScroll}> <div className={styles.dashboardScroll}>
<CustomScrollbar <CustomScrollbar
autoHeightMin="100%" autoHeightMin="100%"
scrollRefCallback={this.setScrollRef}
scrollTop={updateScrollTop} scrollTop={updateScrollTop}
hideHorizontalTrack={true} hideHorizontalTrack={true}
updateAfterMountMs={500} updateAfterMountMs={500}