react-use-intercom
Version:
React Intercom integration without the hassle, driven by hooks.
7 lines (5 loc) • 6.11 kB
JavaScript
import * as o from 'react';
var ae=Object.defineProperty;var R=Object.getOwnPropertySymbols;var H=Object.prototype.hasOwnProperty,K=Object.prototype.propertyIsEnumerable;var F=(e,t,n)=>t in e?ae(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,m=(e,t)=>{for(var n in t||(t={}))H.call(t,n)&&F(e,n,t[n]);if(R)for(var n of R(t))K.call(t,n)&&F(e,n,t[n]);return e};var G=(e,t)=>{var n={};for(var c in e)H.call(e,c)&&t.indexOf(c)<0&&(n[c]=e[c]);if(e!=null&&R)for(var c of R(e))t.indexOf(c)<0&&K.call(e,c)&&(n[c]=e[c]);return n};var p=(e,t)=>{let n="[react-use-intercom]";switch(e){case"info":console.log(`${n} ${t}`);break;case"warn":console.warn(`${n} ${t}`);break;case"error":console.error(`${n} ${t}`);break;default:console.log(`${n} ${t}`);}};var h=typeof window=="undefined",A=e=>(Object.keys(e).forEach(t=>{e[t]&&typeof e[t]=="object"?A(e[t]):e[t]===void 0&&delete e[t];}),e);var se=(e,...t)=>{if(!h&&window.Intercom)return window.Intercom.apply(null,[e,...t]);p("error",`${e} Intercom instance is not initalized yet`);},s=se;var ce=o.createContext(void 0),P=ce;var ie=(e,t=0)=>{var n=window,c=n.Intercom;if(typeof c=="function")c("reattach_activator"),c("update",n.intercomSettings);else {var w=document,l=function(){l.c(arguments);};l.q=[],l.c=function(u){l.q.push(u);},n.Intercom=l;var f=function(){setTimeout(function(){var u=w.createElement("script");u.type="text/javascript",u.async=!0,u.src="https://widget.intercom.io/widget/"+e;var d=w.getElementsByTagName("script")[0];d.parentNode.insertBefore(u,d);},t);};document.readyState==="complete"?f():n.attachEvent?n.attachEvent("onload",f):n.addEventListener("load",f,!1);}},X=ie;var me=e=>({custom_launcher_selector:e.customLauncherSelector,alignment:e.alignment,vertical_padding:e.verticalPadding,horizontal_padding:e.horizontalPadding,hide_default_launcher:e.hideDefaultLauncher,session_duration:e.sessionDuration,action_color:e.actionColor,background_color:e.backgroundColor}),Y=e=>m({company_id:e.companyId,name:e.name,created_at:e.createdAt,plan:e.plan,monthly_spend:e.monthlySpend,user_count:e.userCount,size:e.size,website:e.website,industry:e.industry},e.customAttributes),le=e=>({type:e.type,image_url:e.imageUrl}),ue=e=>{var t;return m({email:e.email,user_id:e.userId,created_at:e.createdAt,name:e.name,phone:e.phone,last_request_at:e.lastRequestAt,unsubscribed_from_emails:e.unsubscribedFromEmails,language_override:e.languageOverride,utm_campaign:e.utmCampaign,utm_content:e.utmContent,utm_medium:e.utmMedium,utm_source:e.utmSource,utm_term:e.utmTerm,avatar:e.avatar&&le(e.avatar),user_hash:e.userHash,company:e.company&&Y(e.company),companies:(t=e.companies)==null?void 0:t.map(Y)},e.customAttributes)},b=e=>A(m(m({},me(e)),ue(e)));var de=we=>{var k=we,{appId:e,autoBoot:t=!1,autoBootProps:n,children:c,onHide:w,onShow:l,onUnreadCountChange:f,onUserEmailSupplied:u,shouldInitialize:d=!h,apiBase:v,initializeDelay:ee}=k,te=G(k,["appId","autoBoot","autoBootProps","children","onHide","onShow","onUnreadCountChange","onUserEmailSupplied","shouldInitialize","apiBase","initializeDelay"]);let g=o.useRef(!1),y=o.useRef(!1),_=Object.keys(te).filter(r=>!r.startsWith("data-"));_.length>0&&p("warn",["some invalid props were passed to IntercomProvider. ",`Please check following props: ${_.join(", ")}.`].join(""));let C=o.useCallback(r=>{if(!window.Intercom&&!d){p("warn","Intercom instance is not initialized because `shouldInitialize` is set to `false` in `IntercomProvider`");return}let i=m(m({app_id:e},v&&{api_base:v}),r&&b(r));window.intercomSettings=i,s("boot",i),g.current=!0;},[v,e,d]),[x,I]=o.useState(!1),oe=o.useCallback(()=>{I(!1),w&&w();},[w,I]),re=o.useCallback(()=>{I(!0),l&&l();},[l,I]);!h&&d&&!y.current&&(X(e,ee),s("onHide",oe),s("onShow",re),s("onUserEmailSupplied",u),f&&s("onUnreadCountChange",f),t&&C(n),y.current=!0);let a=o.useCallback((r,i)=>{if(!window.Intercom&&!d){p("warn","Intercom instance is not initialized because `shouldInitialize` is set to `false` in `IntercomProvider`");return}if(!g.current){p("warn",[`"${r}" was called but Intercom has not booted yet. `,`Please call 'boot' before calling '${r}' or `,"set 'autoBoot' to true in the IntercomProvider."].join(""));return}return i()},[d]),S=o.useCallback(()=>{g.current&&(s("shutdown"),delete window.intercomSettings,g.current=!1);},[]),D=o.useCallback(()=>{g.current&&(s("shutdown"),delete window.Intercom,delete window.intercomSettings,g.current=!1);},[]),M=o.useCallback(()=>{a("update",()=>{let r=Math.floor(new Date().getTime()/1e3);s("update",{last_request_at:r});});},[a]),E=o.useCallback(r=>{a("update",()=>{if(!r){M();return}let i=b(r);window.intercomSettings=m(m({},window.intercomSettings),i),s("update",i);});},[a,M]),T=o.useCallback(()=>{a("hide",()=>{s("hide");});},[a]),$=o.useCallback(()=>{a("show",()=>s("show"));},[a]),N=o.useCallback(()=>{a("showMessages",()=>{s("showMessages");});},[a]),V=o.useCallback(r=>{a("showNewMessage",()=>{r?s("showNewMessage",r):s("showNewMessage");});},[a]),z=o.useCallback(()=>a("getVisitorId",()=>s("getVisitorId")),[a]),L=o.useCallback(r=>{a("startTour",()=>{s("startTour",r);});},[a]),q=o.useCallback(r=>{a("startChecklist",()=>{s("startChecklist",r);});},[a]),O=o.useCallback((r,i)=>{a("trackEvent",()=>{i?s("trackEvent",r,i):s("trackEvent",r);});},[a]),B=o.useCallback(r=>a("showArticle",()=>{s("showArticle",r);}),[a]),U=o.useCallback(r=>a("showSpace",()=>{s("showSpace",r);}),[a]),W=o.useCallback(r=>{a("startSurvey",()=>{s("startSurvey",r);});},[a]),j=o.useCallback(r=>a("showNews",()=>{s("showNews",r);}),[a]),ne=o.useMemo(()=>({boot:C,shutdown:S,hardShutdown:D,update:E,hide:T,show:$,isOpen:x,showMessages:N,showNewMessage:V,getVisitorId:z,startTour:L,startChecklist:q,trackEvent:O,showArticle:B,startSurvey:W,showSpace:U,showNews:j}),[C,S,D,E,T,$,x,N,V,z,L,q,O,B,W,U,j]);return o.createElement(P.Provider,{value:ne},c)},Z=()=>{let e=o.useContext(P);if(e===void 0)throw new Error('"useIntercom" must be used within `IntercomProvider`.');return e};var pe=()=>Z();
export { de as IntercomProvider, pe as useIntercom };
//# sourceMappingURL=out.js.map
//# sourceMappingURL=index.mjs.map