Skip to content

Commit

Permalink
Move initialize and boot out of effect
Browse files Browse the repository at this point in the history
This reverts the changes from a6d7757
  • Loading branch information
devrnt committed Feb 8, 2025
1 parent 6986ffe commit 74ad64c
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions packages/react-use-intercom/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ 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 @@ -69,15 +59,26 @@ export const IntercomProvider: React.FC<
return;
}

// Attach the listeners
if (isBooted.current) {
return;
}

// Register 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);
// the callbacks should be re-registered on a reboot
IntercomAPI('onHide', () => {
setIsOpen(false);
onHide?.();
});
IntercomAPI('onShow', () => {
setIsOpen(true);
onShow?.();
});
IntercomAPI('onUserEmailSupplied', onUserEmailSupplied);

if (onUnreadCountChange)
if (onUnreadCountChange) {
IntercomAPI('onUnreadCountChange', onUnreadCountChange);
}

const metaData: RawIntercomBootProps = {
app_id: appId,
Expand All @@ -92,25 +93,23 @@ export const IntercomProvider: React.FC<
[
apiBase,
appId,
onHideWrapper,
onShowWrapper,
onHide,
onShow,
onUnreadCountChange,
onUserEmailSupplied,
shouldInitialize,
],
);

React.useEffect(() => {
if (!isSSR && shouldInitialize && !isInitialized.current) {
initialize(appId, initializeDelay);

if (autoBoot) {
boot(autoBootProps);
}
if (!isSSR && shouldInitialize && !isInitialized.current) {
initialize(appId, initializeDelay);

isInitialized.current = true;
if (autoBoot) {
boot(autoBootProps);
}
}, [appId, autoBoot, autoBootProps, boot, initializeDelay, shouldInitialize]);

isInitialized.current = true;
}

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

0 comments on commit 74ad64c

Please # to comment.