rn-erxes-sdk 0.3.3 → 0.4.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.
@@ -0,0 +1,323 @@
1
+ import { useEffect, useRef, useState, type ReactNode } from 'react';
2
+ import { Platform } from 'react-native';
3
+
4
+ import { ErxesNativeIOS, type NativeIOSUser } from './nativeIos';
5
+
6
+ /**
7
+ * The identified end user. Same shape the native bridge expects — passing
8
+ * `undefined` leaves the visitor anonymous until you call `setUser` later.
9
+ */
10
+ export type ErxesUser = NativeIOSUser;
11
+
12
+ /**
13
+ * Helpers handed to action `onPress` callbacks (and `onAction`) so you can drive
14
+ * the messenger imperatively from inside a tap handler — show/hide it, toggle the
15
+ * launcher, or swap the user.
16
+ */
17
+ export type ErxesMessengerHelpers = {
18
+ show: () => Promise<void>;
19
+ hide: () => Promise<void>;
20
+ showLauncher: () => Promise<void>;
21
+ hideLauncher: () => Promise<void>;
22
+ setUser: (user: ErxesUser) => Promise<void>;
23
+ clearUser: () => Promise<void>;
24
+ };
25
+
26
+ /**
27
+ * A chat-mode action rendered in the header (`homeActions`) or drawer
28
+ * (`drawerActions`). Only `id`/`title`/`systemIcon` cross the native bridge; the
29
+ * `onPress` callback stays in JS and runs when the matching action is tapped.
30
+ */
31
+ export type ErxesAction = {
32
+ /** Identifier echoed back from native when the action is tapped. */
33
+ id: string;
34
+ /** Display title (drawer rows / accessibility label for header icons). */
35
+ title: string;
36
+ /** SF Symbol name, e.g. "person.crop.circle". */
37
+ systemIcon: string;
38
+ /** Runs when this action is tapped. Receives imperative {@link ErxesMessengerHelpers}. */
39
+ onPress?: (helpers: ErxesMessengerHelpers) => void | Promise<void>;
40
+ };
41
+
42
+ export type ErxesMessengerProps = {
43
+ /** erxes messenger integration id. Required. */
44
+ integrationId: string;
45
+
46
+ /** Full endpoint URL. Provide one of `endpoint`/`serverUrl`/`subDomain`. */
47
+ endpoint?: string;
48
+ /** Alias for `endpoint`. */
49
+ serverUrl?: string;
50
+ /** Sub-domain shorthand, e.g. `'yourcompany.erxes.io'`. */
51
+ subDomain?: string;
52
+
53
+ /** UI shell to present. Defaults to `'classic'` (the sheet-based widget). */
54
+ displayMode?: 'classic' | 'chat';
55
+
56
+ /** Identify the user before connecting. */
57
+ user?: ErxesUser;
58
+ /** Reuse a previously cached customer id. */
59
+ cachedCustomerId?: string;
60
+
61
+ /** Primary accent color as a hex string, e.g. `'#3f78d9'`. */
62
+ primaryColor?: string;
63
+
64
+ /** Controlled visibility. When set, drives show/hide on change. */
65
+ visible?: boolean;
66
+ /** Open the messenger once configured. Defaults to `true` in chat mode. */
67
+ autoOpen?: boolean;
68
+ /** Hide the messenger when the component unmounts. Defaults to `true`. */
69
+ autoHideOnUnmount?: boolean;
70
+ /** Show (`true`) or hide (`false`) the floating launcher after configure. */
71
+ launcherVisible?: boolean;
72
+
73
+ /** Chat-mode header-right actions. Ignored in `'classic'`. */
74
+ homeActions?: ErxesAction[];
75
+ /** Chat-mode drawer top action rows. Ignored in `'classic'`. */
76
+ drawerActions?: ErxesAction[];
77
+
78
+ /**
79
+ * Rendered while the SDK is configuring (between `onLoad` and
80
+ * `onReady`/`onError`), e.g. a spinner shown before the native messenger
81
+ * appears. Returns `null` otherwise. Defaults to rendering nothing.
82
+ */
83
+ renderLoading?: () => ReactNode;
84
+
85
+ /** Fired when setup starts. */
86
+ onLoad?: () => void;
87
+ /** Fired when the connection handshake completes (the messenger is ready). */
88
+ onReady?: () => void;
89
+ /** Fired when the loading state changes (`true` while configuring). */
90
+ onLoadingChange?: (loading: boolean) => void;
91
+ /** Fired when the messenger is shown. */
92
+ onOpen?: () => void;
93
+ /** Fired when the messenger is hidden. */
94
+ onClose?: () => void;
95
+ /** Fired when setup/open/hide fails. */
96
+ onError?: (error: unknown) => void;
97
+ /** Fallback for tapped actions that have no `onPress`. */
98
+ onAction?: (
99
+ id: string,
100
+ helpers: ErxesMessengerHelpers
101
+ ) => void | Promise<void>;
102
+ };
103
+
104
+ /**
105
+ * Drop `onPress` so only the data-only fields the native bridge understands
106
+ * (`id`/`title`/`systemIcon`) cross over. React Native cannot send JS functions
107
+ * to native, so `onPress` is dispatched on the JS side via the action listener.
108
+ */
109
+ function stripActions(actions: ErxesAction[]) {
110
+ return actions.map(({ onPress: _onPress, ...nativeAction }) => nativeAction);
111
+ }
112
+
113
+ /**
114
+ * Declarative wrapper around {@link ErxesNativeIOS}. Configures the native erxes
115
+ * messenger, wires up action taps, and manages the show/hide lifecycle so app
116
+ * code stays a single component. Renders nothing — the messenger UI is presented
117
+ * natively over your app.
118
+ *
119
+ * For advanced/imperative control, use `ErxesNativeIOS` directly.
120
+ */
121
+ export function ErxesMessenger({
122
+ integrationId,
123
+ endpoint,
124
+ serverUrl,
125
+ subDomain,
126
+ displayMode = 'classic',
127
+ user,
128
+ cachedCustomerId,
129
+ primaryColor,
130
+ visible,
131
+ autoOpen = displayMode === 'chat',
132
+ autoHideOnUnmount = true,
133
+ launcherVisible,
134
+ homeActions = [],
135
+ drawerActions = [],
136
+ renderLoading,
137
+ onLoad,
138
+ onReady,
139
+ onOpen,
140
+ onClose,
141
+ onError,
142
+ onAction,
143
+ onLoadingChange,
144
+ }: ErxesMessengerProps) {
145
+ // Keep the latest actions/callback in refs so the action listener (registered
146
+ // once below) always dispatches against current props without re-subscribing.
147
+ const actionsRef = useRef<ErxesAction[]>([]);
148
+ const onActionRef = useRef(onAction);
149
+ const onLoadingChangeRef = useRef(onLoadingChange);
150
+ // Tracks whether we believe the messenger is currently presented, so we never
151
+ // double-present (chat mode auto-presents inside `configure()`) or fire a
152
+ // redundant show/hide. Native gives us no presentation callback, so this is our
153
+ // best-effort intent mirror.
154
+ const shownRef = useRef(false);
155
+
156
+ // Flips true only after native `configure()` resolves. The controlled-`visible`
157
+ // effect gates on this so it never calls `showMessenger()` before `configure()`
158
+ // (the native SDK asserts on that ordering).
159
+ const [configured, setConfigured] = useState(false);
160
+ // True while configuring (between `onLoad` and `onReady`/`onError`); drives
161
+ // `renderLoading`.
162
+ const [loading, setLoading] = useState(false);
163
+
164
+ useEffect(() => {
165
+ onLoadingChangeRef.current = onLoadingChange;
166
+ }, [onLoadingChange]);
167
+
168
+ useEffect(() => {
169
+ onLoadingChangeRef.current?.(loading);
170
+ }, [loading]);
171
+
172
+ useEffect(() => {
173
+ actionsRef.current = [...homeActions, ...drawerActions];
174
+ onActionRef.current = onAction;
175
+ }, [homeActions, drawerActions, onAction]);
176
+
177
+ useEffect(() => {
178
+ if (Platform.OS !== 'ios') {
179
+ return;
180
+ }
181
+
182
+ setConfigured(false);
183
+
184
+ const helpers: ErxesMessengerHelpers = {
185
+ show: async () => {
186
+ await ErxesNativeIOS.showMessenger();
187
+ shownRef.current = true;
188
+ },
189
+ hide: async () => {
190
+ await ErxesNativeIOS.hideMessenger();
191
+ shownRef.current = false;
192
+ },
193
+ showLauncher: () => ErxesNativeIOS.showLauncher(),
194
+ hideLauncher: () => ErxesNativeIOS.hideLauncher(),
195
+ setUser: (nextUser) => ErxesNativeIOS.setUser(nextUser),
196
+ clearUser: () => ErxesNativeIOS.clearUser(),
197
+ };
198
+
199
+ const sub = ErxesNativeIOS.addActionListener(async (id) => {
200
+ const action = actionsRef.current.find((item) => item.id === id);
201
+
202
+ if (action?.onPress) {
203
+ await action.onPress(helpers);
204
+ return;
205
+ }
206
+
207
+ await onActionRef.current?.(id, helpers);
208
+ });
209
+
210
+ // Connection complete (native `MessengerSDK.isReady`): the messenger is
211
+ // truly ready, so end the loading state and notify the host.
212
+ const readySub = ErxesNativeIOS.addReadyListener(() => {
213
+ setLoading(false);
214
+ onReady?.();
215
+ });
216
+
217
+ async function setup() {
218
+ try {
219
+ setLoading(true);
220
+ onLoad?.();
221
+
222
+ if (user) {
223
+ await ErxesNativeIOS.setUser(user);
224
+ }
225
+
226
+ await ErxesNativeIOS.configure({
227
+ integrationId,
228
+ endpoint,
229
+ serverUrl,
230
+ subDomain,
231
+ cachedCustomerId,
232
+ displayMode,
233
+ primaryColor,
234
+ homeActions: stripActions(homeActions),
235
+ drawerActions: stripActions(drawerActions),
236
+ });
237
+
238
+ // `configure()` only kicks off the async connect; `onReady` and the end
239
+ // of `loading` are driven by the ready listener above, not here.
240
+
241
+ // Decide the initial open state. If `visible` is controlled it wins;
242
+ // otherwise fall back to `autoOpen` (defaults to true in chat mode).
243
+ const shouldOpen = visible ?? autoOpen;
244
+
245
+ if (displayMode === 'chat') {
246
+ // Chat mode auto-presents itself inside `configure()` — never call
247
+ // `showMessenger()` for the initial open or we'd present a second one.
248
+ if (shouldOpen) {
249
+ shownRef.current = true;
250
+ onOpen?.();
251
+ } else {
252
+ // Caller wants it closed: undo the native auto-present.
253
+ await ErxesNativeIOS.hideMessenger();
254
+ shownRef.current = false;
255
+ }
256
+ } else {
257
+ // Classic mode: configure does not open anything. Show the launcher
258
+ // and/or present the sheet explicitly.
259
+ if (launcherVisible === true) {
260
+ await ErxesNativeIOS.showLauncher();
261
+ } else if (launcherVisible === false) {
262
+ await ErxesNativeIOS.hideLauncher();
263
+ }
264
+
265
+ if (shouldOpen) {
266
+ await ErxesNativeIOS.showMessenger();
267
+ shownRef.current = true;
268
+ onOpen?.();
269
+ }
270
+ }
271
+
272
+ setConfigured(true);
273
+ } catch (error) {
274
+ // Setup failed before the connection could complete — end loading here
275
+ // since the ready listener will never fire.
276
+ setLoading(false);
277
+ onError?.(error);
278
+ }
279
+ }
280
+
281
+ setup();
282
+
283
+ return () => {
284
+ sub.remove();
285
+ readySub.remove();
286
+
287
+ if (autoHideOnUnmount && shownRef.current) {
288
+ ErxesNativeIOS.hideMessenger();
289
+ shownRef.current = false;
290
+ onClose?.();
291
+ }
292
+ };
293
+ // Re-run setup only when the native config identity changes; callbacks and
294
+ // actions are read through refs so they don't need to be deps here.
295
+ // eslint-disable-next-line react-hooks/exhaustive-deps
296
+ }, [integrationId, endpoint, serverUrl, subDomain, displayMode]);
297
+
298
+ useEffect(() => {
299
+ // Wait until `configure()` has resolved — otherwise `showMessenger()` would
300
+ // race ahead of it and trip the native configure-before-show assertion. The
301
+ // initial open is handled in `setup()`; this only reacts to later changes.
302
+ if (Platform.OS !== 'ios' || visible === undefined || !configured) {
303
+ return;
304
+ }
305
+
306
+ if (visible && !shownRef.current) {
307
+ ErxesNativeIOS.showMessenger();
308
+ shownRef.current = true;
309
+ onOpen?.();
310
+ } else if (!visible && shownRef.current) {
311
+ ErxesNativeIOS.hideMessenger();
312
+ shownRef.current = false;
313
+ onClose?.();
314
+ }
315
+ // eslint-disable-next-line react-hooks/exhaustive-deps
316
+ }, [visible, configured]);
317
+
318
+ if (loading && renderLoading) {
319
+ return renderLoading();
320
+ }
321
+
322
+ return null;
323
+ }
package/src/index.tsx CHANGED
@@ -4,3 +4,11 @@ export type {
4
4
  NativeIOSConfig,
5
5
  NativeIOSUser,
6
6
  } from './nativeIos';
7
+
8
+ export { ErxesMessenger } from './ErxesMessenger';
9
+ export type {
10
+ ErxesUser,
11
+ ErxesAction,
12
+ ErxesMessengerHelpers,
13
+ ErxesMessengerProps,
14
+ } from './ErxesMessenger';
package/src/nativeIos.ts CHANGED
@@ -56,6 +56,9 @@ type NativeIOSModule = {
56
56
  /** Native event name emitted when a chat-mode action is tapped. */
57
57
  const ACTION_EVENT = 'onErxesAction';
58
58
 
59
+ /** Native event name emitted when the connect handshake completes. */
60
+ const READY_EVENT = 'onErxesReady';
61
+
59
62
  const LINKING_ERROR =
60
63
  "The rn-erxes-sdk native iOS module is not linked. Run `pod install` in your app's ios directory and rebuild the app.";
61
64
 
@@ -118,6 +121,18 @@ export const ErxesNativeIOS = {
118
121
  handler(event.id)
119
122
  );
120
123
  },
124
+ /**
125
+ * Listen for the connect handshake completing — i.e. the messenger is ready
126
+ * (`MessengerSDK.isReady`). Fires once per connection; if already connected
127
+ * when you subscribe via a fresh `configure`, it fires again. Returns a
128
+ * subscription — call `.remove()` to stop listening.
129
+ */
130
+ addReadyListener(handler: () => void): EmitterSubscription {
131
+ const emitter = new NativeEventEmitter(
132
+ getNativeModule() as unknown as NativeModule
133
+ );
134
+ return emitter.addListener(READY_EVENT, () => handler());
135
+ },
121
136
  };
122
137
 
123
138
  export type { NativeIOSAction, NativeIOSConfig, NativeIOSUser };