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


##########
superset-frontend/src/components/ErrorMessage/OAuth2RedirectMessage.tsx:
##########
@@ -106,21 +106,60 @@ export function OAuth2RedirectMessage({
   const dispatch = useDispatch();
   const lastHandledTabIdRef = useRef<string>();
 
+  // `chartList` is rebuilt from `Object.keys` on every store update; keeping
+  // the listener state in a ref avoids tearing down/recreating the
+  // BroadcastChannel on each render and the race window that comes with it.
+  const latestStateRef = useRef({
+    source,
+    query,
+    chartId,
+    chartList,
+    dashboardId,
+    errorMitigationFunction,
+  });
+  latestStateRef.current = {
+    source,
+    query,
+    chartId,
+    chartList,
+    dashboardId,
+    errorMitigationFunction,
+  };
+
   useEffect(() => {
+    // Guard against duplicate dispatches if both the BroadcastChannel and the
+    // storage fallback ever deliver the same completion.
+    let handled = false;
     const handleOAuthComplete = (tabId?: string) => {
-      if (tabId !== extra.tab_id || tabId === lastHandledTabIdRef.current) {
+      if (
+        tabId !== extra.tab_id ||
+        tabId === lastHandledTabIdRef.current ||
+        handled
+      ) {
         return;
       }
-
-      if (errorMitigationFunction) {
-        errorMitigationFunction();
-      } else if (source === 'sqllab' && query) {
-        dispatch(reRunQuery(query));
-      } else if (source === 'explore') {
-        dispatch(triggerQuery(true, chartId));
-      } else if (source === 'dashboard') {
-        dispatch(onRefresh(chartList.map(Number), true, 0, dashboardId));
-      } else if (source === 'crud') {
+      const {
+        source: src,
+        query: q,
+        chartId: cId,
+        chartList: cList,
+        dashboardId: dId,
+        errorMitigationFunction: mitigate,
+      } = latestStateRef.current;
+      // `handled`/`lastHandledTabIdRef` are only set below, after this whole
+      // chain, and only reached when a branch actually dispatches — so a
+      // signal that arrives before state is ready (e.g. `query` still null
+      // in SQL Lab) falls through to the catch-all `return` and a later
+      // fallback signal can still succeed.
+      if (mitigate) {
+        mitigate();
+      } else if (src === 'sqllab' && q) {
+        dispatch(reRunQuery(q));
+      } else if (src === 'explore') {
+        dispatch(triggerQuery(true, cId));
+      } else if (src === 'dashboard') {
+        dispatch(onRefresh(cList.map(Number), true, 0, dId));

Review Comment:
   <!-- Bito Reply -->
   The observation is correct. Since `refreshChart` is only invoked after 
`dashboardInfo` is loaded, `dashboardId` will be defined in the `dashboard` 
source branch. You can safely ignore this suggestion as a false positive.



##########
superset-frontend/src/components/ErrorMessage/OAuth2RedirectMessage.tsx:
##########
@@ -106,21 +106,60 @@ export function OAuth2RedirectMessage({
   const dispatch = useDispatch();
   const lastHandledTabIdRef = useRef<string>();
 
+  // `chartList` is rebuilt from `Object.keys` on every store update; keeping
+  // the listener state in a ref avoids tearing down/recreating the
+  // BroadcastChannel on each render and the race window that comes with it.
+  const latestStateRef = useRef({
+    source,
+    query,
+    chartId,
+    chartList,
+    dashboardId,
+    errorMitigationFunction,
+  });
+  latestStateRef.current = {
+    source,
+    query,
+    chartId,
+    chartList,
+    dashboardId,
+    errorMitigationFunction,
+  };

Review Comment:
   <!-- Bito Reply -->
   The reviewer's concern is that the effect at line 212 depends on 
`[extra.tab_id, dispatch]` but uses values from `latestStateRef` (like 
`source`, `query`, `chartList`, `errorMitigationFunction`) that are not in the 
dependency array. Because the effect does not re-subscribe when these values 
change, it may continue to use stale values captured at the time of the initial 
subscription, even though `latestStateRef.current` is updated on every render. 
To ensure the effect uses the most recent state, you should either include 
these dependencies in the effect's dependency array or use a pattern that 
ensures the effect always accesses the current values.
   
   **superset-frontend/src/components/ErrorMessage/OAuth2RedirectMessage.tsx**
   ```
   // `chartList` is rebuilt from `Object.keys` on every store update; keeping
     // the listener state in a ref avoids tearing down/recreating the
     // BroadcastChannel on each render and the race window that comes with it.
     const latestStateRef = useRef({
       source,
       query,
       chartId,
       chartList,
       dashboardId,
       errorMitigationFunction,
     });
     latestStateRef.current = {
       source,
       query,
       chartId,
       chartList,
       dashboardId,
       errorMitigationFunction,
     };
   ```



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