Skip to content

Commit

Permalink
perf: Optimize Dashboard components (#31242)
Browse files Browse the repository at this point in the history
  • Loading branch information
kgabryje authored Dec 2, 2024
1 parent eab888c commit 24d001e
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 195 deletions.
30 changes: 16 additions & 14 deletions superset-frontend/src/dashboard/components/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ import getBootstrapData from 'src/utils/getBootstrapData';
import getChartIdsFromLayout from '../util/getChartIdsFromLayout';
import getLayoutComponentFromChartId from '../util/getLayoutComponentFromChartId';

import {
slicePropShape,
dashboardInfoPropShape,
dashboardStatePropShape,
} from '../util/propShapes';
import { slicePropShape } from '../util/propShapes';
import {
LOG_ACTIONS_HIDE_BROWSER_TAB,
LOG_ACTIONS_MOUNT_DASHBOARD,
Expand All @@ -51,8 +47,10 @@ const propTypes = {
logEvent: PropTypes.func.isRequired,
clearDataMaskState: PropTypes.func.isRequired,
}).isRequired,
dashboardInfo: dashboardInfoPropShape.isRequired,
dashboardState: dashboardStatePropShape.isRequired,
dashboardId: PropTypes.number.isRequired,
editMode: PropTypes.bool,
isPublished: PropTypes.bool,
hasUnsavedChanges: PropTypes.bool,
slices: PropTypes.objectOf(slicePropShape).isRequired,
activeFilters: PropTypes.object.isRequired,
chartConfiguration: PropTypes.object,
Expand Down Expand Up @@ -96,13 +94,13 @@ class Dashboard extends PureComponent {

componentDidMount() {
const bootstrapData = getBootstrapData();
const { dashboardState, layout } = this.props;
const { editMode, isPublished, layout } = this.props;
const eventData = {
is_soft_navigation: Logger.timeOriginOffset > 0,
is_edit_mode: dashboardState.editMode,
is_edit_mode: editMode,
mount_duration: Logger.getTimestamp(),
is_empty: isDashboardEmpty(layout),
is_published: dashboardState.isPublished,
is_published: isPublished,
bootstrap_data_length: bootstrapData.length,
};
const directLinkComponentId = getLocationHash();
Expand Down Expand Up @@ -130,7 +128,7 @@ class Dashboard extends PureComponent {
const currentChartIds = getChartIdsFromLayout(this.props.layout);
const nextChartIds = getChartIdsFromLayout(nextProps.layout);

if (this.props.dashboardInfo.id !== nextProps.dashboardInfo.id) {
if (this.props.dashboardId !== nextProps.dashboardId) {
// single-page-app navigation check
return;
}
Expand All @@ -157,10 +155,14 @@ class Dashboard extends PureComponent {
}

applyCharts() {
const { hasUnsavedChanges, editMode } = this.props.dashboardState;

const {
activeFilters,
ownDataCharts,
chartConfiguration,
hasUnsavedChanges,
editMode,
} = this.props;
const { appliedFilters, appliedOwnDataCharts } = this;
const { activeFilters, ownDataCharts, chartConfiguration } = this.props;
if (
isFeatureEnabled(FeatureFlag.DashboardCrossFilters) &&
!chartConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ describe('DashboardBuilder', () => {
});

it('should render a BuilderComponentPane if editMode=true and user selects "Insert Components" pane', () => {
const { queryAllByTestId } = setup({ dashboardState: { editMode: true } });
const { queryAllByTestId } = setup({
dashboardState: { ...mockState.dashboardState, editMode: true },
});
const builderComponents = queryAllByTestId('mock-builder-component-pane');
expect(builderComponents.length).toBeGreaterThanOrEqual(1);
});
Expand Down Expand Up @@ -241,7 +243,7 @@ describe('DashboardBuilder', () => {

it('should display a loading spinner when saving is in progress', async () => {
const { findByAltText } = setup({
dashboardState: { dashboardIsSaving: true },
dashboardState: { ...mockState.dashboardState, dashboardIsSaving: true },
});

expect(await findByAltText('Loading...')).toBeVisible();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ const StyledDashboardContent = styled.div<{
`}
`;

const ELEMENT_ON_SCREEN_OPTIONS = {
threshold: [1],
};

const DashboardBuilder: FC<DashboardBuilderProps> = () => {
const dispatch = useDispatch();
const uiConfig = useUiConfig();
Expand Down Expand Up @@ -469,9 +473,9 @@ const DashboardBuilder: FC<DashboardBuilderProps> = () => {
nativeFiltersEnabled,
} = useNativeFilters();

const [containerRef, isSticky] = useElementOnScreen<HTMLDivElement>({
threshold: [1],
});
const [containerRef, isSticky] = useElementOnScreen<HTMLDivElement>(
ELEMENT_ON_SCREEN_OPTIONS,
);

const showFilterBar =
(crossFiltersEnabled || nativeFiltersEnabled) && !editMode;
Expand Down Expand Up @@ -581,6 +585,43 @@ const DashboardBuilder: FC<DashboardBuilderProps> = () => {
? 0
: theme.gridUnit * 8;

const renderChild = useCallback(
adjustedWidth => {
const filterBarWidth = dashboardFiltersOpen
? adjustedWidth
: CLOSED_FILTER_BAR_WIDTH;
return (
<FiltersPanel
width={filterBarWidth}
hidden={isReport}
data-test="dashboard-filters-panel"
>
<StickyPanel ref={containerRef} width={filterBarWidth}>
<ErrorBoundary>
<FilterBar
orientation={FilterBarOrientation.Vertical}
verticalConfig={{
filtersOpen: dashboardFiltersOpen,
toggleFiltersBar: toggleDashboardFiltersOpen,
width: filterBarWidth,
height: filterBarHeight,
offset: filterBarOffset,
}}
/>
</ErrorBoundary>
</StickyPanel>
</FiltersPanel>
);
},
[
dashboardFiltersOpen,
toggleDashboardFiltersOpen,
filterBarHeight,
filterBarOffset,
isReport,
],
);

return (
<DashboardWrapper>
{showFilterBar &&
Expand All @@ -593,33 +634,7 @@ const DashboardBuilder: FC<DashboardBuilderProps> = () => {
maxWidth={OPEN_FILTER_BAR_MAX_WIDTH}
initialWidth={OPEN_FILTER_BAR_WIDTH}
>
{adjustedWidth => {
const filterBarWidth = dashboardFiltersOpen
? adjustedWidth
: CLOSED_FILTER_BAR_WIDTH;
return (
<FiltersPanel
width={filterBarWidth}
hidden={isReport}
data-test="dashboard-filters-panel"
>
<StickyPanel ref={containerRef} width={filterBarWidth}>
<ErrorBoundary>
<FilterBar
orientation={FilterBarOrientation.Vertical}
verticalConfig={{
filtersOpen: dashboardFiltersOpen,
toggleFiltersBar: toggleDashboardFiltersOpen,
width: filterBarWidth,
height: filterBarHeight,
offset: filterBarOffset,
}}
/>
</ErrorBoundary>
</StickyPanel>
</FiltersPanel>
);
}}
{renderChild}
</ResizableSidebar>
</>
)}
Expand Down
Loading

0 comments on commit 24d001e

Please sign in to comment.