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


##########
superset-frontend/src/SqlLab/middlewares/persistSqlLabStateEnhancer.ts:
##########
@@ -0,0 +1,184 @@
+/**
+ * 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 type { StoreEnhancer } from 'redux';
+import persistState from 'redux-localstorage';
+import { pickBy } from 'lodash';
+import { isFeatureEnabled, FeatureFlag } from '@superset-ui/core';
+import { filterUnsavedQueryEditorList } from 
'src/SqlLab/components/EditorAutoSync';
+import type {
+  SqlLabRootState,
+  QueryEditor,
+  UnsavedQueryEditor,
+  Table,
+} from '../types';
+import {
+  emptyTablePersistData,
+  emptyQueryResults,
+  clearQueryEditors,
+} from '../utils/reduxStateToLocalStorageHelper';
+import { BYTES_PER_CHAR, KB_STORAGE } from '../constants';
+
+type SqlLabState = SqlLabRootState['sqlLab'];
+
+type ClearEntityHelperValue =
+  | Table[]
+  | SqlLabState['queries']
+  | QueryEditor[]
+  | UnsavedQueryEditor;
+
+interface ClearEntityHelpersMap {
+  tables: (tables: Table[]) => ReturnType<typeof emptyTablePersistData>;
+  queries: (
+    queries: SqlLabState['queries'],
+  ) => ReturnType<typeof emptyQueryResults>;
+  queryEditors: (
+    queryEditors: QueryEditor[],
+  ) => ReturnType<typeof clearQueryEditors>;
+  unsavedQueryEditor: (
+    qe: UnsavedQueryEditor,
+  ) => ReturnType<typeof clearQueryEditors>[number];
+}
+
+const CLEAR_ENTITY_HELPERS_MAP: ClearEntityHelpersMap = {
+  tables: emptyTablePersistData,
+  queries: emptyQueryResults,
+  queryEditors: clearQueryEditors,
+  unsavedQueryEditor: (qe: UnsavedQueryEditor) =>
+    clearQueryEditors([qe as QueryEditor])[0],
+};
+
+interface PersistedSqlLabState {
+  sqlLab?: Partial<SqlLabState>;
+  localStorageUsageInKilobytes?: number;
+}
+
+const sqlLabPersistStateConfig = {
+  paths: ['sqlLab'],
+  config: {
+    slicer:
+      (paths: string[]) =>
+      (state: SqlLabRootState): PersistedSqlLabState => {
+        const subset: PersistedSqlLabState = {};
+        paths.forEach(path => {
+          if (isFeatureEnabled(FeatureFlag.SqllabBackendPersistence)) {
+            const {
+              queryEditors,
+              editorTabLastUpdatedAt,
+              unsavedQueryEditor,
+              tables,
+              queries,
+              tabHistory,
+              lastUpdatedActiveTab,
+              destroyedQueryEditors,
+            } = state.sqlLab;
+            const unsavedQueryEditors = filterUnsavedQueryEditorList(
+              queryEditors,
+              unsavedQueryEditor,
+              editorTabLastUpdatedAt,
+            );
+            const hasUnsavedActiveTabState =
+              tabHistory.slice(-1)[0] !== lastUpdatedActiveTab;
+            const hasUnsavedDeletedQueryEditors =
+              Object.keys(destroyedQueryEditors).length > 0;
+            if (
+              unsavedQueryEditors.length > 0 ||
+              hasUnsavedActiveTabState ||
+              hasUnsavedDeletedQueryEditors
+            ) {
+              const hasFinishedMigrationFromLocalStorage =
+                unsavedQueryEditors.every(
+                  ({ inLocalStorage }) => !inLocalStorage,
+                );
+              subset.sqlLab = {
+                queryEditors: unsavedQueryEditors,
+                ...(!hasFinishedMigrationFromLocalStorage && {
+                  tabHistory,
+                  tables: tables.filter(table => table.inLocalStorage),
+                  queries: pickBy(
+                    queries,
+                    query => query.inLocalStorage && !query.isDataPreview,
+                  ),
+                }),
+                ...(hasUnsavedActiveTabState && {
+                  tabHistory,
+                }),
+                destroyedQueryEditors,
+              };
+            }
+            return;

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Logic Error: Premature Return</b></div>
   <div id="fix">
   
   The slicer function has a `return;` on line 124 that exits early when 
`SqllabBackendPersistence` is enabled, preventing size calculation and proper 
return of the persisted state. This could break persistence and size tracking. 
Remove this `return;` to let execution continue.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ```
    --- superset-frontend/src/SqlLab/middlewares/persistSqlLabStateEnhancer.ts
    +++ superset-frontend/src/SqlLab/middlewares/persistSqlLabStateEnhancer.ts
    @@ -121,7 +121,6 @@
                     destroyedQueryEditors,
                   };
                 }
    -            return;
               }
               // this line is used to remove old data from browser 
localStorage.
               // we used to persist all redux state into localStorage, but
   ```
   
   </div>
   </details>
   
   
   
   </div>
   
   <details>
   <summary><b>Citations</b></summary>
   <ul>
   
   <li>
   Rule Violated: <a 
href="https://github.com/apache/superset/blob/4458fa4/dev-standard.mdc#L47";>dev-standard.mdc:47</a>
   </li>
   
   <li>
   Rule Violated: <a 
href="https://github.com/apache/superset/blob/4458fa4/dev-standard.mdc#L16";>dev-standard.mdc:16</a>
   </li>
   
   </ul>
   </details>
   
   
   
   
   <small><i>Code Review Run #093651</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