fix: sql lab ctrl t behaved differently from clicking (#19420)

* initial approach

To have consistent behaviour when creating a new tab with ctrl+t

* consistent behaviour when creating tabs

* fixed bug and added tests

* updates to pass checks
This commit is contained in:
Luis Casillas
2022-04-15 13:28:40 -05:00
committed by GitHub
parent b8e595413f
commit 56381f4ee8
4 changed files with 102 additions and 18 deletions

View File

@@ -75,6 +75,7 @@ import ShareSqlLabQuery from '../ShareSqlLabQuery';
import SqlEditorLeftBar from '../SqlEditorLeftBar';
import AceEditorWrapper from '../AceEditorWrapper';
import RunQueryActionButton from '../RunQueryActionButton';
import { newQueryTabName } from '../../utils/newQueryTabName';
const LIMIT_DROPDOWN = [10, 100, 1000, 10000, 100000];
const SQL_EDITOR_PADDING = 10;
@@ -333,10 +334,10 @@ class SqlEditor extends React.PureComponent {
key: userOS === 'Windows' ? 'ctrl+q' : 'ctrl+t',
descr: t('New tab'),
func: () => {
const title = newQueryTabName(this.props.queryEditors || []);
this.props.addQueryEditor({
...this.props.queryEditor,
title: t('Untitled query'),
sql: '',
title,
});
},
},
@@ -804,13 +805,12 @@ class SqlEditor extends React.PureComponent {
SqlEditor.defaultProps = defaultProps;
SqlEditor.propTypes = propTypes;
function mapStateToProps(state, props) {
const { sqlLab } = state;
function mapStateToProps({ sqlLab }, props) {
const queryEditor = sqlLab.queryEditors.find(
editor => editor.id === props.queryEditorId,
);
return { sqlLab, ...props, queryEditor };
return { sqlLab, ...props, queryEditor, queryEditors: sqlLab.queryEditors };
}
function mapDispatchToProps(dispatch) {

View File

@@ -31,6 +31,7 @@ import { Tooltip } from 'src/components/Tooltip';
import { detectOS } from 'src/utils/common';
import * as Actions from 'src/SqlLab/actions/sqlLab';
import { EmptyStateBig } from 'src/components/EmptyState';
import { newQueryTabName } from '../../utils/newQueryTabName';
import SqlEditor from '../SqlEditor';
import TabStatusIcon from '../TabStatusIcon';
@@ -262,19 +263,7 @@ class TabbedSqlEditors extends React.PureComponent {
'-- 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 newTitle = newQueryTabName(this.props.queryEditors || []);
const qe = {
title: newTitle,