Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(window): unavailable localStorage and sessionStorage #25599

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 48 additions & 46 deletions superset-frontend/src/SqlLab/reducers/getInitialState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,54 +137,56 @@
* hasn't used SQL Lab after it has been turned on, the state will be stored
* in the browser's local storage.
*/
const localStorageData = localStorage.getItem('redux');
const sqlLabCacheData = localStorageData
? (JSON.parse(localStorageData) as Pick<SqlLabRootState, 'sqlLab'>)
: undefined;
if (localStorageData && sqlLabCacheData?.sqlLab) {
const { sqlLab } = sqlLabCacheData;
try {
const localStorageData = localStorage.getItem('redux');
const sqlLabCacheData = localStorageData
? (JSON.parse(localStorageData) as Pick<SqlLabRootState, 'sqlLab'>)
: undefined;
if (localStorageData && sqlLabCacheData?.sqlLab) {
const { sqlLab } = sqlLabCacheData;

if (sqlLab.queryEditors.length === 0) {
// migration was successful
localStorage.removeItem('redux');
} else {
unsavedQueryEditor = sqlLab.unsavedQueryEditor || unsavedQueryEditor;
// add query editors and tables to state with a special flag so they can
// be migrated if the `SQLLAB_BACKEND_PERSISTENCE` feature flag is on
sqlLab.queryEditors.forEach(qe => {
queryEditors = {
...queryEditors,
[qe.id]: {
...queryEditors[qe.id],
...qe,
name: qe.title || qe.name,
...(unsavedQueryEditor.id === qe.id && unsavedQueryEditor),
inLocalStorage: true,
loaded: true,
},
};
});
const expandedTables = new Set();
tables = sqlLab.tables.reduce((merged, table) => {
const expanded = !expandedTables.has(table.queryEditorId);
if (expanded) {
expandedTables.add(table.queryEditorId);
}
return {
...merged,
[table.id]: {
...tables[table.id],
...table,
expanded,
},
};
}, tables);
Object.values(sqlLab.queries).forEach(query => {
queries[query.id] = { ...query, inLocalStorage: true };
});
tabHistory.push(...sqlLab.tabHistory);
if (sqlLab.queryEditors.length === 0) {
// migration was successful
localStorage.removeItem('redux');
} else {
unsavedQueryEditor = sqlLab.unsavedQueryEditor || unsavedQueryEditor;
// add query editors and tables to state with a special flag so they can
// be migrated if the `SQLLAB_BACKEND_PERSISTENCE` feature flag is on
sqlLab.queryEditors.forEach(qe => {
queryEditors = {
...queryEditors,
[qe.id]: {
...queryEditors[qe.id],
...qe,
name: qe.title || qe.name,
...(unsavedQueryEditor.id === qe.id && unsavedQueryEditor),
inLocalStorage: true,
loaded: true,
},
};
});
const expandedTables = new Set();
tables = sqlLab.tables.reduce((merged, table) => {
const expanded = !expandedTables.has(table.queryEditorId);
if (expanded) {
expandedTables.add(table.queryEditorId);
}
return {
...merged,
[table.id]: {
...tables[table.id],
...table,
expanded,
},
};
}, tables);
Object.values(sqlLab.queries).forEach(query => {
queries[query.id] = { ...query, inLocalStorage: true };
});
tabHistory.push(...sqlLab.tabHistory);
}
}
}
} catch {}

Check failure on line 189 in superset-frontend/src/SqlLab/reducers/getInitialState.ts

View workflow job for this annotation

GitHub Actions / frontend-build

Empty block statement

return {
sqlLab: {
Expand Down
43 changes: 24 additions & 19 deletions superset-frontend/src/SqlLab/reducers/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,14 @@
return { ...state, activeSouthPaneTab: action.tabId };
},
[actions.MIGRATE_QUERY_EDITOR]() {
// remove migrated query editor from localStorage
const { sqlLab } = JSON.parse(localStorage.getItem('redux'));
sqlLab.queryEditors = sqlLab.queryEditors.filter(
qe => qe.id !== action.oldQueryEditor.id,
);
localStorage.setItem('redux', JSON.stringify({ sqlLab }));

try {
// remove migrated query editor from localStorage
const { sqlLab } = JSON.parse(localStorage.getItem('redux'));
sqlLab.queryEditors = sqlLab.queryEditors.filter(
qe => qe.id !== action.oldQueryEditor.id,
);
localStorage.setItem('redux', JSON.stringify({ sqlLab }));
} catch {}

Check failure on line 433 in superset-frontend/src/SqlLab/reducers/sqlLab.js

View workflow job for this annotation

GitHub Actions / frontend-build

Empty block statement
// replace localStorage query editor with the server backed one
return addToArr(
removeFromArr(state, 'queryEditors', action.oldQueryEditor),
Expand All @@ -438,12 +439,14 @@
);
},
[actions.MIGRATE_TABLE]() {
// remove migrated table from localStorage
const { sqlLab } = JSON.parse(localStorage.getItem('redux'));
sqlLab.tables = sqlLab.tables.filter(
table => table.id !== action.oldTable.id,
);
localStorage.setItem('redux', JSON.stringify({ sqlLab }));
try {
// remove migrated table from localStorage
const { sqlLab } = JSON.parse(localStorage.getItem('redux'));
sqlLab.tables = sqlLab.tables.filter(
table => table.id !== action.oldTable.id,
);
localStorage.setItem('redux', JSON.stringify({ sqlLab }));
} catch {}

Check failure on line 449 in superset-frontend/src/SqlLab/reducers/sqlLab.js

View workflow job for this annotation

GitHub Actions / frontend-build

Empty block statement

// replace localStorage table with the server backed one
return addToArr(
Expand All @@ -453,12 +456,14 @@
);
},
[actions.MIGRATE_TAB_HISTORY]() {
// remove migrated tab from localStorage tabHistory
const { sqlLab } = JSON.parse(localStorage.getItem('redux'));
sqlLab.tabHistory = sqlLab.tabHistory.filter(
tabId => tabId !== action.oldId,
);
localStorage.setItem('redux', JSON.stringify({ sqlLab }));
try {
// remove migrated tab from localStorage tabHistory
const { sqlLab } = JSON.parse(localStorage.getItem('redux'));
sqlLab.tabHistory = sqlLab.tabHistory.filter(
tabId => tabId !== action.oldId,
);
localStorage.setItem('redux', JSON.stringify({ sqlLab }));
} catch {}

Check failure on line 466 in superset-frontend/src/SqlLab/reducers/sqlLab.js

View workflow job for this annotation

GitHub Actions / frontend-build

Empty block statement
const tabHistory = state.tabHistory.filter(
tabId => tabId !== action.oldId,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@
);

const showInfoboxCheck = () => {
if (sessionStorage.getItem('showInfobox') === 'false') return false;
try {
if (sessionStorage.getItem('showInfobox') === 'false') return false;
} catch {}

Check failure on line 341 in superset-frontend/src/explore/components/DatasourcePanel/index.tsx

View workflow job for this annotation

GitHub Actions / frontend-build

Empty block statement
return true;
};

Expand Down Expand Up @@ -366,7 +368,11 @@
<StyledInfoboxWrapper>
<Alert
closable
onClose={() => sessionStorage.setItem('showInfobox', 'false')}
onClose={() => {
try {
sessionStorage.setItem('showInfobox', 'false');
} catch {}

Check failure on line 374 in superset-frontend/src/explore/components/DatasourcePanel/index.tsx

View workflow job for this annotation

GitHub Actions / frontend-build

Empty block statement
}}
type="info"
message=""
description={
Expand Down
17 changes: 11 additions & 6 deletions superset-frontend/src/explore/components/SaveModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@
async componentDidMount() {
let { dashboardId } = this.props;
if (!dashboardId) {
const lastDashboard = sessionStorage.getItem(SK_DASHBOARD_ID);
let lastDashboard;
try {
lastDashboard = sessionStorage.getItem(SK_DASHBOARD_ID);
} catch {}

Check failure on line 125 in superset-frontend/src/explore/components/SaveModal.tsx

View workflow job for this annotation

GitHub Actions / frontend-build

Empty block statement
dashboardId = lastDashboard && parseInt(lastDashboard, 10);
}
if (dashboardId) {
Expand Down Expand Up @@ -249,11 +252,13 @@
);
}

if (dashboard) {
sessionStorage.setItem(SK_DASHBOARD_ID, `${dashboard.id}`);
} else {
sessionStorage.removeItem(SK_DASHBOARD_ID);
}
try {
if (dashboard) {
sessionStorage.setItem(SK_DASHBOARD_ID, `${dashboard.id}`);
} else {
sessionStorage.removeItem(SK_DASHBOARD_ID);
}
} catch {}

Check failure on line 261 in superset-frontend/src/explore/components/SaveModal.tsx

View workflow job for this annotation

GitHub Actions / frontend-build

Empty block statement

// Go to new dashboard url
if (gotodash && dashboard) {
Expand Down
17 changes: 12 additions & 5 deletions superset-frontend/src/hooks/useTabId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,23 @@
}

const updateTabId = () => {
const lastTabId = window.localStorage.getItem('last_tab_id');
let lastTabId;
try {
lastTabId = window.localStorage.getItem('last_tab_id');
} catch {}

Check failure on line 55 in superset-frontend/src/hooks/useTabId.ts

View workflow job for this annotation

GitHub Actions / frontend-build

Empty block statement
const newTabId = String(
lastTabId ? Number.parseInt(lastTabId, 10) + 1 : 1,
);
window.sessionStorage.setItem('tab_id', newTabId);
window.localStorage.setItem('last_tab_id', newTabId);
try {
window.sessionStorage.setItem('tab_id', newTabId);
window.localStorage.setItem('last_tab_id', newTabId);
} catch {}

Check failure on line 62 in superset-frontend/src/hooks/useTabId.ts

View workflow job for this annotation

GitHub Actions / frontend-build

Empty block statement
setTabId(newTabId);
};

const storedTabId = window.sessionStorage.getItem('tab_id');
let storedTabId;
try {
storedTabId = window.sessionStorage.getItem('tab_id');
} catch {}
if (storedTabId) {
channel.postMessage({
type: 'REQUESTING_TAB_ID',
Expand Down
Loading