Skip to content
Merged
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
48 changes: 23 additions & 25 deletions src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,26 @@ export const IntercomProvider = ({
].join(''),
);

const [isBooted, setIsBooted] = React.useState(autoBoot);
const memoizedAppId = React.useRef(appId);
const isBooted = React.useRef(autoBoot);

if (!window.Intercom) {
initialize(appId);
initialize(memoizedAppId.current);
// Only add listeners on initialization
if (onHide) IntercomAPI('onHide', onHide);
if (onShow) IntercomAPI('onShow', onShow);
if (onUnreadCountChange)
IntercomAPI('onUnreadCountChange', onUnreadCountChange);

if (autoBoot) {
IntercomAPI('boot', { app_id: appId });
window.intercomSettings = { app_id: appId };
IntercomAPI('boot', { app_id: memoizedAppId.current });
window.intercomSettings = { app_id: memoizedAppId.current };
}
}

const ensureIntercomIsBooted = React.useCallback(
(functionName: string = 'A function', callback: Function) => {
if (!isBooted) {
if (!isBooted.current) {
logger.log(
'warn',
[
Expand All @@ -58,40 +59,37 @@ export const IntercomProvider = ({
}
return callback();
},
[isBooted],
[],
);

const boot = React.useCallback(
(props?: IntercomProps) => {
if (isBooted) return;
const boot = React.useCallback((props?: IntercomProps) => {
if (isBooted.current) return;

const metaData: RawIntercomBootProps = {
app_id: appId,
...(props && mapIntercomPropsToRawIntercomProps(props)),
};
const metaData: RawIntercomBootProps = {
app_id: memoizedAppId.current,
...(props && mapIntercomPropsToRawIntercomProps(props)),
};

window.intercomSettings = metaData;
IntercomAPI('boot', metaData);
setIsBooted(true);
},
[appId, isBooted],
);
window.intercomSettings = metaData;
IntercomAPI('boot', metaData);
isBooted!.current = true;
}, []);

const shutdown = React.useCallback(() => {
if (!isBooted) return;
if (!isBooted.current) return;

IntercomAPI('shutdown');
setIsBooted(false);
}, [isBooted]);
isBooted.current = false;
}, []);

const hardShutdown = React.useCallback(() => {
if (!isBooted) return;
if (!isBooted.current) return;

IntercomAPI('shutdown');
delete window.Intercom;
delete window.intercomSettings;
setIsBooted(false);
}, [isBooted]);
isBooted.current = false;
}, []);

const refresh = React.useCallback(() => {
ensureIntercomIsBooted('update', () => {
Expand Down