@zean-chat/react-native 0.1.10 → 0.1.12
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 +22 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +130 -64
- package/dist/interfaces.d.ts +6 -0
- package/dist/models/sdk.store.d.ts +5 -0
- package/dist/models/sdk.utils.d.ts +8 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,11 +19,32 @@ import { ZeanChat, useZean } from "@zean-chat/react-native";
|
|
|
19
19
|
<ZeanChat appKey="app_your_app_key" user={user ? { id: user.id, name: user.name, email: user.email } : undefined} locale="fr" />;
|
|
20
20
|
|
|
21
21
|
// Anywhere. The chat opens in a modal over the app.
|
|
22
|
-
const { open, close, toggle, unreadCount, identify, signOut, setAttributes } = useZean();
|
|
22
|
+
const { open, close, toggle, unreadCount, identify, signOut, setAttributes, setPushToken } = useZean();
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
Passing `user` signs the person in; `undefined` signs them out. `locale` (`fr`, `en`) and `colorScheme` (`light`, `dark`) default to the phone's. `maxPhotos` sets the photos per message, 1 to 6; 4 by default. The chat keeps the newest 100 messages in your app's storage: it shows them at once and downloads only new ones. To choose the storage, pass `storage`: AsyncStorage, an MMKV instance (any version) or anything with `getItem` and `setItem`. Full guide: [install the chat in React Native and Expo](https://zean.chat/docs/install-react-native), [React Native reference](https://zean.chat/docs/reference-react-native).
|
|
26
26
|
|
|
27
|
+
## Push notifications
|
|
28
|
+
|
|
29
|
+
Zean never sends a push itself: your server does, from the `message.member` webhook.
|
|
30
|
+
|
|
31
|
+
1. In App settings > Developers, set your webhook address.
|
|
32
|
+
2. Give the chat the phone's push token: `useZean().setPushToken(token)`. It is given again after a sign-out by itself.
|
|
33
|
+
3. On `message.member`, your server sends `push.title` and `push.body` to `push.tokens`, with `push.data` as the notification's data.
|
|
34
|
+
4. Open the chat on tap, from a cold start too:
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import { openFromNotification, useZean } from "@zean-chat/react-native";
|
|
38
|
+
|
|
39
|
+
Notifications.addNotificationResponseReceivedListener(response => openFromNotification(response.notification.request.content.data));
|
|
40
|
+
|
|
41
|
+
// The app icon's badge.
|
|
42
|
+
const { unreadCount } = useZean();
|
|
43
|
+
Notifications.setBadgeCountAsync(unreadCount);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`isZeanNotification(data)` tells a Zean push from your own. Full guide: [push notifications](https://zean.chat/docs/push-notifications).
|
|
47
|
+
|
|
27
48
|
## Why Zean
|
|
28
49
|
|
|
29
50
|
- Free plan to start. Paid plans in FCFA, payable by Mobile Money.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { ZeanChat } from './components/ZeanChat';
|
|
2
2
|
export { useZean } from './hooks/use-zean';
|
|
3
|
+
export { openFromNotification } from './models/sdk.store';
|
|
4
|
+
export { isZeanNotification } from './models/sdk.utils';
|
|
3
5
|
export type { ZeanAttributeValue, ZeanAttributes, ZeanChatProps, ZeanColorScheme, ZeanControls, ZeanKeyValueStorage, ZeanLocale, ZeanMMKV, ZeanStorage, ZeanUser } from './interfaces';
|
package/dist/index.js
CHANGED
|
@@ -21,6 +21,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
ZeanChat: () => ZeanChat,
|
|
24
|
+
isZeanNotification: () => isZeanNotification,
|
|
25
|
+
openFromNotification: () => openFromNotification,
|
|
24
26
|
useZean: () => useZean
|
|
25
27
|
});
|
|
26
28
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -65,8 +67,10 @@ var RESERVED_CONTACT_ATTRIBUTES = [
|
|
|
65
67
|
];
|
|
66
68
|
var MISSED_REPLY_DELAY_MS = 60 * 6e4;
|
|
67
69
|
var MAX_MESSAGE_LENGTH = 5e3;
|
|
70
|
+
var TEST_DEVICE_MAX_AGE_MS = 24 * 60 * 60 * 1e3;
|
|
68
71
|
|
|
69
72
|
// ../shared/src/constants/widget.ts
|
|
73
|
+
var EXPO_GO_BUNDLE_ID = "host.exp.exponent";
|
|
70
74
|
var DEFAULT_WIDGET_TEXTS = {
|
|
71
75
|
fr: {
|
|
72
76
|
header: "Bonjour ! Comment pouvons-nous vous aider ?",
|
|
@@ -164,6 +168,15 @@ function languageTag(value) {
|
|
|
164
168
|
}).join("-");
|
|
165
169
|
}
|
|
166
170
|
|
|
171
|
+
// ../shared/src/utils/domains.ts
|
|
172
|
+
function isLocalHost(host) {
|
|
173
|
+
const hostname = host.toLowerCase().replace(/:\d+$/, "");
|
|
174
|
+
return hostname === "localhost" || hostname === "127.0.0.1";
|
|
175
|
+
}
|
|
176
|
+
function isExpoGo(bundle) {
|
|
177
|
+
return bundle.id.toLowerCase() === EXPO_GO_BUNDLE_ID;
|
|
178
|
+
}
|
|
179
|
+
|
|
167
180
|
// ../shared/src/utils/widget.ts
|
|
168
181
|
var WEEKDAYS = { Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6, Sun: 7 };
|
|
169
182
|
function isWithinBusinessHours(hours, at) {
|
|
@@ -257,6 +270,8 @@ function createChatStore(backend, options) {
|
|
|
257
270
|
let identified = false;
|
|
258
271
|
let identifiedKnown = false;
|
|
259
272
|
let watchers = [];
|
|
273
|
+
let pushToken2;
|
|
274
|
+
let pushSent = null;
|
|
260
275
|
let destroyed = false;
|
|
261
276
|
let state = derive();
|
|
262
277
|
const emit2 = () => {
|
|
@@ -266,6 +281,7 @@ function createChatStore(backend, options) {
|
|
|
266
281
|
};
|
|
267
282
|
const args = () => ({ appKey: options.appKey, deviceSecret, host: options.host, bundle: options.bundle });
|
|
268
283
|
const deviceLanguage = options.deviceLanguage ?? options.languages[0];
|
|
284
|
+
const devLoad = options.dev === true || options.host !== void 0 && isLocalHost(options.host) || options.bundle !== void 0 && isExpoGo(options.bundle);
|
|
269
285
|
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
270
286
|
const device = () => ({
|
|
271
287
|
locale: state.locale,
|
|
@@ -278,6 +294,7 @@ function createChatStore(backend, options) {
|
|
|
278
294
|
const useSecret = (secret) => {
|
|
279
295
|
deviceSecret = secret;
|
|
280
296
|
for (const resolve of deviceWaiters.splice(0)) resolve();
|
|
297
|
+
sendPushToken();
|
|
281
298
|
};
|
|
282
299
|
const deviceKnown = () => deviceSecret || destroyed ? Promise.resolve() : new Promise((resolve) => deviceWaiters.push(resolve));
|
|
283
300
|
const starting = (async () => {
|
|
@@ -385,7 +402,7 @@ function createChatStore(backend, options) {
|
|
|
385
402
|
emit2();
|
|
386
403
|
if ("report" in result && result.report && !reported) {
|
|
387
404
|
reported = true;
|
|
388
|
-
backend.call("reportInstall", args()).catch(() => {
|
|
405
|
+
backend.call("reportInstall", { ...args(), ...options.dev ? { dev: true } : {} }).catch(() => {
|
|
389
406
|
});
|
|
390
407
|
}
|
|
391
408
|
startOnce();
|
|
@@ -407,11 +424,22 @@ function createChatStore(backend, options) {
|
|
|
407
424
|
return { ok: false, code: codeOf(error) };
|
|
408
425
|
}
|
|
409
426
|
}
|
|
427
|
+
function sendPushToken() {
|
|
428
|
+
if (pushToken2 === void 0 || !deviceSecret || destroyed) return;
|
|
429
|
+
if (pushSent?.deviceSecret === deviceSecret && pushSent.token === pushToken2) return;
|
|
430
|
+
const sent = { deviceSecret, token: pushToken2 };
|
|
431
|
+
pushSent = sent;
|
|
432
|
+
backend.call("setPushToken", { ...args(), token: pushToken2 }).catch((error) => {
|
|
433
|
+
if (pushSent === sent) pushSent = null;
|
|
434
|
+
if (codeOf(error) === "invalid_push_token") console.warn(`${APP_NAME}: the push token was refused: it is too long.`);
|
|
435
|
+
});
|
|
436
|
+
}
|
|
410
437
|
function startOnce() {
|
|
411
438
|
if (started || state.status === "loading" || state.status === "hidden") return;
|
|
412
439
|
started = true;
|
|
413
|
-
if (state.known
|
|
414
|
-
|
|
440
|
+
if (state.known || devLoad)
|
|
441
|
+
backend.call("start", { ...args(), device: device(), ...options.dev ? { dev: true } : {} }).catch(() => {
|
|
442
|
+
});
|
|
415
443
|
for (const entry of outbox) if (entry.error === null) deliver(entry);
|
|
416
444
|
}
|
|
417
445
|
function handleTyping(result) {
|
|
@@ -857,6 +885,10 @@ function createChatStore(backend, options) {
|
|
|
857
885
|
emit2();
|
|
858
886
|
watchAll();
|
|
859
887
|
},
|
|
888
|
+
setPushToken(token) {
|
|
889
|
+
pushToken2 = token?.trim() || null;
|
|
890
|
+
sendPushToken();
|
|
891
|
+
},
|
|
860
892
|
async joinWithPass(pass) {
|
|
861
893
|
await starting;
|
|
862
894
|
await deviceKnown();
|
|
@@ -6276,66 +6308,6 @@ function backendUrlFor(appKey) {
|
|
|
6276
6308
|
return isStagingKey(appKey) ? STAGING_BACKEND_URL : PRODUCTION_BACKEND_URL;
|
|
6277
6309
|
}
|
|
6278
6310
|
|
|
6279
|
-
// src/models/sdk.store.ts
|
|
6280
|
-
var chat = null;
|
|
6281
|
-
var open = false;
|
|
6282
|
-
var view = { open: false, unread: 0 };
|
|
6283
|
-
var listeners = /* @__PURE__ */ new Set();
|
|
6284
|
-
var identity = null;
|
|
6285
|
-
var signOutWaiting = false;
|
|
6286
|
-
var attributes = {};
|
|
6287
|
-
var emit = () => {
|
|
6288
|
-
const unread = chat?.getState().unread ?? 0;
|
|
6289
|
-
if (view.open === open && view.unread === unread) return;
|
|
6290
|
-
view = { open, unread };
|
|
6291
|
-
for (const listener of listeners) listener();
|
|
6292
|
-
};
|
|
6293
|
-
var setOpen = (next) => {
|
|
6294
|
-
open = next;
|
|
6295
|
-
emit();
|
|
6296
|
-
};
|
|
6297
|
-
var zean = {
|
|
6298
|
-
open: () => setOpen(true),
|
|
6299
|
-
close: () => setOpen(false),
|
|
6300
|
-
toggle: () => setOpen(!open),
|
|
6301
|
-
identify(user) {
|
|
6302
|
-
identity = user;
|
|
6303
|
-
void chat?.identify(user);
|
|
6304
|
-
},
|
|
6305
|
-
signOut() {
|
|
6306
|
-
identity = null;
|
|
6307
|
-
attributes = {};
|
|
6308
|
-
if (chat) void chat.signOut();
|
|
6309
|
-
else signOutWaiting = true;
|
|
6310
|
-
},
|
|
6311
|
-
setAttributes(next) {
|
|
6312
|
-
attributes = { ...attributes, ...next };
|
|
6313
|
-
void chat?.setAttributes(next);
|
|
6314
|
-
}
|
|
6315
|
-
};
|
|
6316
|
-
function attachChat(next) {
|
|
6317
|
-
if (chat) console.warn(`${APP_NAME}: render <ZeanChat> once, near the root of the app.`);
|
|
6318
|
-
chat = next;
|
|
6319
|
-
if (identity) void next.identify(identity);
|
|
6320
|
-
else if (signOutWaiting) void next.signOut();
|
|
6321
|
-
signOutWaiting = false;
|
|
6322
|
-
if (Object.keys(attributes).length > 0) void next.setAttributes(attributes);
|
|
6323
|
-
const stop = next.subscribe(emit);
|
|
6324
|
-
emit();
|
|
6325
|
-
return () => {
|
|
6326
|
-
stop();
|
|
6327
|
-
if (chat === next) chat = null;
|
|
6328
|
-
emit();
|
|
6329
|
-
};
|
|
6330
|
-
}
|
|
6331
|
-
function chatView() {
|
|
6332
|
-
return view;
|
|
6333
|
-
}
|
|
6334
|
-
function subscribeChatView(listener) {
|
|
6335
|
-
listeners.add(listener);
|
|
6336
|
-
return () => listeners.delete(listener);
|
|
6337
|
-
}
|
|
6338
|
-
|
|
6339
6311
|
// src/models/sdk.utils.ts
|
|
6340
6312
|
var import_react_native = require("react-native");
|
|
6341
6313
|
|
|
@@ -6589,6 +6561,27 @@ function deviceInfoBundleId() {
|
|
|
6589
6561
|
return null;
|
|
6590
6562
|
}
|
|
6591
6563
|
}
|
|
6564
|
+
function isDevBuild() {
|
|
6565
|
+
return typeof __DEV__ !== "undefined" && __DEV__;
|
|
6566
|
+
}
|
|
6567
|
+
function isZeanNotification(data) {
|
|
6568
|
+
const value = objectOf(data);
|
|
6569
|
+
if (!value) return false;
|
|
6570
|
+
if (value.zean === "message" && typeof value.conversationId === "string") return true;
|
|
6571
|
+
const inner = objectOf(value.data);
|
|
6572
|
+
return inner !== null && inner.zean === "message" && typeof inner.conversationId === "string";
|
|
6573
|
+
}
|
|
6574
|
+
function objectOf(data) {
|
|
6575
|
+
let value = data;
|
|
6576
|
+
if (typeof value === "string") {
|
|
6577
|
+
try {
|
|
6578
|
+
value = JSON.parse(value);
|
|
6579
|
+
} catch {
|
|
6580
|
+
return null;
|
|
6581
|
+
}
|
|
6582
|
+
}
|
|
6583
|
+
return typeof value === "object" && value !== null ? value : null;
|
|
6584
|
+
}
|
|
6592
6585
|
function deviceOs() {
|
|
6593
6586
|
if (import_react_native.Platform.OS === "ios") return `iOS ${import_react_native.Platform.Version}`;
|
|
6594
6587
|
if (import_react_native.Platform.OS === "android") return `Android ${import_react_native.Platform.constants.Release}`;
|
|
@@ -6624,6 +6617,77 @@ function hideKeyboard() {
|
|
|
6624
6617
|
});
|
|
6625
6618
|
}
|
|
6626
6619
|
|
|
6620
|
+
// src/models/sdk.store.ts
|
|
6621
|
+
var chat = null;
|
|
6622
|
+
var open = false;
|
|
6623
|
+
var view = { open: false, unread: 0 };
|
|
6624
|
+
var listeners = /* @__PURE__ */ new Set();
|
|
6625
|
+
var identity = null;
|
|
6626
|
+
var signOutWaiting = false;
|
|
6627
|
+
var attributes = {};
|
|
6628
|
+
var pushToken;
|
|
6629
|
+
var emit = () => {
|
|
6630
|
+
const unread = chat?.getState().unread ?? 0;
|
|
6631
|
+
if (view.open === open && view.unread === unread) return;
|
|
6632
|
+
view = { open, unread };
|
|
6633
|
+
for (const listener of listeners) listener();
|
|
6634
|
+
};
|
|
6635
|
+
var setOpen = (next) => {
|
|
6636
|
+
open = next;
|
|
6637
|
+
emit();
|
|
6638
|
+
};
|
|
6639
|
+
var zean = {
|
|
6640
|
+
open: () => setOpen(true),
|
|
6641
|
+
close: () => setOpen(false),
|
|
6642
|
+
toggle: () => setOpen(!open),
|
|
6643
|
+
identify(user) {
|
|
6644
|
+
identity = user;
|
|
6645
|
+
void chat?.identify(user);
|
|
6646
|
+
},
|
|
6647
|
+
signOut() {
|
|
6648
|
+
identity = null;
|
|
6649
|
+
attributes = {};
|
|
6650
|
+
if (chat) void chat.signOut();
|
|
6651
|
+
else signOutWaiting = true;
|
|
6652
|
+
},
|
|
6653
|
+
setAttributes(next) {
|
|
6654
|
+
attributes = { ...attributes, ...next };
|
|
6655
|
+
void chat?.setAttributes(next);
|
|
6656
|
+
},
|
|
6657
|
+
setPushToken(token) {
|
|
6658
|
+
pushToken = token;
|
|
6659
|
+
chat?.setPushToken(token);
|
|
6660
|
+
}
|
|
6661
|
+
};
|
|
6662
|
+
function openFromNotification(data) {
|
|
6663
|
+
if (!isZeanNotification(data)) return false;
|
|
6664
|
+
zean.open();
|
|
6665
|
+
return true;
|
|
6666
|
+
}
|
|
6667
|
+
function attachChat(next) {
|
|
6668
|
+
if (chat) console.warn(`${APP_NAME}: render <ZeanChat> once, near the root of the app.`);
|
|
6669
|
+
chat = next;
|
|
6670
|
+
if (identity) void next.identify(identity);
|
|
6671
|
+
else if (signOutWaiting) void next.signOut();
|
|
6672
|
+
signOutWaiting = false;
|
|
6673
|
+
if (Object.keys(attributes).length > 0) void next.setAttributes(attributes);
|
|
6674
|
+
if (pushToken !== void 0) next.setPushToken(pushToken);
|
|
6675
|
+
const stop = next.subscribe(emit);
|
|
6676
|
+
emit();
|
|
6677
|
+
return () => {
|
|
6678
|
+
stop();
|
|
6679
|
+
if (chat === next) chat = null;
|
|
6680
|
+
emit();
|
|
6681
|
+
};
|
|
6682
|
+
}
|
|
6683
|
+
function chatView() {
|
|
6684
|
+
return view;
|
|
6685
|
+
}
|
|
6686
|
+
function subscribeChatView(listener) {
|
|
6687
|
+
listeners.add(listener);
|
|
6688
|
+
return () => listeners.delete(listener);
|
|
6689
|
+
}
|
|
6690
|
+
|
|
6627
6691
|
// src/components/ChatModal.tsx
|
|
6628
6692
|
var import_react16 = require("react");
|
|
6629
6693
|
var import_react_native23 = require("react-native");
|
|
@@ -8213,7 +8277,9 @@ function ZeanChat({
|
|
|
8213
8277
|
// Checked against the App's Bundle ID or Package name: another app with the same key shows no chat.
|
|
8214
8278
|
bundle: appBundle(),
|
|
8215
8279
|
// The Conversation stays on the phone: the next start shows it at once and asks only for newer messages.
|
|
8216
|
-
keep: true
|
|
8280
|
+
keep: true,
|
|
8281
|
+
// A development build: the phone becomes a Test device for App settings > Developers > Webhook.
|
|
8282
|
+
dev: isDevBuild()
|
|
8217
8283
|
});
|
|
8218
8284
|
const detach = attachChat(started);
|
|
8219
8285
|
setChat(started);
|
package/dist/interfaces.d.ts
CHANGED
|
@@ -64,5 +64,11 @@ export interface ZeanControls {
|
|
|
64
64
|
signOut: () => void;
|
|
65
65
|
/** Joined to what was set before. Values the chat cannot keep are refused, and the console says why. */
|
|
66
66
|
setAttributes: (attributes: ZeanAttributes) => void;
|
|
67
|
+
/**
|
|
68
|
+
* The phone's push token, from your push library (`ExponentPushToken[…]`, an FCM or an APNs token), or null once
|
|
69
|
+
* there is none. Zean passes it to your server in each `message.member` webhook event, in `push.tokens`: your server
|
|
70
|
+
* sends the push. Given again after a sign-out by itself; the same token twice sends nothing.
|
|
71
|
+
*/
|
|
72
|
+
setPushToken: (token: string | null) => void;
|
|
67
73
|
unreadCount: number;
|
|
68
74
|
}
|
|
@@ -3,6 +3,11 @@ import type { ZeanControls } from '../interfaces';
|
|
|
3
3
|
import type { ChatView } from './sdk.interface';
|
|
4
4
|
/** The chat's methods. Usable before `<ZeanChat>` mounts: what is asked waits for the chat. */
|
|
5
5
|
export declare const zean: Omit<ZeanControls, 'unreadCount'>;
|
|
6
|
+
/**
|
|
7
|
+
* Opens the chat when a tapped notification is a push for a Member's reply, and says whether it was. Works before
|
|
8
|
+
* `<ZeanChat>` mounts, as when the tap started the app.
|
|
9
|
+
*/
|
|
10
|
+
export declare function openFromNotification(data: unknown): boolean;
|
|
6
11
|
/** `<ZeanChat>` hands over the chat it started. Returns the way to let it go. */
|
|
7
12
|
export declare function attachChat(next: Chat): () => void;
|
|
8
13
|
/** Whether the chat is open and how many replies wait: the same object until one changes. */
|
|
@@ -25,6 +25,14 @@ export declare function appBundle(): AppBundle | undefined;
|
|
|
25
25
|
* back to memory too.
|
|
26
26
|
*/
|
|
27
27
|
export declare function chatStorage(storage?: ZeanStorage): ChatStorage;
|
|
28
|
+
/** A development build: the phone becomes a Test device, which App settings send Test events as. */
|
|
29
|
+
export declare function isDevBuild(): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Whether a notification's data is a push for a Member's reply: `{ zean: 'message', conversationId }`, as the
|
|
32
|
+
* `message.member` webhook's `push.data` gives it. Also as JSON text, or nested in `{ data }`, as some push setups
|
|
33
|
+
* deliver it.
|
|
34
|
+
*/
|
|
35
|
+
export declare function isZeanNotification(data: unknown): boolean;
|
|
28
36
|
/** "iOS 18.2" or "Android 15", for the Contact panel. */
|
|
29
37
|
export declare function deviceOs(): string | undefined;
|
|
30
38
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zean-chat/react-native",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"description": "In-app support chat for React Native and Expo, iOS and Android. Pure JS, no native module. A free Intercom, Crisp and Zendesk mobile SDK alternative.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-native",
|