fix(sqllab): custom url params disappeared (#23952)

This commit is contained in:
JUST.in DO IT 2023-05-08 11:55:56 -07:00 committed by GitHub
parent 9681d85999
commit d23df35087
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 36 deletions

View File

@ -35,7 +35,7 @@ import { newQueryTabName } from 'src/SqlLab/utils/newQueryTabName';
import QueryProvider from 'src/views/QueryProvider'; import QueryProvider from 'src/views/QueryProvider';
fetchMock.get('glob:*/api/v1/database/*', {}); fetchMock.get('glob:*/api/v1/database/*', {});
fetchMock.get('glob:*/savedqueryviewapi/api/get/*', {}); fetchMock.get('glob:*/api/v1/saved_query/*', {});
fetchMock.get('glob:*/kv/*', {}); fetchMock.get('glob:*/kv/*', {});
describe('TabbedSqlEditors', () => { describe('TabbedSqlEditors', () => {
@ -131,6 +131,18 @@ describe('TabbedSqlEditors', () => {
'/superset/sqllab', '/superset/sqllab',
); );
}); });
it('should handle custom url params', async () => {
uriStub.returns({
sql: 1,
dbid: 1,
custom_value: 'str',
extra_attr1: 'true',
});
await mountWithAct();
expect(window.history.replaceState.getCall(0).args[2]).toBe(
'/superset/sqllab?custom_value=str&extra_attr1=true',
);
});
}); });
it('should removeQueryEditor', () => { it('should removeQueryEditor', () => {
wrapper = getWrapper(); wrapper = getWrapper();

View File

@ -17,6 +17,7 @@
* under the License. * under the License.
*/ */
import React from 'react'; import React from 'react';
import pick from 'lodash/pick';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { EditableTabs } from 'src/components/Tabs'; import { EditableTabs } from 'src/components/Tabs';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
@ -117,56 +118,64 @@ class TabbedSqlEditors extends React.PureComponent {
// but for some reason this data isn't being passed properly through // but for some reason this data isn't being passed properly through
// the reducer. // the reducer.
const bootstrapData = getBootstrapData(); const bootstrapData = getBootstrapData();
const query = { const queryParameters = URI(window.location).search(true);
const {
id,
name,
sql,
savedQueryId,
datasourceKey,
queryId,
dbid,
dbname,
schema,
autorun,
new: isNewQuery,
...urlParams
} = {
...bootstrapData.requested_query, ...bootstrapData.requested_query,
...URI(window.location).search(true), ...queryParameters,
}; };
// Popping a new tab based on the querystring // Popping a new tab based on the querystring
if ( if (id || sql || savedQueryId || datasourceKey || queryId) {
query.id || if (id) {
query.sql || this.props.actions.popStoredQuery(id);
query.savedQueryId || } else if (savedQueryId) {
query.datasourceKey || this.props.actions.popSavedQuery(savedQueryId);
query.queryId } else if (queryId) {
) { this.props.actions.popQuery(queryId);
if (query.id) { } else if (datasourceKey) {
this.props.actions.popStoredQuery(query.id); this.props.actions.popDatasourceQuery(datasourceKey, sql);
} else if (query.savedQueryId) { } else if (sql) {
this.props.actions.popSavedQuery(query.savedQueryId); let databaseId = dbid;
} else if (query.queryId) { if (databaseId) {
this.props.actions.popQuery(query.queryId); databaseId = parseInt(databaseId, 10);
} else if (query.datasourceKey) {
this.props.actions.popDatasourceQuery(query.datasourceKey, query.sql);
} else if (query.sql) {
let dbId = query.dbid;
if (dbId) {
dbId = parseInt(dbId, 10);
} else { } else {
const { databases } = this.props; const { databases } = this.props;
const dbName = query.dbname; const databaseName = dbname;
if (dbName) { if (databaseName) {
Object.keys(databases).forEach(db => { Object.keys(databases).forEach(db => {
if (databases[db].database_name === dbName) { if (databases[db].database_name === databaseName) {
dbId = databases[db].id; databaseId = databases[db].id;
} }
}); });
} }
} }
const newQueryEditor = { const newQueryEditor = {
name: query.name, name,
dbId, dbId: databaseId,
schema: query.schema, schema,
autorun: query.autorun, autorun,
sql: query.sql, sql,
}; };
this.props.actions.addQueryEditor(newQueryEditor); this.props.actions.addQueryEditor(newQueryEditor);
} }
this.popNewTab(); this.popNewTab(pick(urlParams, Object.keys(queryParameters)));
} else if (query.new || this.props.queryEditors.length === 0) { } else if (isNewQuery || this.props.queryEditors.length === 0) {
this.newQueryEditor(); this.newQueryEditor();
if (query.new) { if (isNewQuery) {
window.history.replaceState({}, document.title, this.state.sqlLabUrl); window.history.replaceState({}, document.title, this.state.sqlLabUrl);
} }
} else { } else {
@ -187,9 +196,10 @@ class TabbedSqlEditors extends React.PureComponent {
} }
} }
popNewTab() { popNewTab(urlParams) {
// Clean the url in browser history // Clean the url in browser history
window.history.replaceState({}, document.title, this.state.sqlLabUrl); const updatedUrl = `${URI(this.state.sqlLabUrl).query(urlParams)}`;
window.history.replaceState({}, document.title, updatedUrl);
} }
activeQueryEditor() { activeQueryEditor() {