Skip to content

Commit c55ae69

Browse files
committed
Enable retrieving and updating embed_configs
1 parent 3be930b commit c55ae69

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

web/src/app/index.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ChakraContext from '../components/common/ChakraContext';
66
import App from '../components/common/App';
77
import { persistStore } from 'redux-persist';
88
import { RootState, store } from '../state/store';
9-
import { setAppRecording, setIframeInfo, updateIsAppOpen } from '../state/settings/reducer';
9+
import { setAppRecording, setIframeInfo, updateIsAppOpen, setEmbedConfigs } from '../state/settings/reducer';
1010
import { dispatch } from '../state/dispatch';
1111
import { log, queryDOMMap, setMinusxMode, toggleMinusXRoot, queryURL, forwardToTab, gdocRead, gdocWrite, gdocImage, gdocReadSelected, captureVisibleTab, getPendingMessage, gsheetSetUserToken} from './rpc';
1212
import _, { get, isEqual, pick, set } from 'lodash';
@@ -185,6 +185,23 @@ function ProviderApp() {
185185
document.head.appendChild(link)
186186
}
187187

188+
// Fetch JSON configuration
189+
fetch(`${iframeInfo.origin}/minusx.json`)
190+
.then(response => {
191+
if (response.ok) {
192+
return response.json()
193+
}
194+
throw new Error('Failed to fetch minusx.json')
195+
})
196+
.then(config => {
197+
if (config && typeof config === 'object') {
198+
dispatch(setEmbedConfigs(config))
199+
}
200+
})
201+
.catch(error => {
202+
console.warn('Could not fetch embed configuration:', error)
203+
})
204+
188205
return () => {
189206
const link = document.getElementById(linkId)
190207
if (link) {

web/src/components/common/Markdown.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { getApp } from '../../helpers/app'
1212
import { processSQLWithCtesOrModels } from '../../helpers/catalogAsModels'
1313
import { getAllTemplateTagsInQuery, replaceLLMFriendlyIdentifiersInSqlWithModels } from 'apps'
1414
import type { MetabaseModel } from 'apps/types'
15+
import type { EmbedConfigs } from '../../state/settings/reducer'
1516
import { Badge } from "@chakra-ui/react";
1617
import { CodeBlock } from './CodeBlock';
1718
import { BiChevronDown, BiChevronRight } from 'react-icons/bi';
@@ -166,10 +167,11 @@ function ImageComponent(props: any) {
166167
return <ZoomableImage src={props.src} alt={props.alt}/>
167168
}
168169

169-
function generateMetabaseQuestionURL(origin: string, sql: string, databaseId: number | null = null) {
170-
// Get all template tags in the query (we don't have access to snippets here, so pass undefined)
170+
function generateMetabaseQuestionURL(sql: string, databaseId: number | null = null, embedConfigs: EmbedConfigs = {}) {
171+
// Get Metabase origin from embed_configs if available, otherwise use iframe info
171172
const templateTags = getAllTemplateTagsInQuery(sql);
172173

174+
// Get all template tags in the query (we don't have access to snippets here, so pass undefined)
173175
const cardData = {
174176
"dataset_query": {
175177
"database": databaseId,
@@ -186,7 +188,11 @@ function generateMetabaseQuestionURL(origin: string, sql: string, databaseId: nu
186188
};
187189

188190
const hash = btoa(JSON.stringify(cardData));
189-
return `${origin}/question#${hash}`;
191+
const origin = embedConfigs.embed_host;
192+
if (!origin) {
193+
return `${getOrigin()}/question#${hash}`;
194+
}
195+
return `${origin}/question?hash=${hash}`;
190196
}
191197

192198
function extractLastSQLFromMessages(messages: any[], currentMessageIndex: number): string | null {
@@ -232,7 +238,8 @@ export function Markdown({content, messageIndex}: {content: string, messageIndex
232238
const settings = useSelector((state: RootState) => ({
233239
selectedCatalog: state.settings.selectedCatalog,
234240
availableCatalogs: state.settings.availableCatalogs,
235-
modelsMode: state.settings.modelsMode
241+
modelsMode: state.settings.modelsMode,
242+
embed_configs: state.settings.embed_configs
236243
}));
237244
const mxModels = useSelector((state: RootState) => state.cache.mxModels);
238245

@@ -250,13 +257,10 @@ export function Markdown({content, messageIndex}: {content: string, messageIndex
250257
}
251258

252259
if (lastSQL) {
253-
// Get Metabase origin from iframe info
254-
const metabaseOrigin = getOrigin();
255-
256260
// Get current database ID from app state
257261
const databaseId = toolContext?.dbId || null;
258262

259-
const questionURL = generateMetabaseQuestionURL(metabaseOrigin, lastSQL, databaseId);
263+
const questionURL = generateMetabaseQuestionURL(lastSQL, databaseId, settings.embed_configs);
260264

261265
return renderString(content, {
262266
'MX_LAST_SQL_URL': `\n\n --- \n\n Continue your analysis [here](${questionURL})`

web/src/state/settings/reducer.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ export interface SaveCatalogPayload extends Omit<ContextCatalog, 'allowWrite'> {
9999
currentUserId: string
100100
}
101101

102+
export interface EmbedConfigs {
103+
embed_host?: string
104+
}
105+
102106
interface Settings {
103107
isLocal: boolean,
104108
uploadLogs: boolean,
@@ -136,6 +140,7 @@ interface Settings {
136140
manuallyLimitContext: boolean
137141
useV2States: boolean
138142
currentEmail?: string
143+
embed_configs: EmbedConfigs
139144
}
140145

141146
const initialState: Settings = {
@@ -176,7 +181,8 @@ const initialState: Settings = {
176181
metadataHashes: {},
177182
metadataProcessingCache: {},
178183
manuallyLimitContext: false,
179-
useV2States: true
184+
useV2States: true,
185+
embed_configs: {}
180186
}
181187

182188
export const settingsSlice = createSlice({
@@ -410,6 +416,9 @@ export const settingsSlice = createSlice({
410416
},
411417
setUseV2States: (state, action: PayloadAction<boolean>) => {
412418
state.useV2States = action.payload
419+
},
420+
setEmbedConfigs: (state, action: PayloadAction<EmbedConfigs>) => {
421+
state.embed_configs = action.payload
413422
}
414423
},
415424
})
@@ -422,7 +431,7 @@ export const { updateIsLocal, updateUploadLogs,
422431
setIframeInfo, setConfirmChanges, setDemoMode, setAppRecording, setAiRules,
423432
applyTableDiff, setSelectedModels, setDRMode, setAnalystMode, setSelectedCatalog, saveCatalog, deleteCatalog, setMemberships,
424433
setGroupsEnabled, resetDefaultTablesDB, setModelsMode, setViewAllCatalogs, setEnableHighlightHelpers, setUseMemory, addMemory, setCustomCSS, setEnableStyleCustomization, setEnableUserDebugTools, setEnableReviews, setMetadataHash, setMetadataProcessingCache, clearMetadataProcessingCache,
425-
updateManualContextSelection, setUseV2States, setCurrentEmail
434+
updateManualContextSelection, setUseV2States, setCurrentEmail, setEmbedConfigs
426435
} = settingsSlice.actions
427436

428437
export default settingsSlice.reducer

web/src/state/store.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,20 @@ const migrations = {
530530
})
531531
newState.chat.last_warmed_on = latestMessageTime
532532
return newState
533+
},
534+
53: (state: RootState) => {
535+
let newState = {...state}
536+
// Add embed_configs to settings
537+
newState.settings.embed_configs = {}
538+
return newState
533539
}
534540
}
535541

536542
const BLACKLIST = ['billing', 'cache', userStateApi.reducerPath]
537543

538544
const persistConfig = {
539545
key: 'root',
540-
version: 52,
546+
version: 53,
541547
storage,
542548
blacklist: BLACKLIST,
543549
// @ts-ignore

0 commit comments

Comments
 (0)