@thoughtspot/visual-embed-sdk
Version:
ThoughtSpot Embed SDK
135 lines • 6.04 kB
JavaScript
import { ERROR_MESSAGE } from '../errors';
import { Param, ErrorDetailsTypes, EmbedErrorCodes } from '../types';
import { TsEmbed } from './ts-embed';
import { buildSpotterSidebarAppInitData } from './spotter-utils';
import { getQueryParamString, getFilterQuery, getRuntimeParameters, setParamIfDefined } from '../utils';
/**
* Embed ThoughtSpot AI Conversation.
* @version SDK: 1.37.0 | ThoughtSpot: 10.9.0.cl
* @group Embed components
* @example
* ```js
* const conversation = new SpotterEmbed('#tsEmbed', {
* worksheetId: 'worksheetId',
* searchOptions: {
* searchQuery: 'searchQuery',
* },
* });
* conversation.render();
* ```
*/
export class SpotterEmbed extends TsEmbed {
constructor(container, viewConfig) {
viewConfig = {
embedComponentType: 'conversation',
excludeRuntimeFiltersfromURL: true,
excludeRuntimeParametersfromURL: true,
...viewConfig,
};
super(container, viewConfig);
this.viewConfig = viewConfig;
}
/**
* Extends the default APP_INIT payload with `embedParams.spotterSidebarConfig`
* so the conv-assist app can read sidebar configuration on initialisation.
*
* Precedence for `enablePastConversationsSidebar`:
* `spotterSidebarConfig.enablePastConversationsSidebar` wins over the
* deprecated top-level `enablePastConversationsSidebar` flag; if the former
* is absent the latter is used as a fallback.
*
* An invalid `spotterDocumentationUrl` triggers a validation error and is
* excluded from the payload rather than forwarded to the app.
*/
async getAppInitData() {
const defaultAppInitData = await super.getAppInitData();
return buildSpotterSidebarAppInitData(defaultAppInitData, this.viewConfig, this.handleError.bind(this));
}
getEmbedParamsObject() {
const { worksheetId, searchOptions, disableSourceSelection, hideSourceSelection, dataPanelV2, showSpotterLimitations, hideSampleQuestions, runtimeFilters, excludeRuntimeFiltersfromURL, runtimeParameters, excludeRuntimeParametersfromURL, updatedSpotterChatPrompt, enableStopAnswerGenerationEmbed, spotterChatConfig, } = this.viewConfig;
if (!worksheetId) {
this.handleError({
errorType: ErrorDetailsTypes.VALIDATION_ERROR,
message: ERROR_MESSAGE.SPOTTER_EMBED_WORKSHEED_ID_NOT_FOUND,
code: EmbedErrorCodes.WORKSHEET_ID_NOT_FOUND,
error: ERROR_MESSAGE.SPOTTER_EMBED_WORKSHEED_ID_NOT_FOUND,
});
}
const queryParams = this.getBaseQueryParams();
queryParams[Param.SpotterEnabled] = true;
// Boolean params
setParamIfDefined(queryParams, Param.DisableSourceSelection, disableSourceSelection, true);
setParamIfDefined(queryParams, Param.HideSourceSelection, hideSourceSelection, true);
setParamIfDefined(queryParams, Param.DataPanelV2Enabled, dataPanelV2, true);
setParamIfDefined(queryParams, Param.ShowSpotterLimitations, showSpotterLimitations, true);
setParamIfDefined(queryParams, Param.HideSampleQuestions, hideSampleQuestions, true);
setParamIfDefined(queryParams, Param.UpdatedSpotterChatPrompt, updatedSpotterChatPrompt, true);
setParamIfDefined(queryParams, Param.EnableStopAnswerGenerationEmbed, enableStopAnswerGenerationEmbed, true);
// Handle spotterChatConfig params
if (spotterChatConfig) {
const { hideToolResponseCardBranding, toolResponseCardBrandingLabel, } = spotterChatConfig;
setParamIfDefined(queryParams, Param.HideToolResponseCardBranding, hideToolResponseCardBranding, true);
setParamIfDefined(queryParams, Param.ToolResponseCardBrandingLabel, toolResponseCardBrandingLabel);
}
return queryParams;
}
getIframeSrc() {
const { worksheetId, searchOptions, runtimeFilters, excludeRuntimeFiltersfromURL, runtimeParameters, excludeRuntimeParametersfromURL, } = this.viewConfig;
const path = 'insights/conv-assist';
const queryParams = this.getEmbedParamsObject();
let query = '';
const queryParamsString = getQueryParamString(queryParams, true);
if (queryParamsString) {
query = `?${queryParamsString}`;
}
const filterQuery = getFilterQuery(runtimeFilters || []);
if (filterQuery && !excludeRuntimeFiltersfromURL) {
query += `&${filterQuery}`;
}
const parameterQuery = getRuntimeParameters(runtimeParameters || []);
if (parameterQuery && !excludeRuntimeParametersfromURL) {
query += `&${parameterQuery}`;
}
const tsPostHashParams = this.getThoughtSpotPostUrlParams({
worksheet: worksheetId,
query: (searchOptions === null || searchOptions === void 0 ? void 0 : searchOptions.searchQuery) || '',
});
return `${this.getEmbedBasePath(query)}/embed/${path}${tsPostHashParams}`;
}
async render() {
await super.render();
const src = this.getIframeSrc();
await this.renderIFrame(src);
return this;
}
}
/**
* Embed ThoughtSpot AI Conversation.
* Use {@link SpotterEmbed} instead
* @version SDK: 1.37.0 | ThoughtSpot: 10.9.0.cl
* @deprecated from SDK: 1.39.0 | ThoughtSpot: 10.10.0.cl
* @group Embed components
* @example
* ```js
* const conversation = new SpotterEmbed('#tsEmbed', {
* worksheetId: 'worksheetId',
* searchOptions: {
* searchQuery: 'searchQuery',
* },
* });
* conversation.render();
* ```
*/
export class ConversationEmbed extends SpotterEmbed {
constructor(container, viewConfig) {
viewConfig = {
embedComponentType: 'conversation',
excludeRuntimeFiltersfromURL: true,
excludeRuntimeParametersfromURL: true,
...viewConfig,
};
super(container, viewConfig);
this.viewConfig = viewConfig;
}
}
//# sourceMappingURL=conversation.js.map