Skip to content

Commit b67fcd9

Browse files
authored
Merge pull request #648 from stefanoCrmg/feat/showNews-method
feat: add showNews method
2 parents 5a0fe40 + 4cbd43a commit b67fcd9

5 files changed

Lines changed: 43 additions & 1 deletion

File tree

.changeset/giant-students-kneel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-use-intercom': minor
3+
---
4+
5+
Add "showNews" method

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ Used to retrieve all methods bundled with Intercom. These are based on the offic
148148
| showArticle | (articleId: string) => void | opens the Messenger with the specified article by `articleId`
149149
| startSurvey | (surveyId: number) => void | Trigger a survey in the Messenger by `surveyId`
150150
| showSpace | (spaceName: IntercomSpace) => void | Opens the Messenger with the specified space
151+
| showNews | (newsId: number) => void | Opens the Messenger with the specified news by `newsId`
152+
151153

152154
#### Example
153155
```ts

apps/playground/src/modules/useIntercom/useIntercom.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const RawUseIntercomPage = () => {
3939
showArticle,
4040
startSurvey,
4141
showSpace,
42+
showNews,
4243
} = useIntercom();
4344
const handleBoot = React.useCallback(() => boot(), [boot]);
4445

@@ -149,6 +150,10 @@ const RawUseIntercomPage = () => {
149150

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

153+
const handleShowNews = React.useCallback(() => {
154+
showNews(33910172);
155+
}, [showNews]);
156+
152157
return (
153158
<Grid>
154159
<Item>
@@ -289,6 +294,12 @@ const RawUseIntercomPage = () => {
289294
</p>
290295
<Button label="Open space" onClick={handleShowSpace} />
291296
</Item>
297+
<Item>
298+
<p>
299+
opens the Messenger with the given <code>newsId</code>
300+
</p>
301+
<Button label="Open news" onClick={handleShowNews} />
302+
</Item>
292303
</Grid>
293304
);
294305
};

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ export const IntercomProvider: React.FC<
247247
[ensureIntercom],
248248
);
249249

250+
const showNews = React.useCallback(
251+
(newsId: number) =>
252+
ensureIntercom('showNews', () => {
253+
IntercomAPI('showNews', newsId);
254+
}),
255+
[ensureIntercom],
256+
);
257+
250258
const providerValue = React.useMemo<IntercomContextValues>(() => {
251259
return {
252260
boot,
@@ -264,6 +272,7 @@ export const IntercomProvider: React.FC<
264272
showArticle,
265273
startSurvey,
266274
showSpace,
275+
showNews,
267276
};
268277
}, [
269278
boot,
@@ -281,6 +290,7 @@ export const IntercomProvider: React.FC<
281290
showArticle,
282291
startSurvey,
283292
showSpace,
293+
showNews,
284294
]);
285295

286296
return (

packages/react-use-intercom/src/types.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ export type IntercomMethod =
245245
| 'getVisitorId'
246246
| 'startTour'
247247
| 'showArticle'
248-
| 'showSpace';
248+
| 'showSpace'
249+
| 'showNews';
249250

250251
export type RawIntercomProps = RawMessengerAttributes & RawDataAttributes;
251252

@@ -418,6 +419,19 @@ export type IntercomContextValues = {
418419
* @example showSpace('tasks')
419420
*/
420421
showSpace: (spaceName: IntercomSpace) => void;
422+
/**
423+
*
424+
* Opens the messenger with the specified news
425+
*
426+
* @see {@link https://developers.intercom.com/installing-intercom/docs/intercom-javascript#intercomshownews-newsitemid}
427+
*
428+
* @remarks if a news with the given id doesn't exist, Messenger Home is opened instead
429+
*
430+
* @param newsId The id of the news
431+
*
432+
* @example showNews('tasks')
433+
*/
434+
showNews: (newsId: number) => void;
421435
};
422436

423437
export type IntercomProviderProps = {

0 commit comments

Comments
 (0)