rusackas commented on code in PR #41740: URL: https://github.com/apache/superset/pull/41740#discussion_r3525757425
########## superset-frontend/src/dashboard/components/Header/HeadlessAutoRefresh.tsx: ########## @@ -0,0 +1,87 @@ +/** + * 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 { useMemo } from 'react'; +import { bindActionCreators } from 'redux'; +import { useDispatch, useSelector } from 'react-redux'; +import { RootState } from 'src/dashboard/types'; +import { useChartIds } from 'src/dashboard/util/charts/useChartIds'; +import { onRefresh, setRefreshFrequency } from '../../actions/dashboardState'; +import { logEvent } from '../../../logger/actions'; +import { useHeaderAutoRefresh } from './useHeaderAutoRefresh'; + +/** + * Headless component that drives the dashboard auto-refresh timer when the + * dashboard header is not rendered (e.g. standalone mode or `hideTitle: true` + * in embedded dashboards). The auto-refresh logic lives in the header, so + * hiding the header would otherwise stop the refresh interval from ever + * starting. Rendering this component keeps the timer running independently of + * header visibility. It renders nothing. + */ +const HeadlessAutoRefresh = (): null => { + const dispatch = useDispatch(); + const chartIds = useChartIds(); + const dashboardId = useSelector((state: RootState) => state.dashboardInfo.id); + const refreshFrequency = useSelector( + (state: RootState) => state.dashboardState.refreshFrequency ?? 0, + ); + const timedRefreshImmuneSlices = useSelector( + (state: RootState) => + state.dashboardInfo.metadata?.timed_refresh_immune_slices || [], + ); Review Comment: Good catch, wrapped the fallback in `useMemo` to match the header so it stops churning the timer. ########## superset-frontend/src/dashboard/components/Header/HeadlessAutoRefresh.test.tsx: ########## @@ -0,0 +1,97 @@ +/** + * 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 { render } from 'spec/helpers/testing-library'; +import { AutoRefreshProvider } from 'src/dashboard/contexts/AutoRefreshContext'; +import HeadlessAutoRefresh from './HeadlessAutoRefresh'; + +const mockUseHeaderAutoRefresh = jest.fn(); + +jest.mock('./useHeaderAutoRefresh', () => ({ + useHeaderAutoRefresh: (props: unknown) => { + mockUseHeaderAutoRefresh(props); + return { + forceRefresh: jest.fn(), + handlePauseToggle: jest.fn(), + autoRefreshPauseOnInactiveTab: false, + setPauseOnInactiveTab: jest.fn(), + }; + }, +})); + +const initialState = { + dashboardInfo: { + id: 42, + metadata: { timed_refresh_immune_slices: [7] }, + common: { conf: { DASHBOARD_AUTO_REFRESH_MODE: 'fetch' } }, + }, + dashboardState: { + refreshFrequency: 30, + sliceIds: [1, 2, 3], + }, + charts: { + 1: { id: 1, chartUpdateStartTime: 0, chartUpdateEndTime: 0 }, + 2: { id: 2, chartUpdateStartTime: 0, chartUpdateEndTime: 0 }, + }, +}; + +beforeEach(() => { + mockUseHeaderAutoRefresh.mockClear(); +}); + +test('drives the auto-refresh hook from redux state without a header', () => { + const { container } = render( + <AutoRefreshProvider> + <HeadlessAutoRefresh /> + </AutoRefreshProvider>, + { useRedux: true, initialState }, + ); + + // The component renders nothing but must still start the refresh timer. + expect(container).toBeEmptyDOMElement(); + expect(mockUseHeaderAutoRefresh).toHaveBeenCalledTimes(1); + expect(mockUseHeaderAutoRefresh).toHaveBeenCalledWith( + expect.objectContaining({ + chartIds: [1, 2, 3], + dashboardId: 42, + refreshFrequency: 30, + timedRefreshImmuneSlices: [7], + autoRefreshMode: 'fetch', + isLoading: false, + }), + ); Review Comment: Fair, added assertions that the three bound action creators come through as functions. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
