@trycourier/react-hooks 1.14.3-internal.7eae37a.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Courier
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,74 @@
1
+ <!-- START doctoc generated TOC please keep comment here to allow auto update -->
2
+ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3
+
4
+ - [Overview](#overview)
5
+ - [Types](#types)
6
+
7
+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
8
+
9
+ <a name="0overviewmd"></a>
10
+
11
+ ### [Overview](#overview)
12
+
13
+ `@trycourier/react-hooks` exist as a separate package so that you can build your own interface using our api and state management without having to install all the dependencies that `@trycourier/react-inbox` or other `react-dom` based packages include.
14
+
15
+ This also enables using this package with `react-native` in a much simpler way.
16
+
17
+ <a name="1typesmd"></a>
18
+
19
+ ### [Types](#types)
20
+
21
+ ```
22
+ const inbox: IInbox & IInboxActions = useInbox();
23
+
24
+ interface ITab {
25
+ filters: {
26
+ isRead?: boolean;
27
+ };
28
+ label: string;
29
+ id: string;
30
+ }
31
+
32
+ interface IMessage {
33
+ unread?: number;
34
+ messageId: string;
35
+ created: number;
36
+ title: string;
37
+ body: string;
38
+ blocks?: Array<IActionBlock | ITextBlock>;
39
+ icon?: string;
40
+ read?: boolean;
41
+ data?: {
42
+ clickAction: string;
43
+ };
44
+ trackingIds?: {
45
+ clickTrackingId: string;
46
+ deliveredTrackingId: string;
47
+ readTrackingId: string;
48
+ unreadTrackingId: string;
49
+ };
50
+ }
51
+
52
+ interface IInboxActions {
53
+ init: (inbox: IInbox) => void;
54
+ toggleInbox: (isOpen?: boolean) => void;
55
+ setView: (view: "messages" | "preferences") => void;
56
+ setCurrentTab: (newTab: ITab) => void;
57
+ fetchMessages: (params?: IFetchMessagesParams) => void;
58
+ getMessageCount: (params?: IGetMessagesParams) => void;
59
+ markMessageRead: (messageId: string, trackingId: string) => Promise<void>;
60
+ markMessageUnread: (messageId: string, trackingId: string) => Promise<void>;
61
+ markAllAsRead: () => void;
62
+ }
63
+
64
+ interface IInbox {
65
+ isOpen?: boolean;
66
+ tabs?: ITab[];
67
+ currentTab?: ITab;
68
+ isLoading?: boolean;
69
+ messages?: Array<IMessage>;
70
+ startCursor?: string;
71
+ unreadMessageCount?: number;
72
+ view?: "messages" | "preferences";
73
+ }
74
+ ```
@@ -0,0 +1,217 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports["default"] = exports.DEFAULT_TABS = void 0;
9
+
10
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
11
+
12
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
+
14
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
15
+
16
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
17
+
18
+ var DEFAULT_TABS = [{
19
+ id: "unread",
20
+ label: "Unread",
21
+ filters: {
22
+ isRead: false
23
+ }
24
+ }, {
25
+ id: "all",
26
+ label: "All Messages",
27
+ filters: {}
28
+ }];
29
+ exports.DEFAULT_TABS = DEFAULT_TABS;
30
+
31
+ var makeMessage = function makeMessage(message) {
32
+ var _message$content, _message$content2, _message$content3, _message$content4, _message$content5;
33
+
34
+ return {
35
+ blocks: message === null || message === void 0 ? void 0 : (_message$content = message.content) === null || _message$content === void 0 ? void 0 : _message$content.blocks,
36
+ body: message === null || message === void 0 ? void 0 : (_message$content2 = message.content) === null || _message$content2 === void 0 ? void 0 : _message$content2.body,
37
+ created: message.created,
38
+ data: message === null || message === void 0 ? void 0 : (_message$content3 = message.content) === null || _message$content3 === void 0 ? void 0 : _message$content3.data,
39
+ messageId: message.messageId,
40
+ read: message === null || message === void 0 ? void 0 : message.read,
41
+ title: message === null || message === void 0 ? void 0 : (_message$content4 = message.content) === null || _message$content4 === void 0 ? void 0 : _message$content4.title,
42
+ trackingIds: message === null || message === void 0 ? void 0 : (_message$content5 = message.content) === null || _message$content5 === void 0 ? void 0 : _message$content5.trackingIds
43
+ };
44
+ };
45
+
46
+ var initialState = {
47
+ isOpen: false,
48
+ messages: [],
49
+ view: "messages",
50
+ unreadMessageCount: 0,
51
+ tabs: DEFAULT_TABS,
52
+ currentTab: DEFAULT_TABS[0]
53
+ };
54
+
55
+ var _default = function _default() {
56
+ var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
57
+ var action = arguments.length > 1 ? arguments[1] : undefined;
58
+
59
+ switch (action.type) {
60
+ case "inbox/INIT":
61
+ {
62
+ return _objectSpread(_objectSpread({}, state), action.payload);
63
+ }
64
+
65
+ case "inbox/SET_VIEW":
66
+ {
67
+ return _objectSpread(_objectSpread({}, state), {}, {
68
+ view: action.payload
69
+ });
70
+ }
71
+
72
+ case "inbox/TOGGLE_INBOX":
73
+ {
74
+ return _objectSpread(_objectSpread({}, state), {}, {
75
+ isOpen: typeof action.payload === "boolean" ? action.payload : !state.isOpen
76
+ });
77
+ }
78
+
79
+ case "inbox/SET_CURRENT_TAB":
80
+ {
81
+ var _state$currentTab, _action$payload;
82
+
83
+ return _objectSpread(_objectSpread({}, state), {}, {
84
+ messages: ((_state$currentTab = state.currentTab) === null || _state$currentTab === void 0 ? void 0 : _state$currentTab.id) !== ((_action$payload = action.payload) === null || _action$payload === void 0 ? void 0 : _action$payload.id) ? [] : state.messages,
85
+ currentTab: action.payload
86
+ });
87
+ }
88
+
89
+ case "inbox/SET_UNREAD_MESSAGE_COUNT/DONE":
90
+ {
91
+ return _objectSpread(_objectSpread({}, state), {}, {
92
+ unreadMessageCount: action.payload
93
+ });
94
+ }
95
+
96
+ case "inbox/FETCH_MESSAGES/PENDING":
97
+ {
98
+ return _objectSpread(_objectSpread({}, state), {}, {
99
+ isLoading: true
100
+ });
101
+ }
102
+
103
+ case "inbox/FETCH_MESSAGES/DONE":
104
+ {
105
+ var _action$payload2, _action$payload2$mess, _action$payload3, _action$payload4, _state$messages;
106
+
107
+ var newMessages = action === null || action === void 0 ? void 0 : (_action$payload2 = action.payload) === null || _action$payload2 === void 0 ? void 0 : (_action$payload2$mess = _action$payload2.messages) === null || _action$payload2$mess === void 0 ? void 0 : _action$payload2$mess.map(makeMessage);
108
+ return _objectSpread(_objectSpread({}, state), {}, {
109
+ isLoading: false,
110
+ startCursor: action === null || action === void 0 ? void 0 : (_action$payload3 = action.payload) === null || _action$payload3 === void 0 ? void 0 : _action$payload3.startCursor,
111
+ messages: action !== null && action !== void 0 && (_action$payload4 = action.payload) !== null && _action$payload4 !== void 0 && _action$payload4.appendMessages ? [].concat((0, _toConsumableArray2["default"])((_state$messages = state === null || state === void 0 ? void 0 : state.messages) !== null && _state$messages !== void 0 ? _state$messages : []), (0, _toConsumableArray2["default"])(newMessages)) : newMessages
112
+ });
113
+ }
114
+
115
+ case "inbox/MARK_MESSAGE_UNREAD":
116
+ {
117
+ var _state$unreadMessageC, _state$messages2;
118
+
119
+ var unreadMessageCount = ((_state$unreadMessageC = state.unreadMessageCount) !== null && _state$unreadMessageC !== void 0 ? _state$unreadMessageC : 0) + 1;
120
+ return _objectSpread(_objectSpread({}, state), {}, {
121
+ messages: state === null || state === void 0 ? void 0 : (_state$messages2 = state.messages) === null || _state$messages2 === void 0 ? void 0 : _state$messages2.map(function (message) {
122
+ if (message.messageId === action.payload.messageId) {
123
+ return _objectSpread(_objectSpread({}, message), {}, {
124
+ read: false
125
+ });
126
+ }
127
+
128
+ return message;
129
+ }),
130
+ unreadMessageCount: unreadMessageCount
131
+ });
132
+ }
133
+
134
+ case "inbox/MARK_MESSAGE_READ":
135
+ {
136
+ var _state$unreadMessageC2, _state$currentTab2, _state$currentTab2$fi, _state$messages4;
137
+
138
+ var _unreadMessageCount = Math.max(((_state$unreadMessageC2 = state.unreadMessageCount) !== null && _state$unreadMessageC2 !== void 0 ? _state$unreadMessageC2 : 0) - 1, 0);
139
+
140
+ if (((_state$currentTab2 = state.currentTab) === null || _state$currentTab2 === void 0 ? void 0 : (_state$currentTab2$fi = _state$currentTab2.filters) === null || _state$currentTab2$fi === void 0 ? void 0 : _state$currentTab2$fi.isRead) === false) {
141
+ var _state$messages3;
142
+
143
+ return _objectSpread(_objectSpread({}, state), {}, {
144
+ messages: state === null || state === void 0 ? void 0 : (_state$messages3 = state.messages) === null || _state$messages3 === void 0 ? void 0 : _state$messages3.filter(function (message) {
145
+ return message.messageId !== action.payload.messageId;
146
+ }),
147
+ unreadMessageCount: _unreadMessageCount
148
+ });
149
+ }
150
+
151
+ return _objectSpread(_objectSpread({}, state), {}, {
152
+ messages: state === null || state === void 0 ? void 0 : (_state$messages4 = state.messages) === null || _state$messages4 === void 0 ? void 0 : _state$messages4.map(function (message) {
153
+ if (message.messageId === action.payload.messageId) {
154
+ return _objectSpread(_objectSpread({}, message), {}, {
155
+ read: true
156
+ });
157
+ }
158
+
159
+ return message;
160
+ }),
161
+ unreadMessageCount: _unreadMessageCount
162
+ });
163
+ }
164
+
165
+ case "inbox/FETCH_MESSAGES/ERROR":
166
+ {
167
+ return _objectSpread(_objectSpread({}, state), {}, {
168
+ isLoading: false
169
+ });
170
+ }
171
+
172
+ case "inbox/NEW_MESSAGE":
173
+ {
174
+ var _state$unreadMessageC3, _action$payload$messa, _action$payload$data;
175
+
176
+ return _objectSpread(_objectSpread({}, state), {}, {
177
+ unreadMessageCount: ((_state$unreadMessageC3 = state === null || state === void 0 ? void 0 : state.unreadMessageCount) !== null && _state$unreadMessageC3 !== void 0 ? _state$unreadMessageC3 : 0) + 1,
178
+ messages: [{
179
+ created: new Date().getTime(),
180
+ messageId: (_action$payload$messa = action.payload.messageId) !== null && _action$payload$messa !== void 0 ? _action$payload$messa : new Date().getTime(),
181
+ title: action.payload.title,
182
+ body: action.payload.body,
183
+ blocks: action.payload.blocks,
184
+ data: action.payload.data,
185
+ trackingIds: (_action$payload$data = action.payload.data) === null || _action$payload$data === void 0 ? void 0 : _action$payload$data.trackingIds
186
+ }].concat((0, _toConsumableArray2["default"])(state.messages || []))
187
+ });
188
+ }
189
+
190
+ case "inbox/MARK_ALL_AS_READ":
191
+ {
192
+ var _state$currentTab3, _state$currentTab3$fi, _state$messages5;
193
+
194
+ var _unreadMessageCount2 = 0;
195
+
196
+ if (((_state$currentTab3 = state.currentTab) === null || _state$currentTab3 === void 0 ? void 0 : (_state$currentTab3$fi = _state$currentTab3.filters) === null || _state$currentTab3$fi === void 0 ? void 0 : _state$currentTab3$fi.isRead) === false) {
197
+ return _objectSpread(_objectSpread({}, state), {}, {
198
+ messages: [],
199
+ unreadMessageCount: _unreadMessageCount2
200
+ });
201
+ }
202
+
203
+ return _objectSpread(_objectSpread({}, state), {}, {
204
+ messages: state === null || state === void 0 ? void 0 : (_state$messages5 = state.messages) === null || _state$messages5 === void 0 ? void 0 : _state$messages5.map(function (message) {
205
+ return _objectSpread(_objectSpread({}, message), {}, {
206
+ read: true
207
+ });
208
+ }),
209
+ unreadMessageCount: _unreadMessageCount2
210
+ });
211
+ }
212
+ }
213
+
214
+ return state;
215
+ };
216
+
217
+ exports["default"] = _default;
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,204 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports["default"] = void 0;
9
+
10
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
+
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+
14
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
15
+
16
+ var _reactProvider = require("@trycourier/react-provider");
17
+
18
+ var _clientGraphql = require("@trycourier/client-graphql");
19
+
20
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
21
+
22
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
23
+
24
+ var useInboxActions = function useInboxActions() {
25
+ var _useCourier = (0, _reactProvider.useCourier)(),
26
+ apiUrl = _useCourier.apiUrl,
27
+ dispatch = _useCourier.dispatch,
28
+ inbox = _useCourier.inbox,
29
+ clientKey = _useCourier.clientKey,
30
+ userId = _useCourier.userId,
31
+ userSignature = _useCourier.userSignature;
32
+
33
+ var courierClient = (0, _clientGraphql.createCourierClient)({
34
+ apiUrl: apiUrl,
35
+ clientKey: clientKey,
36
+ userId: userId,
37
+ userSignature: userSignature
38
+ });
39
+ var events = (0, _clientGraphql.Events)({
40
+ client: courierClient
41
+ });
42
+ var messages = (0, _clientGraphql.Messages)({
43
+ client: courierClient
44
+ });
45
+ return {
46
+ init: function init(payload) {
47
+ dispatch({
48
+ type: "inbox/INIT",
49
+ payload: payload
50
+ });
51
+
52
+ if (payload.isOpen) {
53
+ var meta = {
54
+ from: inbox === null || inbox === void 0 ? void 0 : inbox.from
55
+ };
56
+ dispatch({
57
+ type: "inbox/FETCH_MESSAGES",
58
+ meta: meta,
59
+ payload: function payload() {
60
+ return messages.getMessages(meta);
61
+ }
62
+ });
63
+ }
64
+ },
65
+ toggleInbox: function toggleInbox(isOpen) {
66
+ dispatch({
67
+ type: "inbox/TOGGLE_INBOX",
68
+ payload: isOpen
69
+ });
70
+ },
71
+ setView: function setView(view) {
72
+ dispatch({
73
+ type: "inbox/SET_VIEW",
74
+ payload: view
75
+ });
76
+ },
77
+ setCurrentTab: function setCurrentTab(newTab) {
78
+ dispatch({
79
+ type: "inbox/SET_CURRENT_TAB",
80
+ payload: newTab
81
+ });
82
+
83
+ var meta = _objectSpread({
84
+ from: inbox === null || inbox === void 0 ? void 0 : inbox.from
85
+ }, newTab === null || newTab === void 0 ? void 0 : newTab.filters);
86
+
87
+ dispatch({
88
+ type: "inbox/FETCH_MESSAGES",
89
+ meta: meta,
90
+ payload: function payload() {
91
+ return messages.getMessages(meta);
92
+ }
93
+ });
94
+ },
95
+ fetchMessages: function fetchMessages(_payload) {
96
+ var params = _objectSpread({
97
+ from: inbox === null || inbox === void 0 ? void 0 : inbox.from
98
+ }, _payload === null || _payload === void 0 ? void 0 : _payload.params);
99
+
100
+ dispatch({
101
+ type: "inbox/FETCH_MESSAGES",
102
+ payload: function payload() {
103
+ return messages.getMessages(params, _payload === null || _payload === void 0 ? void 0 : _payload.after);
104
+ },
105
+ meta: _payload
106
+ });
107
+ },
108
+ getMessageCount: function getMessageCount(params) {
109
+ dispatch({
110
+ type: "inbox/SET_UNREAD_MESSAGE_COUNT",
111
+ payload: function payload() {
112
+ return messages.getMessageCount(params);
113
+ }
114
+ });
115
+ },
116
+ markMessageRead: function () {
117
+ var _markMessageRead = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(messageId, trackingId) {
118
+ return _regenerator["default"].wrap(function _callee$(_context) {
119
+ while (1) {
120
+ switch (_context.prev = _context.next) {
121
+ case 0:
122
+ dispatch({
123
+ type: "inbox/MARK_MESSAGE_READ",
124
+ payload: {
125
+ messageId: messageId
126
+ }
127
+ });
128
+ _context.next = 3;
129
+ return events.trackEvent(trackingId);
130
+
131
+ case 3:
132
+ case "end":
133
+ return _context.stop();
134
+ }
135
+ }
136
+ }, _callee);
137
+ }));
138
+
139
+ function markMessageRead(_x, _x2) {
140
+ return _markMessageRead.apply(this, arguments);
141
+ }
142
+
143
+ return markMessageRead;
144
+ }(),
145
+ markMessageUnread: function () {
146
+ var _markMessageUnread = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(messageId, trackingId) {
147
+ return _regenerator["default"].wrap(function _callee2$(_context2) {
148
+ while (1) {
149
+ switch (_context2.prev = _context2.next) {
150
+ case 0:
151
+ dispatch({
152
+ type: "inbox/MARK_MESSAGE_UNREAD",
153
+ payload: {
154
+ messageId: messageId
155
+ }
156
+ });
157
+ _context2.next = 3;
158
+ return events.trackEvent(trackingId);
159
+
160
+ case 3:
161
+ case "end":
162
+ return _context2.stop();
163
+ }
164
+ }
165
+ }, _callee2);
166
+ }));
167
+
168
+ function markMessageUnread(_x3, _x4) {
169
+ return _markMessageUnread.apply(this, arguments);
170
+ }
171
+
172
+ return markMessageUnread;
173
+ }(),
174
+ markAllAsRead: function () {
175
+ var _markAllAsRead = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
176
+ return _regenerator["default"].wrap(function _callee3$(_context3) {
177
+ while (1) {
178
+ switch (_context3.prev = _context3.next) {
179
+ case 0:
180
+ dispatch({
181
+ type: "inbox/MARK_ALL_AS_READ"
182
+ });
183
+ _context3.next = 3;
184
+ return events.trackEventBatch("read");
185
+
186
+ case 3:
187
+ case "end":
188
+ return _context3.stop();
189
+ }
190
+ }
191
+ }, _callee3);
192
+ }));
193
+
194
+ function markAllAsRead() {
195
+ return _markAllAsRead.apply(this, arguments);
196
+ }
197
+
198
+ return markAllAsRead;
199
+ }()
200
+ };
201
+ };
202
+
203
+ var _default = useInboxActions;
204
+ exports["default"] = _default;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports["default"] = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _reactProvider = require("@trycourier/react-provider");
13
+
14
+ var _react = require("react");
15
+
16
+ var _reducer = _interopRequireDefault(require("./reducer"));
17
+
18
+ var _deepExtend = _interopRequireDefault(require("deep-extend"));
19
+
20
+ var _useInboxActions = _interopRequireDefault(require("./use-inbox-actions"));
21
+
22
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
23
+
24
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
25
+
26
+ var useInbox = function useInbox() {
27
+ var _useCourier = (0, _reactProvider.useCourier)(),
28
+ dispatch = _useCourier.dispatch,
29
+ inbox = _useCourier.inbox,
30
+ transport = _useCourier.transport,
31
+ brand = _useCourier.brand;
32
+
33
+ var actions = (0, _useInboxActions["default"])();
34
+
35
+ if (inbox) {
36
+ var _inbox$brand;
37
+
38
+ inbox.brand = (0, _deepExtend["default"])({}, brand !== null && brand !== void 0 ? brand : {}, (_inbox$brand = inbox.brand) !== null && _inbox$brand !== void 0 ? _inbox$brand : {});
39
+ }
40
+
41
+ (0, _react.useEffect)(function () {
42
+ (0, _reactProvider.registerReducer)("inbox", _reducer["default"]);
43
+ }, []);
44
+ (0, _react.useEffect)(function () {
45
+ transport === null || transport === void 0 ? void 0 : transport.listen({
46
+ id: "inbox-listener",
47
+ listener: function listener(courierEvent) {
48
+ if (!dispatch) {
49
+ return;
50
+ }
51
+
52
+ dispatch({
53
+ type: "inbox/NEW_MESSAGE",
54
+ payload: courierEvent === null || courierEvent === void 0 ? void 0 : courierEvent.data
55
+ });
56
+ }
57
+ });
58
+ }, [transport]);
59
+ return _objectSpread(_objectSpread({}, inbox), actions);
60
+ };
61
+
62
+ var _default = useInbox;
63
+ exports["default"] = _default;
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ Object.defineProperty(exports, "useInbox", {
9
+ enumerable: true,
10
+ get: function get() {
11
+ return _useInbox["default"];
12
+ }
13
+ });
14
+
15
+ var _useInbox = _interopRequireDefault(require("./inbox/use-inbox"));
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@trycourier/react-hooks",
3
+ "version": "1.14.3-internal.7eae37a.0+7eae37a",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "typings/index.d.ts",
7
+ "scripts": {
8
+ "babel": "babel src -d dist --extensions \".ts,.tsx\" --ignore \"src/**/__tests__/**\"",
9
+ "build:watch": "yarn babel --watch",
10
+ "build": "rimraf dist && yarn babel",
11
+ "clean": "rimraf dist",
12
+ "test": "jest -c jest.config.js --runInBand --silent",
13
+ "type-check": "tsc --noEmit",
14
+ "readme": "concat-md --toc --decrease-title-levels --dir-name-as-title docs > README.md",
15
+ "types": "tsc --declaration --outDir typings/ --emitDeclarationOnly --declarationMap --allowJs false --checkJs false"
16
+ },
17
+ "license": "ISC",
18
+ "devDependencies": {
19
+ "@types/deep-extend": "^0.4.32",
20
+ "concat-md": "^0.3.5"
21
+ },
22
+ "dependencies": {
23
+ "@trycourier/client-graphql": "^1.14.3-internal.7eae37a.0+7eae37a",
24
+ "@trycourier/react-provider": "^1.14.3-internal.7eae37a.0+7eae37a",
25
+ "deep-extend": "^0.6.0",
26
+ "rimraf": "^3.0.2"
27
+ },
28
+ "peerDependencies": {
29
+ "react": "^17.0.1"
30
+ },
31
+ "files": [
32
+ "dist/",
33
+ "typings/"
34
+ ],
35
+ "exports": {
36
+ ".": "./dist/index.js",
37
+ "./use-inbox": "./dist/inbox/use-inbox.js"
38
+ },
39
+ "gitHead": "7eae37a6b5318d2e321402d98e2efc2c4d60609b"
40
+ }
@@ -0,0 +1,17 @@
1
+ import { IInbox } from "./types";
2
+ export declare const DEFAULT_TABS: ({
3
+ id: string;
4
+ label: string;
5
+ filters: {
6
+ isRead: boolean;
7
+ };
8
+ } | {
9
+ id: string;
10
+ label: string;
11
+ filters: {
12
+ isRead?: undefined;
13
+ };
14
+ })[];
15
+ declare const _default: (state: IInbox | undefined, action: any) => IInbox;
16
+ export default _default;
17
+ //# sourceMappingURL=reducer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reducer.d.ts","sourceRoot":"","sources":["../../src/inbox/reducer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,MAAM,EAAE,MAAM,SAAS,CAAC;AAE3C,eAAO,MAAM,YAAY;;;;;;;;;;;;IAaxB,CAAC;oEAsBqD,MAAM;AAA7D,wBAkKE"}
@@ -0,0 +1,39 @@
1
+ import { Brand, IActionBlock, ITextBlock } from "@trycourier/react-provider";
2
+ export interface ITab {
3
+ filters: {
4
+ isRead?: boolean;
5
+ };
6
+ label: string;
7
+ id: string;
8
+ }
9
+ export interface IMessage {
10
+ unread?: number;
11
+ messageId: string;
12
+ created: number;
13
+ title: string;
14
+ body: string;
15
+ blocks?: Array<IActionBlock | ITextBlock>;
16
+ icon?: string;
17
+ read?: boolean;
18
+ data?: {
19
+ clickAction: string;
20
+ };
21
+ trackingIds?: {
22
+ clickTrackingId: string;
23
+ deliveredTrackingId: string;
24
+ readTrackingId: string;
25
+ unreadTrackingId: string;
26
+ };
27
+ }
28
+ export interface IInbox {
29
+ brand?: Brand;
30
+ isOpen?: boolean;
31
+ tabs?: ITab[];
32
+ currentTab?: ITab;
33
+ isLoading?: boolean;
34
+ messages?: Array<IMessage>;
35
+ startCursor?: string;
36
+ unreadMessageCount?: number;
37
+ view?: "messages" | "preferences";
38
+ }
39
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/inbox/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AAE7E,MAAM,WAAW,IAAI;IACnB,OAAO,EAAE;QACP,MAAM,CAAC,EAAE,OAAO,CAAC;KAClB,CAAC;IACF,KAAK,EAAE,MAAM,CAAC;IACd,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,MAAM,WAAW,QAAQ;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,KAAK,CAAC,YAAY,GAAG,UAAU,CAAC,CAAC;IAC1C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,IAAI,CAAC,EAAE;QACL,WAAW,EAAE,MAAM,CAAC;KACrB,CAAC;IACF,WAAW,CAAC,EAAE;QACZ,eAAe,EAAE,MAAM,CAAC;QACxB,mBAAmB,EAAE,MAAM,CAAC;QAC5B,cAAc,EAAE,MAAM,CAAC;QACvB,gBAAgB,EAAE,MAAM,CAAC;KAC1B,CAAC;CACH;AAED,MAAM,WAAW,MAAM;IACrB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,IAAI,EAAE,CAAC;IACd,UAAU,CAAC,EAAE,IAAI,CAAC;IAClB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,QAAQ,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC3B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,IAAI,CAAC,EAAE,UAAU,GAAG,aAAa,CAAC;CACnC"}
@@ -0,0 +1,20 @@
1
+ import { IInbox, ITab } from "./types";
2
+ import { IGetMessagesParams } from "@trycourier/client-graphql/typings/messages";
3
+ export interface IFetchMessagesParams {
4
+ params?: IGetMessagesParams;
5
+ after?: string;
6
+ }
7
+ interface IInboxActions {
8
+ init: (inbox: IInbox) => void;
9
+ toggleInbox: (isOpen?: boolean) => void;
10
+ setView: (view: "messages" | "preferences") => void;
11
+ setCurrentTab: (newTab: ITab) => void;
12
+ fetchMessages: (params?: IFetchMessagesParams) => void;
13
+ getMessageCount: (params?: IGetMessagesParams) => void;
14
+ markMessageRead: (messageId: string, trackingId: string) => Promise<void>;
15
+ markMessageUnread: (messageId: string, trackingId: string) => Promise<void>;
16
+ markAllAsRead: () => void;
17
+ }
18
+ declare const useInboxActions: () => IInboxActions;
19
+ export default useInboxActions;
20
+ //# sourceMappingURL=use-inbox-actions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-inbox-actions.d.ts","sourceRoot":"","sources":["../../src/inbox/use-inbox-actions.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAQvC,OAAO,EACL,kBAAkB,EAEnB,MAAM,6CAA6C,CAAC;AAErD,MAAM,WAAW,oBAAoB;IACnC,MAAM,CAAC,EAAE,kBAAkB,CAAC;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,UAAU,aAAa;IACrB,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC9B,WAAW,EAAE,CAAC,MAAM,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;IACxC,OAAO,EAAE,CAAC,IAAI,EAAE,UAAU,GAAG,aAAa,KAAK,IAAI,CAAC;IACpD,aAAa,EAAE,CAAC,MAAM,EAAE,IAAI,KAAK,IAAI,CAAC;IACtC,aAAa,EAAE,CAAC,MAAM,CAAC,EAAE,oBAAoB,KAAK,IAAI,CAAC;IACvD,eAAe,EAAE,CAAC,MAAM,CAAC,EAAE,kBAAkB,KAAK,IAAI,CAAC;IACvD,eAAe,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1E,iBAAiB,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5E,aAAa,EAAE,MAAM,IAAI,CAAC;CAC3B;AAED,QAAA,MAAM,eAAe,QAAO,aAqH3B,CAAC;AAEF,eAAe,eAAe,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { IInbox } from "./types";
2
+ declare const useInbox: () => {
3
+ init: (inbox: IInbox) => void;
4
+ toggleInbox: (isOpen?: boolean | undefined) => void;
5
+ setView: (view: "messages" | "preferences") => void;
6
+ setCurrentTab: (newTab: import("./types").ITab) => void;
7
+ fetchMessages: (params?: import("./use-inbox-actions").IFetchMessagesParams | undefined) => void;
8
+ getMessageCount: (params?: import("@trycourier/client-graphql/typings/messages").IGetMessagesParams | undefined) => void;
9
+ markMessageRead: (messageId: string, trackingId: string) => Promise<void>;
10
+ markMessageUnread: (messageId: string, trackingId: string) => Promise<void>;
11
+ markAllAsRead: () => void;
12
+ brand?: import("@trycourier/react-provider").Brand | undefined;
13
+ isOpen?: boolean | undefined;
14
+ tabs?: import("./types").ITab[] | undefined;
15
+ currentTab?: import("./types").ITab | undefined;
16
+ isLoading?: boolean | undefined;
17
+ messages?: import("./types").IMessage[] | undefined;
18
+ startCursor?: string | undefined;
19
+ unreadMessageCount?: number | undefined;
20
+ view?: "messages" | "preferences" | undefined;
21
+ };
22
+ export default useInbox;
23
+ //# sourceMappingURL=use-inbox.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-inbox.d.ts","sourceRoot":"","sources":["../../src/inbox/use-inbox.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEjC,QAAA,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;CAgCb,CAAC;AAEF,eAAe,QAAQ,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { default as useInbox } from "./inbox/use-inbox";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,mBAAmB,CAAC"}