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(sqllab): dedupe active_tab in tabHistory #23265

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion superset-frontend/src/SqlLab/reducers/getInitialState.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
import { t } from '@superset-ui/core';
import getToastsFromPyFlashMessages from 'src/components/MessageToasts/getToastsFromPyFlashMessages';

export function dedupeTabHistory(tabHistory) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can maybe just skip appending if the last id is the same as current id? Removing duplicates are not necessary when things are working as expected. If a users's localStorage already has bad states because of this, we can just ask them to clear the local storage or close the tabs manually themelves.

return tabHistory.reduce(
(result, tabId) =>
result.slice(-1)[0] === tabId ? result : result.concat(tabId),
[],
);
}

export default function getInitialState({
defaultDbId,
common,
Expand Down Expand Up @@ -193,7 +201,7 @@ export default function getInitialState({
offline: false,
queries,
queryEditors: Object.values(queryEditors),
tabHistory,
tabHistory: dedupeTabHistory(tabHistory),
tables,
queriesLastUpdate: Date.now(),
user,
Expand Down
19 changes: 18 additions & 1 deletion superset-frontend/src/SqlLab/reducers/getInitialState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import getInitialState from './getInitialState';
import getInitialState, { dedupeTabHistory } from './getInitialState';

const apiData = {
defaultDbId: 1,
Expand Down Expand Up @@ -51,4 +51,21 @@ describe('getInitialState', () => {
.templateParams,
).toBeUndefined();
});

describe('dedupeTabHistory', () => {
it('should dedupe the tab history', () => {
[
{ value: [], expected: [] },
{ value: [12, 3, 4, 5, 6], expected: [12, 3, 4, 5, 6] },
{ value: [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], expected: [1, 2] },
{
value: [1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3],
expected: [1, 2, 3],
},
{ value: [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3], expected: [2, 3] },
].forEach(({ value, expected }) => {
expect(dedupeTabHistory(value)).toEqual(expected);
});
});
});
});