codeant-ai-for-open-source[bot] commented on code in PR #34785:
URL: https://github.com/apache/superset/pull/34785#discussion_r3664363577


##########
superset-frontend/src/dashboard/hooks/useDashboardFormData.ts:
##########
@@ -0,0 +1,108 @@
+/**
+ * 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 { useSelector } from 'react-redux';
+import { RootState, DashboardContextFormData } from '../types';
+import { getExtraFormData } from '../components/nativeFilters/utils';
+import { getAllActiveFilters } from '../util/activeAllDashboardFilters';
+import { getFilterIdsAppliedOnChart } from 
'../util/getFilterIdsAppliedOnChart';
+
+/**
+ * Hook that provides dashboard context as formatted formData for charts.
+ * This encapsulates all the complex logic for determining which dashboard
+ * filters, colors, and other context should be applied to a specific chart.
+ *
+ * @param chartId - The ID of the chart to get dashboard context for
+ * @returns Dashboard context formatted as QueryFormData fields
+ */
+export const useDashboardFormData = (
+  chartId: number | null | undefined,
+): DashboardContextFormData => {
+  // Dashboard state selectors
+  const dashboardId = useSelector<RootState, number>(
+    ({ dashboardInfo }) => dashboardInfo.id,
+  );
+
+  const nativeFilters = useSelector(
+    (state: RootState) => state.nativeFilters?.filters,
+  );
+
+  const dataMask = useSelector((state: RootState) => state.dataMask);
+
+  const chartConfiguration = useSelector(
+    (state: RootState) =>
+      state.dashboardInfo.metadata?.chart_configuration || {},
+  );
+
+  const allSliceIds = useSelector(
+    (state: RootState) => state.dashboardState.sliceIds,
+  );
+
+  // Compute dashboard context for the chart
+  return useMemo((): DashboardContextFormData => {
+    const baseContext: DashboardContextFormData = { dashboardId };
+
+    // Early return if we don't have required data or chartId
+    if (
+      chartId == null ||
+      !nativeFilters ||
+      !dataMask ||
+      !chartConfiguration ||
+      !allSliceIds
+    ) {
+      return baseContext;
+    }
+
+    // Get active filters using the same logic as normal dashboard charts
+    const activeFilters = getAllActiveFilters({
+      chartConfiguration,
+      nativeFilters,
+      dataMask,
+      allSliceIds,
+    });
+
+    // Find which filters apply to this specific chart
+    const filterIdsAppliedOnChart = getFilterIdsAppliedOnChart(
+      activeFilters,
+      chartId,
+    );
+
+    // If no filters apply, return just the base context
+    if (filterIdsAppliedOnChart.length === 0) {
+      return baseContext;

Review Comment:
   **Suggestion:** The configured drill-through chart is often not included in 
the current dashboard's `allSliceIds`. `getAllActiveFilters` consequently 
scopes filters to dashboard chart IDs, and this lookup returns no matching 
filters for the drill-through chart, so the rendered target chart silently 
loses active dashboard filter context. Resolve the applicable filters from the 
source chart/dashboard context or explicitly include the configured target 
chart when constructing its scope. [logic error]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Configured drill-through charts can ignore active dashboard filters.
   - ⚠️ Drill results may not match the source dashboard state.
   - ⚠️ StatefulChart receives incomplete dashboard form data.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Configure a dataset to use a chart that is not present on the current 
dashboard as its
   drill-through target; DrillDetailModal passes that configured target ID to
   useDashboardFormData at
   
superset-frontend/src/components/Chart/DrillDetail/DrillDetailModal.tsx:122-125.
   
   2. Open the chart context menu on a dashboard chart and choose 
drill-to-details;
   ChartContextMenu renders DrillDetailModal at
   
superset-frontend/src/components/Chart/ChartContextMenu/ChartContextMenu.tsx:447-457
 and
   supplies the current dashboard context.
   
   3. With an active native filter whose scope contains the source dashboard 
chart but not
   the configured target chart, useDashboardFormData builds activeFilters 
through
   getAllActiveFilters at
   superset-frontend/src/dashboard/hooks/useDashboardFormData.ts:73-79. For 
dashboard-wide
   filters without an explicit target, getAllActiveFilters falls back to
   dashboardState.sliceIds at
   superset-frontend/src/dashboard/util/activeAllDashboardFilters.ts:98-104, 
which excludes
   charts outside the dashboard.
   
   4. getFilterIdsAppliedOnChart checks the target ID against each filter scope 
at
   superset-frontend/src/dashboard/util/getFilterIdsAppliedOnChart.ts:31-37, 
returns an empty
   array at useDashboardFormData.ts:82-89, and drops extra_form_data. 
DrillDetailModal then
   renders the target StatefulChart with this incomplete form data at
   
superset-frontend/src/components/Chart/DrillDetail/DrillDetailPane.tsx:299-305, 
so the
   configured chart loses the active dashboard filter context.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=12d6be01daca416a8b959d6a82c3dd03&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=12d6be01daca416a8b959d6a82c3dd03&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset-frontend/src/dashboard/hooks/useDashboardFormData.ts
   **Line:** 81:89
   **Comment:**
        *Logic Error: The configured drill-through chart is often not included 
in the current dashboard's `allSliceIds`. `getAllActiveFilters` consequently 
scopes filters to dashboard chart IDs, and this lookup returns no matching 
filters for the drill-through chart, so the rendered target chart silently 
loses active dashboard filter context. Resolve the applicable filters from the 
source chart/dashboard context or explicitly include the configured target 
chart when constructing its scope.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F34785&comment_hash=f908b4e75fd6d7dd1f9850e57dc61b4e7f429bccc63ae75aa276b690ed07d863&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F34785&comment_hash=f908b4e75fd6d7dd1f9850e57dc61b4e7f429bccc63ae75aa276b690ed07d863&reaction=dislike'>👎</a>



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