Skip to content

Commit

Permalink
Merge pull request #671 from bananazigzags/main
Browse files Browse the repository at this point in the history
expose showTicket and showConversation
  • Loading branch information
devrnt committed Apr 16, 2024
2 parents f4746ad + 55e4ddf commit a11c11f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-pots-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-use-intercom': minor
---

Exposes showTicket and showConversation methods in useIntercom hook
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ Used to retrieve all methods bundled with Intercom. These are based on the offic
| 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`
| showTicket | (ticketId: number) => void | Opens the Messenger with the specified ticket by `ticketId`
| showConversation | (conversationId: number) => void | Opens the Messenger with the specified conversation by `conversationId`


#### Example
Expand Down Expand Up @@ -182,7 +184,9 @@ const HomePage = () => {
trackEvent,
showArticle,
startSurvey,
showSpace
showSpace,
showTicket,
showConversation
} = useIntercom();

const bootWithProps = () => boot({ name: 'Russo' });
Expand All @@ -200,6 +204,8 @@ const HomePage = () => {
const handleShowArticle = () => showArticle(123456);
const handleStartSurvey = () => startSurvey(123456);
const handleShowSpace = () => showSpace('tasks');
const handleShowTicket = () => showTicket(123);
const handleShowConversation = () => showConversation(123);

return (
<>
Expand All @@ -226,6 +232,8 @@ const HomePage = () => {
<button onClick={handleShowArticle}>Open article in Messenger</button>
<button onClick={handleStartSurvey}>Start survey in Messenger</button>
<button onClick={handleShowSpace}>Open space in Messenger</button>
<button onClick={handleShowTicket}>Open ticket in Messenger</button>
<button onClick={handleShowConversation}>Open conversation in Messenger</button>
</>
);
};
Expand Down
10 changes: 9 additions & 1 deletion packages/react-use-intercom/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,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
| showTicket | (ticketId: number) => void | Opens the Messenger with the specified ticket by `ticketId`
| showConversation | (conversationId: number) => void | Opens the Messenger with the specified conversation by `conversationId`

#### Example
```ts
Expand Down Expand Up @@ -180,7 +182,9 @@ const HomePage = () => {
trackEvent,
showArticle,
startSurvey,
showSpace
showSpace,
showTicket,
showConversation
} = useIntercom();

const bootWithProps = () => boot({ name: 'Russo' });
Expand All @@ -198,6 +202,8 @@ const HomePage = () => {
const handleShowArticle = () => showArticle(123456);
const handleStartSurvey = () => startSurvey(123456);
const handleShowSpace = () => showSpace('tasks');
const handleShowTicket = () => showTicket(123);
const handleShowConversation = () => showConversation(123);

return (
<>
Expand All @@ -224,6 +230,8 @@ const HomePage = () => {
<button onClick={handleShowArticle}>Open article in Messenger</button>
<button onClick={handleStartSurvey}>Start survey in Messenger</button>
<button onClick={handleShowSpace}>Open space in Messenger</button>
<button onClick={handleShowTicket}>Open ticket in Messenger</button>
<button onClick={handleShowConversation}>Open conversation in Messenger</button>
</>
);
};
Expand Down
20 changes: 20 additions & 0 deletions packages/react-use-intercom/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,22 @@ export const IntercomProvider: React.FC<
[ensureIntercom],
);

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

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

const providerValue = React.useMemo<IntercomContextValues>(() => {
return {
boot,
Expand All @@ -283,6 +299,8 @@ export const IntercomProvider: React.FC<
startSurvey,
showSpace,
showNews,
showTicket,
showConversation,
};
}, [
boot,
Expand All @@ -302,6 +320,8 @@ export const IntercomProvider: React.FC<
startSurvey,
showSpace,
showNews,
showTicket,
showConversation,
]);

return (
Expand Down
19 changes: 18 additions & 1 deletion packages/react-use-intercom/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ export type IntercomMethod =
| 'startChecklist'
| 'showArticle'
| 'showSpace'
| 'showNews';
| 'showNews'
| 'showTicket'
| 'showConversation';

export type RawIntercomProps = RawMessengerAttributes & RawDataAttributes;

Expand Down Expand Up @@ -446,6 +448,21 @@ export type IntercomContextValues = {
* @example showNews(123)
*/
showNews: (newsId: number) => void;
/**
* If you would like to trigger a ticket in the Messenger, you can use the showTicket method.
*
* The ticket will be shown within the Messenger, and clicking the Messenger back button will return to the previous context.
*
* If the Messenger is closed when the method is called, it will be opened first and then the ticket will be shown.
* @see {@link https://developers.intercom.com/installing-intercom/web/methods/#intercomshowticket-ticketid}
*/
showTicket: (ticketId: number) => void;
/**
* You can show a conversation programatically in the Messenger by calling showConversation method
*
* @see {@link https://developers.intercom.com/installing-intercom/web/methods/#intercomshowconversation-conversationid}
*/
showConversation: (conversationId: number) => void;
};

export type IntercomProviderProps = {
Expand Down

0 comments on commit a11c11f

Please sign in to comment.