bito-code-review[bot] commented on code in PR #44276:
URL: https://github.com/apache/superset/pull/44276#discussion_r4078733655


##########
superset-frontend/src/core/explore/index.ts:
##########
@@ -0,0 +1,147 @@
+/**
+ * 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 { omit } from 'lodash-es';
+import { explore as exploreApi } from '@apache-superset/core';
+import type {
+  ChartDataResponseResult,
+  JsonObject,
+  QueryFormData,
+} from '@superset-ui/core';
+import { setControlValue } from 'src/explore/actions/exploreActions';
+import { getFormDataFromControls } from 'src/explore/controlUtils';
+import { QUERY_MODE_REQUISITES } from 'src/explore/constants';
+import { requestChartDataResolved } from 'src/components/Chart/chartAction';
+import { store, RootState } from 'src/views/store';
+import { navigation } from '../navigation';
+
+const getExploreState = () => (store.getState() as RootState).explore;
+
+// The Redux slice below is retained across an in-SPA navigation, so
+// checking it alone can't tell a still-active chart from a stale one left
+// over from before the user navigated to another page.
+const isExploreActive = (): boolean => navigation.getPage() === 'explore';
+
+const getChartId: typeof exploreApi.getChartId = () =>
+  isExploreActive()
+    ? (getExploreState().slice?.slice_id ?? undefined)
+    : undefined;
+
+const getControlValues: typeof exploreApi.getControlValues = () =>
+  isExploreActive()
+    ? { ...(getExploreState().form_data as Record<string, unknown>) }
+    : {};
+
+const getControlValue: typeof exploreApi.getControlValue = (name: string) =>
+  isExploreActive()
+    ? (getExploreState().form_data as Record<string, unknown>)[name]
+    : undefined;
+
+// Explore queries with the form data derived from the current `controls`
+// state (see ExploreViewContainer's mapStateToProps), not the 
pre-normalization
+// `form_data` slice, which can lag behind controls filled in by defaults or by
+// mapStateToProps. Building form data the same way keeps getQuery()/
+// getChartData() describing the same data as the chart on screen.
+const getCurrentFormData = (): QueryFormData => {
+  const { controls, hiddenFormData } = getExploreState();
+  const hasQueryMode = !!controls?.query_mode?.value;
+  const fieldsToOmit = hasQueryMode
+    ? Object.keys(hiddenFormData ?? {}).filter(
+        key => !QUERY_MODE_REQUISITES.has(key),
+      )
+    : Object.keys(hiddenFormData ?? {});
+  return omit(
+    getFormDataFromControls(controls ?? {}),
+    fieldsToOmit,
+  ) as QueryFormData;
+};

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Duplicated form-data logic</b></div>
   <div id="fix">
   
   `getCurrentFormData` re-implements the `fieldsToOmit` / 
`omit(getFormDataFromControls(...))` logic already in `ExploreViewContainer`'s 
`mapStateToProps` and `retainQueryModeRequirements` 
(explore/components/ExploreViewContainer/index.tsx:1252-1299). The comment pins 
the 'same data as the chart on screen' invariant, but a copy can silently 
diverge from the screen it must match; a shared helper would enforce it.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #f72289</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset-frontend/src/core/dashboard/index.ts:
##########
@@ -0,0 +1,210 @@
+/**
+ * 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 { dashboard as dashboardApi } from '@apache-superset/core';
+import { isNativeFilter, makeApi, SupersetClient } from '@superset-ui/core';
+import type {
+  DataMask,
+  Divider,
+  Filter,
+  JsonObject,
+  QueryFormData,
+} from '@superset-ui/core';
+import { updateComponents } from 'src/dashboard/actions/dashboardLayout';
+import { dashboardInfoChanged } from 'src/dashboard/actions/dashboardInfo';
+import { applySavedFilterChanges } from 'src/dashboard/actions/nativeFilters';
+import type { SaveFilterChangesType } from 
'src/dashboard/components/nativeFilters/FiltersConfigModal/types';
+import { updateDataMask } from 'src/dataMask/actions';
+import {
+  setChartFormData,
+  triggerQuery,
+} from 'src/components/Chart/chartAction';
+import { applyDefaultFormData } from 'src/explore/store';
+import extractUrlParams from 'src/dashboard/util/extractUrlParams';
+import { store, RootState } from 'src/views/store';
+import { navigation } from '../navigation';
+
+const getState = () => store.getState() as RootState;
+
+// The Redux slices below are retained across an in-SPA navigation, so
+// checking them alone can't tell a still-active dashboard from a stale one
+// left over from before the user navigated to another page.
+const isDashboardActive = (): boolean => navigation.getPage() === 'dashboard';
+
+const requireDashboardId = (): number => {
+  const { id } = getState().dashboardInfo;
+  if (!isDashboardActive() || id == null) {
+    throw new Error('No dashboard is currently active');
+  }
+  return id;
+};
+
+const getDashboardId: typeof dashboardApi.getDashboardId = () =>
+  isDashboardActive() ? (getState().dashboardInfo.id ?? undefined) : undefined;
+
+const getLayout: typeof dashboardApi.getLayout = () =>
+  isDashboardActive() ? { ...getState().dashboardLayout.present } : {};

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>shallow copy shares nested state</b></div>
   <div id="fix">
   
   `getLayout` returns a shallow copy (`{ ...present }`): each node's `meta` is 
still a shared reference into Redux state. A caller mutating 
`layout['CHART-1'].meta.width` corrupts the store outside the reducers. 
`getControlValues` documents this shallow-copy caveat; `getLayout`'s API doc 
does not, so callers have no warning.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #f72289</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset-frontend/src/core/explore/index.ts:
##########
@@ -0,0 +1,147 @@
+/**
+ * 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 { omit } from 'lodash-es';
+import { explore as exploreApi } from '@apache-superset/core';
+import type {
+  ChartDataResponseResult,
+  JsonObject,
+  QueryFormData,
+} from '@superset-ui/core';
+import { setControlValue } from 'src/explore/actions/exploreActions';
+import { getFormDataFromControls } from 'src/explore/controlUtils';
+import { QUERY_MODE_REQUISITES } from 'src/explore/constants';
+import { requestChartDataResolved } from 'src/components/Chart/chartAction';
+import { store, RootState } from 'src/views/store';
+import { navigation } from '../navigation';
+
+const getExploreState = () => (store.getState() as RootState).explore;
+
+// The Redux slice below is retained across an in-SPA navigation, so
+// checking it alone can't tell a still-active chart from a stale one left
+// over from before the user navigated to another page.
+const isExploreActive = (): boolean => navigation.getPage() === 'explore';
+
+const getChartId: typeof exploreApi.getChartId = () =>
+  isExploreActive()
+    ? (getExploreState().slice?.slice_id ?? undefined)
+    : undefined;
+
+const getControlValues: typeof exploreApi.getControlValues = () =>
+  isExploreActive()
+    ? { ...(getExploreState().form_data as Record<string, unknown>) }
+    : {};
+
+const getControlValue: typeof exploreApi.getControlValue = (name: string) =>
+  isExploreActive()
+    ? (getExploreState().form_data as Record<string, unknown>)[name]
+    : undefined;

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Stale form_data read path</b></div>
   <div id="fix">
   
   `getControlValues`/`getControlValue` read the `form_data` slice, which this 
file's own comment (lines 55-59) says can lag behind `controls`, while 
`getQuery`/`getChartData` deliberately derive from `controls`. An extension can 
read a stale control value that disagrees with the query `getQuery()` then 
builds. Consider deriving both from `getCurrentFormData()` or documenting the 
divergence.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #f72289</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



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