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(SQL Editor): names new query tabs correctly #18951

Merged
2 changes: 1 addition & 1 deletion superset-frontend/src/SqlLab/actions/sqlLab.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('async actions', () => {
latestQueryId: null,
selectedText: null,
sql: 'SELECT *\nFROM\nWHERE',
title: 'Untitled Query',
title: 'Untitled Query 1',
schemaOptions: [{ value: 'main', label: 'main', title: 'main' }],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ describe('TabbedSqlEditors', () => {
wrapper.instance().props.actions.addQueryEditor.getCall(0).args[0].title,
).toContain('Untitled Query');
});
it('should properly increment query tab name', () => {
wrapper = getWrapper();
sinon.stub(wrapper.instance().props.actions, 'addQueryEditor');

wrapper.instance().newQueryEditor();
expect(
wrapper.instance().props.actions.addQueryEditor.getCall(0).args[0].title,
).toContain('Untitled Query 2');
});
it('should duplicate query editor', () => {
wrapper = getWrapper();
sinon.stub(wrapper.instance().props.actions, 'cloneQueryToNewTab');
Expand Down
21 changes: 16 additions & 5 deletions superset-frontend/src/SqlLab/components/TabbedSqlEditors/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ const defaultProps = {
scheduleQueryWarning: null,
};

let queryCount = 1;

const TabTitleWrapper = styled.div`
display: flex;
align-items: center;
Expand Down Expand Up @@ -228,7 +226,6 @@ class TabbedSqlEditors extends React.PureComponent {
}

popNewTab() {
queryCount += 1;
// Clean the url in browser history
window.history.replaceState({}, document.title, this.state.sqlLabUrl);
}
Expand All @@ -250,7 +247,6 @@ class TabbedSqlEditors extends React.PureComponent {
}

newQueryEditor() {
queryCount += 1;
const activeQueryEditor = this.activeQueryEditor();
const firstDbId = Math.min(
...Object.values(this.props.databases).map(database => database.id),
Expand All @@ -260,8 +256,23 @@ class TabbedSqlEditors extends React.PureComponent {
: t(
'-- Note: Unless you save your query, these tabs will NOT persist if you clear your cookies or change browsers.\n\n',
);

let newTitle = 'Untitled Query 1';

if (this.props.queryEditors.length > 0) {
const untitledQueryNumbers = this.props.queryEditors
.filter(x => x.title.match(/^Untitled Query (\d+)$/))
.map(x => x.title.replace('Untitled Query ', ''));
if (untitledQueryNumbers.length > 0) {
// When there are query tabs open, and at least one is called "Untitled Query #"
// Where # is a valid number
const largestNumber = Math.max.apply(null, untitledQueryNumbers);
newTitle = t('Untitled Query %s', largestNumber + 1);
}
}

const qe = {
title: t('Untitled Query %s', queryCount),
title: newTitle,
dbId:
activeQueryEditor && activeQueryEditor.dbId
? activeQueryEditor.dbId
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/src/SqlLab/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export const defaultQueryEditor = {
latestQueryId: null,
selectedText: null,
sql: 'SELECT *\nFROM\nWHERE',
title: 'Untitled Query',
title: 'Untitled Query 1',
schemaOptions: [
{
value: 'main',
Expand Down