UNPKG

@botonic/plugin-flow-builder

Version:

Use Flow Builder to show your contents

108 lines 4.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FlowButton = void 0; const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("@botonic/react"); const constants_1 = require("../constants"); const content_fields_base_1 = require("./content-fields-base"); const flow_webview_1 = require("./flow-webview"); const hubtype_fields_1 = require("./hubtype-fields"); class FlowButton extends content_fields_base_1.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 !== hubtype_fields_1.HtNodeWithContentType.WEBVIEW) { return undefined; } return flow_webview_1.FlowWebview.fromHubtypeCMS(targetNode); } trackFlow(request) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (this.flowWebview) { yield this.flowWebview.trackFlow(request); } }); } renderButton(buttonIndex, buttonStyle) { if (buttonStyle === hubtype_fields_1.HtButtonStyle.QUICK_REPLY) { return ((0, jsx_runtime_1.jsx)(react_1.Reply, Object.assign({ payload: this.getButtonPayload(buttonIndex) }, { children: this.text }), this.id)); } return ((0, jsx_runtime_1.jsx)(react_1.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}${constants_1.SOURCE_INFO_SEPARATOR}${buttonIndex}` : undefined; } } exports.FlowButton = FlowButton; //# sourceMappingURL=flow-button.js.map