@trycourier/react-hooks 1.48.6-internal.517ceb8.0 → 1.48.6-internal.d175161.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -15,11 +15,107 @@
15
15
 
16
16
  This also enables using this package with `react-native` in a much simpler way.
17
17
 
18
+ #### Elemental Inbox
19
+
20
+ React Hooks exposes two inbox hooks, `useInbox` and `useElementalInbox`. Elemental inbox is a new inbox
21
+ that takes advantage of Courier's content specification, [Elemental](https://www.courier.com/docs/elemental/).
22
+
23
+ Elemental provides a more advanced format for delivered
24
+ notifications. This includes the ability to add customized buttons, images, and markdown formatted text
25
+ to your messages.
26
+
27
+ See [types](#1typesmd) for details on the interface.
28
+
29
+ #### Example Usage
30
+
31
+ ```tsx
32
+ import { CourierProvider } from "@trycourier/react-provider";
33
+ import { useElementalInbox } from "@trycourier/react-hooks";
34
+
35
+ const MyApp = () => {
36
+ /**
37
+ * Auth token for courier provider, can be a token from Courier's auth/issue-token endpoint
38
+ * or a JWT signed with a valid courier api key. Must include scope: "user_id:<user_id_here> inbox:read:messages"
39
+ *
40
+ * For more information on the auth/issue-token endpoint, visit:
41
+ * https://courier.com/docs/reference/auth/intro/
42
+ */
43
+ const authorization = await fetchAuthToken();
44
+
45
+ return (
46
+ <CourierProvider authorization="abc123">
47
+ <MyInbox />
48
+ </CourierProvider>
49
+ );
50
+ };
51
+
52
+ const MyInbox = () => {
53
+ const inbox = useElementalInbox();
54
+
55
+ useEffect(() => {
56
+ inbox.fetchMessages();
57
+ }, []);
58
+
59
+ // Sets message.read to current date
60
+ const handleReadMessage = (message) => () => {
61
+ inbox.markMessageRead(message.messageId);
62
+ };
63
+
64
+ // Removes message.read
65
+ const handleUnreadMessage = (message) => () => {
66
+ inbox.markMessageUnread(message.messageId);
67
+ };
68
+
69
+ // Archived messages are not included in inbox.fetchMessages()
70
+ const handleArchiveMessage = (message) => () => {
71
+ inbox.markMessageArchived(message.messageId);
72
+ };
73
+
74
+ // If the supplied authorization token is short lived, renew the session with a fresh token
75
+ // proactively before the token is set to expire. Here we use 5 minutes assuming our token only
76
+ // lasts 10 minutes
77
+ useEffect(() => {
78
+ const interval = setInterval(async () => {
79
+ const authorization = await fetchAuthToken();
80
+ inbox.renewSession(authorization);
81
+ }, 300000);
82
+
83
+ // Return a cleanup function to tell react how to stop renewal when the component is unmounted.
84
+ return () => clearInterval(interval);
85
+ }, []);
86
+
87
+ return (
88
+ <>
89
+ {inbox.messages.map((message) => {
90
+ return (
91
+ <Message>
92
+ {message.read ? (
93
+ <>
94
+ <button onClick={handleUnreadMessage(message)}>
95
+ Unread Me
96
+ </button>
97
+ <button onClick={handleArchiveMessage(message)}>
98
+ Archive Me
99
+ </button>
100
+ </>
101
+ ) : (
102
+ <button onClick={handleReadMessage(message)}>Read Me</button>
103
+ )}
104
+ </Message>
105
+ );
106
+ })}
107
+ </>
108
+ );
109
+ };
110
+ ```
111
+
18
112
  <a name="1typesmd"></a>
19
113
 
20
114
  ### [Types](#types)
21
115
 
22
- ```
116
+ Standard Inbox (`useInbox`):
117
+
118
+ ```ts
23
119
  const inbox: IInbox & IInboxActions = useInbox();
24
120
 
25
121
  interface ITab {
@@ -74,6 +170,66 @@ interface IInbox {
74
170
  }
75
171
  ```
76
172
 
173
+ Elemental Inbox (`useElementalInbox`):
174
+
175
+ ```ts
176
+ // This interface defines the return value of useElemental Inbox
177
+ interface IElementalInbox {
178
+ lastMessagesFetched?: number;
179
+ brand?: Brand;
180
+ from?: number;
181
+ isLoading?: boolean;
182
+ isOpen?: boolean;
183
+ messages?: Array<IElementalInboxMessage>;
184
+ startCursor?: string;
185
+ unreadMessageCount?: number;
186
+ view?: "messages" | "preferences";
187
+ /** Fetches messages from the server, sets inbox.messages to the received value */
188
+ fetchMessages: (params?: IFetchMessagesParams) => void;
189
+ /** Returns a count of messages that do not have a message.read date */
190
+ getUnreadMessageCount: (params?: IGetInboxMessagesParams) => void;
191
+ init: (inbox: IElementalInbox) => void;
192
+ /** Marks all messages as read by setting message.read to the current ISO 8601 date */
193
+ markAllAsRead: () => void;
194
+ /** Archives the supplied message, archived messages are not returned by fetchMessages */
195
+ markMessageArchived: (messageId: string) => Promise<void>;
196
+ /** Sets message.read to the current ISO 8601 date */
197
+ markMessageRead: (messageId: string) => Promise<void>;
198
+ /** Removes message.read, signalling that the message is no longer read */
199
+ markMessageUnread: (messageId: string) => Promise<void>;
200
+ setView: (view: "messages" | "preferences") => void;
201
+ toggleInbox: (isOpen?: boolean) => void;
202
+
203
+ /**
204
+ * Allows for renewal of sessions authorized with short lived tokens.
205
+ * For example, if the supplied authorization token lasts 10 minutes,
206
+ * this function can be called with a new token every 5 minutes to ensure
207
+ * messages are received in real time with no interruptions.
208
+ */
209
+ renewSession: (authorization: string) => void;
210
+ }
211
+
212
+ interface IInboxMessage {
213
+ created?: string;
214
+ messageId: string;
215
+ preview?: string;
216
+ /** ISO 8601 date the message was read */
217
+ read?: string;
218
+ title?: string;
219
+ }
220
+
221
+ export interface IFetchMessagesParams {
222
+ params?: IGetInboxMessagesParams;
223
+ after?: string;
224
+ }
225
+
226
+ export interface IGetInboxMessagesParams {
227
+ status?: "read" | "unread";
228
+ limit?: number;
229
+ tags?: string[];
230
+ }
231
+ ```
232
+
77
233
  <a name="2eventsmd"></a>
78
234
 
79
235
  ### [Events](#events)
@@ -95,7 +251,7 @@ Some of these events are called automatically.
95
251
  - Delivered events are called automatically inside the Courier Provider when a message has been delivered through the websocket
96
252
  - Click events are triggered using our `click through tracking` links. Click events will also automatically trigger a `read` event.
97
253
 
98
- #### Manually calling events
254
+ #### Manually calling events (`useInbox` Example)
99
255
 
100
256
  You can call events manually by importing the corresponding function from the react hook.
101
257
 
@@ -138,7 +294,69 @@ const MyInbox = () => {
138
294
 
139
295
  return (
140
296
  <Container>
141
- {inbox.messsages.map((message) => {
297
+ {inbox.messages.map((message) => {
298
+ return (
299
+ <Message>
300
+ {message.read ? (
301
+ <>
302
+ <button onClick={handleUnreadMessage(message)}>
303
+ Unread Me
304
+ </button>
305
+ <button onClick={handleArchiveMessage(message)}>
306
+ Archive Me
307
+ </button>
308
+ </>
309
+ ) : (
310
+ <button onClick={handleReadMessage(message)}>Read Me</button>
311
+ )}
312
+ </Message>
313
+ );
314
+ })}
315
+ </Container>
316
+ );
317
+ };
318
+
319
+ const MyApp = () => {
320
+ return (
321
+ <CourierProvider userId="MY_USER_ID" clientKey="MY_CLIENT_KEY">
322
+ <MyInbox />
323
+ </CourierProvider>
324
+ );
325
+ };
326
+ ```
327
+
328
+ #### Manually calling events (`useElementalInbox` Example)
329
+
330
+ You can call events manually by importing the corresponding function from the react hook.
331
+
332
+ For Example:
333
+
334
+ ```js
335
+ import { CourierProvider } from "@trycourier/react-provider";
336
+ import { useElementalInbox } from "@trycourier/react-hooks";
337
+
338
+ const MyInbox = () => {
339
+ const inbox = useElementalInbox();
340
+
341
+ useEffect(() => {
342
+ inbox.fetchMessages();
343
+ }, []);
344
+
345
+ const handleReadMessage = (message) => () => {
346
+ inbox.markMessageRead(message.messageId);
347
+ };
348
+
349
+ const handleUnreadMessage = (message) => () => {
350
+ inbox.markMessageUnread(message.messageId);
351
+ };
352
+
353
+ const handleArchiveMessage = (message) => () => {
354
+ inbox.markMessageArchived(message.messageId);
355
+ };
356
+
357
+ return (
358
+ <Container>
359
+ {inbox.messages.map((message) => {
142
360
  return (
143
361
  <Message>
144
362
  {message.read ? (
@@ -43,7 +43,8 @@ var useElementalInboxActions = function useElementalInboxActions() {
43
43
  dispatch = _useCourier.dispatch,
44
44
  inbox = _useCourier.inbox,
45
45
  userId = _useCourier.userId,
46
- userSignature = _useCourier.userSignature;
46
+ userSignature = _useCourier.userSignature,
47
+ transport = _useCourier.transport;
47
48
 
48
49
  var courierClient = (0, _clientGraphql.createCourierClient)({
49
50
  authorization: authorization,
@@ -192,6 +193,30 @@ var useElementalInboxActions = function useElementalInboxActions() {
192
193
  }
193
194
 
194
195
  return markMessageArchived;
196
+ }(),
197
+ renewSession: function () {
198
+ var _renewSession = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(token) {
199
+ return _regenerator["default"].wrap(function _callee5$(_context5) {
200
+ while (1) {
201
+ switch (_context5.prev = _context5.next) {
202
+ case 0:
203
+ if (transport instanceof _reactProvider.CourierTransport) {
204
+ transport.renewSession(token);
205
+ }
206
+
207
+ case 1:
208
+ case "end":
209
+ return _context5.stop();
210
+ }
211
+ }
212
+ }, _callee5);
213
+ }));
214
+
215
+ function renewSession(_x4) {
216
+ return _renewSession.apply(this, arguments);
217
+ }
218
+
219
+ return renewSession;
195
220
  }()
196
221
  };
197
222
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trycourier/react-hooks",
3
- "version": "1.48.6-internal.517ceb8.0+517ceb8",
3
+ "version": "1.48.6-internal.d175161.0+d175161",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "typings/index.d.ts",
@@ -20,7 +20,7 @@
20
20
  "concat-md": "^0.3.5"
21
21
  },
22
22
  "dependencies": {
23
- "@trycourier/client-graphql": "^1.48.6-internal.517ceb8.0+517ceb8",
23
+ "@trycourier/client-graphql": "^1.48.6-internal.d175161.0+d175161",
24
24
  "deep-extend": "^0.6.0",
25
25
  "rimraf": "^3.0.2"
26
26
  },
@@ -36,5 +36,5 @@
36
36
  ".": "./dist/index.js",
37
37
  "./use-inbox": "./dist/inbox/use-inbox.js"
38
38
  },
39
- "gitHead": "517ceb82177ad9b1fd05a47196c1f8c66a3a7b04"
39
+ "gitHead": "d175161b39549991b55ff7fae9e5512af738ad83"
40
40
  }
@@ -5,15 +5,28 @@ export interface IFetchMessagesParams {
5
5
  after?: string;
6
6
  }
7
7
  interface IInboxActions {
8
+ /** Fetches messages from the server, sets inbox.messages to the received value */
8
9
  fetchMessages: (params?: IFetchMessagesParams) => void;
10
+ /** Returns a count of messages that do not have a message.read date */
9
11
  getUnreadMessageCount: (params?: IGetInboxMessagesParams) => void;
10
12
  init: (inbox: IElementalInbox) => void;
13
+ /** Marks all messages as read by setting message.read to the current ISO 8601 date */
11
14
  markAllAsRead: () => void;
15
+ /** Archives the supplied message, archived messages are not returned by fetchMessages */
12
16
  markMessageArchived: (messageId: string) => Promise<void>;
17
+ /** Sets message.read to the current ISO 8601 date */
13
18
  markMessageRead: (messageId: string) => Promise<void>;
19
+ /** Removes message.read, signalling that the message is no longer read */
14
20
  markMessageUnread: (messageId: string) => Promise<void>;
15
21
  setView: (view: "messages" | "preferences") => void;
16
22
  toggleInbox: (isOpen?: boolean) => void;
23
+ /**
24
+ * Allows for renewal of sessions authorized with short lived tokens.
25
+ * For example, if the supplied authorization token lasts 10 minutes,
26
+ * this function can be called with a new token every 5 minutes to ensure
27
+ * messages are received in real time with no interruptions.
28
+ */
29
+ renewSession: (authorization: string) => void;
17
30
  }
18
31
  declare const useElementalInboxActions: () => IInboxActions;
19
32
  export default useElementalInboxActions;
@@ -1 +1 @@
1
- {"version":3,"file":"use-inbox-actions.d.ts","sourceRoot":"","sources":["../../../src/inbox/elemental-inbox/use-inbox-actions.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C,OAAO,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AASrE,MAAM,WAAW,oBAAoB;IACnC,MAAM,CAAC,EAAE,uBAAuB,CAAC;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,UAAU,aAAa;IACrB,aAAa,EAAE,CAAC,MAAM,CAAC,EAAE,oBAAoB,KAAK,IAAI,CAAC;IACvD,qBAAqB,EAAE,CAAC,MAAM,CAAC,EAAE,uBAAuB,KAAK,IAAI,CAAC;IAClE,IAAI,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAC;IACvC,aAAa,EAAE,MAAM,IAAI,CAAC;IAC1B,mBAAmB,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,OAAO,CAA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