UNPKG

@botonic/plugin-flow-builder

Version:

Use Flow Builder to show your contents

77 lines 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FlowText = 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 tracking_1 = require("../tracking"); const utils_1 = require("../utils"); const content_fields_base_1 = require("./content-fields-base"); const flow_button_1 = require("./flow-button"); const hubtype_fields_1 = require("./hubtype-fields"); class FlowText extends content_fields_base_1.ContentFieldsBase { constructor() { super(...arguments); this.text = ''; this.buttons = []; this.buttonStyle = hubtype_fields_1.HtButtonStyle.BUTTON; } static fromHubtypeCMS(cmsText, locale, cmsApi) { const newText = new FlowText(cmsText.id); newText.code = cmsText.code; newText.buttonStyle = cmsText.content.buttons_style || hubtype_fields_1.HtButtonStyle.BUTTON; newText.text = this.getTextByLocale(locale, cmsText.content.text); newText.buttons = cmsText.content.buttons.map(button => flow_button_1.FlowButton.fromHubtypeCMS(button, locale, cmsApi)); newText.followUp = cmsText.follow_up; return newText; } static replaceVariables(text, request) { const matches = text.match(constants_1.VARIABLE_PATTERN); let replacedText = text; if (matches && request) { matches.forEach(match => { const keyPath = match.slice(1, -1); const botVariable = keyPath.endsWith(constants_1.ACCESS_TOKEN_VARIABLE_KEY) ? match : (0, utils_1.getValueFromKeyPath)(request, keyPath); // TODO In local if change variable and render multiple times the value is always the last update replacedText = replacedText.replace(match, this.isValidType(botVariable) ? botVariable : match); }); } return replacedText; } static isValidType(botVariable) { const validTypes = ['boolean', 'string', 'number']; return validTypes.includes(typeof botVariable); } trackFlow(request) { return tslib_1.__awaiter(this, void 0, void 0, function* () { yield (0, tracking_1.trackOneContent)(request, this); for (const button of this.buttons) { yield button.trackFlow(request); } }); } static fromAIAgent(id, textMessage) { if (textMessage.type === 'text') { return (0, jsx_runtime_1.jsx)(react_1.Text, { children: textMessage.content.text }, id); } return ((0, jsx_runtime_1.jsxs)(react_1.Text, { children: [textMessage.content.text, textMessage.content.buttons.map((button, buttonIndex) => { const buttonData = { id: `${id}-button-${buttonIndex}`, text: button.text, url: button.url, payload: button.payload || `${constants_1.EMPTY_PAYLOAD}${constants_1.SOURCE_INFO_SEPARATOR}${buttonIndex}`, }; return flow_button_1.FlowButton.fromAIAgent(buttonData).renderButton(buttonIndex); })] }, id)); } toBotonic(id, request) { const replacedText = FlowText.replaceVariables(this.text, request); return ((0, jsx_runtime_1.jsxs)(react_1.Text, { children: [replacedText, this.buttons.map((button, buttonIndex) => button.renderButton(buttonIndex, this.buttonStyle))] }, id)); } } exports.FlowText = FlowText; //# sourceMappingURL=flow-text.js.map