UNPKG

react-live-chat-customerly

Version:

The Live Chat component for React Apps by Customerly.

274 lines (234 loc) 7.71 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var React = require('react'); function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } var NAME = "customerly"; var log = function log(level, message) { { switch (level) { case "info": console.log("[" + NAME + "] " + message); break; case "warn": console.warn("[" + NAME + "] " + message); break; case "error": console.error("[" + NAME + "] " + message); break; default: console.log("[" + NAME + "] " + message); } } }; var isSSR = typeof window === "undefined"; var Caller = function Caller(method) { if (!isSSR && window.customerly) { var _window$customerly; for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } return (_window$customerly = window.customerly)[method].apply(_window$customerly, args); } else { log("error", method + " Customerly is not initialized yet"); } }; var CustomerlyContext = /*#__PURE__*/React.createContext(undefined); var MESSENGER_DOMAIN = "https://messenger.customerly.io/launcher.js"; var MESSENGER_BETA_DOMAIN = "https://messenger.staging.customerly.io/launcher.js"; //@ts-nocheck var initialize = function initialize(_ref) { var beta = _ref.beta; var w = window, d = document, name_key = "customerly", queue_key = "queue", load_key = "load", settings_key = "settings", c = w[name_key] = w[name_key] || []; if (c.initialized) { return void c["throw"]("[customerly] SDK already initialized. Snippet included twice."); } c.initialized = !0; c.loaded = !1; c.methods = ["event", "attribute", "update", "show", "hide", "open", "close"]; c[queue_key] = []; c["throw"] = function (message) { w.console && !c.debug && console.error && console.error(message); }; c.factory = function (e) { return function () { var n = Array.prototype.slice.call(arguments); // eslint-disable-next-line no-sequences return n.unshift(e), c[queue_key].push(n), c; }; }; c[load_key] = function (settings) { c[settings_key] = settings || {}; if (c.loaded) { return void c["throw"]("[customerly] SDK already loaded. Use `customerly.update` to change settings."); } c.loaded = !0; var s = d.createElement("script"); s.type = "text/javascript"; s.async = !0; s.src = beta ? MESSENGER_BETA_DOMAIN : MESSENGER_DOMAIN; var e = d.getElementsByTagName("script")[0]; e.parentNode.insertBefore(s, e); }; c.methods.forEach(function (e) { c[e] = c.factory(e); }); }; var CustomerlyProvider = function CustomerlyProvider(_ref) { var appId = _ref.appId, beta = _ref.beta, children = _ref.children; var canInitialize = !isSSR; var isLoaded = React.useRef(false); var isInitialized = React.useRef(false); var load = React.useCallback(function (settings) { if (!window.customerly && !canInitialize) { return; } if (isLoaded.current) { return; } Caller("load", _extends({}, settings, { app_id: appId })); isLoaded.current = true; }, [appId, canInitialize]); if (!isSSR && canInitialize && !isInitialized.current) { initialize({ beta: beta }); isInitialized.current = true; } var safeCall = React.useCallback(function (callback) { if (!window.customerly && !canInitialize) { return; } return callback(); }, [canInitialize]); var update = React.useCallback(function (settings) { safeCall(function () { Caller("update", _extends({}, settings, { app_id: appId })); }); }, [appId, safeCall]); var hide = React.useCallback(function () { safeCall(function () { Caller("hide"); }); }, [safeCall]); var show = React.useCallback(function () { safeCall(function () { Caller("show"); }); }, [safeCall]); var open = React.useCallback(function () { safeCall(function () { Caller("open"); }); }, [safeCall]); var close = React.useCallback(function () { safeCall(function () { Caller("close"); }); }, [safeCall]); var event = React.useCallback(function (eventName) { safeCall(function () { Caller("event", eventName); }); }, [safeCall]); var attribute = React.useCallback(function (attributeIdentifier, attributeValue) { safeCall(function () { Caller("attribute", attributeIdentifier, attributeValue); }); }, [safeCall]); var showArticle = React.useCallback(function (collectionSlugOrArticleId, articleSlug) { safeCall(function () { Caller("showArticle", collectionSlugOrArticleId, articleSlug); }); }, [safeCall]); var showBookMeeting = React.useCallback(function () { safeCall(function () { Caller("showBookMeeting"); }); }, [safeCall]); var showNewMessage = React.useCallback(function (message) { safeCall(function () { Caller("showNewMessage", message); }); }, [safeCall]); var sendNewMessage = React.useCallback(function (message) { safeCall(function () { Caller("sendNewMessage", message); }); }, [safeCall]); var logout = React.useCallback(function () { safeCall(function () { Caller("logout"); }); }, [safeCall]); var registerCallback = React.useCallback(function (callback) { safeCall(function () { window.customerly[callback.type] = callback["function"]; }); }, [safeCall]); var providerValue = React.useMemo(function () { return { load: load, update: update, open: open, hide: hide, show: show, close: close, event: event, attribute: attribute, showArticle: showArticle, showBookMeeting: showBookMeeting, showNewMessage: showNewMessage, sendNewMessage: sendNewMessage, logout: logout, registerCallback: registerCallback }; }, [attribute, close, event, hide, load, logout, open, sendNewMessage, showArticle, showBookMeeting, show, showNewMessage, update, registerCallback]); return React.createElement(CustomerlyContext.Provider, { value: providerValue }, children); }; var useCustomerlyContext = function useCustomerlyContext() { var context = React.useContext(CustomerlyContext); if ( context === undefined) { throw new Error("`useCustomerly` must be used within `CustomerlyProvider`."); } return context; }; var useCustomerly = function useCustomerly() { return useCustomerlyContext(); }; (function (ChatflowNotificationCallbackItemType) { ChatflowNotificationCallbackItemType["Article"] = "article"; ChatflowNotificationCallbackItemType["Attributes"] = "attributes"; ChatflowNotificationCallbackItemType["Button"] = "button"; ChatflowNotificationCallbackItemType["CalendarButton"] = "calendar_button"; ChatflowNotificationCallbackItemType["CloseButton"] = "close_button"; ChatflowNotificationCallbackItemType["StartConversationButton"] = "start_conversation_button"; })(exports.ChatflowNotificationCallbackItemType || (exports.ChatflowNotificationCallbackItemType = {})); exports.CustomerlyProvider = CustomerlyProvider; exports.useCustomerly = useCustomerly; //# sourceMappingURL=react-live-chat-customerly.cjs.development.js.map