UNPKG

@botonic/plugin-flow-builder

Version:

Use Flow Builder to show your contents

104 lines 4.27 kB
import { __awaiter } from "tslib"; import { jsx as _jsx } from "react/jsx-runtime"; import { Button, Reply } from '@botonic/react'; import { SOURCE_INFO_SEPARATOR } from '../constants'; import { ContentFieldsBase } from './content-fields-base'; import { FlowWebview } from './flow-webview'; import { HtButtonStyle, HtNodeWithContentType, } from './hubtype-fields'; export class FlowButton extends ContentFieldsBase { constructor() { super(...arguments); this.text = ''; } static fromHubtypeCMS(cmsButton, locale, cmsApi) { const urlId = this.getUrlId(cmsButton, locale); const newButton = new FlowButton(cmsButton.id); newButton.text = this.getTextByLocale(locale, cmsButton.text); if (cmsButton.target) { const webview = this.getTargetWebview(cmsApi, cmsButton.target.id); if (webview) { newButton.flowWebview = webview; const params = this.getWebviewParams(webview, cmsApi); newButton.webview = { name: webview.webviewComponentName }; newButton.params = params; } else { newButton.payload = cmsApi.getPayload(cmsButton.target); } } if (cmsButton.url && urlId) { const urlNode = cmsApi.getNodeById(urlId); newButton.url = urlNode.content.url; } return newButton; } static getWebviewParams(webview, cmsApi) { const params = { webviewId: webview.webviewTargetId, }; const exitSuccessContentID = this.getExitSuccessContentID(webview, cmsApi); if (exitSuccessContentID) { params.exitSuccessContentID = exitSuccessContentID; } return params; } static getExitSuccessContentID(webview, cmsApi) { var _a, _b; const webviewSuccessExit = (_a = webview.exits) === null || _a === void 0 ? void 0 : _a.find(exit => exit.name === 'Success'); const exitSuccessId = (_b = webviewSuccessExit === null || webviewSuccessExit === void 0 ? void 0 : webviewSuccessExit.target) === null || _b === void 0 ? void 0 : _b.id; if (!exitSuccessId) { return undefined; } const exitNode = cmsApi.getNodeById(exitSuccessId); return exitNode.code; } static fromAIAgent(button) { const newButton = new FlowButton(button.id); newButton.text = button.text; if (button.url) { newButton.url = button.url; } else { newButton.payload = button.payload; } return newButton; } static fromRating(button) { var _a; const newButton = new FlowButton(button.id); newButton.text = button.text; newButton.payload = button.payload; newButton.target = (_a = button.target) === null || _a === void 0 ? void 0 : _a.id; return newButton; } static getUrlId(cmsButton, locale) { var _a; return (_a = cmsButton.url.find(url => url.locale === locale)) === null || _a === void 0 ? void 0 : _a.id; } static getTargetWebview(cmsApi, targetId) { const targetNode = cmsApi.getNodeById(targetId); if (targetNode.type !== HtNodeWithContentType.WEBVIEW) { return undefined; } return FlowWebview.fromHubtypeCMS(targetNode); } trackFlow(request) { return __awaiter(this, void 0, void 0, function* () { if (this.flowWebview) { yield this.flowWebview.trackFlow(request); } }); } renderButton(buttonIndex, buttonStyle) { if (buttonStyle === HtButtonStyle.QUICK_REPLY) { return (_jsx(Reply, Object.assign({ payload: this.getButtonPayload(buttonIndex) }, { children: this.text }), this.id)); } return (_jsx(Button, Object.assign({ url: this.url, payload: this.getButtonPayload(buttonIndex), target: this.target, webview: this.webview, params: this.params }, { children: this.text }), this.id)); } getButtonPayload(buttonIndex) { return this.payload ? `${this.payload}${SOURCE_INFO_SEPARATOR}${buttonIndex}` : undefined; } } //# sourceMappingURL=flow-button.js.map