UNPKG

@botonic/react

Version:

Build Chatbots using React

36 lines 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebviewContainer = void 0; const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = require("react"); const constants_1 = require("../../constants"); const contexts_1 = require("../../contexts"); const context_1 = require("../context"); const header_1 = require("./header"); const styles_1 = require("./styles"); const WebviewContainer = () => { const { closeWebview, getThemeProperty, webchatState } = (0, react_1.useContext)(context_1.WebchatContext); const webviewRequestContext = { closeWebview: (options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return yield closeWebview(options); }), params: webchatState.webviewParams || {}, session: webchatState.session || {}, }; const Webview = webchatState.webview; const close = (e) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { if (e.data === 'botonicCloseWebview') { console.log('Received close message from webview'); yield closeWebview(); } }); (0, react_1.useEffect)(() => { window.addEventListener('message', close, false); return () => window.removeEventListener('message', close, false); }, []); // TODO: Review how to split the logic of rendering a webview in local development and production // In local development, Webview is a component. In production it is the URL of the webview const isUrlToWebview = typeof Webview === 'string'; return ((0, jsx_runtime_1.jsx)(contexts_1.WebviewRequestContext.Provider, Object.assign({ value: webviewRequestContext }, { children: (0, jsx_runtime_1.jsxs)(styles_1.StyledWebview, Object.assign({ role: constants_1.ROLES.WEBVIEW, style: Object.assign({}, getThemeProperty(constants_1.WEBCHAT.CUSTOM_PROPERTIES.webviewStyle)) }, { children: [(0, jsx_runtime_1.jsx)(header_1.WebviewHeader, {}), (0, jsx_runtime_1.jsx)(styles_1.StyledWebviewContent, { children: isUrlToWebview ? ((0, jsx_runtime_1.jsx)(styles_1.StyledFrame, { src: Webview })) : ((0, jsx_runtime_1.jsx)(styles_1.StyledFrameAsDiv, { children: (0, jsx_runtime_1.jsx)(Webview, {}) })) })] })) }))); }; exports.WebviewContainer = WebviewContainer; //# sourceMappingURL=index.js.map