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


##########
superset-frontend/src/components/ErrorMessage/OAuth2RedirectMessage.tsx:
##########
@@ -99,29 +99,61 @@ export function OAuth2RedirectMessage({
 
   const dispatch = useDispatch();
 
+  // `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,
+  });
+  latestStateRef.current = {
+    source,
+    query,
+    chartId,
+    chartList,
+    dashboardId,
+  };
+
   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) {
+      if (tabId !== extra.tab_id || handled) {
         return;
       }
-      if (source === 'sqllab' && query) {
-        dispatch(reRunQuery(query));
-      } else if (source === 'explore' && chartId) {
-        dispatch(triggerQuery(true, chartId));
-      } else if (source === 'dashboard') {
-        dispatch(onRefresh(chartList.map(Number), true, 0, dashboardId));
+      handled = true;
+      const {
+        source: src,
+        query: q,
+        chartId: cId,
+        chartList: cList,
+        dashboardId: dId,
+      } = latestStateRef.current;
+      if (src === 'sqllab' && q) {
+        dispatch(reRunQuery(q));
+      } else if (src === 'explore' && cId) {
+        dispatch(triggerQuery(true, cId));
+      } else if (src === 'dashboard') {
+        dispatch(onRefresh(cList.map(Number), true, 0, dId));

Review Comment:
   Yes — this comment is **relevant**.
   
   The current logic can mark the event as `handled` **before** confirming that 
any refresh/re-run actually happened. That means an early signal arriving 
before `query`, `chartId`, or dashboard state is ready could permanently block 
the later fallback signal.
   
   ### Recommended fix
   Move `handled = true` so it is set **only after** a dispatch path succeeds.
   
   For example:
   
   ```ts
   const handleOAuthComplete = (tabId?: string) => {
     if (tabId !== extra.tab_id || handled) {
       return;
     }
   
     const {
       source: src,
       query: q,
       chartId: cId,
       chartList: cList,
       dashboardId: dId,
     } = latestStateRef.current;
   
     if (src === 'sqllab' && q) {
       handled = true;
       dispatch(reRunQuery(q));
     } else if (src === 'explore' && cId) {
       handled = true;
       dispatch(triggerQuery(true, cId));
     } else if (src === 'dashboard') {
       handled = true;
       dispatch(onRefresh(cList.map(Number), true, 0, dId));
     }
   };
   ```
   
   This preserves the deduplication behavior, but avoids dropping a later valid 
signal when the first one arrives too early.
   
   If you want, I can also review the remaining comments on this PR and help 
fix those too.



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