@applicaster/zapp-react-native-utils 13.0.20 → 13.0.21-rc.1

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.
@@ -12,8 +12,14 @@ export function calculateReadingTime(
12
12
  minimumPause: number = 500,
13
13
  announcementDelay: number = 700
14
14
  ): number {
15
- const words = text
16
- .trim()
15
+ if (!text) {
16
+ return 0;
17
+ }
18
+
19
+ const trimmed = text.trim();
20
+
21
+ // Count words (split on whitespace and punctuation, keep alnum boundaries)
22
+ const words = trimmed
17
23
  .split(/(?<=\d)(?=[a-zA-Z])|(?<=[a-zA-Z])(?=\d)|[^\w\s]+|\s+/)
18
24
  .filter(Boolean).length;
19
25
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "13.0.20",
3
+ "version": "13.0.21-rc.1",
4
4
  "description": "Applicaster Zapp React Native utilities package",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "homepage": "https://github.com/applicaster/quickbrick#readme",
29
29
  "dependencies": {
30
- "@applicaster/applicaster-types": "13.0.20",
30
+ "@applicaster/applicaster-types": "13.0.21-rc.1",
31
31
  "buffer": "^5.2.1",
32
32
  "camelize": "^1.0.0",
33
33
  "dayjs": "^1.11.10",
@@ -1,3 +1,34 @@
1
+ import React from "react";
2
+
3
+ import { WrappedWithProviders } from "@applicaster/zapp-react-native-utils/testUtils";
4
+
5
+ import {
6
+ getInflatedDataSourceUrl,
7
+ getSearchContext,
8
+ useGetUrlInflater,
9
+ } from "../useInflatedUrl";
10
+
11
+ import { reactHooksLogger } from "../../logger";
12
+
13
+ jest.mock("../../navigation", () => ({
14
+ useRoute: () => ({ pathname: "/mock/path" }),
15
+ }));
16
+
17
+ // mock contexts hooks
18
+ jest.mock("@applicaster/zapp-react-native-ui-components/Contexts", () => ({
19
+ ZappPipesEntryContext: {
20
+ useZappPipesContext: () => [
21
+ { id: "entry-1", extensions: { showId: "A123" } },
22
+ ],
23
+ },
24
+ ZappPipesSearchContext: {
25
+ useZappPipesContext: () => ["user%20query"],
26
+ },
27
+ ZappPipesScreenContext: {
28
+ useZappPipesContext: () => [{ id: "screen-1" }],
29
+ },
30
+ }));
31
+
1
32
  jest.mock("../../logger", () => ({
2
33
  reactHooksLogger: {
3
34
  warning: jest.fn(),
@@ -5,13 +36,6 @@ jest.mock("../../logger", () => ({
5
36
  },
6
37
  }));
7
38
 
8
- const {
9
- getInflatedDataSourceUrl,
10
- getSearchContext,
11
- } = require("../useInflatedUrl");
12
-
13
- const { reactHooksLogger } = require("../../logger");
14
-
15
39
  let mockStore;
16
40
 
17
41
  describe("getInflatedDataSourceUrl", () => {
@@ -188,3 +212,34 @@ describe("getSearchContext", () => {
188
212
  expect(result).toHaveProperty(mapping.queryParam.property, searchContext);
189
213
  });
190
214
  });
215
+
216
+ describe("useGetUrlInflater", () => {
217
+ const { renderHook } = require("@testing-library/react-hooks");
218
+
219
+ it("returns original url when mapping is not provided", () => {
220
+ const { result } = renderHook(() => useGetUrlInflater(), {
221
+ wrapper: WrappedWithProviders,
222
+ });
223
+
224
+ const src = "https://foo.com/feed";
225
+ expect(result.current(src)).toBe(src);
226
+ });
227
+
228
+ it("inflates url using entry/search/screen contexts when mapping provided", () => {
229
+ const { result } = renderHook(() => useGetUrlInflater(), {
230
+ wrapper: ({ children }) => (
231
+ <WrappedWithProviders>{children}</WrappedWithProviders>
232
+ ),
233
+ });
234
+
235
+ const source = "https://foo.com/shows/{{showId}}?q={{q}}";
236
+
237
+ const mapping = {
238
+ showId: { source: "entry", property: "extensions.showId" },
239
+ q: { source: "search", property: "q" },
240
+ };
241
+
242
+ const url = result.current(source, mapping);
243
+ expect(url).toBe("https://foo.com/shows/A123?q=user%20query");
244
+ });
245
+ });
@@ -1,4 +1,4 @@
1
- import { useMemo } from "react";
1
+ import { useCallback, useMemo } from "react";
2
2
  import * as R from "ramda";
3
3
 
4
4
  import {
@@ -178,22 +178,19 @@ export function getSearchContext(
178
178
  return { [property]: encodeIfNeeded(searchContext) };
179
179
  }
180
180
 
181
- export function useInflatedUrl({
182
- feedUrl,
183
- mapping,
184
- }: {
185
- feedUrl?: string;
186
- mapping?: ZappTypeMapping;
187
- }) {
181
+ export const useGetUrlInflater = () => {
188
182
  const { pathname } = useRoute();
189
183
 
190
184
  const [entryContext] = ZappPipesEntryContext.useZappPipesContext(pathname);
191
185
  const [searchContext] = ZappPipesSearchContext.useZappPipesContext();
192
186
  const [screenContext] = ZappPipesScreenContext.useZappPipesContext();
193
187
 
194
- const url = useMemo(
195
- () =>
196
- mapping
188
+ return useCallback(
189
+ (
190
+ feedUrl: Nullable<string>,
191
+ mapping?: ZappTypeMapping
192
+ ): Nullable<string> => {
193
+ return mapping
197
194
  ? getInflatedDataSourceUrl({
198
195
  source: feedUrl,
199
196
  contexts: {
@@ -203,8 +200,24 @@ export function useInflatedUrl({
203
200
  },
204
201
  mapping,
205
202
  })
206
- : feedUrl,
207
- [feedUrl, mapping]
203
+ : feedUrl;
204
+ },
205
+ [entryContext, screenContext, searchContext]
206
+ );
207
+ };
208
+
209
+ export function useInflatedUrl({
210
+ feedUrl,
211
+ mapping,
212
+ }: {
213
+ feedUrl?: string;
214
+ mapping?: ZappTypeMapping;
215
+ }) {
216
+ const urlInflater = useGetUrlInflater();
217
+
218
+ const url = useMemo(
219
+ () => urlInflater(feedUrl, mapping),
220
+ [urlInflater, feedUrl, mapping]
208
221
  );
209
222
 
210
223
  if (!feedUrl) {
@@ -0,0 +1,240 @@
1
+ import { useNavigation, useRivers, useScreenContext } from "../../reactHooks";
2
+ import { createLogger } from "../../logger";
3
+ import { useCallback, useMemo, useRef, useEffect } from "react";
4
+
5
+ export enum NavigationCallbackOptions {
6
+ DEFAULT = "default",
7
+ GO_HOME = "go_home",
8
+ GO_BACK = "go_back",
9
+ GO_TO_SCREEN = "go_to_screen",
10
+ }
11
+
12
+ export enum ResultType {
13
+ login = "login",
14
+ logout = "logout",
15
+ }
16
+
17
+ export type CallbackResult = hookCallbackArgs & {
18
+ options?: {
19
+ resultType?: ResultType;
20
+ navigator?: QuickBrickAppNavigator;
21
+ };
22
+ };
23
+
24
+ export type ScreenResultCallback = (args: CallbackResult) => void | undefined;
25
+
26
+ export const CALLBACK_NAVIGATION_KEY = "completion_action";
27
+
28
+ export const CALLBACK_NAVIGATION_GO_TO_SCREEN_KEY =
29
+ "completion_action_navigation_go_to_screen";
30
+
31
+ type NavKeys = {
32
+ action: NavigationCallbackOptions;
33
+ targetScreenId: string | null;
34
+ } | null;
35
+
36
+ type General = Record<string, unknown>;
37
+
38
+ const LogPrefix = "useCallbackNavigationAction:";
39
+
40
+ const { log_info, log_verbose, log_debug } = createLogger({
41
+ subsystem: "hook-navigation-callback",
42
+ });
43
+
44
+ const legacyMappingKeys = {
45
+ "quick-brick-login-flow": {
46
+ actionType: "login_completion_action",
47
+ targetScreen: "navigate_to_login_screen",
48
+ },
49
+ "quick-brick-user-account-ui-component": {
50
+ actionType: "callbackAction",
51
+ },
52
+ "quick-brick-login-multi-login-providers.login": {
53
+ actionType: "login_completion_action",
54
+ targetScreen: "navigate_to_login_screen",
55
+ },
56
+ "quick-brick-login-multi-login-providers.logout": {
57
+ actionType: "logout_completion_action",
58
+ targetScreen: "navigate_to_logout_screen",
59
+ },
60
+ "quick-brick-storefront": {
61
+ actionType: "purchase_completion_action",
62
+ targetScreen: "navigate_to_screen_after_purchase",
63
+ },
64
+ "zapp_login_plugin_oauth_tv_2_0.login": {
65
+ actionType: "login_completion_action",
66
+ targetScreen: "navigate_to_login_screen",
67
+ },
68
+ "zapp_login_plugin_oauth_tv_2_0.logout": {
69
+ actionType: "logout_completion_action",
70
+ targetScreen: "navigate_to_logout_screen",
71
+ },
72
+ };
73
+
74
+ const NAV_ACTIONS = (
75
+ Object.values(NavigationCallbackOptions) as string[]
76
+ ).filter((value) => value !== NavigationCallbackOptions.DEFAULT);
77
+
78
+ const isNavAction = (v: unknown): v is NavigationCallbackOptions =>
79
+ typeof v === "string" && NAV_ACTIONS.includes(v);
80
+
81
+ export const getNavigationKeys = (
82
+ item?: ZappUIComponent | ZappRiver,
83
+ resultType: ResultType | null = null
84
+ ): NavKeys => {
85
+ const general = (item?.general ?? {}) as General;
86
+
87
+ const pluginIdentifier = (item as any).identifier ?? item?.type ?? "";
88
+
89
+ const legacy =
90
+ legacyMappingKeys[`${pluginIdentifier}.${resultType}`] ??
91
+ legacyMappingKeys[pluginIdentifier] ??
92
+ {};
93
+
94
+ const actionKey = resultType
95
+ ? `${resultType}_${CALLBACK_NAVIGATION_KEY}`
96
+ : CALLBACK_NAVIGATION_KEY;
97
+
98
+ const rawAction =
99
+ (general as General)[actionKey] ??
100
+ (legacy.actionType ? (general as General)[legacy.actionType] : undefined);
101
+
102
+ const action: NavigationCallbackOptions | null = isNavAction(rawAction)
103
+ ? rawAction
104
+ : null;
105
+
106
+ if (!action) return null;
107
+
108
+ let targetScreenId: string | null = null;
109
+
110
+ if (action === NavigationCallbackOptions.GO_TO_SCREEN) {
111
+ const screenKey = resultType
112
+ ? `${resultType}_${CALLBACK_NAVIGATION_GO_TO_SCREEN_KEY}`
113
+ : CALLBACK_NAVIGATION_GO_TO_SCREEN_KEY;
114
+
115
+ const screenId: string | null =
116
+ ((general as General)[screenKey] as string) ??
117
+ (legacy.targetScreen
118
+ ? ((general as General)[legacy.targetScreen] as string)
119
+ : undefined);
120
+
121
+ if (screenId) {
122
+ targetScreenId = screenId.length > 0 ? screenId : null;
123
+ }
124
+ }
125
+
126
+ return { action, targetScreenId };
127
+ };
128
+
129
+ export const useCallbackNavigationAction = (
130
+ item?: ZappUIComponent | ZappRiver
131
+ ): ((
132
+ args: CallbackResult,
133
+ hookCallback?: hookCallback
134
+ ) => void | undefined) => {
135
+ const navigation = useNavigation();
136
+ const rivers = useRivers();
137
+ const screenContext = useScreenContext();
138
+
139
+ const navigationRef = useRef(navigation);
140
+
141
+ useEffect(() => {
142
+ navigationRef.current = navigation;
143
+ }, [navigation]);
144
+
145
+ const overrideCallbackFromComponent = useMemo(() => {
146
+ log_verbose(`${LogPrefix}: overridden callbackAction by component`);
147
+
148
+ // TODO: Check if we have better option where to store overridden callback action
149
+ return screenContext?.options?.callback;
150
+ }, [screenContext?.options?.callback]);
151
+
152
+ if (typeof __DEV__ !== "undefined" && __DEV__) {
153
+ log_verbose(`${LogPrefix} screenContext`, { screenContext, item });
154
+ }
155
+
156
+ const callbackAction = useCallback<hookCallback>(
157
+ (args: CallbackResult, hookCallback: hookCallback = null) => {
158
+ if (!args.success) {
159
+ log_debug(
160
+ `${LogPrefix} callback called with no success, use original callback`
161
+ );
162
+
163
+ hookCallback?.(args);
164
+
165
+ return;
166
+ }
167
+
168
+ if (args.cancelled) {
169
+ log_debug(
170
+ `${LogPrefix} callback called but cancelled, use original callback`
171
+ );
172
+
173
+ hookCallback?.(args);
174
+
175
+ return;
176
+ }
177
+
178
+ const data = getNavigationKeys(item, args.options?.resultType ?? null);
179
+
180
+ if (!data) {
181
+ hookCallback?.(args);
182
+
183
+ return;
184
+ }
185
+
186
+ hookCallback?.({ ...args, success: false, cancelled: true });
187
+ const currentNavigation = navigationRef.current;
188
+
189
+ switch (data.action) {
190
+ case NavigationCallbackOptions.GO_BACK: {
191
+ if (currentNavigation.canGoBack()) {
192
+ currentNavigation.goBack();
193
+
194
+ log_info(`${LogPrefix} performing 'GO BACK' action`);
195
+ } else {
196
+ log_info(`${LogPrefix} cannot perform 'GO BACK' action — ignoring`);
197
+ }
198
+
199
+ break;
200
+ }
201
+
202
+ case NavigationCallbackOptions.GO_HOME: {
203
+ currentNavigation.goHome();
204
+ log_info(`${LogPrefix} performing 'GO HOME' action`);
205
+ break;
206
+ }
207
+
208
+ case NavigationCallbackOptions.GO_TO_SCREEN: {
209
+ const screenId = data.targetScreenId;
210
+
211
+ if (!screenId) {
212
+ log_info(`${LogPrefix} no screenId provided — ignoring`);
213
+ break;
214
+ }
215
+
216
+ const screen = rivers[screenId];
217
+
218
+ if (screen) {
219
+ currentNavigation.replace(screen);
220
+
221
+ log_info(
222
+ `${LogPrefix} performing 'GO TO SCREEN' action to screen: ${screenId}`
223
+ );
224
+ } else {
225
+ log_info(`${LogPrefix} no screen provided — ignoring`);
226
+ }
227
+
228
+ break;
229
+ }
230
+
231
+ default: {
232
+ break;
233
+ }
234
+ }
235
+ },
236
+ [item, rivers]
237
+ );
238
+
239
+ return overrideCallbackFromComponent || callbackAction;
240
+ };
@@ -0,0 +1,76 @@
1
+ const NavigationCallbackOptions = {
2
+ DEFAULT: "default",
3
+ GO_HOME: "go_home",
4
+ GO_BACK: "go_back",
5
+ GO_TO_SCREEN: "go_to_screen",
6
+ };
7
+
8
+ const ResultType = {
9
+ login: "login",
10
+ logout: "logout",
11
+ };
12
+
13
+ const CALLBACK_NAVIGATION_KEY = "completion_action";
14
+
15
+ const CALLBACK_NAVIGATION_GO_TO_SCREEN_KEY =
16
+ "completion_action_navigation_go_to_screen";
17
+
18
+ const callbackKeyPrefix = (key, prefix, keySeparator = "_") =>
19
+ prefix ? `${prefix}${keySeparator}${key}` : key;
20
+
21
+ const extendManifestWithHookCallback = (prefix = null) => ({
22
+ group: true,
23
+ label: "CallBack Navigation",
24
+ folded: true,
25
+ fields: [
26
+ {
27
+ type: "select",
28
+ key: callbackKeyPrefix(CALLBACK_NAVIGATION_KEY, prefix),
29
+ label: callbackKeyPrefix(
30
+ "Callback Navigation",
31
+ prefix?.toUpperCase(),
32
+ " "
33
+ ),
34
+ label_tooltip:
35
+ "Defines what navigation action should be performed after the callback is called.",
36
+ options: [
37
+ {
38
+ text: "Use default flow",
39
+ value: NavigationCallbackOptions.DEFAULT,
40
+ },
41
+ {
42
+ text: "Go Back to home screen",
43
+ value: NavigationCallbackOptions.GO_HOME,
44
+ },
45
+ {
46
+ text: "Go Back to previous screen",
47
+ value: NavigationCallbackOptions.GO_BACK,
48
+ },
49
+ {
50
+ text: "Move to specific screen",
51
+ value: NavigationCallbackOptions.GO_TO_SCREEN,
52
+ },
53
+ ],
54
+ initial_value: "default",
55
+ },
56
+ {
57
+ type: "screen_selector",
58
+ key: callbackKeyPrefix(CALLBACK_NAVIGATION_GO_TO_SCREEN_KEY, prefix),
59
+ label: callbackKeyPrefix(
60
+ "Navigate to screen",
61
+ prefix?.toUpperCase(),
62
+ " "
63
+ ),
64
+ label_tooltip: "Screen you wish to navigate to after success purchase",
65
+ rules: "conditional",
66
+ conditional_fields: [
67
+ {
68
+ key: `general/${callbackKeyPrefix(CALLBACK_NAVIGATION_KEY, prefix)}`,
69
+ condition_value: NavigationCallbackOptions.GO_TO_SCREEN,
70
+ },
71
+ ],
72
+ },
73
+ ],
74
+ });
75
+
76
+ module.exports = { extendManifestWithHookCallback, ResultType };
@@ -0,0 +1,19 @@
1
+ import { useCallback } from "react";
2
+ import {
3
+ CallbackResult,
4
+ useCallbackNavigationAction,
5
+ } from "./callbackNavigationAction";
6
+
7
+ export const useCallbackActions = (
8
+ item?: ZappUIComponent | ZappRiver,
9
+ hookCallback?: hookCallback
10
+ ): hookCallback => {
11
+ const navigationAction = useCallbackNavigationAction(item);
12
+
13
+ return useCallback(
14
+ async (data: CallbackResult) => {
15
+ navigationAction(data, hookCallback);
16
+ },
17
+ [navigationAction, hookCallback]
18
+ );
19
+ };