Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
* specific language governing permissions and limitations
* under the License.
*/
import { Fragment, useCallback, memo } from 'react';
import { Fragment, useCallback, memo, useEffect } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { useDispatch, useSelector } from 'react-redux';
import { t } from '@superset-ui/core';
import { styled } from '@apache-superset/core/ui';

import { EditableTitle, EmptyState } from '@superset-ui/core/components';
import { setEditMode } from 'src/dashboard/actions/dashboardState';
import { setEditMode, onRefresh } from 'src/dashboard/actions/dashboardState';
import getChartIdsFromComponent from 'src/dashboard/util/getChartIdsFromComponent';
import DashboardComponent from 'src/dashboard/containers/DashboardComponent';
import AnchorLink from 'src/dashboard/components/AnchorLink';
import {
Expand All @@ -37,6 +38,9 @@ import { TAB_TYPE } from 'src/dashboard/util/componentTypes';
export const RENDER_TAB = 'RENDER_TAB';
export const RENDER_TAB_CONTENT = 'RENDER_TAB_CONTENT';

// Delay before refreshing charts to ensure they are fully mounted
const CHART_MOUNT_DELAY = 100;

const propTypes = {
dashboardId: PropTypes.number.isRequired,
id: PropTypes.string.isRequired,
Expand Down Expand Up @@ -114,6 +118,49 @@ const renderDraggableContent = dropProps =>
const Tab = props => {
const dispatch = useDispatch();
const canEdit = useSelector(state => state.dashboardInfo.dash_edit_perm);
const dashboardLayout = useSelector(state => state.dashboardLayout.present);
const lastRefreshTime = useSelector(
state => state.dashboardState.lastRefreshTime,
);
const tabActivationTimes = useSelector(
state => state.dashboardState.tabActivationTimes || {},
);
const dashboardInfo = useSelector(state => state.dashboardInfo);
Comment on lines 114 to +129

This comment was marked as resolved.


// Check if tab needs refresh when it becomes visible
useEffect(() => {
Comment thread
msyavuz marked this conversation as resolved.
if (props.renderType === RENDER_TAB_CONTENT && props.isComponentVisible) {
const tabId = props.id;
const tabActivationTime = tabActivationTimes[tabId] || 0;

// If a refresh occurred while this tab was inactive,
// refresh the charts in this tab now that it's visible
if (
lastRefreshTime &&
tabActivationTime &&
lastRefreshTime > tabActivationTime
) {
const chartIds = getChartIdsFromComponent(tabId, dashboardLayout);
if (chartIds.length > 0) {
// Small delay to ensure charts are fully mounted
setTimeout(() => {
// Refresh charts in this tab
dispatch(onRefresh(chartIds, true, 0, dashboardInfo.id));
}, CHART_MOUNT_DELAY);
Comment thread
msyavuz marked this conversation as resolved.
Outdated
}
}
}
}, [
props.isComponentVisible,
props.renderType,
props.id,
lastRefreshTime,
tabActivationTimes,
dashboardLayout,
Comment thread
EnxDev marked this conversation as resolved.
dashboardInfo.id,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A bit worried about the dependancy array, have seen that dependancy array might sometimes cause an infinite number of calls , especially , dispatch and dashboardlayout which can be a huge object

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't see any issues from manual testing

dispatch,
]);
Comment on lines +131 to +157

This comment was marked as resolved.


const handleChangeTab = useCallback(
({ pathToTabIndex }) => {
props.setDirectPathToChild(pathToTabIndex);
Expand Down
20 changes: 19 additions & 1 deletion superset-frontend/src/dashboard/reducers/dashboardState.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ import { HYDRATE_DASHBOARD } from '../actions/hydrate';
export default function dashboardStateReducer(state = {}, action) {
const actionHandlers = {
[HYDRATE_DASHBOARD]() {
return { ...state, ...action.data.dashboardState };
const hydratedState = { ...state, ...action.data.dashboardState };
// Initialize tab activation times for initially active tabs
if (hydratedState.activeTabs && hydratedState.activeTabs.length > 0) {
const now = Date.now();
hydratedState.tabActivationTimes =
hydratedState.tabActivationTimes || {};
hydratedState.activeTabs.forEach(tabId => {
hydratedState.tabActivationTimes[tabId] = now;
});
}
return hydratedState;
},
[ADD_SLICE]() {
const updatedSliceIds = new Set(state.sliceIds);
Expand Down Expand Up @@ -181,6 +191,7 @@ export default function dashboardStateReducer(state = {}, action) {
return {
...state,
isRefreshing: true,
lastRefreshTime: Date.now(),
};
},
[ON_FILTERS_REFRESH]() {
Expand Down Expand Up @@ -216,10 +227,17 @@ export default function dashboardStateReducer(state = {}, action) {
.difference(new Set(action.activeTabs))
.union(new Set(action.inactiveTabs));

// Track when each tab was last activated
const tabActivationTimes = { ...(state.tabActivationTimes || {}) };
action.activeTabs.forEach(tabId => {
tabActivationTimes[tabId] = Date.now();
});

return {
...state,
inactiveTabs: Array.from(newInactiveTabs),
activeTabs: Array.from(newActiveTabs.union(new Set(action.activeTabs))),
tabActivationTimes,
};
},
[SET_ACTIVE_TABS]() {
Expand Down
29 changes: 29 additions & 0 deletions superset-frontend/src/dashboard/util/charts/useAllChartIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { useSelector } from 'react-redux';
import { useMemo } from 'react';
import { RootState } from 'src/dashboard/types';
import getChartIdsFromLayout from '../getChartIdsFromLayout';

export const useAllChartIds = () => {
const layout = useSelector(
(state: RootState) => state.dashboardLayout.present,
);
return useMemo(() => getChartIdsFromLayout(layout), [layout]);

This comment was marked as resolved.

};
44 changes: 44 additions & 0 deletions superset-frontend/src/dashboard/util/getChartIdsFromComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { CHART_TYPE } from './componentTypes';
import type { DashboardLayout } from '../types';

export default function getChartIdsFromComponent(
componentId: string,
layout: DashboardLayout,
): number[] {
Comment on lines +22 to +25

This comment was marked as resolved.

const chartIds: number[] = [];
const component = layout[componentId];

if (!component) return chartIds;

// If this component is a chart, add its ID
if (component.type === CHART_TYPE && component.meta?.chartId) {
chartIds.push(component.meta.chartId);
}

// Recursively check children
if (component.children) {
component.children.forEach((childId: string) => {
chartIds.push(...getChartIdsFromComponent(childId, layout));
});
}

return chartIds;
}
Loading