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


##########
superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.tsx:
##########
@@ -90,170 +90,201 @@ const TabTitle = styled.span`
   text-transform: none;
 `;
 
-const AddTabIconWrapper = styled.span`
-  display: inline-flex;
-  vertical-align: middle;
-`;
-
 // Get the user's OS
 const userOS = detectOS();
 
 type TabbedSqlEditorsProps = ReturnType<typeof mergeProps>;
 
-class TabbedSqlEditors extends PureComponent<TabbedSqlEditorsProps> {
-  constructor(props: TabbedSqlEditorsProps) {
-    super(props);
-    this.removeQueryEditor = this.removeQueryEditor.bind(this);
-    this.handleSelect = this.handleSelect.bind(this);
-    this.handleEdit = this.handleEdit.bind(this);
-  }
+function TabbedSqlEditors({
+  actions,
+  queryEditors = DEFAULT_PROPS.queryEditors,
+  queries,
+  tabHistory,
+  displayLimit,
+  offline = DEFAULT_PROPS.offline,
+  defaultQueryLimit,
+  maxRow,
+  saveQueryWarning = DEFAULT_PROPS.saveQueryWarning,
+  scheduleQueryWarning = DEFAULT_PROPS.scheduleQueryWarning,
+}: TabbedSqlEditorsProps) {
+  const activeQueryEditor = useMemo(() => {
+    if (tabHistory.length === 0) {
+      return queryEditors[0];
+    }
+    const qeid = tabHistory[tabHistory.length - 1];
+    return queryEditors.find(qe => qe.id === qeid) || null;
+  }, [tabHistory, queryEditors]);
+
+  // Track the last persisted resultsKey we fetched, so the effect retries when
+  // the active query editor resolves after mount (or its latest query changes)
+  // but dedupes when the same resultsKey has already been fetched.
+  const fetchedResultsKeyRef = useRef<string | null>(null);
 
-  componentDidMount() {
-    const qe = this.activeQueryEditor();
-    const latestQuery = this.props.queries[qe?.latestQueryId || ''];
+  // Fetch query results for the active editor's latest query when its
+  // persisted resultsKey changes (equivalent to componentDidMount, but 
resilient
+  // to async hydration of activeQueryEditor).
+  useEffect(() => {
+    const latestQuery = queries[activeQueryEditor?.latestQueryId || ''];
+    const resultsKey = latestQuery?.resultsKey;
     if (
       isFeatureEnabled(FeatureFlag.SqllabBackendPersistence) &&
-      latestQuery?.resultsKey
+      resultsKey &&
+      fetchedResultsKeyRef.current !== resultsKey
     ) {
+      fetchedResultsKeyRef.current = resultsKey;
       // when results are not stored in localStorage they need to be
       // fetched from the results backend (if configured)
-      this.props.actions.fetchQueryResults(
-        latestQuery,
-        this.props.displayLimit,
-      );
+      actions.fetchQueryResults(latestQuery, displayLimit);
     }
-  }
+  }, [queries, activeQueryEditor, actions, displayLimit]);
 
-  activeQueryEditor() {
-    if (this.props.tabHistory.length === 0) {
-      return this.props.queryEditors[0];
-    }
-    const qeid = this.props.tabHistory[this.props.tabHistory.length - 1];
-    return this.props.queryEditors.find(qe => qe.id === qeid) || null;
-  }
+  const newQueryEditor = useCallback(() => {
+    actions.addNewQueryEditor();
+  }, [actions]);
 
-  newQueryEditor() {
-    this.props.actions.addNewQueryEditor();
-  }
+  const removeQueryEditor = useCallback(
+    (qe: QueryEditor) => {
+      actions.removeQueryEditor(qe);
+    },
+    [actions],
+  );
 
-  handleSelect(key: string) {
-    const qeid = this.props.tabHistory[this.props.tabHistory.length - 1];
-    if (key !== qeid) {
-      const queryEditor = this.props.queryEditors.find(qe => qe.id === key);
-      if (!queryEditor) {
-        return;
+  const handleSelect = useCallback(
+    (key: string) => {
+      const qeid = tabHistory[tabHistory.length - 1];
+      if (key !== qeid) {
+        const queryEditor = queryEditors.find(qe => qe.id === key);
+        if (!queryEditor) {
+          return;
+        }
+        actions.setActiveQueryEditor(queryEditor);
       }
-      this.props.actions.setActiveQueryEditor(queryEditor);
-    }
-  }
+    },
+    [tabHistory, queryEditors, actions],
+  );
 
-  handleEdit(key: string, action: string) {
-    if (action === 'remove') {
-      const qe = this.props.queryEditors.find(qe => qe.id === key);
-      if (qe) {
-        this.removeQueryEditor(qe);
+  const handleEdit = useCallback(
+    (key: string, action: string) => {
+      if (action === 'remove') {
+        const qe = queryEditors.find(qe => qe.id === key);
+        if (qe) {
+          removeQueryEditor(qe);
+        }
       }
-    }
-    if (action === 'add') {
-      Logger.markTimeOrigin();
-      this.newQueryEditor();
-    }
-  }
-
-  removeQueryEditor(qe: QueryEditor) {
-    this.props.actions.removeQueryEditor(qe);
-  }
+      if (action === 'add') {
+        Logger.markTimeOrigin();
+        newQueryEditor();
+      }
+    },
+    [queryEditors, removeQueryEditor, newQueryEditor],
+  );
 
-  onTabClicked = () => {
+  const onTabClicked = useCallback(() => {
     Logger.markTimeOrigin();
-    const noQueryEditors = this.props.queryEditors?.length === 0;
+    const noQueryEditors = queryEditors?.length === 0;
     if (noQueryEditors) {
-      this.newQueryEditor();
+      newQueryEditor();
     }
+  }, [queryEditors, newQueryEditor]);

Review Comment:
   **Suggestion:** The tab click handler creates a new query tab whenever there 
are zero editors, but it does not check the `offline` flag. In offline mode 
this bypasses the intended "no new tabs" restriction (the add button is hidden, 
but clicking the empty-state tab still creates one). Guard this path with 
`!offline` before calling `newQueryEditor`. [logic error]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ⚠️ Offline SqlLab still allows new query tabs.
   - ⚠️ Offline UX inconsistent with disabled add-tab button.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Open the SQL Lab App, which renders `TabbedSqlEditors` inside `App` at
   `superset-frontend/src/SqlLab/components/App/index.tsx:13-23` 
(`<TabbedSqlEditors />`).
   
   2. Put SQL Lab into offline mode by dispatching `setUserOffline(true)` from
   `superset-frontend/src/SqlLab/actions/sqlLab.ts:28-30`, which is handled by 
the reducer at
   `superset-frontend/src/SqlLab/reducers/sqlLab.ts:32-34` to set 
`state.sqlLab.offline =
   true`, then passed into `TabbedSqlEditors` via `mapStateToProps` at
   `superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.tsx:31-38`.
   
   3. With `sqlLab.queryEditors` containing at least one editor, close all 
query tabs using
   the tab close buttons; this triggers `handleEdit` at 
`TabbedSqlEditors/index.tsx:166-179`
   with `action === 'remove'`, calling `removeQueryEditor` which dispatches
   `REMOVE_QUERY_EDITOR` and is processed by the reducer at
   `superset-frontend/src/SqlLab/reducers/sqlLab.ts:150-199` until
   `state.sqlLab.queryEditors` becomes an empty array and `tabItems` is set to
   `[emptyTabState]` at `index.tsx:241-253`.
   
   4. Still in offline mode (so `hideAdd={offline}` at `index.tsx:260-263` 
hides the standard
   "Add tab" button), click the remaining empty-state tab labeled "Add a new 
tab" rendered
   from `emptyTabState.label` at `index.tsx:218-239`; `EditableTabs` invokes
   `onTabClick={onTabClicked}` at `index.tsx:260-264`, which runs 
`onTabClicked` at
   `index.tsx:182-188`, sees `noQueryEditors === true`, and calls 
`newQueryEditor()`
   (index.tsx:141-143), creating a new query editor despite offline mode and 
thus bypassing
   the intended "disable new tab when offline" restriction.
   ```
   </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=acabf79dbd3847cf8550268eb2d98b42&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=acabf79dbd3847cf8550268eb2d98b42&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/SqlLab/components/TabbedSqlEditors/index.tsx
   **Line:** 182:188
   **Comment:**
        *Logic Error: The tab click handler creates a new query tab whenever 
there are zero editors, but it does not check the `offline` flag. In offline 
mode this bypasses the intended "no new tabs" restriction (the add button is 
hidden, but clicking the empty-state tab still creates one). Guard this path 
with `!offline` before calling `newQueryEditor`.
   
   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%2F39461&comment_hash=2e5f0300f4057621a2fcad2c33cf83320ceb6630ff22866038c68c26b3e590de&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39461&comment_hash=2e5f0300f4057621a2fcad2c33cf83320ceb6630ff22866038c68c26b3e590de&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