Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
devrnt committed Jun 14, 2023
1 parent e13a07c commit 2fd239a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
9 changes: 8 additions & 1 deletion apps/playground/src/modules/useIntercom/useIntercom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const RawUseIntercomPage = () => {
showArticle,
startSurvey,
showSpace,
unreadCount,
} = useIntercom();
const handleBoot = React.useCallback(() => boot(), [boot]);

Expand Down Expand Up @@ -289,13 +290,19 @@ const RawUseIntercomPage = () => {
</p>
<Button label="Open space" onClick={handleShowSpace} />
</Item>
<Item>
<p>Unread messages count: {unreadCount}</p>
</Item>
</Grid>
);
};

const UseIntercomPage = () => {
return (
<IntercomProvider appId="jcabc7e3">
<IntercomProvider
appId="jcabc7e3"
onUnreadCountChange={(count) => alert(`another one ${count}`)}
>
<RawUseIntercomPage />
</IntercomProvider>
);
Expand Down
15 changes: 12 additions & 3 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 [unreadCount, setUnReadCount] = React.useState(0);

// Allow data-x attributes, see https://github.com/devrnt/react-use-intercom/issues/478
const invalidPropKeys = Object.keys(rest).filter(
Expand Down Expand Up @@ -83,16 +84,22 @@ export const IntercomProvider: React.FC<
if (onShow) onShow();
}, [onShow, setIsOpen]);

const onUnReadCountChangeWrapper = React.useCallback(
(unreadCount: number) => {
setUnReadCount(unreadCount);
if (onUnreadCountChange) onUnreadCountChange(unreadCount);
},
[onUnreadCountChange],
);

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

// attach listeners
IntercomAPI('onHide', onHideWrapper);
IntercomAPI('onShow', onShowWrapper);
IntercomAPI('onUserEmailSupplied', onUserEmailSupplied);

if (onUnreadCountChange)
IntercomAPI('onUnreadCountChange', onUnreadCountChange);
IntercomAPI('onUnreadCountChange', onUnReadCountChangeWrapper);

if (autoBoot) {
boot(autoBootProps);
Expand Down Expand Up @@ -264,6 +271,7 @@ export const IntercomProvider: React.FC<
showArticle,
startSurvey,
showSpace,
unreadCount,
};
}, [
boot,
Expand All @@ -281,6 +289,7 @@ export const IntercomProvider: React.FC<
showArticle,
startSurvey,
showSpace,
unreadCount,
]);

return (
Expand Down
5 changes: 5 additions & 0 deletions packages/react-use-intercom/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,11 @@ export type IntercomContextValues = {
* @example showSpace('tasks')
*/
showSpace: (spaceName: IntercomSpace) => void;
/**
*
* @remark The unread messages count. Updated whenever the current number of unread messages changes.
*/
unreadCount: number;
};

export type IntercomProviderProps = {
Expand Down

0 comments on commit 2fd239a

Please sign in to comment.