@trycourier/courier-react-native 2.0.0-beta1 → 2.0.0-beta3
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/ios/CourierReactNative-Bridging-Header.h +1 -0
- package/ios/CourierReactNative.xcodeproj/project.pbxproj +5 -3
- package/ios/CourierReactNative.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
- package/ios/CourierReactNative.xcodeproj/project.xcworkspace/xcuserdata/mike.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- package/ios/CourierReactNative.xcodeproj/xcuserdata/mike.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
- package/ios/CourierReactNativeDelegate.h +18 -0
- package/ios/CourierReactNativeDelegate.m +125 -0
- package/ios/CourierReactNativeModule.m +54 -2
- package/ios/CourierReactNativeModule.swift +104 -70
- package/ios/CourierReactNativeViewManager.m +1 -3
- package/ios/CourierReactNativeViewManager.swift +14 -18
- package/lib/commonjs/hooks/useCourier.js +224 -0
- package/lib/commonjs/hooks/useCourier.js.map +1 -0
- package/lib/commonjs/index.js +215 -13
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/models/CourierAuthenticationListener.js +21 -0
- package/lib/commonjs/models/CourierAuthenticationListener.js.map +1 -0
- package/lib/commonjs/models/CourierPushListener.js +15 -0
- package/lib/commonjs/models/CourierPushListener.js.map +1 -0
- package/lib/commonjs/views/CourierInboxView.js +4 -2
- package/lib/commonjs/views/CourierInboxView.js.map +1 -1
- package/lib/module/hooks/useCourier.js +213 -0
- package/lib/module/hooks/useCourier.js.map +1 -0
- package/lib/module/index.js +188 -14
- package/lib/module/index.js.map +1 -1
- package/lib/module/models/CourierAuthenticationListener.js +13 -0
- package/lib/module/models/CourierAuthenticationListener.js.map +1 -0
- package/lib/module/models/CourierPushListener.js +8 -0
- package/lib/module/models/CourierPushListener.js.map +1 -0
- package/lib/module/views/CourierInboxView.js +4 -2
- package/lib/module/views/CourierInboxView.js.map +1 -1
- package/lib/typescript/hooks/useCourier.d.ts +52 -0
- package/lib/typescript/hooks/useCourier.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +96 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/models/CourierAuthenticationListener.d.ts +7 -0
- package/lib/typescript/models/CourierAuthenticationListener.d.ts.map +1 -0
- package/lib/typescript/models/CourierPushListener.d.ts +7 -0
- package/lib/typescript/models/CourierPushListener.d.ts.map +1 -0
- package/lib/typescript/views/CourierInboxView.d.ts +4 -2
- package/lib/typescript/views/CourierInboxView.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useCourier.tsx +291 -0
- package/src/index.tsx +208 -15
- package/src/models/CourierAuthenticationListener.tsx +19 -0
- package/src/models/CourierPushListener.tsx +13 -0
- package/src/views/CourierInboxView.tsx +5 -4
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import { CourierInboxListener } from './models/CourierInboxListener';
|
|
2
|
+
import { CourierPushListener } from './models/CourierPushListener';
|
|
3
|
+
import { CourierAuthenticationListener } from './models/CourierAuthenticationListener';
|
|
2
4
|
import { InboxMessage } from './models/InboxMessage';
|
|
3
5
|
export { CourierInboxView } from './views/CourierInboxView';
|
|
6
|
+
export { CourierProvider, useCourier } from './hooks/useCourier';
|
|
7
|
+
export { CourierInboxListener } from './models/CourierInboxListener';
|
|
8
|
+
export { CourierPushListener } from './models/CourierPushListener';
|
|
9
|
+
export { CourierAuthenticationListener } from './models/CourierAuthenticationListener';
|
|
4
10
|
declare class Courier {
|
|
11
|
+
readonly PUSH_NOTIFICATION_CLICKED = "pushNotificationClicked";
|
|
12
|
+
readonly PUSH_NOTIFICATION_DELIVERED = "pushNotificationDelivered";
|
|
5
13
|
private static _sharedInstance;
|
|
6
14
|
private _isDebugging;
|
|
7
15
|
private debugListener;
|
|
@@ -13,13 +21,63 @@ declare class Courier {
|
|
|
13
21
|
* Defaults to the React __DEV__ mode
|
|
14
22
|
* @example Courier.setIsDebugging(true)
|
|
15
23
|
*/
|
|
16
|
-
setIsDebugging(isDebugging: boolean):
|
|
24
|
+
setIsDebugging(isDebugging: boolean): boolean;
|
|
17
25
|
get isDebugging(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* TODO
|
|
28
|
+
* @param props
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
iOSForegroundPresentationOptions(props: {
|
|
32
|
+
options: ('sound' | 'badge' | 'list' | 'banner')[];
|
|
33
|
+
}): string;
|
|
34
|
+
/**
|
|
35
|
+
* Sets the current Apple Push Notification Service (APNS) token
|
|
36
|
+
* using Courier token management apis
|
|
37
|
+
* @example const apnsToken = await Courier.apnsToken
|
|
38
|
+
*/
|
|
39
|
+
get apnsToken(): string | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Sets the current Firebase Cloud Messaging (FCM) token
|
|
42
|
+
* using Courier token management apis
|
|
43
|
+
* @example const fcmToken = await Courier.fcmToken
|
|
44
|
+
*/
|
|
45
|
+
get fcmToken(): string | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Sets the current Firebase Cloud Messaging (FCM) token
|
|
48
|
+
* using Courier token management apis
|
|
49
|
+
* @example await setFcmToken('asdf...asdf')
|
|
50
|
+
*/
|
|
51
|
+
setFcmToken(props: {
|
|
52
|
+
token: string;
|
|
53
|
+
}): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Gets notification permission status at a system level.
|
|
56
|
+
* @example const permissionStatus = await Courier.getNotificationPermissionStatus()
|
|
57
|
+
*/
|
|
58
|
+
getNotificationPermissionStatus(): Promise<string>;
|
|
59
|
+
/**
|
|
60
|
+
* Requests notification permission status at a system level.
|
|
61
|
+
* Returns the string associated with the permission status.
|
|
62
|
+
* Will return the current status and will not present a popup
|
|
63
|
+
* if the user has already been asked for permission.
|
|
64
|
+
* @example const permissionStatus = await Courier.requestNotificationPermission()
|
|
65
|
+
*/
|
|
66
|
+
requestNotificationPermission(): Promise<string>;
|
|
67
|
+
/**
|
|
68
|
+
* @example
|
|
69
|
+
TODO
|
|
70
|
+
* @returns function that can be used to unsubscribe from registered listeners
|
|
71
|
+
*/
|
|
72
|
+
addPushNotificationListener(props: {
|
|
73
|
+
onPushNotificationClicked?: (push: any) => void;
|
|
74
|
+
onPushNotificationDelivered?: (push: any) => void;
|
|
75
|
+
}): CourierPushListener;
|
|
18
76
|
/**
|
|
19
77
|
* Returns the current user id stored in local native storage
|
|
20
78
|
* @example const userId = await Courier.userId
|
|
21
79
|
*/
|
|
22
|
-
get userId():
|
|
80
|
+
get userId(): string | undefined;
|
|
23
81
|
/**
|
|
24
82
|
* Signs user in and persists signin in between sessions
|
|
25
83
|
* using native level storage apis
|
|
@@ -41,6 +99,28 @@ declare class Courier {
|
|
|
41
99
|
clientKey?: string;
|
|
42
100
|
userId: string;
|
|
43
101
|
}): Promise<void>;
|
|
102
|
+
/**
|
|
103
|
+
* TODO
|
|
104
|
+
* @param props
|
|
105
|
+
* @returns
|
|
106
|
+
*/
|
|
107
|
+
signOut(): Promise<void>;
|
|
108
|
+
/**
|
|
109
|
+
* TODO
|
|
110
|
+
* @param props
|
|
111
|
+
* @returns
|
|
112
|
+
*/
|
|
113
|
+
addAuthenticationListener(props: {
|
|
114
|
+
onUserChanged: (userId?: string) => void;
|
|
115
|
+
}): CourierAuthenticationListener;
|
|
116
|
+
/**
|
|
117
|
+
* TODO
|
|
118
|
+
* @param props
|
|
119
|
+
* @returns
|
|
120
|
+
*/
|
|
121
|
+
removeAuthenticationListener(props: {
|
|
122
|
+
listenerId: string;
|
|
123
|
+
}): string;
|
|
44
124
|
/**
|
|
45
125
|
* TODO
|
|
46
126
|
* @param props
|
|
@@ -87,6 +167,20 @@ declare class Courier {
|
|
|
87
167
|
* @returns
|
|
88
168
|
*/
|
|
89
169
|
refreshInbox(): Promise<void>;
|
|
170
|
+
/**
|
|
171
|
+
* TODO
|
|
172
|
+
* @param props
|
|
173
|
+
* @returns
|
|
174
|
+
*/
|
|
175
|
+
fetchNextPageOfMessages(): Promise<InboxMessage[]>;
|
|
176
|
+
/**
|
|
177
|
+
* TODO
|
|
178
|
+
* @param props
|
|
179
|
+
* @returns
|
|
180
|
+
*/
|
|
181
|
+
setInboxPaginationLimit(props: {
|
|
182
|
+
limit: number;
|
|
183
|
+
}): void;
|
|
90
184
|
}
|
|
91
185
|
export default Courier;
|
|
92
186
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AASA,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AACvF,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAGrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,6BAA6B,EAAE,MAAM,wCAAwC,CAAC;AAuBvF,cAAM,OAAO;IAEX,QAAQ,CAAC,yBAAyB,6BAA6B;IAC/D,QAAQ,CAAC,2BAA2B,+BAA+B;IAEnE,OAAO,CAAC,MAAM,CAAC,eAAe,CAAU;IACxC,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,aAAa,CAAkC;;IAWvD,WAAkB,MAAM,IAAI,OAAO,CAQlC;YAEa,WAAW;IAKzB;;;;OAIG;IACI,cAAc,CAAC,WAAW,EAAE,OAAO,GAAG,OAAO;IAqB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AAU,CAAC,EAAE,MAAM,CAAA;IACnB,aAAa,CAAC,EAAE,mBAAmB,CAAA;IAEnC,MAAM;CAUd"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EmitterSubscription } from "react-native";
|
|
2
|
+
export declare class CourierPushListener {
|
|
3
|
+
onNotificationClickedListener?: EmitterSubscription;
|
|
4
|
+
onNotificationDeliveredListener?: EmitterSubscription;
|
|
5
|
+
remove(): void;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=CourierPushListener.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CourierPushListener.d.ts","sourceRoot":"","sources":["../../../src/models/CourierPushListener.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAEnD,qBAAa,mBAAmB;IAEvB,6BAA6B,CAAC,EAAE,mBAAmB,CAAA;IACnD,+BAA+B,CAAC,EAAE,mBAAmB,CAAA;IAErD,MAAM;CAKd"}
|
|
@@ -4,8 +4,10 @@ import CourierInboxTheme from "src/models/CourierInboxTheme";
|
|
|
4
4
|
import { InboxAction } from "src/models/InboxAction";
|
|
5
5
|
import { InboxMessage } from "src/models/InboxMessage";
|
|
6
6
|
type CourierInboxViewProps = {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
theme?: {
|
|
8
|
+
light?: CourierInboxTheme;
|
|
9
|
+
dark?: CourierInboxTheme;
|
|
10
|
+
};
|
|
9
11
|
onClickInboxMessageAtIndex?: (message: InboxMessage, index: number) => void;
|
|
10
12
|
onClickInboxActionForMessageAtIndex?: (action: InboxAction, message: InboxMessage, index: number) => void;
|
|
11
13
|
onScrollInbox?: (offsetY: number, offsetX: number) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CourierInboxView.d.ts","sourceRoot":"","sources":["../../../src/views/CourierInboxView.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAA+C,SAAS,EAAE,MAAM,cAAc,CAAC;AACtF,OAAO,iBAAiB,MAAM,8BAA8B,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAEvD,KAAK,qBAAqB,GAAG;IAC3B,
|
|
1
|
+
{"version":3,"file":"CourierInboxView.d.ts","sourceRoot":"","sources":["../../../src/views/CourierInboxView.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAA+C,SAAS,EAAE,MAAM,cAAc,CAAC;AACtF,OAAO,iBAAiB,MAAM,8BAA8B,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAEvD,KAAK,qBAAqB,GAAG;IAC3B,KAAK,CAAC,EAAE;QACN,KAAK,CAAC,EAAE,iBAAiB,CAAC;QAC1B,IAAI,CAAC,EAAE,iBAAiB,CAAA;KACzB,CAAC;IACF,0BAA0B,CAAC,EAAE,CAAC,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC5E,mCAAmC,CAAC,EAAE,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAC1G,aAAa,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAC3D,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB,CAAC;AAiBF,eAAO,MAAM,gBAAgB,UAAW,qBAAqB,sBAqD5D,CAAA"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import React, { createContext, ReactNode, useContext, useEffect, useState } from 'react';
|
|
2
|
+
import { InboxMessage } from 'src/models/InboxMessage';
|
|
3
|
+
import Courier, { CourierPushListener, CourierInboxListener, CourierAuthenticationListener } from '..';
|
|
4
|
+
|
|
5
|
+
let authListener: CourierAuthenticationListener | undefined = undefined
|
|
6
|
+
let pushListener: CourierPushListener | undefined = undefined
|
|
7
|
+
let inboxListener: CourierInboxListener | undefined = undefined
|
|
8
|
+
|
|
9
|
+
interface CourierContextType {
|
|
10
|
+
auth?: {
|
|
11
|
+
isLoading: boolean;
|
|
12
|
+
error?: string;
|
|
13
|
+
userId?: string;
|
|
14
|
+
signIn: (props: { accessToken: string, clientKey?: string, userId: string }) => Promise<void>;
|
|
15
|
+
signOut: () => Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
push?: {
|
|
18
|
+
delivered: any;
|
|
19
|
+
clicked: any;
|
|
20
|
+
tokens: {
|
|
21
|
+
apns?: string,
|
|
22
|
+
fcm?: string;
|
|
23
|
+
},
|
|
24
|
+
notificationPermissionStatus?: string;
|
|
25
|
+
requestNotificationPermission: () => Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
inbox?: {
|
|
28
|
+
isLoading: boolean;
|
|
29
|
+
error?: string;
|
|
30
|
+
messages: InboxMessage[];
|
|
31
|
+
unreadMessageCount: number;
|
|
32
|
+
totalMessageCount: number;
|
|
33
|
+
canPaginate: boolean;
|
|
34
|
+
setPaginationLimit: (limit: number) => void;
|
|
35
|
+
fetchNextPageOfMessages: () => Promise<InboxMessage[]>;
|
|
36
|
+
refresh: () => Promise<void>;
|
|
37
|
+
isRefreshing: boolean;
|
|
38
|
+
readAllMessages: () => Promise<void>;
|
|
39
|
+
readMessage: (messageId: string) => Promise<void>;
|
|
40
|
+
unreadMessage: (messageId: string) => Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const CourierContext = createContext<CourierContextType | undefined>(undefined);
|
|
45
|
+
|
|
46
|
+
export const CourierProvider: React.FC<{ listeners: ('auth' | 'push' | 'inbox')[], children: ReactNode }> = ({ listeners, children }) => {
|
|
47
|
+
|
|
48
|
+
// Auth
|
|
49
|
+
const [auth_userId, auth_setUserId] = useState<string | undefined>(undefined);
|
|
50
|
+
const [auth_isLoading, auth_setIsLoading] = useState<boolean>(false);
|
|
51
|
+
const [auth_error, auth_setError] = useState<string | undefined>(undefined);
|
|
52
|
+
|
|
53
|
+
// Push
|
|
54
|
+
const [push_pushNotificationDelivered, inbox_setPushNotificationDelivered] = useState<any>(undefined);
|
|
55
|
+
const [push_pushNotificationClicked, inbox_setPushNotificationClicked] = useState<any>(undefined);
|
|
56
|
+
const [push_apnsToken, push_setApnsToken] = useState<string | undefined>(undefined);
|
|
57
|
+
const [push_fcmToken, push_setFcmToken] = useState<string | undefined>(undefined);
|
|
58
|
+
const [push_notificationPermission, push_setNotificationPermission] = useState<string | undefined>(undefined);
|
|
59
|
+
|
|
60
|
+
// Inbox
|
|
61
|
+
const [inbox_isLoading, inbox_setIsLoading] = useState<boolean>(false);
|
|
62
|
+
const [inbox_isRefreshing, inbox_setIsRefreshing] = useState<boolean>(false);
|
|
63
|
+
const [inbox_error, inbox_setError] = useState<string | undefined>(undefined);
|
|
64
|
+
const [inbox_messages, inbox_setMessages] = useState<InboxMessage[]>([]);
|
|
65
|
+
const [inbox_unreadMessageCount, inbox_setUnreadMessageCount] = useState<number>(0);
|
|
66
|
+
const [inbox_totalMessageCount, inbox_setTotalMessageCount] = useState<number>(0);
|
|
67
|
+
const [inbox_canPaginate, inbox_setCanPaginate] = useState<boolean>(false);
|
|
68
|
+
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
|
|
71
|
+
// Get the initial values
|
|
72
|
+
const userId = Courier.shared.userId;
|
|
73
|
+
auth_setUserId(userId);
|
|
74
|
+
|
|
75
|
+
// Permissions
|
|
76
|
+
syncNotificationPermissions();
|
|
77
|
+
|
|
78
|
+
// Push tokens
|
|
79
|
+
syncTokens();
|
|
80
|
+
|
|
81
|
+
}, [])
|
|
82
|
+
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
|
|
85
|
+
authListener?.remove();
|
|
86
|
+
pushListener?.remove();
|
|
87
|
+
inboxListener?.remove();
|
|
88
|
+
|
|
89
|
+
// Push
|
|
90
|
+
if (listeners.includes('auth')) {
|
|
91
|
+
|
|
92
|
+
authListener = Courier.shared.addAuthenticationListener({
|
|
93
|
+
onUserChanged: (userId) => {
|
|
94
|
+
auth_setUserId(userId);
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Push
|
|
101
|
+
if (listeners.includes('push')) {
|
|
102
|
+
|
|
103
|
+
pushListener = Courier.shared.addPushNotificationListener({
|
|
104
|
+
onPushNotificationDelivered: (push) => inbox_setPushNotificationDelivered(push),
|
|
105
|
+
onPushNotificationClicked: (push) => inbox_setPushNotificationClicked(push)
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Inbox
|
|
111
|
+
if (listeners.includes('inbox')) {
|
|
112
|
+
|
|
113
|
+
inboxListener = Courier.shared.addInboxListener({
|
|
114
|
+
onInitialLoad: () => {
|
|
115
|
+
inbox_setIsLoading(true);
|
|
116
|
+
inbox_setError(undefined);
|
|
117
|
+
},
|
|
118
|
+
onError: (error) => {
|
|
119
|
+
inbox_setIsLoading(false);
|
|
120
|
+
inbox_setError(error);
|
|
121
|
+
},
|
|
122
|
+
onMessagesChanged: (messages, unreadMessageCount, totalMessageCount, canPaginate) => {
|
|
123
|
+
inbox_setIsLoading(false);
|
|
124
|
+
inbox_setError(undefined);
|
|
125
|
+
inbox_setMessages(messages);
|
|
126
|
+
inbox_setUnreadMessageCount(unreadMessageCount);
|
|
127
|
+
inbox_setTotalMessageCount(totalMessageCount);
|
|
128
|
+
inbox_setCanPaginate(canPaginate);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return () => {
|
|
135
|
+
authListener?.remove();
|
|
136
|
+
pushListener?.remove();
|
|
137
|
+
inboxListener?.remove();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
}, [listeners]);
|
|
141
|
+
|
|
142
|
+
const signIn = async (props: { accessToken: string, clientKey?: string, userId: string }): Promise<void> => {
|
|
143
|
+
|
|
144
|
+
auth_setIsLoading(true);
|
|
145
|
+
auth_setError(undefined);
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
await Courier.shared.signIn(props)
|
|
149
|
+
} catch (error) {
|
|
150
|
+
auth_setError(error as string);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
auth_setIsLoading(false);
|
|
154
|
+
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const signOut = async (): Promise<void> => {
|
|
158
|
+
|
|
159
|
+
auth_setIsLoading(true);
|
|
160
|
+
auth_setError(undefined);
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
await Courier.shared.signOut();
|
|
164
|
+
} catch (error) {
|
|
165
|
+
auth_setError(error as string);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
auth_setIsLoading(false);
|
|
169
|
+
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const syncNotificationPermissions = async () => {
|
|
173
|
+
|
|
174
|
+
const status = await Courier.shared.getNotificationPermissionStatus();
|
|
175
|
+
push_setNotificationPermission(status);
|
|
176
|
+
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const syncTokens = () => {
|
|
180
|
+
|
|
181
|
+
// APNS
|
|
182
|
+
const apnsToken = Courier.shared.apnsToken;
|
|
183
|
+
push_setApnsToken(apnsToken);
|
|
184
|
+
|
|
185
|
+
const fcmToken = Courier.shared.fcmToken;
|
|
186
|
+
push_setFcmToken(fcmToken);
|
|
187
|
+
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const requestNotificationPermission = async () => {
|
|
191
|
+
const status = await Courier.shared.requestNotificationPermission();
|
|
192
|
+
push_setNotificationPermission(status);
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const setPaginationLimit = (limit: number) => {
|
|
196
|
+
Courier.shared.setInboxPaginationLimit({ limit });
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const fetchNextPageOfMessages = (): Promise<InboxMessage[]> => {
|
|
200
|
+
return Courier.shared.fetchNextPageOfMessages();
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const refresh = async () => {
|
|
204
|
+
inbox_setIsRefreshing(true);
|
|
205
|
+
await Courier.shared.refreshInbox();
|
|
206
|
+
inbox_setIsRefreshing(false);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const readAllMessages = () => {
|
|
210
|
+
|
|
211
|
+
// Skip if no user is found
|
|
212
|
+
if (!Courier.shared.userId) {
|
|
213
|
+
return Promise.resolve();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Read the messages
|
|
217
|
+
return Courier.shared.readAllInboxMessages();
|
|
218
|
+
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const readMessage = (messageId: string) => {
|
|
222
|
+
return Courier.shared.readMessage({ messageId });
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const unreadMessage = (messageId: string) => {
|
|
226
|
+
return Courier.shared.unreadMessage({ messageId });
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
return (
|
|
230
|
+
<CourierContext.Provider value={{
|
|
231
|
+
auth: {
|
|
232
|
+
isLoading: auth_isLoading,
|
|
233
|
+
error: auth_error,
|
|
234
|
+
userId: auth_userId,
|
|
235
|
+
signIn,
|
|
236
|
+
signOut
|
|
237
|
+
},
|
|
238
|
+
push: {
|
|
239
|
+
delivered: push_pushNotificationDelivered,
|
|
240
|
+
clicked: push_pushNotificationClicked,
|
|
241
|
+
tokens: {
|
|
242
|
+
fcm: push_fcmToken,
|
|
243
|
+
apns: push_apnsToken,
|
|
244
|
+
},
|
|
245
|
+
notificationPermissionStatus: push_notificationPermission,
|
|
246
|
+
requestNotificationPermission,
|
|
247
|
+
},
|
|
248
|
+
inbox: {
|
|
249
|
+
isLoading: inbox_isLoading,
|
|
250
|
+
error: inbox_error,
|
|
251
|
+
messages: inbox_messages,
|
|
252
|
+
unreadMessageCount: inbox_unreadMessageCount,
|
|
253
|
+
totalMessageCount: inbox_totalMessageCount,
|
|
254
|
+
canPaginate: inbox_canPaginate,
|
|
255
|
+
setPaginationLimit,
|
|
256
|
+
fetchNextPageOfMessages,
|
|
257
|
+
refresh,
|
|
258
|
+
isRefreshing: inbox_isRefreshing,
|
|
259
|
+
readAllMessages,
|
|
260
|
+
readMessage,
|
|
261
|
+
unreadMessage,
|
|
262
|
+
}
|
|
263
|
+
}}>
|
|
264
|
+
{children}
|
|
265
|
+
</CourierContext.Provider>
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
interface UseCourierProps {
|
|
271
|
+
inbox?: {
|
|
272
|
+
paginationLimit?: number;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export const useCourier = (props: UseCourierProps = {}): CourierContextType => {
|
|
277
|
+
|
|
278
|
+
const context = useContext(CourierContext);
|
|
279
|
+
|
|
280
|
+
if (!context) {
|
|
281
|
+
throw new Error('useCourier must be used within an CourierProvider');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Set the initial pagination limit if needed
|
|
285
|
+
if (props.inbox?.paginationLimit) {
|
|
286
|
+
context.inbox?.setPaginationLimit(props.inbox?.paginationLimit)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return context;
|
|
290
|
+
|
|
291
|
+
};
|