@screeb/react-native 3.3.2 → 4.0.2

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/src/index.tsx CHANGED
@@ -3,11 +3,25 @@ import {
3
3
  NativeEventEmitter,
4
4
  NativeModules,
5
5
  Platform,
6
+ View,
7
+ type ViewProps,
6
8
  } from 'react-native';
7
9
  import ScreebReactNative from './NativeScreebReactNative';
8
10
 
9
- // biome-ignore lint/suspicious/noExplicitAny: .
10
- let emitter: any;
11
+ type ScreebEvent = {
12
+ hookId?: string;
13
+ nativeHookId?: string;
14
+ payload?: unknown;
15
+ };
16
+ type ScreebEventEmitter = {
17
+ addListener(
18
+ eventType: 'ScreebEvent',
19
+ listener: (event: ScreebEvent) => void
20
+ ): { remove: () => void };
21
+ };
22
+
23
+ let emitter: ScreebEventEmitter | undefined;
24
+ let eventSubscription: { remove: () => void } | undefined;
11
25
 
12
26
  type HookFn = (payload: string) => unknown | Promise<unknown>;
13
27
  type HookMap = {
@@ -20,6 +34,10 @@ type InitOptions = {
20
34
  isDebugMode?: boolean;
21
35
  disableMirror?: boolean;
22
36
  };
37
+ export type ScreebPrivacyViewProps = ViewProps;
38
+ export type ScreebIdProps = ViewProps & {
39
+ id: string;
40
+ };
23
41
 
24
42
  // initSdk
25
43
  export function initSdk(
@@ -30,36 +48,9 @@ export function initSdk(
30
48
  initOptions?: InitOptions,
31
49
  language?: string
32
50
  ) {
33
- // Use NativeEventEmitter on both platforms; pass the native module on iOS
34
- // and rely on the default emitter on Android.
35
- if (Platform.OS === 'ios') {
36
- emitter = new NativeEventEmitter(NativeModules.ScreebReactNative);
37
- } else {
38
- emitter = DeviceEventEmitter;
39
- }
40
-
41
- emitter.addListener('ScreebEvent', handleEvent);
42
-
43
- let mapHooksId: HookIdsMap | undefined;
44
- if (hooks != null) {
45
- mapHooksId = {};
46
- Object.keys(hooks).forEach((key) => {
47
- if (key === 'version') {
48
- const v = hooks.version ?? undefined;
49
- if (v)
50
- mapHooksId = { ...mapHooksId, version: v } as {
51
- [key: string]: string;
52
- };
53
- } else {
54
- const uuid = Date.now().toString() + Math.random().toString() + key;
55
- const fn = (hooks as Record<string, unknown>)[key];
56
- if (typeof fn === 'function') {
57
- hooksRegistry.set(uuid, fn as HookFn);
58
- }
59
- mapHooksId = { ...mapHooksId, [key]: uuid };
60
- }
61
- });
62
- }
51
+ registerEventListener();
52
+ hooksRegistry.clear();
53
+ const mapHooksId = buildHooksMap(hooks);
63
54
 
64
55
  return ScreebReactNative.initSdk(
65
56
  channelId,
@@ -130,26 +121,7 @@ export function startSurvey(
130
121
  language?: string,
131
122
  distributionId?: string
132
123
  ) {
133
- let mapHooksId: HookIdsMap | undefined;
134
- if (hooks !== undefined) {
135
- mapHooksId = {};
136
- Object.keys(hooks).forEach((key) => {
137
- if (key === 'version') {
138
- const v = hooks.version ?? undefined;
139
- if (v)
140
- mapHooksId = { ...mapHooksId, version: v } as {
141
- [key: string]: string;
142
- };
143
- } else {
144
- const uuid = Date.now().toString() + Math.random().toString() + key;
145
- const fn = (hooks as Record<string, unknown>)[key];
146
- if (typeof fn === 'function') {
147
- hooksRegistry.set(uuid, fn as HookFn);
148
- }
149
- mapHooksId = { ...mapHooksId, [key]: uuid };
150
- }
151
- });
152
- }
124
+ const mapHooksId = buildHooksMap(hooks);
153
125
  return ScreebReactNative.startSurvey(
154
126
  surveyId,
155
127
  allowMultipleResponses ?? true,
@@ -171,26 +143,7 @@ export function startMessage(
171
143
  language?: string,
172
144
  distributionId?: string
173
145
  ) {
174
- let mapHooksId: HookIdsMap | undefined;
175
- if (hooks !== undefined) {
176
- mapHooksId = {};
177
- Object.keys(hooks).forEach((key) => {
178
- if (key === 'version') {
179
- const v = hooks.version ?? undefined;
180
- if (v)
181
- mapHooksId = { ...mapHooksId, version: v } as {
182
- [key: string]: string;
183
- };
184
- } else {
185
- const uuid = Date.now().toString() + Math.random().toString() + key;
186
- const fn = (hooks as Record<string, unknown>)[key];
187
- if (typeof fn === 'function') {
188
- hooksRegistry.set(uuid, fn as HookFn);
189
- }
190
- mapHooksId = { ...mapHooksId, [key]: uuid };
191
- }
192
- });
193
- }
146
+ const mapHooksId = buildHooksMap(hooks);
194
147
  return ScreebReactNative.startMessage(
195
148
  messageId,
196
149
  allowMultipleResponses ?? true,
@@ -232,12 +185,19 @@ export function getIdentity() {
232
185
  return ScreebReactNative.getIdentity();
233
186
  }
234
187
 
188
+ // handleDeepLink — forward a Screeb deep link (screeb-<channel-id> scheme) to
189
+ // the SDK: editor, survey and message links open in-app. Wire it to your
190
+ // linking events (e.g. expo-linking / RN Linking url listener).
191
+ export function handleDeepLink(url: string) {
192
+ return ScreebReactNative.handleDeepLink(url);
193
+ }
194
+
235
195
  // closeSdk
236
196
  export function closeSdk() {
237
- if (emitter) {
238
- emitter.removeAllListeners('ScreebEvent');
239
- emitter = undefined;
240
- }
197
+ eventSubscription?.remove();
198
+ eventSubscription = undefined;
199
+ emitter = undefined;
200
+ hooksRegistry.clear();
241
201
  return ScreebReactNative.closeSdk();
242
202
  }
243
203
 
@@ -251,12 +211,103 @@ export function closeMessage(messageId?: string) {
251
211
  return ScreebReactNative.closeMessage(messageId);
252
212
  }
253
213
 
214
+ export function ScreebMaskText(props: ScreebPrivacyViewProps) {
215
+ const marker = screebPlatformMarker({
216
+ android: 'screeb-mask-text',
217
+ ios: 'screebMaskText',
218
+ });
219
+
220
+ return (
221
+ <View
222
+ {...props}
223
+ accessibilityLabel={marker}
224
+ collapsable={false}
225
+ nativeID={marker}
226
+ />
227
+ );
228
+ }
229
+
230
+ export function ScreebNoCapture(props: ScreebPrivacyViewProps) {
231
+ const marker = screebPlatformMarker({
232
+ android: 'screeb-no-capture',
233
+ ios: 'screebNoCapture',
234
+ });
235
+
236
+ return (
237
+ <View
238
+ {...props}
239
+ accessibilityLabel={marker}
240
+ collapsable={false}
241
+ nativeID={marker}
242
+ />
243
+ );
244
+ }
245
+
246
+ export function ScreebId({ id, ...props }: ScreebIdProps) {
247
+ const marker = `screebId:${id}`;
248
+
249
+ return (
250
+ <View
251
+ {...props}
252
+ accessibilityLabel={marker}
253
+ collapsable={false}
254
+ nativeID={marker}
255
+ />
256
+ );
257
+ }
258
+
254
259
  const hooksRegistry = new Map<
255
260
  string,
256
261
  (payload: string) => unknown | Promise<unknown>
257
262
  >();
258
263
 
259
- function handleEvent(event: { hookId?: string; payload?: unknown }) {
264
+ function screebPlatformMarker(markers: { android: string; ios: string }) {
265
+ return Platform.OS === 'android' ? markers.android : markers.ios;
266
+ }
267
+
268
+ function registerEventListener() {
269
+ eventSubscription?.remove();
270
+
271
+ // Use NativeEventEmitter on both platforms; pass the native module on iOS
272
+ // and rely on the default emitter on Android.
273
+ if (Platform.OS === 'ios') {
274
+ emitter = new NativeEventEmitter(
275
+ NativeModules.ScreebReactNative
276
+ ) as unknown as ScreebEventEmitter;
277
+ } else {
278
+ emitter = DeviceEventEmitter as unknown as ScreebEventEmitter;
279
+ }
280
+
281
+ eventSubscription = emitter?.addListener('ScreebEvent', handleEvent);
282
+ }
283
+
284
+ function buildHooksMap(hooks?: HookMap | null): HookIdsMap | undefined {
285
+ if (hooks == null) {
286
+ return undefined;
287
+ }
288
+
289
+ const mapHooksId: HookIdsMap = {};
290
+ Object.keys(hooks).forEach((key) => {
291
+ if (key === 'version') {
292
+ const version = hooks.version ?? undefined;
293
+ if (version) {
294
+ mapHooksId.version = version;
295
+ }
296
+ return;
297
+ }
298
+
299
+ const uuid = Date.now().toString() + Math.random().toString() + key;
300
+ const fn = (hooks as Record<string, unknown>)[key];
301
+ if (typeof fn === 'function') {
302
+ hooksRegistry.set(uuid, fn as HookFn);
303
+ }
304
+ mapHooksId[key] = uuid;
305
+ });
306
+
307
+ return mapHooksId;
308
+ }
309
+
310
+ function handleEvent(event: ScreebEvent) {
260
311
  if (event?.hookId != null) {
261
312
  const hook = hooksRegistry.get(event.hookId);
262
313
  if (hook != null) {
@@ -265,12 +316,19 @@ function handleEvent(event: { hookId?: string; payload?: unknown }) {
265
316
  ? JSON.stringify(event.payload)
266
317
  : event.payload
267
318
  : '{}';
268
- const result = hook(payload);
269
- const parsedPayload = JSON.parse(payload);
270
- const originalHookId = parsedPayload?.hook_id;
319
+ let originalHookId = event.nativeHookId;
320
+ if (!originalHookId) {
321
+ try {
322
+ const parsedPayload = JSON.parse(payload) as { hook_id?: string };
323
+ originalHookId = parsedPayload?.hook_id;
324
+ } catch (error) {
325
+ console.error(error);
326
+ return;
327
+ }
328
+ }
271
329
  if (originalHookId) {
272
- if (result instanceof Promise) {
273
- result
330
+ try {
331
+ Promise.resolve(hook(payload))
274
332
  .then((hookResult) => {
275
333
  ScreebReactNative.onHookResult(originalHookId, {
276
334
  result: hookResult,
@@ -278,9 +336,15 @@ function handleEvent(event: { hookId?: string; payload?: unknown }) {
278
336
  })
279
337
  .catch((error) => {
280
338
  console.error(error);
339
+ ScreebReactNative.onHookResult(originalHookId, {
340
+ result: false,
341
+ });
281
342
  });
282
- } else {
283
- ScreebReactNative.onHookResult(originalHookId, { result });
343
+ } catch (error) {
344
+ console.error(error);
345
+ ScreebReactNative.onHookResult(originalHookId, {
346
+ result: false,
347
+ });
284
348
  }
285
349
  }
286
350
  }
@@ -329,14 +393,18 @@ function normalizeValue(value: unknown): unknown {
329
393
 
330
394
  // Format payloads so DateTime properties are correctly interpreted by the SDK
331
395
  function formatDateValue(value: Date): string {
332
- const timezoneOffsetHours = -value.getTimezoneOffset() / 60;
333
- const sign = timezoneOffsetHours >= 0 ? '+' : '-';
334
- const offset = Math.abs(timezoneOffsetHours).toString().padStart(2, '0');
396
+ const timezoneOffsetMinutes = -value.getTimezoneOffset();
397
+ const sign = timezoneOffsetMinutes >= 0 ? '+' : '-';
398
+ const absOffset = Math.abs(timezoneOffsetMinutes);
399
+ const offsetHours = Math.floor(absOffset / 60)
400
+ .toString()
401
+ .padStart(2, '0');
402
+ const offsetMinutes = (absOffset % 60).toString().padStart(2, '0');
335
403
  const pad = (n: number, l = 2) => n.toString().padStart(l, '0');
336
404
  const isoWithoutTimezone = `${value.getFullYear()}-${pad(
337
405
  value.getMonth() + 1
338
406
  )}-${pad(value.getDate())}T${pad(value.getHours())}:${pad(
339
407
  value.getMinutes()
340
408
  )}:${pad(value.getSeconds())}.${pad(value.getMilliseconds(), 3)}`;
341
- return `${isoWithoutTimezone}${sign}${offset}:00`;
409
+ return `${isoWithoutTimezone}${sign}${offsetHours}:${offsetMinutes}`;
342
410
  }