Skip to content

Commit 118741a

Browse files
authored
Merge pull request #677 from devrnt/666-calling-shutdown-one-time-lose-all-the-intercomcontext-reactivity-until-the-page-is-refreshed
Move initialisation to effect and register callbacks in boot method
2 parents ba9e197 + a6d7757 commit 118741a

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

.changeset/serious-pears-confess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-use-intercom': patch
3+
---
4+
5+
Re-attach callbacks in boot method

packages/react-use-intercom/src/provider.tsx

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const IntercomProvider: React.FC<
3232
}) => {
3333
const isBooted = React.useRef(false);
3434
const isInitialized = React.useRef(false);
35+
const [isOpen, setIsOpen] = React.useState(false);
3536

3637
// Allow data-x attributes, see https://github.com/devrnt/react-use-intercom/issues/478
3738
const invalidPropKeys = Object.keys(rest).filter(
@@ -48,6 +49,16 @@ export const IntercomProvider: React.FC<
4849
);
4950
}
5051

52+
const onHideWrapper = React.useCallback(() => {
53+
setIsOpen(false);
54+
if (onHide) onHide();
55+
}, [onHide, setIsOpen]);
56+
57+
const onShowWrapper = React.useCallback(() => {
58+
setIsOpen(true);
59+
if (onShow) onShow();
60+
}, [onShow, setIsOpen]);
61+
5162
const boot = React.useCallback(
5263
(props?: IntercomProps) => {
5364
if (!window.Intercom && !shouldInitialize) {
@@ -58,6 +69,16 @@ export const IntercomProvider: React.FC<
5869
return;
5970
}
6071

72+
// Attach the listeners
73+
// This is done in the booth method because after shutting down
74+
// the callbacks should be re-registered
75+
IntercomAPI('onHide', onHideWrapper);
76+
IntercomAPI('onShow', onShowWrapper);
77+
IntercomAPI('onUserEmailSupplied', onUserEmailSupplied);
78+
79+
if (onUnreadCountChange)
80+
IntercomAPI('onUnreadCountChange', onUnreadCountChange);
81+
6182
const metaData: RawIntercomBootProps = {
6283
app_id: appId,
6384
...(apiBase && { api_base: apiBase }),
@@ -68,38 +89,28 @@ export const IntercomProvider: React.FC<
6889
IntercomAPI('boot', metaData);
6990
isBooted.current = true;
7091
},
71-
[apiBase, appId, shouldInitialize],
92+
[
93+
apiBase,
94+
appId,
95+
onHideWrapper,
96+
onShowWrapper,
97+
onUnreadCountChange,
98+
onUserEmailSupplied,
99+
shouldInitialize,
100+
],
72101
);
73102

74-
const [isOpen, setIsOpen] = React.useState(false);
103+
React.useEffect(() => {
104+
if (!isSSR && shouldInitialize && !isInitialized.current) {
105+
initialize(appId, initializeDelay);
75106

76-
const onHideWrapper = React.useCallback(() => {
77-
setIsOpen(false);
78-
if (onHide) onHide();
79-
}, [onHide, setIsOpen]);
80-
81-
const onShowWrapper = React.useCallback(() => {
82-
setIsOpen(true);
83-
if (onShow) onShow();
84-
}, [onShow, setIsOpen]);
85-
86-
if (!isSSR && shouldInitialize && !isInitialized.current) {
87-
initialize(appId, initializeDelay);
88-
89-
// attach listeners
90-
IntercomAPI('onHide', onHideWrapper);
91-
IntercomAPI('onShow', onShowWrapper);
92-
IntercomAPI('onUserEmailSupplied', onUserEmailSupplied);
93-
94-
if (onUnreadCountChange)
95-
IntercomAPI('onUnreadCountChange', onUnreadCountChange);
107+
if (autoBoot) {
108+
boot(autoBootProps);
109+
}
96110

97-
if (autoBoot) {
98-
boot(autoBootProps);
111+
isInitialized.current = true;
99112
}
100-
101-
isInitialized.current = true;
102-
}
113+
}, [appId, autoBoot, autoBootProps, boot, initializeDelay, shouldInitialize]);
103114

104115
const ensureIntercom = React.useCallback(
105116
(functionName: string, callback: (() => void) | (() => string)) => {

0 commit comments

Comments
 (0)