@botonic/react
Version:
Build Chatbots using React
106 lines • 4.7 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 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 };
}
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 s = this.state.session;
try {
const baseUrl = s._hubtype_api || 'https://api.hubtype.com';
const resp = yield (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 === 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 = {
getString: (stringId) => (0, core_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.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) {
(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