UNPKG

@botonic/react

Version:

Build Chatbots using React

102 lines 4.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebviewApp = void 0; const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); // @ts-nocheck const i18n_1 = require("@botonic/core/lib/esm/i18n"); const legacy_types_1 = require("@botonic/core/lib/esm/models/legacy-types"); const utils_1 = require("@botonic/core/lib/esm/utils"); const axios_1 = tslib_1.__importDefault(require("axios")); const react_1 = tslib_1.__importDefault(require("react")); const react_dom_1 = require("react-dom"); const react_router_dom_1 = require("react-router-dom"); const contexts_1 = require("./contexts"); class App extends react_1.default.Component { constructor(props) { super(props); const url = new URL(window.location.href); const params = Array.from(url.searchParams.entries()) .filter(([key, value]) => key != 'context') .reduce((o, [key, value]) => { o[key] = value; return o; }, {}); const session = JSON.parse(url.searchParams.get('context') || {}); this.state = { session, params }; } async close(options) { let payload = options ? options.payload : null; if (options.path) payload = `__PATH_PAYLOAD__${options.path}`; if (payload) { if (options.params) { payload = `${payload}?${(0, utils_1.params2queryString)(options.params)}`; } const s = this.state.session; try { const baseUrl = s._hubtype_api || 'https://api.hubtype.com'; const resp = await (0, axios_1.default)({ method: 'post', url: `${baseUrl}/v1/bots/${s.bot.id}/send_postback/`, // eslint-disable-next-line @typescript-eslint/naming-convention data: { payload: payload, chat_id: s.user.id }, }); } catch (e) { console.log(e); } } const provider = this.state.session.user.provider; const impId = this.state.session.user.imp_id; if (provider === legacy_types_1.PROVIDER.WHATSAPP) { const phone_number = this.state.session.user.unformatted_phone_number; location.href = 'https://wa.me/' + phone_number; } if (provider === legacy_types_1.PROVIDER.TELEGRAM) { location.href = 'https://t.me/' + impId; } if (provider === legacy_types_1.PROVIDER.APPLE) { location.href = 'https://bcrw.apple.com/urn:biz:' + impId; } if (provider === legacy_types_1.PROVIDER.TWITTER) { location.href = 'https://twitter.com/messages/compose?recipient_id=' + impId; } if (provider === legacy_types_1.PROVIDER.INSTAGRAM) { window.close(); } if (provider === legacy_types_1.PROVIDER.FACEBOOK) { try { window.MessengerExtensions.requestCloseBrowser(() => undefined, err => console.log(err)); } catch (e) { } } if (provider === legacy_types_1.PROVIDER.WEBCHAT) { try { await parent.postMessage('botonicCloseWebview', '*'); } catch (e) { } } } render() { const requestContext = { getString: stringId => (0, i18n_1.getString)(this.props.locales, this.state.session.__locale, stringId), session: this.state.session || {}, params: this.state.params || {}, closeWebview: this.close.bind(this), }; return ((0, jsx_runtime_1.jsx)(contexts_1.RequestContext.Provider, Object.assign({ value: requestContext }, { children: this.props.webviews.map((Webview, i) => ((0, jsx_runtime_1.jsx)(react_router_dom_1.Route, { path: `/${Webview.name}`, component: Webview }, i))) }))); } } class WebviewApp { constructor({ webviews, locales }) { this.webviews = webviews; this.locales = locales; } render(dest) { (0, react_dom_1.render)((0, jsx_runtime_1.jsx)(react_router_dom_1.BrowserRouter, { children: (0, jsx_runtime_1.jsx)(App, { webviews: this.webviews, locales: this.locales }) }), dest); } } exports.WebviewApp = WebviewApp; //# sourceMappingURL=webview-app.js.map