@trycourier/react-hooks 1.62.3-internal.6997551.0 → 2.0.1-internal.2d2dd97.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.
Files changed (2) hide show
  1. package/README.md +158 -167
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -15,101 +15,6 @@
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
- * you must also include the "inbox:write:events" scope if making a write request or mutation as well.
40
- *
41
- * For more information on the auth/issue-token endpoint, visit:
42
- * https://courier.com/docs/reference/auth/intro/
43
- */
44
- const authorization = await fetchAuthToken();
45
-
46
- return (
47
- <CourierProvider authorization="abc123" userId="MY_USER_ID">
48
- <MyInbox />
49
- </CourierProvider>
50
- );
51
- };
52
-
53
- const MyInbox = () => {
54
- const inbox = useElementalInbox();
55
-
56
- useEffect(() => {
57
- inbox.fetchMessages();
58
- }, []);
59
-
60
- // Sets message.read to current date
61
- const handleReadMessage = (message) => () => {
62
- inbox.markMessageRead(message.messageId);
63
- };
64
-
65
- // Removes message.read
66
- const handleUnreadMessage = (message) => () => {
67
- inbox.markMessageUnread(message.messageId);
68
- };
69
-
70
- // Archived messages are not included in inbox.fetchMessages()
71
- const handleArchiveMessage = (message) => () => {
72
- inbox.markMessageArchived(message.messageId);
73
- };
74
-
75
- // If the supplied authorization token is short lived, renew the session with a fresh token
76
- // proactively before the token is set to expire. Here we use 5 minutes assuming our token only
77
- // lasts 10 minutes
78
- useEffect(() => {
79
- const interval = setInterval(async () => {
80
- const authorization = await fetchAuthToken();
81
- inbox.renewSession(authorization);
82
- }, 300000);
83
-
84
- // Return a cleanup function to tell react how to stop renewal when the component is unmounted.
85
- return () => clearInterval(interval);
86
- }, []);
87
-
88
- return (
89
- <>
90
- {inbox.messages.map((message) => {
91
- return (
92
- <Message>
93
- {message.read ? (
94
- <>
95
- <button onClick={handleUnreadMessage(message)}>
96
- Unread Me
97
- </button>
98
- <button onClick={handleArchiveMessage(message)}>
99
- Archive Me
100
- </button>
101
- </>
102
- ) : (
103
- <button onClick={handleReadMessage(message)}>Read Me</button>
104
- )}
105
- </Message>
106
- );
107
- })}
108
- </>
109
- );
110
- };
111
- ```
112
-
113
18
  <a name="1typesmd"></a>
114
19
 
115
20
  ### [Types](#types)
@@ -119,14 +24,6 @@ Standard Inbox (`useInbox`):
119
24
  ```ts
120
25
  const inbox: IInbox & IInboxActions = useInbox();
121
26
 
122
- interface ITab {
123
- filters: {
124
- isRead?: boolean;
125
- };
126
- id: string;
127
- label: string;
128
- }
129
-
130
27
  interface IMessage {
131
28
  blocks?: Array<IActionBlock | ITextBlock>;
132
29
  body: string;
@@ -155,83 +52,20 @@ interface IInboxActions {
155
52
  markAllAsRead: () => void;
156
53
  markMessageRead: (messageId: string, trackingId: string) => Promise<void>;
157
54
  markMessageUnread: (messageId: string, trackingId: string) => Promise<void>;
158
- setCurrentTab: (newTab: ITab) => void;
159
55
  setView: (view: "messages" | "preferences") => void;
160
56
  toggleInbox: (isOpen?: boolean) => void;
161
57
  }
162
58
 
163
59
  interface IInbox {
164
- currentTab?: ITab;
165
60
  isLoading?: boolean;
166
61
  isOpen?: boolean;
167
62
  messages?: Array<IMessage>;
168
63
  startCursor?: string;
169
- tabs?: ITab[];
170
64
  unreadMessageCount?: number;
171
65
  view?: "messages" | "preferences";
172
66
  }
173
67
  ```
174
68
 
175
- Elemental Inbox (`useElementalInbox`):
176
-
177
- ```ts
178
- // This interface defines the return value of useElemental Inbox
179
- interface IElementalInbox {
180
- brand?: Brand;
181
- from?: number;
182
- isLoading?: boolean;
183
- isOpen?: boolean;
184
- lastMessagesFetched?: number;
185
- messages?: Array<IElementalInboxMessage>;
186
- startCursor?: string;
187
- unreadMessageCount?: number;
188
- view?: "messages" | "preferences";
189
- /** Fetches messages from the server, sets inbox.messages to the received value */
190
- fetchMessages: (params?: IFetchMessagesParams) => void;
191
- /** Returns a count of messages that do not have a message.read date */
192
- getUnreadMessageCount: (params?: IGetInboxMessagesParams) => void;
193
- init: (inbox: IElementalInbox) => void;
194
- /** Marks all messages as read by setting message.read to the current ISO 8601 date */
195
- markAllAsRead: () => void;
196
- /** Archives the supplied message, archived messages are not returned by fetchMessages */
197
- markMessageArchived: (messageId: string) => Promise<void>;
198
- /** Sets message.read to the current ISO 8601 date */
199
- markMessageRead: (messageId: string) => Promise<void>;
200
- /** Removes message.read, signalling that the message is no longer read */
201
- markMessageUnread: (messageId: string) => Promise<void>;
202
- setView: (view: "messages" | "preferences") => void;
203
- toggleInbox: (isOpen?: boolean) => void;
204
-
205
- /**
206
- * Allows for renewal of sessions authorized with short lived tokens.
207
- * For example, if the supplied authorization token lasts 10 minutes,
208
- * this function can be called with a new token every 5 minutes to ensure
209
- * messages are received in real time with no interruptions.
210
- */
211
- renewSession: (authorization: string) => void;
212
- }
213
-
214
- interface IInboxMessage {
215
- created?: string;
216
- messageId: string;
217
- preview?: string;
218
- /** ISO 8601 date the message was read */
219
- read?: string;
220
- title?: string;
221
- }
222
-
223
- export interface IFetchMessagesParams {
224
- params?: IGetInboxMessagesParams;
225
- after?: string;
226
- }
227
-
228
- export interface IGetInboxMessagesParams {
229
- status?: "read" | "unread";
230
- limit?: number;
231
- tags?: string[];
232
- }
233
- ```
234
-
235
69
  <a name="2eventsmd"></a>
236
70
 
237
71
  ### [Events](#events)
@@ -242,7 +76,7 @@ Inbox supports a few different events that can be triggered on the client side.
242
76
 
243
77
  These events are:
244
78
 
245
- - Delivered
79
+ - Opened
246
80
  - Read
247
81
  - Unread
248
82
  - Click
@@ -327,6 +161,68 @@ const MyApp = () => {
327
161
  };
328
162
  ```
329
163
 
164
+ <a name="3use-elemental-inboxmd"></a>
165
+
166
+ > useElementalInbox is in **BETA**
167
+
168
+ ```ts
169
+ interface IInboxMessage {
170
+ created?: string;
171
+ messageId: string;
172
+ preview?: string;
173
+ /** ISO 8601 date the message was read */
174
+ read?: string;
175
+ title?: string;
176
+ }
177
+
178
+ export interface IFetchMessagesParams {
179
+ params?: IGetInboxMessagesParams;
180
+ after?: string;
181
+ }
182
+
183
+ export interface IGetInboxMessagesParams {
184
+ status?: "read" | "unread";
185
+ limit?: number;
186
+ tags?: string[];
187
+ }
188
+
189
+ // This interface defines the return value of useElemental Inbox
190
+ interface IElementalInbox {
191
+ brand?: Brand;
192
+ from?: number;
193
+ isLoading?: boolean;
194
+ isOpen?: boolean;
195
+ lastMessagesFetched?: number;
196
+ messages?: Array<IElementalInboxMessage>;
197
+ startCursor?: string;
198
+ unreadMessageCount?: number;
199
+ view?: "messages" | "preferences";
200
+ /** Fetches messages from the server, sets inbox.messages to the received value */
201
+ fetchMessages: (params?: IFetchMessagesParams) => void;
202
+ /** Returns a count of messages that do not have a message.read date */
203
+ getUnreadMessageCount: (params?: IGetInboxMessagesParams) => void;
204
+ init: (inbox: IElementalInbox) => void;
205
+ /** Marks all messages as read by setting message.read to the current ISO 8601 date */
206
+ markAllAsRead: () => void;
207
+ /** Archives the supplied message, archived messages are not returned by fetchMessages */
208
+ markMessageArchived: (messageId: string) => Promise<void>;
209
+ /** Sets message.read to the current ISO 8601 date */
210
+ markMessageRead: (messageId: string) => Promise<void>;
211
+ /** Removes message.read, signalling that the message is no longer read */
212
+ markMessageUnread: (messageId: string) => Promise<void>;
213
+ setView: (view: "messages" | "preferences") => void;
214
+ toggleInbox: (isOpen?: boolean) => void;
215
+
216
+ /**
217
+ * Allows for renewal of sessions authorized with short lived tokens.
218
+ * For example, if the supplied authorization token lasts 10 minutes,
219
+ * this function can be called with a new token every 5 minutes to ensure
220
+ * messages are received in real time with no interruptions.
221
+ */
222
+ renewSession: (authorization: string) => void;
223
+ }
224
+ ```
225
+
330
226
  #### Manually calling events (`useElementalInbox` Example)
331
227
 
332
228
  You can call events manually by importing the corresponding function from the react hook.
@@ -388,3 +284,98 @@ const MyApp = () => {
388
284
  );
389
285
  };
390
286
  ```
287
+
288
+ #### Elemental Inbox
289
+
290
+ React Hooks exposes two inbox hooks, `useInbox` and `useElementalInbox`. Elemental inbox is a new inbox
291
+ that takes advantage of Courier's content specification, [Elemental](https://www.courier.com/docs/elemental/).
292
+
293
+ Elemental provides a more advanced format for delivered
294
+ notifications. This includes the ability to add customized buttons, images, and markdown formatted text
295
+ to your messages.
296
+
297
+ See [types](#1typesmd) for details on the interface.
298
+
299
+ #### Example Usage
300
+
301
+ ```tsx
302
+ import { CourierProvider } from "@trycourier/react-provider";
303
+ import { useElementalInbox } from "@trycourier/react-hooks";
304
+
305
+ const MyApp = () => {
306
+ /**
307
+ * Auth token for courier provider, can be a token from Courier's auth/issue-token endpoint
308
+ * or a JWT signed with a valid courier api key. Must include scope: "user_id:<user_id_here> inbox:read:messages",
309
+ * you must also include the "inbox:write:events" scope if making a write request or mutation as well.
310
+ *
311
+ * For more information on the auth/issue-token endpoint, visit:
312
+ * https://courier.com/docs/reference/auth/intro/
313
+ */
314
+ const authorization = await fetchAuthToken();
315
+
316
+ return (
317
+ <CourierProvider authorization="abc123" userId="MY_USER_ID">
318
+ <MyInbox />
319
+ </CourierProvider>
320
+ );
321
+ };
322
+
323
+ const MyInbox = () => {
324
+ const inbox = useElementalInbox();
325
+
326
+ useEffect(() => {
327
+ inbox.fetchMessages();
328
+ }, []);
329
+
330
+ // Sets message.read to current date
331
+ const handleReadMessage = (message) => () => {
332
+ inbox.markMessageRead(message.messageId);
333
+ };
334
+
335
+ // Removes message.read
336
+ const handleUnreadMessage = (message) => () => {
337
+ inbox.markMessageUnread(message.messageId);
338
+ };
339
+
340
+ // Archived messages are not included in inbox.fetchMessages()
341
+ const handleArchiveMessage = (message) => () => {
342
+ inbox.markMessageArchived(message.messageId);
343
+ };
344
+
345
+ // If the supplied authorization token is short lived, renew the session with a fresh token
346
+ // proactively before the token is set to expire. Here we use 5 minutes assuming our token only
347
+ // lasts 10 minutes
348
+ useEffect(() => {
349
+ const interval = setInterval(async () => {
350
+ const authorization = await fetchAuthToken();
351
+ inbox.renewSession(authorization);
352
+ }, 300000);
353
+
354
+ // Return a cleanup function to tell react how to stop renewal when the component is unmounted.
355
+ return () => clearInterval(interval);
356
+ }, []);
357
+
358
+ return (
359
+ <>
360
+ {inbox.messages.map((message) => {
361
+ return (
362
+ <Message>
363
+ {message.read ? (
364
+ <>
365
+ <button onClick={handleUnreadMessage(message)}>
366
+ Unread Me
367
+ </button>
368
+ <button onClick={handleArchiveMessage(message)}>
369
+ Archive Me
370
+ </button>
371
+ </>
372
+ ) : (
373
+ <button onClick={handleReadMessage(message)}>Read Me</button>
374
+ )}
375
+ </Message>
376
+ );
377
+ })}
378
+ </>
379
+ );
380
+ };
381
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trycourier/react-hooks",
3
- "version": "1.62.3-internal.6997551.0+6997551",
3
+ "version": "2.0.1-internal.2d2dd97.0+2d2dd97",
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.62.3-internal.6997551.0+6997551",
23
+ "@trycourier/client-graphql": "^2.0.1-internal.2d2dd97.0+2d2dd97",
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": "69975515ba610266714a3ee576ee0f31ba19ed13"
39
+ "gitHead": "2d2dd9794f242e01a9fd4e158dff931a7fe67904"
40
40
  }