react-use-intercom
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -277,24 +277,32 @@ 'use strict'; | ||
var isBooted = React.useRef(autoBoot); | ||
var isBooted = React.useRef(false); | ||
var isInitialized = React.useRef(false); | ||
if (!isEmptyObject(rest) && "development" !== "production") log('error', ['some invalid props were passed to IntercomProvider. ', "Please check following props: " + Object.keys(rest).join(', ') + "."].join('')); | ||
var boot = React.useCallback(function (props) { | ||
if (!window.Intercom && !shouldInitialize) { | ||
log('warn', 'Intercom instance is not initialized because `shouldInitialize` is set to `false` in `IntercomProvider`'); | ||
return; | ||
} | ||
if (!isSSR && !window.Intercom && shouldInitialize) { | ||
initialize(appId, initializeDelay); // Only add listeners on initialization | ||
if (isBooted.current) return; | ||
var metaData = _extends({ | ||
app_id: appId | ||
}, apiBase && { | ||
api_base: apiBase | ||
}, props && mapIntercomPropsToRawIntercomProps(props)); | ||
window.intercomSettings = metaData; | ||
IntercomAPI('boot', metaData); | ||
isBooted.current = true; | ||
}, [apiBase, appId, shouldInitialize]); | ||
if (!isSSR && shouldInitialize && !isInitialized.current) { | ||
initialize(appId, initializeDelay); // attach listeners | ||
if (onHide) IntercomAPI('onHide', onHide); | ||
if (onShow) IntercomAPI('onShow', onShow); | ||
if (onUnreadCountChange) IntercomAPI('onUnreadCountChange', onUnreadCountChange); | ||
if (autoBoot) { | ||
IntercomAPI('boot', _extends({ | ||
app_id: appId | ||
}, apiBase && { | ||
api_base: apiBase | ||
})); | ||
window.intercomSettings = _extends({ | ||
app_id: appId | ||
}, apiBase && { | ||
api_base: apiBase | ||
}); | ||
} | ||
if (autoBoot) boot(); | ||
isInitialized.current = true; | ||
} | ||
@@ -319,20 +327,2 @@ | ||
}, [shouldInitialize]); | ||
var boot = React.useCallback(function (props) { | ||
if (!window.Intercom && !shouldInitialize) { | ||
log('warn', 'Intercom instance is not initialized because `shouldInitialize` is set to `false` in `IntercomProvider`'); | ||
return; | ||
} | ||
if (isBooted.current) return; | ||
var metaData = _extends({ | ||
app_id: appId | ||
}, apiBase && { | ||
api_base: apiBase | ||
}, props && mapIntercomPropsToRawIntercomProps(props)); | ||
window.intercomSettings = metaData; | ||
IntercomAPI('boot', metaData); | ||
isBooted.current = true; | ||
}, [apiBase, appId, shouldInitialize]); | ||
var shutdown = React.useCallback(function () { | ||
@@ -339,0 +329,0 @@ if (!isBooted.current) return; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");function t(){return(t=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e}).apply(this,arguments)}var n="undefined"==typeof window,o=function(e){if(!n&&window.Intercom){for(var t=arguments.length,o=new Array(t>1?t-1:0),a=1;a<t;a++)o[a-1]=arguments[a];return window.Intercom.apply(null,[e].concat(o))}},a=e.createContext({boot:function(){},shutdown:function(){},hardShutdown:function(){},update:function(){},hide:function(){},show:function(){},showMessages:function(){},showNewMessages:function(){},getVisitorId:function(){return""},startTour:function(){},trackEvent:function(){}}),r=function(e){return t({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)},i=function(e){return{type:e.type,image_url:e.imageUrl}},u=function(e){return function e(t){return Object.keys(t).forEach((function(n){t[n]&&"object"==typeof t[n]?e(t[n]):void 0===t[n]&&delete t[n]})),t}(t({},{custom_launcher_selector:(n=e).customLauncherSelector,alignment:n.alignment,vertical_padding:n.verticalPadding,horizontal_padding:n.horizontalPadding,hide_default_launcher:n.hideDefaultLauncher,session_duration:n.sessionDuration,action_color:n.actionColor,background_color:n.backgroundColor},function(e){var n;return t({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&&i(e.avatar),user_hash:e.userHash,company:e.company&&r(e.company),companies:null==(n=e.companies)?void 0:n.map(r)},e.customAttributes)}(e)));var n};exports.IntercomProvider=function(r){var i=r.appId,c=r.autoBoot,s=void 0!==c&&c,d=r.children,l=r.onHide,f=r.onShow,m=r.onUnreadCountChange,p=r.shouldInitialize,h=void 0===p?!n:p,w=r.apiBase,g=r.initializeDelay,v=function(e,t){if(null==e)return{};var n,o,a={},r=Object.keys(e);for(o=0;o<r.length;o++)t.indexOf(n=r[o])>=0||(a[n]=e[n]);return a}(r,["appId","autoBoot","children","onHide","onShow","onUnreadCountChange","shouldInitialize","apiBase","initializeDelay"]),_=e.useRef(s);Object.keys(v),n||window.Intercom||!h||(function(e,t){void 0===t&&(t=0);var n=window,o=n.Intercom;if("function"==typeof o)o("reattach_activator"),o("update",n.intercomSettings);else{var a=document,r=function e(){e.c(arguments)};r.q=[],r.c=function(e){r.q.push(e)},n.Intercom=r;var i=function(){setTimeout((function(){var t=a.createElement("script");t.type="text/javascript",t.async=!0,t.src="https://widget.intercom.io/widget/"+e;var n=a.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n)}),t)};"complete"===document.readyState?i():n.attachEvent?n.attachEvent("onload",i):n.addEventListener("load",i,!1)}}(i,g),l&&o("onHide",l),f&&o("onShow",f),m&&o("onUnreadCountChange",m),s&&(o("boot",t({app_id:i},w&&{api_base:w})),window.intercomSettings=t({app_id:i},w&&{api_base:w})));var b=e.useCallback((function(e,t){if((window.Intercom||h)&&_.current)return t()}),[h]),y=e.useCallback((function(e){if((window.Intercom||h)&&!_.current){var n=t({app_id:i},w&&{api_base:w},e&&u(e));window.intercomSettings=n,o("boot",n),_.current=!0}}),[w,i,h]),C=e.useCallback((function(){_.current&&(o("shutdown"),_.current=!1)}),[]),k=e.useCallback((function(){_.current&&(o("shutdown"),delete window.Intercom,delete window.intercomSettings,_.current=!1)}),[]),I=e.useCallback((function(){b("update",(function(){var e=(new Date).getTime();o("update",{last_requested_at:e})}))}),[b]),S=e.useCallback((function(e){b("update",(function(){if(e){var n=u(e);window.intercomSettings=t({},window.intercomSettings,n),o("update",n)}else I()}))}),[b,I]),E=e.useCallback((function(){b("hide",(function(){o("hide")}))}),[b]),M=e.useCallback((function(){b("show",(function(){return o("show")}))}),[b]),O=e.useCallback((function(){b("showMessages",(function(){o("showMessages")}))}),[b]),j=e.useCallback((function(e){b("showNewMessage",(function(){e?o("showNewMessage",e):o("showNewMessage")}))}),[b]),z=e.useCallback((function(){return b("getVisitorId",(function(){return o("getVisitorId")}))}),[b]),T=e.useCallback((function(e){b("startTour",(function(){o("startTour",e)}))}),[b]),x=e.useCallback((function(e,t){b("trackEvent",(function(){t?o("trackEvent",e,t):o("trackEvent",e)}))}),[b]),N=e.useMemo((function(){return{boot:y,shutdown:C,hardShutdown:k,update:S,hide:E,show:M,showMessages:O,showNewMessages:j,getVisitorId:z,startTour:T,trackEvent:x}}),[y,C,k,S,E,M,O,j,z,T,x]),q=e.useMemo((function(){return d}),[d]);return e.createElement(a.Provider,{value:N},q)},exports.useIntercom=function(){return e.useContext(a)}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");function t(){return(t=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e}).apply(this,arguments)}var n="undefined"==typeof window,o=function(e){if(!n&&window.Intercom){for(var t=arguments.length,o=new Array(t>1?t-1:0),r=1;r<t;r++)o[r-1]=arguments[r];return window.Intercom.apply(null,[e].concat(o))}},r=e.createContext({boot:function(){},shutdown:function(){},hardShutdown:function(){},update:function(){},hide:function(){},show:function(){},showMessages:function(){},showNewMessages:function(){},getVisitorId:function(){return""},startTour:function(){},trackEvent:function(){}}),a=function(e){return t({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)},u=function(e){return{type:e.type,image_url:e.imageUrl}},i=function(e){return function e(t){return Object.keys(t).forEach((function(n){t[n]&&"object"==typeof t[n]?e(t[n]):void 0===t[n]&&delete t[n]})),t}(t({},{custom_launcher_selector:(n=e).customLauncherSelector,alignment:n.alignment,vertical_padding:n.verticalPadding,horizontal_padding:n.horizontalPadding,hide_default_launcher:n.hideDefaultLauncher,session_duration:n.sessionDuration,action_color:n.actionColor,background_color:n.backgroundColor},function(e){var n;return t({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&&u(e.avatar),user_hash:e.userHash,company:e.company&&a(e.company),companies:null==(n=e.companies)?void 0:n.map(a)},e.customAttributes)}(e)));var n};exports.IntercomProvider=function(a){var u=a.appId,c=a.autoBoot,s=void 0!==c&&c,d=a.children,l=a.onHide,f=a.onShow,m=a.onUnreadCountChange,h=a.shouldInitialize,p=void 0===h?!n:h,w=a.apiBase,g=a.initializeDelay,v=function(e,t){if(null==e)return{};var n,o,r={},a=Object.keys(e);for(o=0;o<a.length;o++)t.indexOf(n=a[o])>=0||(r[n]=e[n]);return r}(a,["appId","autoBoot","children","onHide","onShow","onUnreadCountChange","shouldInitialize","apiBase","initializeDelay"]),b=e.useRef(!1),_=e.useRef(!1);Object.keys(v);var y=e.useCallback((function(e){if((window.Intercom||p)&&!b.current){var n=t({app_id:u},w&&{api_base:w},e&&i(e));window.intercomSettings=n,o("boot",n),b.current=!0}}),[w,u,p]);n||!p||_.current||(function(e,t){void 0===t&&(t=0);var n=window,o=n.Intercom;if("function"==typeof o)o("reattach_activator"),o("update",n.intercomSettings);else{var r=document,a=function e(){e.c(arguments)};a.q=[],a.c=function(e){a.q.push(e)},n.Intercom=a;var u=function(){setTimeout((function(){var t=r.createElement("script");t.type="text/javascript",t.async=!0,t.src="https://widget.intercom.io/widget/"+e;var n=r.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n)}),t)};"complete"===document.readyState?u():n.attachEvent?n.attachEvent("onload",u):n.addEventListener("load",u,!1)}}(u,g),l&&o("onHide",l),f&&o("onShow",f),m&&o("onUnreadCountChange",m),s&&y(),_.current=!0);var C=e.useCallback((function(e,t){if((window.Intercom||p)&&b.current)return t()}),[p]),k=e.useCallback((function(){b.current&&(o("shutdown"),b.current=!1)}),[]),I=e.useCallback((function(){b.current&&(o("shutdown"),delete window.Intercom,delete window.intercomSettings,b.current=!1)}),[]),S=e.useCallback((function(){C("update",(function(){var e=(new Date).getTime();o("update",{last_requested_at:e})}))}),[C]),E=e.useCallback((function(e){C("update",(function(){if(e){var n=i(e);window.intercomSettings=t({},window.intercomSettings,n),o("update",n)}else S()}))}),[C,S]),M=e.useCallback((function(){C("hide",(function(){o("hide")}))}),[C]),O=e.useCallback((function(){C("show",(function(){return o("show")}))}),[C]),j=e.useCallback((function(){C("showMessages",(function(){o("showMessages")}))}),[C]),z=e.useCallback((function(e){C("showNewMessage",(function(){e?o("showNewMessage",e):o("showNewMessage")}))}),[C]),T=e.useCallback((function(){return C("getVisitorId",(function(){return o("getVisitorId")}))}),[C]),x=e.useCallback((function(e){C("startTour",(function(){o("startTour",e)}))}),[C]),N=e.useCallback((function(e,t){C("trackEvent",(function(){t?o("trackEvent",e,t):o("trackEvent",e)}))}),[C]),q=e.useMemo((function(){return{boot:y,shutdown:k,hardShutdown:I,update:E,hide:M,show:O,showMessages:j,showNewMessages:z,getVisitorId:T,startTour:x,trackEvent:N}}),[y,k,I,E,M,O,j,z,T,x,N]),A=e.useMemo((function(){return d}),[d]);return e.createElement(r.Provider,{value:q},A)},exports.useIntercom=function(){return e.useContext(r)}; | ||
//# sourceMappingURL=react-use-intercom.cjs.production.min.js.map |
@@ -273,24 +273,32 @@ import { createContext, useRef, useCallback, useMemo, createElement, useContext } from 'react'; | ||
var isBooted = useRef(autoBoot); | ||
var isBooted = useRef(false); | ||
var isInitialized = useRef(false); | ||
if (!isEmptyObject(rest) && process.env.NODE_ENV !== "production") log('error', ['some invalid props were passed to IntercomProvider. ', "Please check following props: " + Object.keys(rest).join(', ') + "."].join('')); | ||
var boot = useCallback(function (props) { | ||
if (!window.Intercom && !shouldInitialize) { | ||
log('warn', 'Intercom instance is not initialized because `shouldInitialize` is set to `false` in `IntercomProvider`'); | ||
return; | ||
} | ||
if (!isSSR && !window.Intercom && shouldInitialize) { | ||
initialize(appId, initializeDelay); // Only add listeners on initialization | ||
if (isBooted.current) return; | ||
var metaData = _extends({ | ||
app_id: appId | ||
}, apiBase && { | ||
api_base: apiBase | ||
}, props && mapIntercomPropsToRawIntercomProps(props)); | ||
window.intercomSettings = metaData; | ||
IntercomAPI('boot', metaData); | ||
isBooted.current = true; | ||
}, [apiBase, appId, shouldInitialize]); | ||
if (!isSSR && shouldInitialize && !isInitialized.current) { | ||
initialize(appId, initializeDelay); // attach listeners | ||
if (onHide) IntercomAPI('onHide', onHide); | ||
if (onShow) IntercomAPI('onShow', onShow); | ||
if (onUnreadCountChange) IntercomAPI('onUnreadCountChange', onUnreadCountChange); | ||
if (autoBoot) { | ||
IntercomAPI('boot', _extends({ | ||
app_id: appId | ||
}, apiBase && { | ||
api_base: apiBase | ||
})); | ||
window.intercomSettings = _extends({ | ||
app_id: appId | ||
}, apiBase && { | ||
api_base: apiBase | ||
}); | ||
} | ||
if (autoBoot) boot(); | ||
isInitialized.current = true; | ||
} | ||
@@ -315,20 +323,2 @@ | ||
}, [shouldInitialize]); | ||
var boot = useCallback(function (props) { | ||
if (!window.Intercom && !shouldInitialize) { | ||
log('warn', 'Intercom instance is not initialized because `shouldInitialize` is set to `false` in `IntercomProvider`'); | ||
return; | ||
} | ||
if (isBooted.current) return; | ||
var metaData = _extends({ | ||
app_id: appId | ||
}, apiBase && { | ||
api_base: apiBase | ||
}, props && mapIntercomPropsToRawIntercomProps(props)); | ||
window.intercomSettings = metaData; | ||
IntercomAPI('boot', metaData); | ||
isBooted.current = true; | ||
}, [apiBase, appId, shouldInitialize]); | ||
var shutdown = useCallback(function () { | ||
@@ -335,0 +325,0 @@ if (!isBooted.current) return; |
@@ -7,3 +7,3 @@ { | ||
"homepage": "https://github.com/devrnt/react-use-intercom#readme", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"license": "MIT", | ||
@@ -81,4 +81,4 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@testing-library/react": "^11.1.1", | ||
"@testing-library/react-hooks": "^5.0.0", | ||
"@testing-library/react": "^12.0.0", | ||
"@testing-library/react-hooks": "^7.0.0", | ||
"@types/jsdom": "^16.2.5", | ||
@@ -88,4 +88,4 @@ "@types/react": "^17.0.0", | ||
"bundlesize": "^0.18.0", | ||
"cypress": "7.2.0", | ||
"dotenv": "^9.0.1", | ||
"cypress": "7.6.0", | ||
"dotenv": "^10.0.0", | ||
"eslint-plugin-simple-import-sort": "^7.0.0", | ||
@@ -92,0 +92,0 @@ "husky": "^6.0.0", |
@@ -19,2 +19,3 @@ <p align="center"><img src="https://raw.githubusercontent.com/devrnt/react-use-intercom/master/assets/logo.png" alt="Logo" height="120px" style="margin-top: 20px;"/></p> | ||
* Safeguard for SSR environments (NextJS, Gatsby) | ||
* Compatible to hook into existing Intercom intance (loaded by [Segment](https://segment.com/)) | ||
@@ -21,0 +22,0 @@ ## Installation |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
290
173341
2089