UNPKG

@botonic/react

Version:

Build Chatbots using React

73 lines 3.75 kB
import { __awaiter, __rest } from "tslib"; import { jsx as _jsx } from "react/jsx-runtime"; import React from 'react'; import { createRoot } from 'react-dom/client'; import { SENDERS } from './index-types'; import { ReactBot } from './react-bot'; import { onDOMLoaded } from './util/dom'; import { WebchatDev } from './webchat/webchat-dev'; import { WebchatApp } from './webchat-app'; export class DevApp extends WebchatApp { constructor(_a) { var { theme = {}, persistentMenu, coverComponent, blockInputs, enableEmojiPicker, enableAttachments, enableUserInput, enableAnimations, shadowDOM, hostId, storage, storageKey, onInit, onOpen, onClose, onMessage, onTrackEvent } = _a, botOptions = __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 ReactBot(Object.assign({}, botOptions)); } getComponent(host, optionsAtRuntime = {}) { let { theme = {}, storage, storageKey, onInit, onOpen, onClose, onMessage, onTrackEvent, hostId } = optionsAtRuntime, webchatOptions = __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 (_jsx(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 = {}) { onDOMLoaded(() => { const devAppComponent = this.getComponent(dest, optionsAtRuntime); const container = this.getReactMountNode(dest); const reactRoot = createRoot(container); reactRoot.render(devAppComponent); }); } onUserInput({ input, session, lastRoutePath }) { return __awaiter(this, void 0, void 0, function* () { this.onMessage && this.onMessage(this, { sentBy: SENDERS.user, message: input, }); const resp = yield this.bot.input({ input, session, lastRoutePath }); this.onMessage && resp.response.map(r => this.onMessage(this, { sentBy: SENDERS.bot, message: r, })); this.webchatRef.current.addBotResponse(resp); }); } } //# sourceMappingURL=dev-app.js.map