Skip to content

Move initialisation to effect and register callbacks in boot method #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading