From 44f8e861a5b2a7f901ea09311c7753591b9e5401 Mon Sep 17 00:00:00 2001 From: FrancescoCastaldi Date: Mon, 12 Oct 2026 23:45:50 +0200 Subject: [PATCH] fix(dashboard): preserve custom refresh interval draft, clamp minimum, and support options prop (#44981) --- .../RefreshFrequencySelect.test.tsx | 99 ++++++++++++++++++- .../RefreshFrequencySelect.tsx | 43 ++++---- 2 files changed, 124 insertions(+), 18 deletions(-) diff --git a/superset-frontend/src/dashboard/components/RefreshFrequency/RefreshFrequencySelect.test.tsx b/superset-frontend/src/dashboard/components/RefreshFrequency/RefreshFrequencySelect.test.tsx index e8493de151db..c762cd3cf3c8 100644 --- a/superset-frontend/src/dashboard/components/RefreshFrequency/RefreshFrequencySelect.test.tsx +++ b/superset-frontend/src/dashboard/components/RefreshFrequency/RefreshFrequencySelect.test.tsx @@ -18,11 +18,18 @@ */ import { t } from '@apache-superset/core/translation'; import { useState } from 'react'; -import { render, screen, userEvent } from 'spec/helpers/testing-library'; +import { + fireEvent, + render, + screen, + userEvent, +} from 'spec/helpers/testing-library'; import { CUSTOM_REFRESH_FREQUENCY, + getCustomValue, getRefreshFrequencyOptions, getRefreshWarningMessage, + isPresetValue, RefreshFrequencySelect, REFRESH_FREQUENCY_OPTIONS, validateRefreshFrequency, @@ -299,3 +306,93 @@ test('selecting a preset still snaps selection to that preset', async () => { expect(screen.getByRole('radio', { name: /Custom/i })).not.toBeChecked(); expect(screen.getByRole('spinbutton')).toBeDisabled(); }); + +test('isPresetValue and getCustomValue work with default and custom options', () => { + expect(isPresetValue(10)).toBe(true); + expect(isPresetValue(42)).toBe(false); + expect(isPresetValue(CUSTOM_REFRESH_FREQUENCY)).toBe(false); + + expect(getCustomValue(10)).toBe(''); + expect(getCustomValue(0)).toBe(''); + expect(getCustomValue(42)).toBe('42'); +}); + +test('uses options prop override when provided', () => { + const propOptions: [number, string][] = [ + [0, "Don't refresh"], + [5, '5 seconds'], + [25, '25 seconds'], + ]; + + render( + , + { + useRedux: true, + initialState: { + common: { conf: { DASHBOARD_AUTO_REFRESH_INTERVALS: INTERVALS } }, + }, + }, + ); + + expect(screen.getByRole('radio', { name: '5 seconds' })).toBeInTheDocument(); + expect(screen.getByRole('radio', { name: '25 seconds' })).toBeInTheDocument(); + expect( + screen.queryByRole('radio', { name: '10 minutes' }), + ).not.toBeInTheDocument(); +}); + +test('preserves previously entered custom value when switching between preset and custom', async () => { + render(, { + useRedux: true, + initialState: { + common: { conf: { DASHBOARD_AUTO_REFRESH_INTERVALS: INTERVALS } }, + }, + }); + + const customRadio = screen.getByRole('radio', { name: /Custom/i }); + const presetRadio = screen.getByRole('radio', { name: '10 minutes' }); + const input = screen.getByRole('spinbutton'); + + await userEvent.click(customRadio); + fireEvent.change(input, { target: { value: '120' } }); + expect(input).toHaveValue(120); + + await userEvent.click(presetRadio); + expect(presetRadio).toBeChecked(); + expect(input).toBeDisabled(); + + await userEvent.click(customRadio); + expect(customRadio).toBeChecked(); + expect(input).toBeEnabled(); + expect(input).toHaveValue(120); +}); + +test('clamps custom draft below minimum interval when switching preset to custom', async () => { + render(, { + useRedux: true, + initialState: { + common: { conf: { DASHBOARD_AUTO_REFRESH_INTERVALS: INTERVALS } }, + }, + }); + + const customRadio = screen.getByRole('radio', { name: /Custom/i }); + const presetRadio = screen.getByRole('radio', { name: '10 minutes' }); + const input = screen.getByRole('spinbutton'); + + await userEvent.click(customRadio); + fireEvent.change(input, { target: { value: '-5' } }); + expect(input).toHaveValue(-5); + + await userEvent.click(presetRadio); + expect(presetRadio).toBeChecked(); + expect(input).toBeDisabled(); + + await userEvent.click(customRadio); + expect(customRadio).toBeChecked(); + expect(input).toBeEnabled(); + expect(input).toHaveValue(1); +}); diff --git a/superset-frontend/src/dashboard/components/RefreshFrequency/RefreshFrequencySelect.tsx b/superset-frontend/src/dashboard/components/RefreshFrequency/RefreshFrequencySelect.tsx index 6d9385cf0ff7..69c6232191ee 100644 --- a/superset-frontend/src/dashboard/components/RefreshFrequency/RefreshFrequencySelect.tsx +++ b/superset-frontend/src/dashboard/components/RefreshFrequency/RefreshFrequencySelect.tsx @@ -75,7 +75,10 @@ export const REFRESH_FREQUENCY_OPTIONS = [ { value: CUSTOM_REFRESH_FREQUENCY, label: t('Custom') }, ]; -const isPresetValue = (frequency: number, options: RefreshFrequencyOption[]) => +export const isPresetValue = ( + frequency: number, + options: RefreshFrequencyOption[] = REFRESH_FREQUENCY_OPTIONS, +) => options.some( option => option.value === frequency && option.value !== CUSTOM_REFRESH_FREQUENCY, @@ -131,9 +134,9 @@ export const getRefreshFrequencyOptions = ( : REFRESH_FREQUENCY_OPTIONS; }; -const getCustomValue = ( +export const getCustomValue = ( frequency: number, - options: RefreshFrequencyOption[], + options: RefreshFrequencyOption[] = REFRESH_FREQUENCY_OPTIONS, ) => !isPresetValue(frequency, options) && frequency > 0 ? frequency.toString() @@ -153,9 +156,10 @@ const normalizeRefreshLimitSeconds = ( return refreshLimit; }; -interface RefreshFrequencySelectProps { +export interface RefreshFrequencySelectProps { value: number; onChange: (value: number) => void; + options?: [number, string][]; } /** @@ -165,6 +169,7 @@ interface RefreshFrequencySelectProps { export const RefreshFrequencySelect = ({ value, onChange, + options: optionsProp, }: RefreshFrequencySelectProps) => { const configuredIntervals = useSelector( (state: RootState) => @@ -175,8 +180,8 @@ export const RefreshFrequencySelect = ({ state.common?.conf?.DASHBOARD_AUTO_REFRESH_INTERVALS, ); const options = useMemo( - () => getRefreshFrequencyOptions(configuredIntervals), - [configuredIntervals], + () => getRefreshFrequencyOptions(optionsProp ?? configuredIntervals), + [optionsProp, configuredIntervals], ); const presets = options.filter( option => option.value !== CUSTOM_REFRESH_FREQUENCY, @@ -207,15 +212,15 @@ export const RefreshFrequencySelect = ({ ? value : CUSTOM_REFRESH_FREQUENCY; setRadioSelection(selection); - setCustomValue( - selection === CUSTOM_REFRESH_FREQUENCY - ? // getCustomValue would blank a value that collides with a preset; - // a custom-originated value is the input's own text. - cameFromCustomControls + if (selection === CUSTOM_REFRESH_FREQUENCY) { + setCustomValue( + // getCustomValue would blank a value that collides with a preset; + // a custom-originated value is the input's own text. + cameFromCustomControls ? value.toString() - : getCustomValue(value, options) - : '', - ); + : getCustomValue(value, options), + ); + } if (!cameFromCustomControls) { lastCustomEmittedValue.current = null; } @@ -226,11 +231,15 @@ export const RefreshFrequencySelect = ({ setRadioSelection(selectedValue); if (selectedValue === CUSTOM_REFRESH_FREQUENCY) { - // Custom selected - use current custom value or minimum - const numValue = parseInt(customValue, 10) || MINIMUM_REFRESH_INTERVAL; + // Custom selected - use current custom value if >= minimum, otherwise fallback to minimum + const parsed = parseInt(customValue, 10); + const numValue = + !Number.isNaN(parsed) && parsed >= MINIMUM_REFRESH_INTERVAL + ? parsed + : MINIMUM_REFRESH_INTERVAL; lastCustomEmittedValue.current = numValue; onChange(numValue); - if (!customValue) { + if (Number.isNaN(parsed) || parsed < MINIMUM_REFRESH_INTERVAL) { setCustomValue(MINIMUM_REFRESH_INTERVAL.toString()); } } else {