@botonic/react
Version:
Build Chatbots using React
77 lines • 4.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevApp = void 0;
const tslib_1 = require("tslib");
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = tslib_1.__importDefault(require("react"));
const client_1 = require("react-dom/client");
const index_types_1 = require("./index-types");
const react_bot_1 = require("./react-bot");
const dom_1 = require("./util/dom");
const webchat_dev_1 = require("./webchat/webchat-dev");
const webchat_app_1 = require("./webchat-app");
class DevApp extends webchat_app_1.WebchatApp {
constructor(_a) {
var { theme = {}, persistentMenu, coverComponent, blockInputs, enableEmojiPicker, enableAttachments, enableUserInput, enableAnimations, shadowDOM, hostId, storage, storageKey, onInit, onOpen, onClose, onMessage, onTrackEvent } = _a, botOptions = tslib_1.__rest(_a, ["theme", "persistentMenu", "coverComponent", "blockInputs", "enableEmojiPicker", "enableAttachments", "enableUserInput", "enableAnimations", "shadowDOM", "hostId", "storage", "storageKey", "onInit", "onOpen", "onClose", "onMessage", "onTrackEvent"]);
super({
theme,
persistentMenu,
coverComponent,
blockInputs,
enableEmojiPicker,
enableAttachments,
enableUserInput,
enableAnimations,
shadowDOM,
hostId,
storage,
storageKey,
onInit,
onOpen,
onClose,
onMessage,
onTrackEvent,
});
this.bot = new react_bot_1.ReactBot(Object.assign({}, botOptions));
}
getComponent(host, optionsAtRuntime = {}) {
let { theme = {}, storage, storageKey, onInit, onOpen, onClose, onMessage, onTrackEvent, hostId } = optionsAtRuntime, webchatOptions = tslib_1.__rest(optionsAtRuntime, ["theme", "storage", "storageKey", "onInit", "onOpen", "onClose", "onMessage", "onTrackEvent", "hostId"]);
theme = super.createInitialTheme(optionsAtRuntime);
storage = storage || this.storage;
storageKey = storageKey || this.storageKey;
this.onInit = onInit || this.onInit;
this.onOpen = onOpen || this.onOpen;
this.onClose = onClose || this.onClose;
this.onMessage = onMessage || this.onMessage;
this.onTrackEvent = onTrackEvent || this.onTrackEvent;
this.hostId = hostId || this.hostId;
this.createRootElement(host);
return ((0, jsx_runtime_1.jsx)(webchat_dev_1.WebchatDev, Object.assign({}, webchatOptions, { ref: this.webchatRef, host: this.host, shadowDOM: this.shadowDOM, theme: theme, storage: storage, storageKey: storageKey, onInit: (...args) => this.onInitWebchat(...args), onOpen: (...args) => this.onOpenWebchat(...args), onClose: (...args) => this.onCloseWebchat(...args), onUserInput: (...args) => this.onUserInput(...args), onTrackEvent: (...args) => this.onTrackEvent(...args) })));
}
render(dest, optionsAtRuntime = {}) {
(0, dom_1.onDOMLoaded)(() => {
const devAppComponent = this.getComponent(dest, optionsAtRuntime);
const container = this.getReactMountNode(dest);
const reactRoot = (0, client_1.createRoot)(container);
reactRoot.render(devAppComponent);
});
}
onUserInput({ input, session, lastRoutePath }) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
this.onMessage &&
this.onMessage(this, {
sentBy: index_types_1.SENDERS.user,
message: input,
});
const resp = yield this.bot.input({ input, session, lastRoutePath });
this.onMessage &&
resp.response.map(r => this.onMessage(this, {
sentBy: index_types_1.SENDERS.bot,
message: r,
}));
this.webchatRef.current.addBotResponse(resp);
});
}
}
exports.DevApp = DevApp;
//# sourceMappingURL=dev-app.js.map