Skip to content

Commit be14a38

Browse files
authored
Merge pull request #636 from nurcinozer/feat/showSpace-method
feat: add showSpace method
2 parents 8bf5feb + e30828d commit be14a38

6 files changed

Lines changed: 52 additions & 3 deletions

File tree

.changeset/tame-chairs-build.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 "showSpace" method

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Used to retrieve all methods bundled with Intercom. These are based on the offic
140140
| trackEvent | (event: string, metaData?: object) => void | submits an `event` with optional `metaData`
141141
| showArticle | (articleId: string) => void | opens the Messenger with the specified article by `articleId`
142142
| startSurvey | (surveyId: number) => void | Trigger a survey in the Messenger by `surveyId`
143+
| showSpace | (spaceName: IntercomSpace) => void | Opens the Messenger with the specified space
143144

144145
#### Example
145146
```javascript
@@ -169,7 +170,8 @@ const HomePage = () => {
169170
startTour,
170171
trackEvent,
171172
showArticle,
172-
startSurvey
173+
startSurvey,
174+
showSpace
173175
} = useIntercom();
174176

175177
const bootWithProps = () => boot({ name: 'Russo' });
@@ -185,6 +187,7 @@ const HomePage = () => {
185187
});
186188
const handleShowArticle = () => showArticle(123456);
187189
const handleStartSurvey = () => startSurvey(123456);
190+
const handleShowSpace = () => showSpace('tasks');
188191

189192
return (
190193
<>
@@ -209,6 +212,7 @@ const HomePage = () => {
209212
</button>
210213
<button onClick={handleShowArticle}>Open article in Messenger</button>
211214
<button onClick={handleStartSurvey}>Start survey in Messenger</button>
215+
<button onClick={handleShowSpace}>Open space in Messenger</button>
212216
</>
213217
);
214218
};

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

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

@@ -142,6 +143,10 @@ const RawUseIntercomPage = () => {
142143
showArticle(4013997);
143144
}, [showArticle]);
144145

146+
const handleShowSpace = React.useCallback(() => {
147+
showSpace('messages');
148+
}, [showSpace]);
149+
145150
const handleStartSurvey = () => startSurvey(29938254);
146151

147152
return (
@@ -278,6 +283,12 @@ const RawUseIntercomPage = () => {
278283
onClick={handleStartSurvey}
279284
/>
280285
</Item>
286+
<Item>
287+
<p>
288+
opens the Messenger with the given <code>space</code>
289+
</p>
290+
<Button label="Open space" onClick={handleShowSpace} />
291+
</Item>
281292
</Grid>
282293
);
283294
};

packages/react-use-intercom/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Used to retrieve all methods bundled with Intercom. These are based on the offic
140140
| trackEvent | (event: string, metaData?: object) => void | submits an `event` with optional `metaData`
141141
| showArticle | (articleId: string) => void | opens the Messenger with the specified article by `articleId`
142142
| startSurvey | (surveyId: number) => void | Trigger a survey in the Messenger by `surveyId`
143+
| showSpace | (spaceName: IntercomSpace) => void | Opens the Messenger with the specified space
143144

144145
#### Example
145146
```javascript
@@ -169,7 +170,8 @@ const HomePage = () => {
169170
startTour,
170171
trackEvent,
171172
showArticle,
172-
startSurvey
173+
startSurvey,
174+
showSpace
173175
} = useIntercom();
174176

175177
const bootWithProps = () => boot({ name: 'Russo' });
@@ -185,6 +187,7 @@ const HomePage = () => {
185187
});
186188
const handleShowArticle = () => showArticle(123456);
187189
const handleStartSurvey = () => startSurvey(123456);
190+
const handleShowSpace = () => showSpace('tasks');
188191

189192
return (
190193
<>
@@ -209,6 +212,7 @@ const HomePage = () => {
209212
</button>
210213
<button onClick={handleShowArticle}>Open article in Messenger</button>
211214
<button onClick={handleStartSurvey}>Start survey in Messenger</button>
215+
<button onClick={handleShowSpace}>Open space in Messenger</button>
212216
</>
213217
);
214218
};

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
IntercomContextValues,
1010
IntercomProps,
1111
IntercomProviderProps,
12+
IntercomSpace,
1213
RawIntercomBootProps,
1314
} from './types';
1415
import { isSSR } from './utils';
@@ -229,6 +230,14 @@ export const IntercomProvider: React.FC<
229230
[ensureIntercom],
230231
);
231232

233+
const showSpace = React.useCallback(
234+
(spaceName: IntercomSpace) =>
235+
ensureIntercom('showSpace', () => {
236+
IntercomAPI('showSpace', spaceName);
237+
}),
238+
[ensureIntercom],
239+
);
240+
232241
const startSurvey = React.useCallback(
233242
(surveyId: number) => {
234243
ensureIntercom('startSurvey', () => {
@@ -254,6 +263,7 @@ export const IntercomProvider: React.FC<
254263
trackEvent,
255264
showArticle,
256265
startSurvey,
266+
showSpace,
257267
};
258268
}, [
259269
boot,
@@ -270,6 +280,7 @@ export const IntercomProvider: React.FC<
270280
trackEvent,
271281
showArticle,
272282
startSurvey,
283+
showSpace,
273284
]);
274285

275286
return (

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ export type IntercomMethod =
244244
| 'trackEvent'
245245
| 'getVisitorId'
246246
| 'startTour'
247-
| 'showArticle';
247+
| 'showArticle'
248+
| 'showSpace';
248249

249250
export type RawIntercomProps = RawMessengerAttributes & RawDataAttributes;
250251

@@ -262,6 +263,8 @@ export type IntercomBootProps = {
262263

263264
export type LogLevel = 'info' | 'error' | 'warn';
264265

266+
export type IntercomSpace = 'home' | 'messages' | 'help' | 'news' | 'tasks';
267+
265268
export type IntercomContextValues = {
266269
/**
267270
* If you'd like to control when Intercom is loaded, you can use the 'boot' method.
@@ -404,6 +407,17 @@ export type IntercomContextValues = {
404407
* @param surveyId The id of the survey
405408
*/
406409
startSurvey: (surveyId: number) => void;
410+
/**
411+
*
412+
* @see {@link https://developers.intercom.com/installing-intercom/docs/intercom-javascript#intercomshowspace-spacename}
413+
*
414+
* @remarks if a space with the given name doesn't exist, you will see a type error
415+
*
416+
* @param spaceName The name of the space
417+
*
418+
* @example showSpace('tasks')
419+
*/
420+
showSpace: (spaceName: IntercomSpace) => void;
407421
};
408422

409423
export type IntercomProviderProps = {

0 commit comments

Comments
 (0)