rusackas commented on code in PR #37625:
URL: https://github.com/apache/superset/pull/37625#discussion_r2770115672


##########
superset-frontend/src/dashboard/components/Header/useHeaderActionsDropdownMenu.tsx:
##########
@@ -283,7 +288,12 @@ export const useHeaderActionsMenu = ({
     }
 
     // Set filter mapping
-    if (editMode && !isEmpty(dashboardInfo?.metadata?.filter_scopes)) {
+    if (
+      editMode &&
+      !isEmpty(
+        (dashboardInfo?.metadata as Record<string, unknown>)?.filter_scopes,

Review Comment:
   Same — will be addressed by the `DashboardMetadata` interface follow-up.



##########
superset-frontend/src/dashboard/reducers/dashboardInfo.ts:
##########
@@ -0,0 +1,320 @@
+/**
+ * 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 {
+  ChartCustomization,
+  ChartCustomizationDivider,
+  ColumnOption,
+  JsonObject,
+} from '@superset-ui/core';
+import {
+  DASHBOARD_INFO_UPDATED,
+  SET_FILTER_BAR_ORIENTATION,
+  SET_CROSS_FILTERS_ENABLED,
+  DASHBOARD_INFO_FILTERS_CHANGED,
+} from '../actions/dashboardInfo';
+import {
+  SAVE_CHART_CUSTOMIZATION_COMPLETE,
+  SET_CHART_CUSTOMIZATION_DATA_LOADING,
+  SET_CHART_CUSTOMIZATION_DATA,
+  SET_PENDING_CHART_CUSTOMIZATION,
+  CLEAR_PENDING_CHART_CUSTOMIZATION,
+  CLEAR_ALL_PENDING_CHART_CUSTOMIZATIONS,
+  CLEAR_ALL_CHART_CUSTOMIZATIONS,
+} from '../actions/chartCustomizationActions';
+import { HYDRATE_DASHBOARD } from '../actions/hydrate';
+import { DashboardInfo, FilterBarOrientation } from '../types';
+
+interface FilterConfigItem extends JsonObject {
+  id: string;
+  chartsInScope?: number[];
+  tabsInScope?: string[];
+  type?: string;
+  targets?: { datasetId?: number; [key: string]: unknown }[];
+}
+
+interface DashboardInfoAction {
+  type: string;
+  newInfo?: Partial<DashboardInfo> & {
+    theme_id?: number | null;
+  };
+  filterBarOrientation?: FilterBarOrientation;
+  crossFiltersEnabled?: boolean;
+  chartCustomization?: (ChartCustomization | ChartCustomizationDivider)[];
+  itemId?: string;
+  isLoading?: boolean;
+  data?: ColumnOption[];
+  pendingCustomization?: ChartCustomization;
+  [key: string]: unknown;
+}
+
+interface HydrateDashboardAction {
+  type: typeof HYDRATE_DASHBOARD;
+  data: {
+    dashboardInfo: DashboardInfo;
+    [key: string]: unknown;
+  };
+}
+
+type DashboardInfoReducerAction = DashboardInfoAction | HydrateDashboardAction;
+
+type DashboardInfoState = Partial<DashboardInfo> & {
+  last_modified_time?: number;
+  [key: string]: unknown;
+};
+
+function isHydrateAction(
+  action: DashboardInfoReducerAction,
+): action is HydrateDashboardAction {
+  return action.type === HYDRATE_DASHBOARD;
+}
+
+function preserveScopes(
+  existingConfig: FilterConfigItem[] | undefined,
+  incomingConfig: FilterConfigItem[] | undefined,
+): FilterConfigItem[] {
+  const existingScopesMap = (existingConfig || []).reduce<
+    Record<string, { chartsInScope?: number[]; tabsInScope?: string[] }>
+  >((acc, item) => {
+    if (item.chartsInScope != null || item.tabsInScope != null) {
+      acc[item.id] = {
+        chartsInScope: item.chartsInScope,
+        tabsInScope: item.tabsInScope,
+      };
+    }
+    return acc;
+  }, {});
+
+  return (incomingConfig || []).map(item => {
+    const existingScopes = existingScopesMap[item.id];
+    if (item.chartsInScope == null && existingScopes) {
+      return {
+        ...item,
+        chartsInScope: existingScopes.chartsInScope,
+        tabsInScope: existingScopes.tabsInScope,
+      };
+    }
+    return item;
+  });
+}
+
+export default function dashboardInfoReducer(
+  state: DashboardInfoState = {},
+  action: DashboardInfoReducerAction,
+): DashboardInfoState {
+  switch (action.type) {
+    case DASHBOARD_INFO_UPDATED: {
+      const dashAction = action as DashboardInfoAction;
+      const newInfo = dashAction.newInfo || {};
+      const { theme_id: themeId, ...otherInfo } = newInfo;
+      const updatedState: DashboardInfoState = {
+        ...state,
+        ...otherInfo,
+        last_modified_time: Math.round(new Date().getTime() / 1000),
+      };
+
+      if (themeId !== undefined) {
+        if (themeId === null) {
+          updatedState.theme = null;
+        } else {
+          updatedState.theme = { id: themeId, name: `Theme ${themeId}` };
+        }
+      }
+
+      return updatedState;
+    }
+    case DASHBOARD_INFO_FILTERS_CHANGED: {
+      const dashAction = action as DashboardInfoAction;
+      const existingConfig =
+        (state.metadata?.native_filter_configuration as FilterConfigItem[]) ||
+        [];
+      const existingScopesMap = existingConfig.reduce<
+        Record<string, { chartsInScope?: number[]; tabsInScope?: string[] }>
+      >((acc, filter) => {
+        if (filter.chartsInScope != null || filter.tabsInScope != null) {
+          acc[filter.id] = {
+            chartsInScope: filter.chartsInScope,
+            tabsInScope: filter.tabsInScope,
+          };
+        }
+        return acc;
+      }, {});
+
+      const newConfigWithScopes = (
+        (dashAction.newInfo as FilterConfigItem[]) || []
+      ).map((filter: FilterConfigItem) => {
+        const existingScopes = existingScopesMap[filter.id];
+        if (filter.chartsInScope == null && existingScopes) {
+          return {
+            ...filter,
+            chartsInScope: existingScopes.chartsInScope,
+            tabsInScope: existingScopes.tabsInScope,
+          };
+        }
+        return filter;
+      });
+
+      return {
+        ...state,
+        metadata: {
+          ...state.metadata,
+          native_filter_configuration: newConfigWithScopes,
+        } as DashboardInfo['metadata'],
+        last_modified_time: Math.round(new Date().getTime() / 1000),
+      };
+    }
+    case HYDRATE_DASHBOARD: {
+      if (!isHydrateAction(action)) return state;
+      const incomingMetadata = action.data.dashboardInfo.metadata || {};
+
+      const mergedFilterConfig = preserveScopes(
+        state.metadata?.native_filter_configuration as
+          | FilterConfigItem[]
+          | undefined,
+        incomingMetadata.native_filter_configuration as
+          | FilterConfigItem[]
+          | undefined,
+      );
+
+      const mergedCustomizationConfig = preserveScopes(
+        state.metadata?.chart_customization_config as
+          | FilterConfigItem[]
+          | undefined,
+        incomingMetadata.chart_customization_config as
+          | FilterConfigItem[]
+          | undefined,
+      );

Review Comment:
   Strong agree — a `DashboardMetadata` interface would centralize the type for 
`metadata` and eliminate casts across at least 4 files (this reducer, 
useHeaderActionsDropdownMenu, Chart.tsx selectors, etc.). The challenge is that 
`metadata` is a JSON blob from the backend with many optional fields that vary 
by dashboard version. Defining the interface requires auditing all the places 
metadata is read/written to ensure completeness. Definitely worth a dedicated 
follow-up PR.



-- 
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]

Reply via email to