You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ju...@apache.org on 2023/08/21 16:42:42 UTC

[superset] branch master updated: chore(sqllab): Relocate user in SqlLab to root (#25010)

This is an automated email from the ASF dual-hosted git repository.

justinpark pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new b339613844 chore(sqllab): Relocate user in SqlLab to root (#25010)
b339613844 is described below

commit b339613844aa5707c2cecc1f68a062dcf20c0c30
Author: JUST.in DO IT <ju...@airbnb.com>
AuthorDate: Mon Aug 21 09:42:31 2023 -0700

    chore(sqllab): Relocate user in SqlLab to root (#25010)
---
 superset-frontend/src/SqlLab/App.jsx                   | 13 ++-----------
 .../SqlLab/components/QueryTable/QueryTable.test.jsx   |  2 +-
 .../src/SqlLab/components/QueryTable/index.tsx         |  2 +-
 .../src/SqlLab/components/SaveDatasetModal/index.tsx   |  6 +-----
 .../src/SqlLab/components/SouthPane/index.tsx          |  4 ++--
 superset-frontend/src/SqlLab/fixtures.ts               |  2 +-
 .../src/SqlLab/reducers/getInitialState.js             |  4 +---
 .../src/SqlLab/reducers/getInitialState.test.ts        |  2 +-
 superset-frontend/src/SqlLab/types.ts                  | 18 ++----------------
 superset-frontend/src/views/store.ts                   |  2 +-
 10 files changed, 13 insertions(+), 42 deletions(-)

diff --git a/superset-frontend/src/SqlLab/App.jsx b/superset-frontend/src/SqlLab/App.jsx
index 8f36b7e2ab..37a45fc6fb 100644
--- a/superset-frontend/src/SqlLab/App.jsx
+++ b/superset-frontend/src/SqlLab/App.jsx
@@ -27,7 +27,7 @@ import {
   isFeatureEnabled,
 } from '@superset-ui/core';
 import { GlobalStyles } from 'src/GlobalStyles';
-import { setupStore } from 'src/views/store';
+import { setupStore, userReducer } from 'src/views/store';
 import setupExtensions from 'src/setup/setupExtensions';
 import getBootstrapData from 'src/utils/getBootstrapData';
 import { tableApiUtil } from 'src/hooks/apiResources/tables';
@@ -78,12 +78,6 @@ const sqlLabPersistStateConfig = {
         }
       });
 
-      if (subset.sqlLab?.user) {
-        // Don't persist the user.
-        // User should really not be stored under the "sqlLab" field. Oh well.
-        delete subset.sqlLab.user;
-      }
-
       const data = JSON.stringify(subset);
       // 2 digit precision
       const currentSize =
@@ -105,9 +99,6 @@ const sqlLabPersistStateConfig = {
           ...initialState.sqlLab,
         },
       };
-      // Filter out any user data that may have been persisted in an older version.
-      // Get user from bootstrap data instead, every time
-      result.sqlLab.user = initialState.sqlLab.user;
       return result;
     },
   },
@@ -115,7 +106,7 @@ const sqlLabPersistStateConfig = {
 
 export const store = setupStore({
   initialState,
-  rootReducers: reducers,
+  rootReducers: { ...reducers, user: userReducer },
   ...(!isFeatureEnabled(FeatureFlag.SQLLAB_BACKEND_PERSISTENCE) && {
     enhancers: [
       persistState(
diff --git a/superset-frontend/src/SqlLab/components/QueryTable/QueryTable.test.jsx b/superset-frontend/src/SqlLab/components/QueryTable/QueryTable.test.jsx
index 76784695a8..4aa8351796 100644
--- a/superset-frontend/src/SqlLab/components/QueryTable/QueryTable.test.jsx
+++ b/superset-frontend/src/SqlLab/components/QueryTable/QueryTable.test.jsx
@@ -41,7 +41,7 @@ describe('QueryTable', () => {
   it('renders a proper table', () => {
     const mockStore = configureStore([thunk]);
     const store = mockStore({
-      sqlLab: user,
+      user,
     });
 
     const wrapper = mount(
diff --git a/superset-frontend/src/SqlLab/components/QueryTable/index.tsx b/superset-frontend/src/SqlLab/components/QueryTable/index.tsx
index 96e1f4568d..1ea83de58c 100644
--- a/superset-frontend/src/SqlLab/components/QueryTable/index.tsx
+++ b/superset-frontend/src/SqlLab/components/QueryTable/index.tsx
@@ -105,7 +105,7 @@ const QueryTable = ({
     [columns],
   );
 
-  const user = useSelector<SqlLabRootState, User>(state => state.sqlLab.user);
+  const user = useSelector<SqlLabRootState, User>(state => state.user);
 
   const data = useMemo(() => {
     const restoreSql = (query: QueryResponse) => {
diff --git a/superset-frontend/src/SqlLab/components/SaveDatasetModal/index.tsx b/superset-frontend/src/SqlLab/components/SaveDatasetModal/index.tsx
index 7f605967ad..8ef9fa4847 100644
--- a/superset-frontend/src/SqlLab/components/SaveDatasetModal/index.tsx
+++ b/superset-frontend/src/SqlLab/components/SaveDatasetModal/index.tsx
@@ -42,8 +42,6 @@ import {
   DatasetRadioState,
   EXPLORE_CHART_DEFAULT,
   DatasetOwner,
-  SqlLabExploreRootState,
-  getInitialState,
   SqlLabRootState,
 } from 'src/SqlLab/types';
 import { mountExploreUrl } from 'src/explore/exploreUtils';
@@ -177,9 +175,7 @@ export const SaveDatasetModal = ({
   >(undefined);
   const [loading, setLoading] = useState<boolean>(false);
 
-  const user = useSelector<SqlLabExploreRootState, User>(user =>
-    getInitialState(user),
-  );
+  const user = useSelector<SqlLabRootState, User>(state => state.user);
   const dispatch = useDispatch<(dispatch: any) => Promise<JsonObject>>();
 
   const createWindow = (url: string) => {
diff --git a/superset-frontend/src/SqlLab/components/SouthPane/index.tsx b/superset-frontend/src/SqlLab/components/SouthPane/index.tsx
index c2b0cc3beb..4e38a76f9c 100644
--- a/superset-frontend/src/SqlLab/components/SouthPane/index.tsx
+++ b/superset-frontend/src/SqlLab/components/SouthPane/index.tsx
@@ -107,8 +107,8 @@ const SouthPane = ({
   const dispatch = useDispatch();
 
   const { editorQueries, dataPreviewQueries, databases, offline, user } =
-    useSelector(({ sqlLab }: SqlLabRootState) => {
-      const { databases, offline, user, queries, tables } = sqlLab;
+    useSelector(({ user, sqlLab }: SqlLabRootState) => {
+      const { databases, offline, queries, tables } = sqlLab;
       const dataPreviewQueries = tables
         .filter(
           ({ dataPreviewQueryId, queryEditorId: qeId }) =>
diff --git a/superset-frontend/src/SqlLab/fixtures.ts b/superset-frontend/src/SqlLab/fixtures.ts
index 0afd1c4149..4f6ad9ceb5 100644
--- a/superset-frontend/src/SqlLab/fixtures.ts
+++ b/superset-frontend/src/SqlLab/fixtures.ts
@@ -660,10 +660,10 @@ export const initialState = {
     workspaceQueries: [],
     queriesLastUpdate: 0,
     activeSouthPaneTab: 'Results',
-    user: { user },
     unsavedQueryEditor: {},
   },
   messageToasts: [],
+  user,
   common: {
     conf: {
       DEFAULT_SQLLAB_LIMIT: 1000,
diff --git a/superset-frontend/src/SqlLab/reducers/getInitialState.js b/superset-frontend/src/SqlLab/reducers/getInitialState.js
index 1e3ac94d11..5bc225824c 100644
--- a/superset-frontend/src/SqlLab/reducers/getInitialState.js
+++ b/superset-frontend/src/SqlLab/reducers/getInitialState.js
@@ -33,7 +33,6 @@ export default function getInitialState({
   tab_state_ids: tabStateIds = [],
   databases,
   queries: queries_,
-  requested_query: requestedQuery,
   user,
 }) {
   /**
@@ -200,11 +199,9 @@ export default function getInitialState({
       tabHistory: dedupeTabHistory(tabHistory),
       tables: Object.values(tables),
       queriesLastUpdate: Date.now(),
-      user,
       unsavedQueryEditor,
       queryCostEstimates: {},
     },
-    requestedQuery,
     messageToasts: getToastsFromPyFlashMessages(
       (common || {}).flash_messages || [],
     ),
@@ -213,5 +210,6 @@ export default function getInitialState({
       flash_messages: common.flash_messages,
       conf: common.conf,
     },
+    user,
   };
 }
diff --git a/superset-frontend/src/SqlLab/reducers/getInitialState.test.ts b/superset-frontend/src/SqlLab/reducers/getInitialState.test.ts
index a3c71cbd88..af074c3cc4 100644
--- a/superset-frontend/src/SqlLab/reducers/getInitialState.test.ts
+++ b/superset-frontend/src/SqlLab/reducers/getInitialState.test.ts
@@ -42,7 +42,7 @@ const apiDataWithTabState = {
 };
 describe('getInitialState', () => {
   it('should output the user that is passed in', () => {
-    expect(getInitialState(apiData).sqlLab.user.userId).toEqual(1);
+    expect(getInitialState(apiData).user.userId).toEqual(1);
   });
   it('should return undefined instead of null for templateParams', () => {
     expect(
diff --git a/superset-frontend/src/SqlLab/types.ts b/superset-frontend/src/SqlLab/types.ts
index b1a8812471..eb0d89a98d 100644
--- a/superset-frontend/src/SqlLab/types.ts
+++ b/superset-frontend/src/SqlLab/types.ts
@@ -19,7 +19,6 @@
 import { JsonObject, QueryResponse } from '@superset-ui/core';
 import { UserWithPermissionsAndRoles } from 'src/types/bootstrapTypes';
 import { ToastType } from 'src/components/MessageToasts/types';
-import { RootState } from 'src/dashboard/types';
 import { DropdownButtonProps } from 'src/components/DropdownButton';
 import { ButtonProps } from 'src/components/Button';
 
@@ -66,33 +65,20 @@ export type SqlLabRootState = {
     tabHistory: string[]; // default is activeTab ? [activeTab.id.toString()] : []
     tables: Record<string, any>[];
     queriesLastUpdate: number;
-    user: UserWithPermissionsAndRoles;
     errorMessage: string | null;
     unsavedQueryEditor: Partial<QueryEditor>;
     queryCostEstimates?: Record<string, QueryCostEstimate>;
+    editorTabLastUpdatedAt?: number;
   };
   localStorageUsageInKilobytes: number;
   messageToasts: toastState[];
+  user: UserWithPermissionsAndRoles;
   common: {
     flash_messages: string[];
     conf: JsonObject;
   };
 };
 
-export type SqlLabExploreRootState = SqlLabRootState | RootState;
-
-export const getInitialState = (state: SqlLabExploreRootState) => {
-  if (state.hasOwnProperty('sqlLab')) {
-    const {
-      sqlLab: { user },
-    } = state as SqlLabRootState;
-    return user;
-  }
-
-  const { user } = state as RootState;
-  return user as UserWithPermissionsAndRoles;
-};
-
 export enum DatasetRadioState {
   SAVE_NEW = 1,
   OVERWRITE_DATASET = 2,
diff --git a/superset-frontend/src/views/store.ts b/superset-frontend/src/views/store.ts
index cf03207f48..b4fa3b4055 100644
--- a/superset-frontend/src/views/store.ts
+++ b/superset-frontend/src/views/store.ts
@@ -66,7 +66,7 @@ export type UserLoadedAction = {
   user: UserWithPermissionsAndRoles;
 };
 
-const userReducer = (
+export const userReducer = (
   user = bootstrapData.user || {},
   action: UserLoadedAction,
 ): BootstrapUser | UndefinedUser => {