Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dashboard): run query for chart only if visible #21488

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions superset-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions superset-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
"react-draggable": "^4.4.3",
"react-gravatar": "^2.6.1",
"react-hot-loader": "^4.12.20",
"react-intersection-observer": "^9.4.0",
"react-js-cron": "^1.2.0",
"react-json-tree": "^0.11.2",
"react-jsonschema-form": "^1.2.0",
Expand Down
49 changes: 33 additions & 16 deletions superset-frontend/src/components/Chart/Chart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
import PropTypes from 'prop-types';
import React from 'react';
import { InView, defaultFallbackInView } from 'react-intersection-observer';
import { styled, logging, t, ensureIsArray } from '@superset-ui/core';

import { isFeatureEnabled, FeatureFlag } from 'src/featureFlags';
Expand Down Expand Up @@ -119,11 +120,18 @@ const MonospaceDiv = styled.div`
white-space: pre-wrap;
`;

defaultFallbackInView(true);

class Chart extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
inView: false,
};
this.handleRenderContainerFailure =
this.handleRenderContainerFailure.bind(this);
this.runQuery = this.runQuery.bind(this);
this.handleChartInView = this.handleChartInView.bind(this);
}

componentDidMount() {
Expand Down Expand Up @@ -151,6 +159,9 @@ class Chart extends React.PureComponent {
}

runQuery() {
if (!this.state.inView) {
return;
}
if (this.props.chartId > 0 && isFeatureEnabled(FeatureFlag.CLIENT_CACHE)) {
// Load saved chart with a GET request
this.props.actions.getSavedChart(
Expand Down Expand Up @@ -193,6 +204,10 @@ class Chart extends React.PureComponent {
});
}

handleChartInView(inView) {
this.setState({ inView });
}

renderErrorMessage(queryResponse) {
const {
chartId,
Expand Down Expand Up @@ -299,22 +314,24 @@ class Chart extends React.PureComponent {
onError={this.handleRenderContainerFailure}
showMessage={false}
>
<Styles
data-ui-anchor="chart"
className="chart-container"
data-test="chart-container"
height={height}
width={width}
>
<div className="slice_container" data-test="slice-container">
<ChartRenderer
{...this.props}
source={this.props.dashboardId ? 'dashboard' : 'explore'}
data-test={this.props.vizType}
/>
</div>
{isLoading && !isDeactivatedViz && <Loading />}
</Styles>
<InView triggerOnce onChange={this.handleChartInView}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a rootMargin to page bottom so charts near the bottom but below the fold can load faster (so users can see less loading states)?

<Styles
data-ui-anchor="chart"
className="chart-container"
data-test="chart-container"
height={height}
width={width}
>
<div className="slice_container" data-test="slice-container">
<ChartRenderer
{...this.props}
source={this.props.dashboardId ? 'dashboard' : 'explore'}
data-test={this.props.vizType}
/>
</div>
{isLoading && !isDeactivatedViz && <Loading />}
</Styles>
</InView>
</ErrorBoundary>
);
}
Expand Down