@botonic/react
Version:
Build Chatbots using React
74 lines • 3.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevApp = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
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({ theme = {}, persistentMenu, coverComponent, blockInputs, enableEmojiPicker, enableAttachments, enableUserInput, enableAnimations, shadowDOM, hostId, storage, storageKey, onInit, onOpen, onClose, onMessage, onTrackEvent, webviews, ...botOptions }) {
super({
theme,
persistentMenu,
coverComponent,
blockInputs,
enableEmojiPicker,
enableAttachments,
enableUserInput,
enableAnimations,
shadowDOM,
hostId,
storage,
storageKey,
onInit,
onOpen,
onClose,
onMessage,
onTrackEvent,
});
this.webviews = webviews;
this.bot = new react_bot_1.ReactBot({
...botOptions,
});
}
getComponent(host, optionsAtRuntime = {}) {
let { theme = {}, storage, storageKey, onInit, onOpen, onClose, onMessage, onTrackEvent, hostId, ...webchatOptions } = optionsAtRuntime;
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, { ...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), webviews: this.webviews }));
}
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);
});
}
async onUserInput({ input, session, lastRoutePath }) {
this.onMessage?.(this, {
sentBy: index_types_1.SENDERS.user,
message: input,
});
const resp = await 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