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
29 changes: 24 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,31 @@ 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;

if (this.props.queryEditors.length > 0) {
const untitledQueryNumbers = this.props.queryEditors
.filter(x => x.title.includes('Untitled Query '))
.map(x => x.title.replace('Untitled Query ', ''))
.filter(x => !Number.isNaN(Number(x)));
cccs-Dustin marked this conversation as resolved.
Show resolved Hide resolved
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);
} else {
// When there are query tabs open but none of them are called "Untitled Query #"
// Where # is a valid number
newTitle = 'Untitled Query 1';
}
} else {
// When there are no query tabs currently open
newTitle = 'Untitled Query 1';
}
cccs-Dustin marked this conversation as resolved.
Show resolved Hide resolved

const qe = {
title: t('Untitled Query %s', queryCount),
title: newTitle,
dbId:
activeQueryEditor && activeQueryEditor.dbId
? activeQueryEditor.dbId
Expand Down