Skip to content

Commit 541c94c

Browse files
dont wait for db id (#292)
* show mx on all pages * choose if only db, null if otherwise * handle all cases when dbid is null * wip random page * make collections page work * Update dbId detection message in TaskUI --------- Co-authored-by: Sreejith <1743700+ppsreejith@users.noreply.github.com>
1 parent 87a551b commit 541c94c

File tree

8 files changed

+51
-43
lines changed

8 files changed

+51
-43
lines changed

apps/src/metabase/appController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ export class MetabaseController extends AppController<MetabaseAppState> {
426426
// return await this.updateSQLQuery({ sql, executeImmediately: true, _type: "csv", ctes: _ctes });
427427
// }
428428
}
429-
else if (pageType === 'dashboard') {
429+
else if ((pageType === 'dashboard') || (pageType === 'unknown')) {
430430
// if (hasTemplateTagsOrParams) {
431431
return await this.runSQLQueryWithParams({ sql, template_tags, parameters, ctes: _ctes });
432432
// } else {

apps/src/metabase/appState.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ export class MetabaseState extends DefaultAppState<MetabaseAppState> {
133133
const getState = this.useStore().getState
134134
const dbId = await getSelectedDbId();
135135
let toolEnabledNew = shouldEnable(elements, url);
136-
if (dbId === undefined || dbId === null) {
137-
toolEnabledNew = {
138-
value: false,
139-
reason: "Unable to detect correct database. Please navigate to a SQL query page to enable MinusX."
140-
}
141-
}
136+
// if (dbId === undefined || dbId === null) {
137+
// toolEnabledNew = {
138+
// value: false,
139+
// reason: "Unable to detect correct database. Please navigate to a SQL query page to enable MinusX."
140+
// }
141+
// }
142142
const pageType: MetabasePageType = determineMetabasePageType(elements, url);
143143
getState().update((oldState) => ({
144144
...oldState,
@@ -527,30 +527,6 @@ function determineMetabasePageType(elements: DOMQueryMapResponse, url: string):
527527
}
528528

529529
function shouldEnable(elements: DOMQueryMapResponse, url: string) {
530-
const hash = btoa(JSON.stringify({
531-
"dataset_query": {
532-
"database": null,
533-
"type": "native",
534-
"native": {
535-
"query": "",
536-
"template-tags": {}
537-
}
538-
},
539-
"display": "table",
540-
"parameters": [],
541-
"visualization_settings": {},
542-
"type": "question"
543-
}))
544-
const SQLQueryURL = new URL(url).origin + '/question#' + hash;
545-
const MBQLURL = new URL(url).origin + '/question/notebook';
546-
const reason = `To use MinusX on Metabase, head over to the [SQL query](${SQLQueryURL}), [Question Builder](${MBQLURL}) or any of your Dashboard pages!`
547-
const metabasePageType = determineMetabasePageType(elements, url);
548-
if (metabasePageType === 'unknown') {
549-
return {
550-
value: false,
551-
reason: reason
552-
};
553-
}
554530
return {
555531
value: true,
556532
reason: "",

apps/src/metabase/helpers/DOMToState.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { catalogAsModels } from 'web';
1717
import { canUseModelsModeForCatalog } from '../../../../web/src/helpers/catalogAsModels';
1818
import { getMBQLAppState } from './mbql/appState';
1919
import { isMBQLPageUrl, MBQLInfo } from './mbql/utils';
20+
import { isSQLPageUrl } from './sql/utils';
2021
import { getModelsWithFields, getSelectedAndRelevantModels, modifySqlForMetabaseModels} from './metabaseModels';
2122
import { MetabaseAppStateSQLEditorV2, MetabaseAppStateType, processCard } from './analystModeTypes';
2223
import { MetabaseTableOrModel } from './metabaseAPITypes';
@@ -181,6 +182,7 @@ export async function convertDOMtoStateSQLQueryV2() : Promise<MetabaseAppStateSQ
181182
const sqlErrorMessage = await getSqlErrorMessage();
182183
// add tables in the sql as relevant tables, after fetching their fields
183184
let relevantTablesWithFields: FormattedTable[] = []
185+
const pageType = isSQLPageUrl(metabaseUrl) ? MetabaseAppStateType.SQLEditor : MetabaseAppStateType.RandomPage;
184186
{
185187
const dbTables = await getAllRelevantTablesForSelectedDb(dbId || 0)
186188
const sqlTables = await getTablesFromSqlRegex(sqlQuery)
@@ -198,7 +200,7 @@ export async function convertDOMtoStateSQLQueryV2() : Promise<MetabaseAppStateSQ
198200
relevantTablesWithFields = sqlTablesWithFields
199201
}
200202
return {
201-
type: MetabaseAppStateType.SQLEditor,
203+
type: pageType,
202204
version: '2',
203205
metabaseOrigin,
204206
metabaseUrl,
@@ -346,10 +348,6 @@ export async function convertDOMtoState() {
346348
if (isMBQLPageUrl(url) || (qlType === 'query')) {
347349
return await convertDOMtoStateMBQLQuery();
348350
}
349-
// const appSettings = RPCs.getAppSettings()
350-
// if(appSettings.semanticPlanner) {
351-
// return await semanticQueryState();
352-
// }
353351
const appSettings = RPCs.getAppSettings()
354352
if (appSettings.useV2States && appSettings.analystMode) {
355353
return await convertDOMtoStateSQLQueryV2();

apps/src/metabase/helpers/analystModeTypes.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export enum MetabaseAppStateType {
88
SQLEditor = 'metabaseSQLEditor',
99
Dashboard = 'metabaseDashboard',
1010
SemanticQuery = 'metabaseSemanticQuery',
11-
MBQLEditor = 'metabaseMBQLEditor'
11+
MBQLEditor = 'metabaseMBQLEditor',
12+
RandomPage = 'metabaseRandomPage'
1213
}
1314

1415
interface MetabaseAppStateBase {
@@ -22,7 +23,7 @@ interface MetabaseAppStateBase {
2223
}
2324

2425
export interface MetabaseAppStateSQLEditorV2 extends MetabaseAppStateBase {
25-
type: MetabaseAppStateType.SQLEditor
26+
type: MetabaseAppStateType.SQLEditor | MetabaseAppStateType.RandomPage
2627
version: '2'
2728
currentCard: Card
2829
outputMarkdown: string

apps/src/metabase/helpers/metabaseStateAPI.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,26 @@ export async function getSelectedDbId(): Promise<number | undefined> {
5757
dbId = parseInt(dbId);
5858
} catch (e) {}
5959
} else {
60-
// this works for both MBQL and SQL pages
60+
// this works for all pages
61+
// if there is only one database, it will return the id
62+
// else if there is only one non-sample database, it will return the id
63+
// else (0 dbs, or >1 non sample dbs) it will return undefined
6164
dbId = await getMetabaseState('qb.card.dataset_query.database');
6265
if (!dbId) {
6366
const entity_dbs = await getMetabaseState('entities.databases') as object;
64-
dbId = _.find(entity_dbs, (db: any) => !db.is_sample)?.id || Object.keys(entity_dbs)[0];
67+
const all_dbs = Object.entries(entity_dbs)
68+
if (all_dbs.length === 1) {
69+
dbId = all_dbs[0][1].id;
70+
}
71+
else {
72+
const non_sample_dbs = all_dbs.filter(([key, value]) => !value.is_sample)
73+
if (non_sample_dbs.length > 1) {
74+
console.log('More than one DB found', non_sample_dbs);
75+
}
76+
else {
77+
dbId = non_sample_dbs[0][1].id;
78+
}
79+
}
6580
}
6681
}
6782

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const isSQLPageUrl = (url: string) => {
2+
return url.includes('/question');
3+
}

web/src/components/common/ChatContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const ChatContent: React.FC<{content: ChatMessageContent, messageIndex?:
1515
const url = useAppStore((state) => state.toolContext)?.url || ''
1616
const pageType = useAppStore((state) => state.toolContext)?.pageType || ''
1717
if (content.type == 'DEFAULT') {
18-
const contentText = (pageType === 'dashboard' && role === 'assistant') ? `${content.text} {{MX_LAST_SQL_URL}}` : content.text;
18+
const contentText = ((pageType === 'dashboard' || pageType === 'unknown') && role === 'assistant') ? `${content.text} {{MX_LAST_SQL_URL}}` : content.text;
1919
return (
2020
<div>
2121
{content.images.map(image => (

web/src/components/common/TaskUI.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,24 @@ const TaskUI = forwardRef<HTMLTextAreaElement>((_props, ref) => {
248248
toastDescription = "Please enter a valid message/question"
249249
preventRunTask = true
250250
}
251-
else if (isUndefined(get(toolContext, 'dbId'))) {
251+
else if (isUndefined(get(toolContext, 'dbId')) && toolContext.pageType == 'sql') {
252252
toastTitle = 'No database selected'
253-
toastDescription = "Please select a database"
253+
toastDescription = "You can select a specific database to use in the top left corner of the SQL editor"
254+
preventRunTask = true
255+
}
256+
else if (isUndefined(get(toolContext, 'dbId')) && toolContext.pageType == 'mbql') {
257+
toastTitle = 'No database selected'
258+
toastDescription = "You can select a specific database by selecting any table / model as data source"
259+
preventRunTask = true
260+
}
261+
else if (isUndefined(get(toolContext, 'dbId')) && toolContext.pageType == 'dashboard') {
262+
toastTitle = 'No database selected'
263+
toastDescription = "We can't tell which database to use! You can select a database either in the SQL editor or by selecting a table or model in the MBQL editor"
264+
preventRunTask = true
265+
}
266+
else if (isUndefined(get(toolContext, 'dbId')) && toolContext.pageType == 'unknown') {
267+
toastTitle = 'No database selected'
268+
toastDescription = "We can't tell which database to use! You can select a database by navigating to a dashboard, the SQL editor, or the MBQL editor"
254269
preventRunTask = true
255270
}
256271
else if (toolContext.pageType === 'dashboard' && (!drMode)) {

0 commit comments

Comments
 (0)