@botonic/react
Version:
Build Chatbots using React
108 lines • 4.72 kB
JavaScript
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 core_1 = require("@botonic/core");
const axios_1 = tslib_1.__importDefault(require("axios"));
const react_1 = tslib_1.__importDefault(require("react"));
const client_1 = require("react-dom/client");
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 urlContext = url.searchParams.get('context');
const session = JSON.parse(urlContext || '{}');
this.state = { session, params };
}
close(options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
let payload = options ? options.payload : null;
if (options === null || options === void 0 ? void 0 : options.path) {
payload = `__PATH_PAYLOAD__${options.path}`;
}
if (payload) {
if (options === null || options === void 0 ? void 0 : options.params) {
payload = `${payload}?${(0, core_1.params2queryString)(options.params)}`;
}
const session = this.state.session;
try {
const baseUrl = session._hubtype_api || 'https://api.hubtype.com';
const url = `${baseUrl}/v1/bots/${session.bot.id}/send_postback/`;
// eslint-disable-next-line @typescript-eslint/naming-convention
const data = { payload: payload, chat_id: session.user.id };
yield axios_1.default.post(url, data);
}
catch (e) {
console.log(e);
}
}
const provider = this.state.session.user.provider;
const impId = this.state.session.user.imp_id;
if (provider === core_1.PROVIDER.WHATSAPP) {
const phone_number = this.state.session.user.unformatted_phone_number;
location.href = 'https://wa.me/' + phone_number;
}
if (provider === core_1.PROVIDER.TELEGRAM) {
location.href = 'https://t.me/' + impId;
}
if (provider === core_1.PROVIDER.APPLE) {
location.href = 'https://bcrw.apple.com/urn:biz:' + impId;
}
if (provider === core_1.PROVIDER.TWITTER) {
location.href =
'https://twitter.com/messages/compose?recipient_id=' + impId;
}
if (provider === core_1.PROVIDER.INSTAGRAM) {
window.close();
}
if (provider === core_1.PROVIDER.FACEBOOK) {
try {
window.MessengerExtensions.requestCloseBrowser(function success() { }, function error(err) {
window.close();
});
}
catch (e) {
window.close();
}
}
if (provider === core_1.PROVIDER.WEBCHAT) {
try {
yield parent.postMessage('botonicCloseWebview', '*');
}
catch (e) { }
}
});
}
render() {
const webviewRequestContext = {
session: this.state.session || {},
params: this.state.params || {},
closeWebview: this.close.bind(this),
};
return ((0, jsx_runtime_1.jsx)(contexts_1.WebviewRequestContext.Provider, Object.assign({ value: webviewRequestContext }, { 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) {
const component = ((0, jsx_runtime_1.jsx)(react_router_dom_1.BrowserRouter, { children: (0, jsx_runtime_1.jsx)(App, { webviews: this.webviews, locales: this.locales }) }));
const container = dest;
const reactRoot = (0, client_1.createRoot)(container);
reactRoot.render(component);
}
}
exports.WebviewApp = WebviewApp;
//# sourceMappingURL=webview-app.js.map
;