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

fix: time grain can't be removed in explore #21644

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
isAdhocColumn,
isPhysicalColumn,
ensureIsArray,
isDefined,
} from '@superset-ui/core';

import {
Expand Down Expand Up @@ -183,7 +184,16 @@ const granularity: SharedControlConfig<'SelectControl'> = {
const time_grain_sqla: SharedControlConfig<'SelectControl'> = {
type: 'SelectControl',
label: TIME_FILTER_LABELS.time_grain_sqla,
default: 'P1D',
initialValue: (control: ControlState, state: ControlPanelState) => {
if (!isDefined(state)) {
// If a chart is in a Dashboard, the ControlPanelState is empty.
return control.value;
}
// If a chart is a new one that isn't saved, the 'time_grain_sqla' isn't in the form_data.
return 'time_grain_sqla' in (state?.form_data ?? {})
? state.form_data?.time_grain_sqla
: 'P1D';
Comment on lines +192 to +195
Copy link
Member

Choose a reason for hiding this comment

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

Is there some reason we're not just doing state.form_data?.time_grain_sqla || 'P1D' here?

Copy link
Member Author

Choose a reason for hiding this comment

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

When Time Grain control be set to "Original value" or just removal, the time_grain_sqla in the form_data will be null. If time_grain_sqla get a null value, the 'P1D' will always be set to control, in this case, the chart will never use an empty time grain value.
For instance:

  1. make a Line Chart and set Original value to Time Grain, and then save it; next, go to chart list; then, reopen the chart.
  2. if use state.form_data?.time_grain_sqla || 'P1D' the Time Grain will return P1D

},
description: t(
'The time granularity for the visualization. This ' +
'applies a date transformation to alter ' +
Expand All @@ -192,7 +202,7 @@ const time_grain_sqla: SharedControlConfig<'SelectControl'> = {
'engine basis in the Superset source code.',
),
mapStateToProps: ({ datasource }) => ({
choices: (datasource as Dataset)?.time_grain_sqla || null,
choices: (datasource as Dataset)?.time_grain_sqla || [],
}),
visibility: ({ controls }) => {
if (!isFeatureEnabled(FeatureFlag.GENERIC_CHART_AXES)) {
Expand Down