Skip to content

Commit

Permalink
Merge pull request #677 from devrnt/666-calling-shutdown-one-time-los…
Browse files Browse the repository at this point in the history
…e-all-the-intercomcontext-reactivity-until-the-page-is-refreshed

Move initialisation to effect and register callbacks in boot method
  • Loading branch information
devrnt committed Jun 13, 2024
2 parents ba9e197 + a6d7757 commit 118741a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-pears-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-use-intercom': patch
---

Re-attach callbacks in boot method
65 changes: 38 additions & 27 deletions packages/react-use-intercom/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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) {
Expand All @@ -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 }),
Expand All @@ -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)) => {
Expand Down

0 comments on commit 118741a

Please sign in to comment.