diff --git a/.changeset/serious-pears-confess.md b/.changeset/serious-pears-confess.md new file mode 100644 index 00000000..3c5287b5 --- /dev/null +++ b/.changeset/serious-pears-confess.md @@ -0,0 +1,5 @@ +--- +'react-use-intercom': patch +--- + +Re-attach callbacks in boot method diff --git a/packages/react-use-intercom/src/provider.tsx b/packages/react-use-intercom/src/provider.tsx index 83d8076a..6145ef5e 100644 --- a/packages/react-use-intercom/src/provider.tsx +++ b/packages/react-use-intercom/src/provider.tsx @@ -32,6 +32,7 @@ export const IntercomProvider: React.FC< }) => { const isBooted = React.useRef(false); const isInitialized = React.useRef(false); + const [isOpen, setIsOpen] = React.useState(false); // Allow data-x attributes, see https://github.com/devrnt/react-use-intercom/issues/478 const invalidPropKeys = Object.keys(rest).filter( @@ -48,6 +49,16 @@ export const IntercomProvider: React.FC< ); } + const onHideWrapper = React.useCallback(() => { + setIsOpen(false); + if (onHide) onHide(); + }, [onHide, setIsOpen]); + + const onShowWrapper = React.useCallback(() => { + setIsOpen(true); + if (onShow) onShow(); + }, [onShow, setIsOpen]); + const boot = React.useCallback( (props?: IntercomProps) => { if (!window.Intercom && !shouldInitialize) { @@ -58,6 +69,16 @@ export const IntercomProvider: React.FC< return; } + // Attach the listeners + // This is done in the booth method because after shutting down + // the callbacks should be re-registered + IntercomAPI('onHide', onHideWrapper); + IntercomAPI('onShow', onShowWrapper); + IntercomAPI('onUserEmailSupplied', onUserEmailSupplied); + + if (onUnreadCountChange) + IntercomAPI('onUnreadCountChange', onUnreadCountChange); + const metaData: RawIntercomBootProps = { app_id: appId, ...(apiBase && { api_base: apiBase }), @@ -68,38 +89,28 @@ export const IntercomProvider: React.FC< IntercomAPI('boot', metaData); isBooted.current = true; }, - [apiBase, appId, shouldInitialize], + [ + apiBase, + appId, + onHideWrapper, + onShowWrapper, + onUnreadCountChange, + onUserEmailSupplied, + shouldInitialize, + ], ); - const [isOpen, setIsOpen] = React.useState(false); + React.useEffect(() => { + if (!isSSR && shouldInitialize && !isInitialized.current) { + initialize(appId, initializeDelay); - const onHideWrapper = React.useCallback(() => { - setIsOpen(false); - if (onHide) onHide(); - }, [onHide, setIsOpen]); - - const onShowWrapper = React.useCallback(() => { - setIsOpen(true); - if (onShow) onShow(); - }, [onShow, setIsOpen]); - - if (!isSSR && shouldInitialize && !isInitialized.current) { - initialize(appId, initializeDelay); - - // attach listeners - IntercomAPI('onHide', onHideWrapper); - IntercomAPI('onShow', onShowWrapper); - IntercomAPI('onUserEmailSupplied', onUserEmailSupplied); - - if (onUnreadCountChange) - IntercomAPI('onUnreadCountChange', onUnreadCountChange); + if (autoBoot) { + boot(autoBootProps); + } - if (autoBoot) { - boot(autoBootProps); + isInitialized.current = true; } - - isInitialized.current = true; - } + }, [appId, autoBoot, autoBootProps, boot, initializeDelay, shouldInitialize]); const ensureIntercom = React.useCallback( (functionName: string, callback: (() => void) | (() => string)) => {