@applicaster/zapp-react-native-utils 14.0.0-rc.50 → 14.0.0-rc.52

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.
Files changed (37) hide show
  1. package/actionsExecutor/ActionExecutorContext.tsx +60 -84
  2. package/actionsExecutor/ScreenActions.ts +164 -0
  3. package/actionsExecutor/StorageActions.ts +110 -0
  4. package/actionsExecutor/feedDecorator.ts +171 -0
  5. package/actionsExecutor/screenResolver.ts +11 -0
  6. package/analyticsUtils/AnalyticsEvents/helper.ts +1 -1
  7. package/analyticsUtils/__tests__/analyticsUtils.test.js +0 -11
  8. package/appUtils/contextKeysManager/contextResolver.ts +42 -1
  9. package/appUtils/focusManager/__tests__/__snapshots__/focusManager.test.js.snap +3 -0
  10. package/appUtils/focusManager/__tests__/focusManager.test.js +1 -1
  11. package/appUtils/focusManager/index.ts +72 -12
  12. package/appUtils/focusManagerAux/utils/index.ts +88 -3
  13. package/package.json +2 -2
  14. package/reactHooks/cell-click/__tests__/index.test.js +3 -0
  15. package/reactHooks/cell-click/index.ts +8 -1
  16. package/reactHooks/feed/__tests__/useBatchLoading.test.tsx +8 -2
  17. package/reactHooks/feed/__tests__/useFeedLoader.test.tsx +71 -31
  18. package/reactHooks/feed/index.ts +2 -0
  19. package/reactHooks/feed/useBatchLoading.ts +14 -9
  20. package/reactHooks/feed/useFeedLoader.tsx +36 -38
  21. package/reactHooks/feed/useLoadPipesDataDispatch.ts +57 -0
  22. package/reactHooks/navigation/useRoute.ts +7 -2
  23. package/reactHooks/navigation/useScreenStateStore.ts +8 -0
  24. package/reactHooks/state/index.ts +1 -1
  25. package/reactHooks/state/useHomeRiver.ts +4 -2
  26. package/screenPickerUtils/index.ts +7 -0
  27. package/storage/ScreenSingleValueProvider.ts +204 -0
  28. package/storage/ScreenStateMultiSelectProvider.ts +293 -0
  29. package/storage/StorageMultiSelectProvider.ts +192 -0
  30. package/storage/StorageSingleSelectProvider.ts +108 -0
  31. package/utils/__tests__/find.test.ts +36 -0
  32. package/utils/__tests__/pathOr.test.ts +37 -0
  33. package/utils/__tests__/startsWith.test.ts +30 -0
  34. package/utils/find.ts +3 -0
  35. package/utils/index.ts +6 -0
  36. package/utils/pathOr.ts +5 -0
  37. package/utils/startsWith.ts +9 -0
@@ -11,3 +11,5 @@ export { useBuildPipesUrl } from "./useBuildPipesUrl";
11
11
  export { usePipesCacheReset } from "./usePipesCacheReset";
12
12
 
13
13
  export { useBatchLoading } from "./useBatchLoading";
14
+
15
+ export { useLoadPipesDataDispatch } from "./useLoadPipesDataDispatch";
@@ -1,15 +1,12 @@
1
1
  import { complement, compose, isNil, map, min, prop, take, uniq } from "ramda";
2
2
  import * as React from "react";
3
- import {
4
- ZappPipes,
5
- useAppDispatch,
6
- useZappPipesFeed,
7
- } from "@applicaster/zapp-react-native-redux";
3
+ import { useZappPipesFeed } from "@applicaster/zapp-react-native-redux";
8
4
  import { isNilOrEmpty } from "../../reactUtils/helpers";
9
5
  import { ZappPipesSearchContext } from "@applicaster/zapp-react-native-ui-components/Contexts";
10
6
  import {
11
7
  getInflatedDataSourceUrl,
12
8
  getSearchContext,
9
+ useLoadPipesDataDispatch,
13
10
  } from "@applicaster/zapp-react-native-utils/reactHooks";
14
11
  import { isGallery } from "@applicaster/zapp-react-native-utils/componentsUtils";
15
12
  import { useScreenContext } from "../screen";
@@ -65,7 +62,6 @@ export const useBatchLoading = (
65
62
  componentsToRender: { data?: ZappDataSource; component_type: string }[],
66
63
  options: Options
67
64
  ) => {
68
- const dispatch = useAppDispatch();
69
65
  const { screen: screenContext, entry: entryContext } = useScreenContext();
70
66
  const [searchContext] = ZappPipesSearchContext.useZappPipesContext();
71
67
  const [hasEverBeenReady, setHasEverBeenReady] = React.useState(false);
@@ -122,6 +118,8 @@ export const useBatchLoading = (
122
118
 
123
119
  const feeds = useZappPipesFeed(feedUrls);
124
120
 
121
+ const loadPipesDataDispatcher = useLoadPipesDataDispatch();
122
+
125
123
  // dispatch loadPipesData for each feed that is not loaded
126
124
  const runBatchLoading = React.useCallback(() => {
127
125
  batchComponents.forEach((rawData: any) => {
@@ -140,13 +138,20 @@ export const useBatchLoading = (
140
138
 
141
139
  if (mappedFeedUrl) {
142
140
  // 4. load data
143
- return dispatch(
144
- ZappPipes.loadPipesData(mappedFeedUrl, { riverId: options.riverId })
141
+ return loadPipesDataDispatcher(
142
+ mappedFeedUrl,
143
+ {
144
+ riverId: options.riverId,
145
+ },
146
+ {
147
+ withResolvers: true,
148
+ withScreenRouteMapping: true,
149
+ }
145
150
  );
146
151
  }
147
152
  }
148
153
  });
149
- }, [feedUrls, feeds]);
154
+ }, [feedUrls, feeds, loadPipesDataDispatcher]);
150
155
 
151
156
  React.useEffect(() => {
152
157
  runBatchLoading();
@@ -1,15 +1,11 @@
1
1
  import React, { useEffect } from "react";
2
2
 
3
- import {
4
- ZappPipes,
5
- useAppDispatch,
6
- useZappPipesFeed,
7
- } from "@applicaster/zapp-react-native-redux";
3
+ import { useZappPipesFeed } from "@applicaster/zapp-react-native-redux";
8
4
 
9
5
  import { reactHooksLogger } from "../logger";
10
6
  import { shouldDispatchData, useIsInitialRender } from "../utils";
11
7
  import { useInflatedUrl } from "./useInflatedUrl";
12
- import { useRoute } from "../navigation";
8
+ import { useLoadPipesDataDispatch } from "./useLoadPipesDataDispatch";
13
9
 
14
10
  const logger = reactHooksLogger.addSubsystem("useFeedLoader");
15
11
 
@@ -51,29 +47,26 @@ export const useFeedLoader = ({
51
47
  }, []);
52
48
 
53
49
  const isInitialRender = useIsInitialRender();
54
- const dispatch = useAppDispatch();
55
- const { screenData } = useRoute();
56
50
 
57
51
  const callableFeedUrl = useInflatedUrl({ feedUrl, mapping });
58
52
 
59
53
  const currentFeed = useZappPipesFeed(callableFeedUrl);
60
54
 
61
- const riverId =
62
- (screenData as LegacyNavigationScreenData)?.targetScreen?.id ??
63
- screenData?.id;
55
+ const loadPipesDataDispatcher = useLoadPipesDataDispatch();
64
56
 
65
57
  const reloadData = React.useCallback<ReloadDataFunction>(
66
58
  (silentRefresh = true, callback) => {
67
- if (callableFeedUrl) {
68
- dispatch(
69
- ZappPipes.loadPipesData(callableFeedUrl, {
70
- clearCache: true,
71
- silentRefresh,
72
- callback,
73
- riverId,
74
- })
75
- );
76
- }
59
+ loadPipesDataDispatcher(
60
+ callableFeedUrl,
61
+ {
62
+ clearCache: true,
63
+ silentRefresh,
64
+ callback,
65
+ },
66
+ {
67
+ withResolvers: true,
68
+ }
69
+ );
77
70
  },
78
71
  [callableFeedUrl]
79
72
  );
@@ -82,15 +75,16 @@ export const useFeedLoader = ({
82
75
  if (callableFeedUrl) {
83
76
  const nextFeed = currentFeed?.data?.next;
84
77
 
85
- if (nextFeed) {
86
- dispatch(
87
- ZappPipes.loadPipesData(nextFeed, {
88
- silentRefresh: true,
89
- parentFeed: callableFeedUrl,
90
- riverId,
91
- })
92
- );
93
- }
78
+ loadPipesDataDispatcher(
79
+ nextFeed,
80
+ {
81
+ silentRefresh: true,
82
+ parentFeed: callableFeedUrl,
83
+ },
84
+ {
85
+ withResolvers: true,
86
+ }
87
+ );
94
88
  }
95
89
  }, [callableFeedUrl, currentFeed?.data?.next]);
96
90
 
@@ -99,12 +93,16 @@ export const useFeedLoader = ({
99
93
  shouldDispatchData(callableFeedUrl, currentFeed, pipesOptions.clearCache)
100
94
  ) {
101
95
  if (callableFeedUrl && !pipesOptions.skipLoading) {
102
- dispatch(
103
- ZappPipes.loadPipesData(callableFeedUrl, {
96
+ loadPipesDataDispatcher(
97
+ callableFeedUrl,
98
+ {
104
99
  ...pipesOptions,
105
100
  clearCache: true,
106
- riverId,
107
- })
101
+ },
102
+ {
103
+ withResolvers: true,
104
+ withScreenRouteMapping: true,
105
+ }
108
106
  );
109
107
  } else if (!callableFeedUrl) {
110
108
  logger.info({
@@ -133,11 +131,11 @@ export const useFeedLoader = ({
133
131
  // Reload feed when feedUrl changes, unless skipLoading is true
134
132
  useEffect(() => {
135
133
  if (!isInitialRender && callableFeedUrl && !pipesOptions.skipLoading) {
136
- dispatch(
137
- ZappPipes.loadPipesData(callableFeedUrl, { ...pipesOptions, riverId })
138
- );
134
+ loadPipesDataDispatcher(callableFeedUrl, pipesOptions, {
135
+ withResolvers: true,
136
+ });
139
137
  }
140
- }, [callableFeedUrl]);
138
+ }, [callableFeedUrl, isInitialRender, pipesOptions.skipLoading]);
141
139
 
142
140
  return React.useMemo(() => {
143
141
  if (!callableFeedUrl || !feedUrl) {
@@ -0,0 +1,57 @@
1
+ import {
2
+ useAppDispatch,
3
+ ZappPipes,
4
+ } from "@applicaster/zapp-react-native-redux";
5
+ import { applyScreenRouteDefaults } from "@applicaster/zapp-react-native-redux/ZappPipes/feedProcessor";
6
+ import React from "react";
7
+ import { useScreenResolvers } from "../../actionsExecutor/screenResolver";
8
+ import { useRoute } from "../navigation";
9
+ import { useScreenStateStore } from "../navigation/useScreenStateStore";
10
+
11
+ export const useLoadPipesDataDispatch = () => {
12
+ const screenStateStore = useScreenStateStore();
13
+ const resolvers = useScreenResolvers();
14
+ const dispatch = useAppDispatch();
15
+
16
+ const { pathname, screenData } = useRoute();
17
+
18
+ const riverId =
19
+ (screenData as LegacyNavigationScreenData)?.targetScreen?.id ??
20
+ screenData?.id;
21
+
22
+ const onLoadCB = (options) => (data, _err) => {
23
+ options?.callback?.();
24
+
25
+ if (data) {
26
+ applyScreenRouteDefaults(data, screenStateStore, pathname);
27
+ }
28
+ };
29
+
30
+ return React.useCallback(
31
+ (
32
+ url: string,
33
+ options = {},
34
+ {
35
+ withResolvers = false,
36
+ withScreenRouteMapping = false,
37
+ }: {
38
+ withResolvers?: boolean;
39
+ withScreenRouteMapping?: boolean;
40
+ } = {}
41
+ ) => {
42
+ if (url) {
43
+ dispatch(
44
+ ZappPipes.loadPipesData(url, {
45
+ riverId,
46
+ resolvers: withResolvers ? resolvers : undefined,
47
+ ...options,
48
+ callback: withScreenRouteMapping
49
+ ? onLoadCB(options)
50
+ : options?.callback,
51
+ })
52
+ );
53
+ }
54
+ },
55
+ []
56
+ );
57
+ };
@@ -28,14 +28,19 @@ const isHookPathname = (pathname: string) => /^\/hooks\//.test(pathname);
28
28
 
29
29
  type VariousScreenData = LegacyNavigationScreenData | ZappRiver | ZappEntry;
30
30
 
31
- export const useRoute = (): {
31
+ export const useRoute = (
32
+ useLegacy = true
33
+ ): {
32
34
  screenData: VariousScreenData;
33
35
  pathname: string;
34
36
  } => {
35
37
  const pathname = usePathname() || "";
36
38
  const navigator = useNavigation();
39
+ const screenContext = useContext(ScreenDataContext);
37
40
 
38
- const screenDataContext = legacyScreenData(useContext(ScreenDataContext));
41
+ const screenDataContext = useLegacy
42
+ ? legacyScreenData(screenContext)
43
+ : screenContext;
39
44
 
40
45
  const { plugins, contentTypes, rivers } = usePickFromState([
41
46
  "plugins",
@@ -0,0 +1,8 @@
1
+ import { useScreenContextV2 } from "../screen/useScreenContext";
2
+ import { useMemo } from "react";
3
+
4
+ export const useScreenStateStore = () => {
5
+ const _stateStore = useScreenContextV2()._stateStore;
6
+
7
+ return useMemo(() => _stateStore, []);
8
+ };
@@ -1,5 +1,5 @@
1
1
  export { useRivers } from "./useRivers";
2
2
 
3
- export { useHomeRiver } from "./useHomeRiver";
3
+ export { useHomeRiver, getHomeRiver } from "./useHomeRiver";
4
4
 
5
5
  export { ZStoreProvider, useZStore } from "./ZStoreProvider";
@@ -1,8 +1,10 @@
1
- import * as R from "ramda";
2
1
  import { useRivers } from "./useRivers";
3
2
 
3
+ export const getHomeRiver = (rivers: Record<string, ZappRiver>) =>
4
+ Object.values(rivers).find((river: ZappRiver) => river.home);
5
+
4
6
  export const useHomeRiver = () => {
5
7
  const rivers = useRivers();
6
8
 
7
- return R.compose(R.find(R.propEq("home", true)), R.values)(rivers);
9
+ return getHomeRiver(rivers);
8
10
  };
@@ -0,0 +1,7 @@
1
+ export const getFocusableId = (id) => `PickerItem.${id}`;
2
+
3
+ export const getPickerSelectorId = (id) => `PickerSelector.${id}`;
4
+
5
+ export const SCREEN_PICKER_CONTAINER = "ScreenPickerContainer";
6
+
7
+ export const getScreenPickerId = (id) => `${SCREEN_PICKER_CONTAINER}.${id}`;
@@ -0,0 +1,204 @@
1
+ import { BehaviorSubject } from "rxjs";
2
+ import { SingleValueProvider } from "./StorageSingleSelectProvider";
3
+ import { createLogger } from "../logger";
4
+ import { bridgeLogger } from "../../zapp-react-native-bridge/logger";
5
+ import { useScreenStateStore } from "../reactHooks/navigation/useScreenStateStore";
6
+
7
+ export const { log_debug, log_error } = createLogger({
8
+ category: "ScreenSingleValueProvider",
9
+ subsystem: "zapp-react-native-bridge",
10
+ parent: bridgeLogger,
11
+ });
12
+
13
+ export class ScreenSingleValueProvider implements SingleValueProvider {
14
+ // @ts-ignore
15
+ private valueSubject: BehaviorSubject<string | null>;
16
+ private screenStateStoreUnsubscribe: (() => void) | null = null;
17
+
18
+ private static providers: Record<
19
+ string,
20
+ Record<string, SingleValueProvider>
21
+ > = {};
22
+
23
+ public static getProvider(
24
+ key: string,
25
+ screenRoute: string,
26
+ screenStateStore: ReturnType<typeof useScreenStateStore>
27
+ ): SingleValueProvider {
28
+ if (!key) {
29
+ throw new Error("ScreenSingleValueProvider: Key is required");
30
+ }
31
+
32
+ if (!screenRoute) {
33
+ throw new Error("ScreenSingleValueProvider: Screen route is required");
34
+ }
35
+
36
+ if (!screenStateStore) {
37
+ throw new Error(
38
+ "ScreenSingleValueProvider: screen state store is required"
39
+ );
40
+ }
41
+
42
+ if (!this.providers[screenRoute]) {
43
+ this.providers[screenRoute] = {};
44
+ }
45
+
46
+ try {
47
+ if (!this.providers[screenRoute][key]) {
48
+ this.providers[screenRoute][key] = new ScreenSingleValueProvider(
49
+ key,
50
+ screenRoute,
51
+ screenStateStore
52
+ );
53
+ } else {
54
+ (
55
+ this.providers[screenRoute][key] as ScreenSingleValueProvider
56
+ ).updateStore(screenStateStore);
57
+ }
58
+ } catch (error) {
59
+ log_error("Failed to create provider", { key, screenRoute, error });
60
+ throw error;
61
+ }
62
+
63
+ return this.providers[screenRoute][key];
64
+ }
65
+
66
+ // @ts-ignore
67
+ private constructor(
68
+ private key: string,
69
+ route: string,
70
+ private screenStateStore: ReturnType<typeof useScreenStateStore>
71
+ ) {
72
+ if (!key) {
73
+ throw new Error("ScreenSingleValueProvider: Key is required");
74
+ }
75
+
76
+ if (!screenStateStore) {
77
+ throw new Error(
78
+ "ScreenSingleValueProvider: screen state store is required"
79
+ );
80
+ }
81
+
82
+ this.key = key;
83
+ this.valueSubject = new BehaviorSubject<string | null>(null);
84
+
85
+ this.setupScreenStateSubscription();
86
+
87
+ void this.getValueAsync();
88
+
89
+ log_debug("ScreenSingleValueProvider: Initializing", { key, route });
90
+ }
91
+
92
+ private updateStore(
93
+ screenStateStore: ReturnType<typeof useScreenStateStore>
94
+ ): void {
95
+ this.cleanup();
96
+ this.screenStateStore = screenStateStore;
97
+ this.setupScreenStateSubscription();
98
+ void this.getValueAsync();
99
+ }
100
+
101
+ private setupScreenStateSubscription(): void {
102
+ this.screenStateStoreUnsubscribe = this.screenStateStore.subscribe(
103
+ (state) => ({
104
+ value: state.data[this.key],
105
+ exists: this.key in state.data,
106
+ })
107
+ // (current, previous) => {
108
+ // if (!current.exists && previous.exists) {
109
+ // log_debug(
110
+ // `ScreenSingleValueProvider: Key deleted from store: ${this.key}`
111
+ // );
112
+
113
+ // // TODO: If we need to handle deletion, we can do it here
114
+ // }
115
+
116
+ // if (current.value !== previous.value) {
117
+ // this.valueSubject.next(current.value || null);
118
+
119
+ // log_debug(`ScreenSingleValueProvider: Key updated: ${this.key}`, {
120
+ // previous: previous.value,
121
+ // current: current.value,
122
+ // });
123
+ // }
124
+ // }
125
+ );
126
+
127
+ log_debug(
128
+ `ScreenSingleValueProvider: screen state store subscription setup for key: ${this.key}`
129
+ );
130
+ }
131
+
132
+ private cleanup(): void {
133
+ if (this.screenStateStoreUnsubscribe) {
134
+ this.screenStateStoreUnsubscribe();
135
+ this.screenStateStoreUnsubscribe = null;
136
+
137
+ log_debug(
138
+ `ScreenSingleValueProvider: Unsubscribed from screen state store for key: ${this.key}`
139
+ );
140
+ }
141
+ }
142
+
143
+ clearValue(): Promise<void> {
144
+ this.screenStateStore.getState().removeValue(this.key);
145
+ this.valueSubject.next(null);
146
+
147
+ log_debug(`clearValue: value cleared for key: ${this.key}`);
148
+
149
+ return Promise.resolve();
150
+ }
151
+
152
+ getObservable(): BehaviorSubject<string | null> {
153
+ return this.valueSubject;
154
+ }
155
+
156
+ getValue(): string | null {
157
+ return this.valueSubject.value;
158
+ }
159
+
160
+ getValueAsync(): Promise<string | null> {
161
+ const currentValue = this.screenStateStore.getState().data[this.key];
162
+ const value = currentValue || null;
163
+ const selected = this.getValue();
164
+ const valuesAreEqual = value === selected;
165
+
166
+ const bothEmpty =
167
+ (value === null || value === "") &&
168
+ (selected === null || selected === "");
169
+
170
+ if (!this.valueSubject.closed && !valuesAreEqual && !bothEmpty) {
171
+ this.valueSubject.next(value);
172
+ }
173
+
174
+ return Promise.resolve(value);
175
+ }
176
+
177
+ setValue(value: string): Promise<void> {
178
+ this.screenStateStore.getState().setValue(this.key, value);
179
+ this.valueSubject.next(value);
180
+
181
+ log_debug(`setValue: value set for key: ${this.key}`, { value });
182
+
183
+ return Promise.resolve();
184
+ }
185
+
186
+ // TODO: remove if not needed
187
+
188
+ public static cleanupRoute(screenRoute: string): void {
189
+ if (this.providers[screenRoute]) {
190
+ Object.values(this.providers[screenRoute]).forEach((provider) => {
191
+ (provider as ScreenSingleValueProvider).destroy();
192
+ });
193
+
194
+ delete this.providers[screenRoute];
195
+ }
196
+ }
197
+
198
+ // TODO: remove if not needed
199
+
200
+ public destroy(): void {
201
+ this.valueSubject.complete();
202
+ this.cleanup();
203
+ }
204
+ }