UNPKG

@robylon/robylon-web-react-sdk

Version:
3 lines (2 loc) 3.27 kB
"use strict";var e=require("react"),r=function(){return r=Object.assign||function(e){for(var r,o=1,n=arguments.length;o<n;o++)for(var i in r=arguments[o])Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i]);return e},r.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var o="http://localhost:3000",n="development",i=function(i){var t=e.useState({isLoading:!0,isInitialized:!1,error:null}),l=t[0],d=t[1],a={copilotUrl:o,environment:n}.copilotUrl,u=function(){var e,o,n,t,l,a,u,s,c,v;if(window.initializeRobylonSdk)try{window.initializeRobylonSdk({chatbot_id:i.chatbotId,user_id:i.userId,token:i.token,user_profile:r(r({},(null==i?void 0:i.userProfile)||{}),{email:null===(e=null==i?void 0:i.userProfile)||void 0===e?void 0:e.email,name:null===(o=null==i?void 0:i.userProfile)||void 0===o?void 0:o.name,mobile:null===(n=null==i?void 0:i.userProfile)||void 0===n?void 0:n.mobile,platform:"Web",OS:(null===(t=null==i?void 0:i.userProfile)||void 0===t?void 0:t.OS)||(null===(l=navigator.userAgentData)||void 0===l?void 0:l.platform)||(/Windows|Mac|Linux|Android|iOS/.exec(navigator.userAgent)||["Unknown"])[0],browser:(null===(a=null==i?void 0:i.userProfile)||void 0===a?void 0:a.browser)||(null===(c=null===(s=null===(u=navigator.userAgentData)||void 0===u?void 0:u.brands)||void 0===s?void 0:s[0])||void 0===c?void 0:c.brand)||(/Chrome|Firefox|Safari|Edge|Opera/.exec(navigator.userAgent)||["Unknown"])[0],sdk_version:(null===(v=null==i?void 0:i.userProfile)||void 0===v?void 0:v.sdkVersion)||"1.1.1"})}),d((function(e){return r(r({},e),{isInitialized:!0,isLoading:!1})}))}catch(e){d((function(o){return r(r({},o),{error:e instanceof Error?e.message:"Failed to initialize SDK",isLoading:!1})}))}else d((function(e){return r(r({},e),{error:"SDK not found",isLoading:!1})}))};return e.useEffect((function(){return function(){d((function(e){return r(r({},e),{isLoading:!0})})),console.log("copilotUrl====>",a);var e=document.createElement("script");return e.src=a+"/embed.min.js",e.defer=!0,e.onload=u,e.onerror=function(){d((function(e){return r(r({},e),{error:"Failed to load SDK script",isLoading:!1})}))},document.body.appendChild(e),function(){document.body.removeChild(e)}}()}),[i.chatbotId,i.userId]),l},t=e.createElement("div",{className:"flex items-center justify-center p-4 bg-white rounded-lg shadow"},e.createElement("div",{className:"animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900"})),l=function(r){var o=r.error;return e.createElement("div",{className:"p-4 bg-red-50 border border-red-200 rounded-lg"},e.createElement("p",{className:"text-red-600"},o))};exports.RobylonChatbot=function(r){var o=r.loadingComponent,n=void 0===o?t:o,d=r.errorComponent,a=void 0===d?l:d,u=function(e,r){var o={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&r.indexOf(n)<0&&(o[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(n=Object.getOwnPropertySymbols(e);i<n.length;i++)r.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(e,n[i])&&(o[n[i]]=e[n[i]])}return o}(r,["loadingComponent","errorComponent"]),s=i(u),c=s.isLoading,v=s.error;s.isInitialized;return c?e.createElement(e.Fragment,null,n):v?e.createElement(a,{error:v}):null},exports.useChatbot=i; //# sourceMappingURL=index.js.map