Skip to content
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

feat: add showNews method #648

Merged
merged 2 commits into from
Dec 18, 2023
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/giant-students-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-use-intercom': minor
---

Add "showNews" method
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ Used to retrieve all methods bundled with Intercom. These are based on the offic
| showArticle | (articleId: string) => void | opens the Messenger with the specified article by `articleId`
| startSurvey | (surveyId: number) => void | Trigger a survey in the Messenger by `surveyId`
| showSpace | (spaceName: IntercomSpace) => void | Opens the Messenger with the specified space
| showNews | (newsId: number) => void | Opens the Messenger with the specified news by `newsId`


#### Example
```ts
Expand Down
11 changes: 11 additions & 0 deletions 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,
showNews,
} = useIntercom();
const handleBoot = React.useCallback(() => boot(), [boot]);

Expand Down Expand Up @@ -149,6 +150,10 @@ const RawUseIntercomPage = () => {

const handleStartSurvey = () => startSurvey(29938254);

const handleShowNews = React.useCallback(() => {
showNews(33910172);
}, [showNews]);

return (
<Grid>
<Item>
Expand Down Expand Up @@ -289,6 +294,12 @@ const RawUseIntercomPage = () => {
</p>
<Button label="Open space" onClick={handleShowSpace} />
</Item>
<Item>
<p>
opens the Messenger with the given <code>newsId</code>
</p>
<Button label="Open news" onClick={handleShowNews} />
</Item>
</Grid>
);
};
Expand Down
10 changes: 10 additions & 0 deletions packages/react-use-intercom/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ export const IntercomProvider: React.FC<
[ensureIntercom],
);

const showNews = React.useCallback(
(newsId: number) =>
ensureIntercom('showNews', () => {
IntercomAPI('showNews', newsId);
}),
[ensureIntercom],
);

const providerValue = React.useMemo<IntercomContextValues>(() => {
return {
boot,
Expand All @@ -264,6 +272,7 @@ export const IntercomProvider: React.FC<
showArticle,
startSurvey,
showSpace,
showNews,
};
}, [
boot,
Expand All @@ -281,6 +290,7 @@ export const IntercomProvider: React.FC<
showArticle,
startSurvey,
showSpace,
showNews,
]);

return (
Expand Down
16 changes: 15 additions & 1 deletion packages/react-use-intercom/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ export type IntercomMethod =
| 'getVisitorId'
| 'startTour'
| 'showArticle'
| 'showSpace';
| 'showSpace'
| 'showNews';

export type RawIntercomProps = RawMessengerAttributes & RawDataAttributes;

Expand Down Expand Up @@ -418,6 +419,19 @@ export type IntercomContextValues = {
* @example showSpace('tasks')
*/
showSpace: (spaceName: IntercomSpace) => void;
/**
*
* Opens the messenger with the specified news
*
* @see {@link https://developers.intercom.com/installing-intercom/docs/intercom-javascript#intercomshownews-newsitemid}
*
* @remarks if a news with the given id doesn't exist, Messenger Home is opened instead
*
* @param newsId The id of the news
*
* @example showNews('tasks')
*/
showNews: (newsId: number) => void;
};

export type IntercomProviderProps = {
Expand Down
Loading