Skip to content

Commit c5be8ee

Browse files
authored
Merge pull request #627 from devrnt/622-implement-new-apis
622 implement new apis
2 parents ebe8f26 + ce4b4ed commit c5be8ee

20 files changed

Lines changed: 785 additions & 388 deletions

File tree

.changeset/smart-mice-occur.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+
Extend API with `onUserEmailSupplied` event and `startSurvey` method

.github/workflows/e2e-testing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
# build playground
3939
# build: pnpm dev:playground
4040
# start playground
41-
start: pnpm dev:playground
41+
start: pnpm dev
4242
command: pnpm e2e
4343
# FIXME: this breaks it
4444
# wait-on: 'http://localhost:5173'

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Place the `IntercomProvider` as high as possible in your application. This will
7979
| onHide | () => void | triggered when the Messenger hides | false | |
8080
| onShow | () => void | triggered when the Messenger shows | false | |
8181
| onUnreadCountChange | (number) => void | triggered when the current number of unread messages changes | false | |
82+
| onUserEmailSupplied | () => void | triggered when a visitor enters their email into the Messenger | false | |
8283
| shouldInitialize | boolean | indicates if the Intercom should be initialized. Can be used in multistaged environment | false | true |
8384
| apiBase | string | If you need to route your Messenger requests through a different endpoint than the default. Generally speaking, this is not needed.<br/> Format: `https://${INTERCOM_APP_ID}.intercom-messenger.com` (See: [https://github.com/devrnt/react-use-intercom/pull/96](https://github.com/devrnt/react-use-intercom/pull/96)) | false | |
8485
| initializeDelay | number | Indicates if the intercom initialization should be delayed, delay is in ms, defaults to 0. See https://github.com/devrnt/react-use-intercom/pull/236 | false | |
@@ -95,13 +96,17 @@ const App = () => {
9596
console.log('Intercom has a new unread message');
9697
setUnreadMessagesCount(amount);
9798
};
99+
const onUserEmailSupplied = () => {
100+
console.log('Visitor has entered email');
101+
};
98102

99103
return (
100104
<IntercomProvider
101105
appId={INTERCOM_APP_ID}
102106
onHide={onHide}
103107
onShow={onShow}
104108
onUnreadCountChange={onUnreadCountChange}
109+
onUserEmailSupplied={onUserEmailSupplied}
105110
autoBoot
106111
>
107112
<p>Hi there, I am a child of the IntercomProvider</p>
@@ -134,6 +139,7 @@ Used to retrieve all methods bundled with Intercom. These are based on the offic
134139
| startTour | (tourId: number) => void | starts a tour based on the `tourId` |
135140
| trackEvent | (event: string, metaData?: object) => void | submits an `event` with optional `metaData`
136141
| showArticle | (articleId: string) => void | opens the Messenger with the specified article by `articleId`
142+
| startSurvey | (surveyId: number) => void | Trigger a survey in the Messenger by `surveyId`
137143

138144
#### Example
139145
```javascript
@@ -162,7 +168,8 @@ const HomePage = () => {
162168
getVisitorId,
163169
startTour,
164170
trackEvent,
165-
showArticle
171+
showArticle,
172+
startSurvey
166173
} = useIntercom();
167174

168175
const bootWithProps = () => boot({ name: 'Russo' });
@@ -177,6 +184,7 @@ const HomePage = () => {
177184
name: 'Russo',
178185
});
179186
const handleShowArticle = () => showArticle(123456);
187+
const handleStartSurvey = () => startSurvey(123456);
180188

181189
return (
182190
<>
@@ -200,6 +208,7 @@ const HomePage = () => {
200208
Track event with metadata
201209
</button>
202210
<button onClick={handleShowArticle}>Open article in Messenger</button>
211+
<button onClick={handleStartSurvey}>Start survey in Messenger</button>
203212
</>
204213
);
205214
};

apps/playground/src/modules/provider/provider.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from 'react';
2-
32
import { IntercomProvider } from 'react-use-intercom';
43

54
const Provider = () => {

apps/playground/src/modules/provider/providerEvents.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
2+
import { IntercomProvider, useIntercom } from 'react-use-intercom';
23
import styled from 'styled-components';
34

4-
import { IntercomProvider, useIntercom } from 'react-use-intercom';
55
import { Button } from '../common';
66

77
const Grid = styled.div`
@@ -25,11 +25,13 @@ const Item = styled.div`
2525
type RawProviderEventsProps = {
2626
onShowText: string;
2727
onHideText: string;
28+
onUserEmailSuppliedText: string;
2829
};
2930

3031
const RawProviderEvents = ({
3132
onShowText,
3233
onHideText,
34+
onUserEmailSuppliedText,
3335
}: RawProviderEventsProps) => {
3436
const { boot, show, hide } = useIntercom();
3537

@@ -38,6 +40,7 @@ const RawProviderEvents = ({
3840
<p>Intercom children</p>
3941
<p data-cy="onShowText">{onShowText}</p>
4042
<p data-cy="onHideText">{onHideText}</p>
43+
<p data-cy="onUserEmailSuppliedText">{onUserEmailSuppliedText}</p>
4144

4245
<Grid>
4346
<Item>
@@ -61,13 +64,26 @@ const RawProviderEvents = ({
6164
const ProviderEvents = () => {
6265
const [onShowText, setOnShowText] = React.useState('default');
6366
const [onHideText, setHideText] = React.useState('default');
67+
const [onUserEmailSuppliedText, setOnUserEmailSupplied] =
68+
React.useState('default');
6469

6570
const onShow = React.useCallback(() => setOnShowText('show was called'), []);
6671
const onHide = React.useCallback(() => setHideText('hide was called'), []);
72+
const onUserEmailSupplied = () =>
73+
setOnUserEmailSupplied('on user email supplied was called');
6774

6875
return (
69-
<IntercomProvider appId="jcabc7e3" onShow={onShow} onHide={onHide}>
70-
<RawProviderEvents onShowText={onShowText} onHideText={onHideText} />
76+
<IntercomProvider
77+
appId="jcabc7e3"
78+
onShow={onShow}
79+
onHide={onHide}
80+
onUserEmailSupplied={onUserEmailSupplied}
81+
>
82+
<RawProviderEvents
83+
onUserEmailSuppliedText={onUserEmailSuppliedText}
84+
onShowText={onShowText}
85+
onHideText={onHideText}
86+
/>
7187
</IntercomProvider>
7288
);
7389
};

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
2+
import { IntercomProvider, useIntercom } from 'react-use-intercom';
23
import styled from 'styled-components';
34

4-
import { IntercomProvider, useIntercom } from 'react-use-intercom';
55
import { Button } from '../common';
66

77
const Grid = styled.div`
@@ -23,6 +23,8 @@ const Item = styled.div`
2323
`;
2424

2525
const RawUseIntercomPage = () => {
26+
const [visitorId, setVisitorId] = React.useState<string | null>(null);
27+
2628
const {
2729
boot,
2830
shutdown,
@@ -35,12 +37,14 @@ const RawUseIntercomPage = () => {
3537
getVisitorId,
3638
trackEvent,
3739
showArticle,
40+
startSurvey,
3841
} = useIntercom();
3942
const handleBoot = React.useCallback(() => boot(), [boot]);
4043

41-
const handleSeededBoot = React.useCallback(() => boot({ name: 'Russo' }), [
42-
boot,
43-
]);
44+
const handleSeededBoot = React.useCallback(
45+
() => boot({ name: 'Russo' }),
46+
[boot],
47+
);
4448

4549
const handleExtendedSeededBoot = React.useCallback(
4650
() =>
@@ -111,9 +115,10 @@ const RawUseIntercomPage = () => {
111115
update({ name: 'ponas' });
112116
}, [update]);
113117

114-
const handleNewMessages = React.useCallback(() => showNewMessage(), [
115-
showNewMessage,
116-
]);
118+
const handleNewMessages = React.useCallback(
119+
() => showNewMessage(),
120+
[showNewMessage],
121+
);
117122

118123
const handleNewMessagesWithContent = React.useCallback(
119124
() => showNewMessage('pre-definded-content'),
@@ -137,7 +142,7 @@ const RawUseIntercomPage = () => {
137142
showArticle(4013997);
138143
}, [showArticle]);
139144

140-
const [visitorId, setVisitorId] = React.useState<string | null>(null);
145+
const handleStartSurvey = () => startSurvey(29938254);
141146

142147
return (
143148
<Grid>
@@ -240,6 +245,7 @@ const RawUseIntercomPage = () => {
240245
data-cy="visitorId"
241246
onClick={handleGetVisitorId}
242247
/>
248+
{visitorId ? <p data-cy="visitorIdValue">{visitorId}</p> : null}
243249
</Item>
244250
<Item>
245251
<p>
@@ -262,8 +268,16 @@ const RawUseIntercomPage = () => {
262268
</p>
263269
<Button label="Open article" onClick={handleShowArticle} />
264270
</Item>
265-
266-
{visitorId && <p data-cy="visitorIdValue">{visitorId}</p>}
271+
<Item>
272+
<p>
273+
start survey with the given <code>surveyId</code>
274+
</p>
275+
<Button
276+
data-cy="start-survey"
277+
label="Start survey"
278+
onClick={handleStartSurvey}
279+
/>
280+
</Item>
267281
</Grid>
268282
);
269283
};

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"private": true,
33
"packageManager": "pnpm@7.26.3",
44
"scripts": {
5-
"dev": "turbo run dev --filter=react-use-intercom --filter=playground --no-cache --continue",
6-
"dev:playground": "turbo run dev --parallel --filter=playground --filter=react-use-intercom",
5+
"dev": "turbo run dev --parallel --filter=playground --filter=react-use-intercom",
76
"dev:examples": "turbo run dev --parallel --filter=*-example --filter=react-use-intercom",
87
"test": "turbo run test",
98
"build": "turbo run build",
@@ -12,6 +11,7 @@
1211
"lint:fix": "turbo run lint:fix",
1312
"clean": "turbo run clean && rm -rf node_modules",
1413
"e2e": "turbo run e2e --filter=react-use-intercom",
14+
"e2e:open": "turbo run e2e:open --filter=react-use-intercom",
1515
"changeset": "changeset",
1616
"release": "turbo run build --filter=react-use-intercom && changeset publish"
1717
},
@@ -24,6 +24,6 @@
2424
"eslint-plugin-prettier": "^4.2.1",
2525
"eslint-plugin-simple-import-sort": "^10.0.0",
2626
"prettier": "^2.8.3",
27-
"turbo": "^1.7.3"
27+
"turbo": "^1.8.3"
2828
}
2929
}

packages/react-use-intercom/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Place the `IntercomProvider` as high as possible in your application. This will
7979
| onHide | () => void | triggered when the Messenger hides | false | |
8080
| onShow | () => void | triggered when the Messenger shows | false | |
8181
| onUnreadCountChange | (number) => void | triggered when the current number of unread messages changes | false | |
82+
| onUserEmailSupplied | () => void | triggered when a visitor enters their email into the Messenger | false | |
8283
| shouldInitialize | boolean | indicates if the Intercom should be initialized. Can be used in multistaged environment | false | true |
8384
| apiBase | string | If you need to route your Messenger requests through a different endpoint than the default. Generally speaking, this is not needed.<br/> Format: `https://${INTERCOM_APP_ID}.intercom-messenger.com` (See: [https://github.com/devrnt/react-use-intercom/pull/96](https://github.com/devrnt/react-use-intercom/pull/96)) | false | |
8485
| initializeDelay | number | Indicates if the intercom initialization should be delayed, delay is in ms, defaults to 0. See https://github.com/devrnt/react-use-intercom/pull/236 | false | |
@@ -95,13 +96,17 @@ const App = () => {
9596
console.log('Intercom has a new unread message');
9697
setUnreadMessagesCount(amount);
9798
};
99+
const onUserEmailSupplied = () => {
100+
console.log('Visitor has entered email');
101+
};
98102

99103
return (
100104
<IntercomProvider
101105
appId={INTERCOM_APP_ID}
102106
onHide={onHide}
103107
onShow={onShow}
104108
onUnreadCountChange={onUnreadCountChange}
109+
onUserEmailSupplied={onUserEmailSupplied}
105110
autoBoot
106111
>
107112
<p>Hi there, I am a child of the IntercomProvider</p>
@@ -134,6 +139,7 @@ Used to retrieve all methods bundled with Intercom. These are based on the offic
134139
| startTour | (tourId: number) => void | starts a tour based on the `tourId` |
135140
| trackEvent | (event: string, metaData?: object) => void | submits an `event` with optional `metaData`
136141
| showArticle | (articleId: string) => void | opens the Messenger with the specified article by `articleId`
142+
| startSurvey | (surveyId: number) => void | Trigger a survey in the Messenger by `surveyId`
137143

138144
#### Example
139145
```javascript
@@ -162,7 +168,8 @@ const HomePage = () => {
162168
getVisitorId,
163169
startTour,
164170
trackEvent,
165-
showArticle
171+
showArticle,
172+
startSurvey
166173
} = useIntercom();
167174

168175
const bootWithProps = () => boot({ name: 'Russo' });
@@ -177,6 +184,7 @@ const HomePage = () => {
177184
name: 'Russo',
178185
});
179186
const handleShowArticle = () => showArticle(123456);
187+
const handleStartSurvey = () => startSurvey(123456);
180188

181189
return (
182190
<>
@@ -200,6 +208,7 @@ const HomePage = () => {
200208
Track event with metadata
201209
</button>
202210
<button onClick={handleShowArticle}>Open article in Messenger</button>
211+
<button onClick={handleStartSurvey}>Start survey in Messenger</button>
203212
</>
204213
);
205214
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/// <reference types="cypress" />
22

33
describe('hide', () => {
4-
it('should show when calling `show`', () => {
4+
it('should hide when calling `hide`', () => {
55
cy.visit('/useIntercom');
66

77
cy.get('[data-cy=boot]').click();
88

99
cy.get('[data-cy=show]').click();
10-
cy.get('.intercom-messenger-frame > iframe').should('be.visible');
10+
cy.get('iframe[name="intercom-messenger-frame"]').should('be.visible');
1111

12-
cy.get('[data-cy=hide]').click({ force: true });
13-
cy.get('.intercom-messenger-frame > iframe').should('not.exist');
12+
cy.get('[data-cy=hide]').click();
13+
cy.get('iframe[name="intercom-messenger-frame"]').should('not.be.visible');
1414
});
1515
});

0 commit comments

Comments
 (0)