codeant-ai-for-open-source[bot] commented on code in PR #34785: URL: https://github.com/apache/superset/pull/34785#discussion_r4051129106
########## 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: Yes. The context should be derived from the **source chart ID**, not the configured drill-through target ID. The source chart is the chart whose dashboard filters are active, while the target chart may not be present in the dashboard and therefore cannot be resolved by `getAllActiveFilters` against `allSliceIds`. Update `DrillDetailModal` to pass the source `chartId`: ```typescript const dashboardContextFormData = useDashboardFormData(chartId); ``` The hook can retain its current behavior and treat its argument as the chart whose filter scope should be evaluated. The resulting `extra_form_data` can then be applied to the configured target chart through `drillThroughFormData`. This preserves filters scoped specifically to the source chart as well as dashboard-wide filters, while still rendering the selected target chart. The hook documentation and parameter naming should be updated to make this distinction explicit, and the tests should verify that the target chart ID is not required to be present in `allSliceIds`. ########## superset/datasets/schemas.py: ########## @@ -174,6 +174,7 @@ class DatasetPutSchema(Schema): columns = fields.List(fields.Nested(DatasetColumnsPutSchema)) metrics = fields.List(fields.Nested(DatasetMetricsPutSchema)) folders = fields.List(fields.Nested(FolderSchema), required=False) + drill_through_chart_id = fields.Integer(allow_none=True) Review Comment: Yes. The update path should enforce the same invariant as the UI: `drill_through_chart_id` must be `NULL` or reference a chart whose datasource is the dataset being updated. The schema field alone only validates the integer type, so API/import callers can bypass the UI restriction. Validate during dataset update, ideally in the command/API validation layer where the dataset and `Slice` are available, and return a `422`/validation error for mismatches. For example: ```python if drill_through_chart_id is not None: chart = db.session.get(Slice, drill_through_chart_id) if chart is None or ( chart.datasource_id != dataset.id or chart.datasource_type != "table" ): raise ValidationError( {"drill_through_chart_id": [ "The selected chart must use this dataset." ]} ) ``` This should also be applied to dataset imports or any other write path that can persist `drill_through_chart_id`. Keeping the check server-side prevents malformed API requests from configuring cross-dataset charts and avoids invalid drill filters or queries. -- 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]
