@applicaster/zapp-react-native-utils 15.0.0-rc.12 → 15.0.0-rc.120
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 +0 -6
- package/actionsExecutor/ActionExecutorContext.tsx +3 -6
- package/actionsExecutor/feedDecorator.ts +6 -6
- package/adsUtils/__tests__/createVMAP.test.ts +419 -0
- package/adsUtils/index.ts +2 -2
- package/analyticsUtils/README.md +1 -1
- package/analyticsUtils/analyticsMapper.ts +10 -2
- package/appDataUtils/__tests__/urlScheme.test.ts +678 -0
- package/appUtils/HooksManager/__tests__/__snapshots__/hooksManager.test.js.snap +0 -188
- package/appUtils/HooksManager/__tests__/hooksManager.test.js +16 -2
- package/appUtils/HooksManager/index.ts +10 -10
- package/appUtils/RiverFocusManager/{index.js → index.ts} +25 -18
- package/appUtils/accessibilityManager/__tests__/utils.test.ts +360 -0
- package/appUtils/accessibilityManager/const.ts +4 -0
- package/appUtils/accessibilityManager/hooks.ts +20 -13
- package/appUtils/accessibilityManager/index.ts +28 -1
- package/appUtils/accessibilityManager/utils.ts +59 -8
- package/appUtils/contextKeysManager/__tests__/getKeys/failure.test.ts +7 -2
- package/appUtils/contextKeysManager/__tests__/getKeys/success.test.ts +48 -0
- package/appUtils/contextKeysManager/contextResolver.ts +51 -22
- package/appUtils/contextKeysManager/index.ts +65 -10
- package/appUtils/focusManager/__tests__/__snapshots__/focusManager.test.js.snap +4 -0
- package/appUtils/focusManager/index.ios.ts +59 -3
- package/appUtils/focusManager/treeDataStructure/Tree/__tests__/Tree.test.js +46 -0
- package/appUtils/focusManager/treeDataStructure/Tree/index.js +18 -18
- package/appUtils/focusManagerAux/utils/index.ios.ts +122 -0
- package/appUtils/focusManagerAux/utils/index.ts +21 -5
- package/appUtils/focusManagerAux/utils/utils.ios.ts +234 -0
- package/appUtils/keyCodes/keys/keys.web.ts +1 -4
- package/appUtils/localizationsHelper.ts +4 -0
- package/appUtils/orientationHelper.ts +2 -4
- package/appUtils/platform/platformUtils.ts +117 -18
- package/appUtils/playerManager/OverlayObserver/OverlaysObserver.ts +94 -4
- package/appUtils/playerManager/OverlayObserver/utils.ts +32 -20
- package/appUtils/playerManager/player.ts +4 -0
- package/appUtils/playerManager/playerNative.ts +31 -17
- package/appUtils/playerManager/usePlayerState.tsx +14 -2
- package/arrayUtils/__tests__/allTruthy.test.ts +24 -0
- package/arrayUtils/__tests__/anyThruthy.test.ts +24 -0
- package/arrayUtils/index.ts +5 -0
- package/cellUtils/index.ts +32 -0
- package/cloudEventsUtils/__tests__/index.test.ts +529 -0
- package/cloudEventsUtils/index.ts +65 -1
- package/configurationUtils/__tests__/imageSrcFromMediaItem.test.ts +38 -0
- package/configurationUtils/__tests__/manifestKeyParser.test.ts +26 -26
- package/configurationUtils/index.ts +17 -11
- package/dateUtils/__tests__/dayjs.test.ts +330 -0
- package/enumUtils/__tests__/getEnumKeyByEnumValue.test.ts +207 -0
- package/errorUtils/__tests__/GeneralError.test.ts +97 -0
- package/errorUtils/__tests__/HttpStatusCode.test.ts +344 -0
- package/errorUtils/__tests__/MissingPluginError.test.ts +113 -0
- package/errorUtils/__tests__/NetworkError.test.ts +202 -0
- package/errorUtils/__tests__/getParsedResponse.test.ts +188 -0
- package/errorUtils/__tests__/invariant.test.ts +112 -0
- package/focusManager/aux/index.ts +1 -1
- package/headersUtils/__tests__/headersUtils.test.js +11 -1
- package/headersUtils/index.ts +2 -1
- package/manifestUtils/defaultManifestConfigurations/player.js +115 -11
- package/manifestUtils/keys.js +21 -0
- package/manifestUtils/platformIsTV.js +13 -0
- package/manifestUtils/sharedConfiguration/screenPicker/utils.js +1 -0
- package/manifestUtils/tvAction/container/index.js +1 -1
- package/navigationUtils/index.ts +15 -5
- package/numberUtils/__tests__/toNumber.test.ts +27 -0
- package/numberUtils/__tests__/toPositiveNumber.test.ts +193 -0
- package/numberUtils/index.ts +23 -1
- package/package.json +4 -4
- package/playerUtils/usePlayerTTS.ts +8 -3
- package/pluginUtils/index.ts +4 -0
- package/reactHooks/advertising/index.ts +2 -2
- package/reactHooks/analytics/__tests__/useSendAnalyticsOnPress.test.ts +537 -0
- package/reactHooks/app/__tests__/useAppState.test.ts +1 -1
- package/reactHooks/autoscrolling/__tests__/useTrackCurrentAutoScrollingElement.test.ts +1 -1
- package/reactHooks/autoscrolling/__tests__/useTrackedView.test.tsx +1 -2
- package/reactHooks/cell-click/__tests__/index.test.js +1 -3
- package/reactHooks/configuration/__tests__/index.test.tsx +1 -1
- package/reactHooks/connection/__tests__/index.test.js +1 -1
- package/reactHooks/debugging/__tests__/index.test.js +4 -4
- package/reactHooks/dev/__tests__/useReRenderLog.test.ts +188 -0
- package/reactHooks/device/useIsTablet.tsx +14 -19
- package/reactHooks/device/useMemoizedIsTablet.ts +3 -3
- package/reactHooks/events/index.ts +20 -0
- package/reactHooks/feed/__tests__/useBatchLoading.test.tsx +32 -23
- package/reactHooks/feed/__tests__/useBuildPipesUrl.test.tsx +19 -19
- package/reactHooks/feed/__tests__/useEntryScreenId.test.tsx +4 -1
- package/reactHooks/feed/__tests__/useFeedLoader.test.tsx +42 -30
- package/reactHooks/feed/__tests__/{useInflatedUrl.test.ts → useInflatedUrl.test.tsx} +62 -7
- package/reactHooks/feed/index.ts +0 -2
- package/reactHooks/feed/useBatchLoading.ts +7 -1
- package/reactHooks/feed/useEntryScreenId.ts +2 -2
- package/reactHooks/feed/useInflatedUrl.ts +44 -18
- package/reactHooks/feed/usePipesCacheReset.ts +3 -1
- package/reactHooks/flatList/useLoadNextPageIfNeeded.ts +13 -16
- package/reactHooks/hookModal/hooks/useHookModalScreenData.ts +12 -8
- package/reactHooks/index.ts +2 -0
- package/reactHooks/layout/__tests__/index.test.tsx +1 -1
- package/reactHooks/layout/__tests__/useLayoutVersion.test.tsx +1 -1
- package/reactHooks/layout/index.ts +1 -1
- package/reactHooks/layout/useDimensions/__tests__/{useDimensions.test.ts → useDimensions.test.tsx} +105 -25
- package/reactHooks/layout/useDimensions/useDimensions.ts +2 -2
- package/reactHooks/navigation/__tests__/index.test.tsx +40 -9
- package/reactHooks/navigation/index.ts +27 -11
- package/reactHooks/navigation/useRoute.ts +11 -7
- package/reactHooks/player/TVSeekControlller/TVSeekController.ts +27 -10
- package/reactHooks/player/__tests__/useAutoSeek._test.tsx +1 -1
- package/reactHooks/player/__tests__/useTapSeek._test.ts +1 -1
- package/reactHooks/resolvers/__tests__/useCellResolver.test.tsx +1 -1
- package/reactHooks/resolvers/__tests__/useComponentResolver.test.tsx +1 -1
- package/reactHooks/resolvers/useCellResolver.ts +6 -2
- package/reactHooks/resolvers/useComponentResolver.ts +19 -3
- package/reactHooks/screen/__tests__/useCurrentScreenData.test.tsx +2 -2
- package/reactHooks/screen/__tests__/useScreenBackgroundColor.test.tsx +1 -1
- package/reactHooks/screen/__tests__/useScreenData.test.tsx +1 -1
- package/reactHooks/screen/__tests__/useTargetScreenData.test.tsx +12 -4
- package/reactHooks/screen/useTargetScreenData.ts +4 -2
- package/reactHooks/state/__tests__/useComponentScreenState.test.ts +246 -0
- package/reactHooks/state/index.ts +2 -0
- package/reactHooks/state/useComponentScreenState.ts +45 -0
- package/reactHooks/state/useRefWithInitialValue.ts +10 -0
- package/reactHooks/state/useRivers.ts +1 -1
- package/reactHooks/ui/__tests__/useFadeOutWhenBlurred.test.ts +580 -0
- package/reactHooks/usePluginConfiguration.ts +2 -2
- package/reactHooks/utils/__tests__/index.test.js +1 -1
- package/rectUtils/__tests__/index.test.ts +549 -0
- package/rectUtils/index.ts +2 -2
- package/refreshUtils/RefreshCoordinator/__tests__/refreshCoordinator.test.ts +161 -0
- package/refreshUtils/RefreshCoordinator/index.ts +216 -0
- package/refreshUtils/RefreshCoordinator/utils/__tests__/getDataRefreshConfig.test.ts +104 -0
- package/refreshUtils/RefreshCoordinator/utils/index.ts +29 -0
- package/screenPickerUtils/__tests__/index.test.ts +333 -0
- package/screenPickerUtils/index.ts +5 -0
- package/screenState/__tests__/index.test.ts +1 -1
- package/screenUtils/index.ts +3 -0
- package/searchUtils/const.ts +7 -0
- package/searchUtils/index.ts +3 -0
- package/services/storageServiceSync.web.ts +1 -1
- package/stringUtils/index.ts +1 -1
- package/testUtils/index.tsx +30 -21
- package/time/__tests__/BackgroundTimer.test.ts +156 -0
- package/time/__tests__/Timer.test.ts +236 -0
- package/typeGuards/__tests__/isString.test.ts +21 -0
- package/typeGuards/index.ts +4 -0
- package/utils/__tests__/clone.test.ts +158 -0
- package/utils/__tests__/mapAccum.test.ts +73 -0
- package/utils/__tests__/mergeRight.test.ts +48 -0
- package/utils/__tests__/path.test.ts +7 -0
- package/utils/__tests__/selectors.test.ts +124 -0
- package/utils/clone.ts +7 -0
- package/utils/index.ts +22 -1
- package/utils/mapAccum.ts +23 -0
- package/utils/mergeRight.ts +5 -0
- package/utils/path.ts +6 -3
- package/utils/pathOr.ts +5 -1
- package/utils/selectors.ts +46 -0
- package/zappFrameworkUtils/HookCallback/callbackNavigationAction.ts +49 -12
- package/zappFrameworkUtils/HookCallback/hookCallbackManifestExtensions.config.js +1 -1
- package/reactHooks/componentsMap/index.ts +0 -55
- package/reactHooks/feed/__tests__/useFeedRefresh.test.tsx +0 -75
- package/reactHooks/feed/useFeedRefresh.tsx +0 -65
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
2
2
|
import { BackHandler } from "react-native";
|
|
3
|
+
import { shallow } from "zustand/shallow";
|
|
3
4
|
|
|
4
5
|
import {
|
|
5
6
|
useContentTypes,
|
|
6
|
-
|
|
7
|
+
usePlugins,
|
|
7
8
|
} from "@applicaster/zapp-react-native-redux/hooks";
|
|
8
9
|
import { HooksManager } from "@applicaster/zapp-react-native-utils/appUtils/HooksManager";
|
|
9
10
|
|
|
10
11
|
import { LONG_KEY_PRESS_TIMEOUT } from "@applicaster/quick-brick-core/const";
|
|
11
|
-
import {
|
|
12
|
-
import { HookModalContextT } from "@applicaster/zapp-react-native-ui-components/Contexts/ZappHookModalContext";
|
|
12
|
+
import { useZappHookModalStore } from "@applicaster/zapp-react-native-ui-components/Contexts/ZappHookModalContext";
|
|
13
13
|
import { HOOKS_EVENTS } from "../../appUtils/HooksManager/constants";
|
|
14
14
|
import { getRiverFromRoute, getTargetRoute } from "../../navigationUtils";
|
|
15
15
|
import { useConnectionInfo } from "../connection";
|
|
16
16
|
|
|
17
17
|
import { isTV, isWeb } from "@applicaster/zapp-react-native-utils/reactUtils";
|
|
18
|
+
import { pick } from "@applicaster/zapp-react-native-utils/utils";
|
|
18
19
|
import { useNavbarState } from "../screen";
|
|
20
|
+
import { useRivers } from "../state";
|
|
21
|
+
import { useLayoutVersion } from "../layout";
|
|
19
22
|
|
|
20
23
|
export { useNavigation } from "./useNavigation";
|
|
21
24
|
|
|
@@ -86,6 +89,10 @@ export function isNavBarVisible(
|
|
|
86
89
|
if (route.startsWith("/hooks/")) {
|
|
87
90
|
const module = screenData?.module;
|
|
88
91
|
|
|
92
|
+
if (module === undefined) {
|
|
93
|
+
return showNavBar;
|
|
94
|
+
}
|
|
95
|
+
|
|
89
96
|
if (module?.presentFullScreen) {
|
|
90
97
|
return false;
|
|
91
98
|
}
|
|
@@ -162,13 +169,22 @@ export const useZappHooksForEntry = (
|
|
|
162
169
|
setIsHooksExecutionInProgress,
|
|
163
170
|
setIsPresentingFullScreen,
|
|
164
171
|
setIsRunningInBackground,
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
+
} = useZappHookModalStore(
|
|
173
|
+
(state) =>
|
|
174
|
+
pick(state, [
|
|
175
|
+
"setState",
|
|
176
|
+
"resetState",
|
|
177
|
+
"setIsHooksExecutionInProgress",
|
|
178
|
+
"setIsPresentingFullScreen",
|
|
179
|
+
"setIsRunningInBackground",
|
|
180
|
+
]),
|
|
181
|
+
shallow
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
const plugins = usePlugins();
|
|
185
|
+
const rivers = useRivers();
|
|
186
|
+
|
|
187
|
+
const layoutVersion = useLayoutVersion();
|
|
172
188
|
|
|
173
189
|
const contentTypes = useContentTypes();
|
|
174
190
|
|
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
|
|
4
4
|
import { useContext } from "react";
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
useContentTypes,
|
|
8
|
+
usePlugins,
|
|
9
|
+
} from "@applicaster/zapp-react-native-redux/hooks";
|
|
7
10
|
|
|
8
11
|
import { legacyScreenData } from "@applicaster/quick-brick-core/App/NavigationProvider/utils";
|
|
9
12
|
|
|
@@ -14,6 +17,7 @@ import { useNavigation } from "./useNavigation";
|
|
|
14
17
|
import { useModalStoreState } from "../../modalState";
|
|
15
18
|
import { ScreenDataContext } from "@applicaster/zapp-react-native-ui-components/Contexts/ScreenDataContext";
|
|
16
19
|
import { usePathname } from "./usePathname";
|
|
20
|
+
import { useRivers } from "../state";
|
|
17
21
|
|
|
18
22
|
// starts with modal/
|
|
19
23
|
const isModalPathname = (pathname: string) => /^modal\//.test(pathname);
|
|
@@ -42,17 +46,17 @@ export const useRoute = (
|
|
|
42
46
|
? legacyScreenData(screenContext)
|
|
43
47
|
: screenContext;
|
|
44
48
|
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"contentTypes",
|
|
49
|
-
]);
|
|
49
|
+
const plugins = usePlugins();
|
|
50
|
+
const rivers = useRivers();
|
|
51
|
+
const contentTypes = useContentTypes();
|
|
50
52
|
|
|
51
53
|
const modalState = useModalStoreState();
|
|
52
54
|
|
|
53
55
|
const modalScreenData = modalState.screen;
|
|
54
56
|
|
|
55
|
-
const hookModalScreenData = useHookModalScreenData(
|
|
57
|
+
const hookModalScreenData = useHookModalScreenData(
|
|
58
|
+
!isHookModalPathname(pathname)
|
|
59
|
+
);
|
|
56
60
|
|
|
57
61
|
const videoModalScreenData =
|
|
58
62
|
navigator?.videoModalState?.item &&
|
|
@@ -147,17 +147,34 @@ export class TVSeekController
|
|
|
147
147
|
|
|
148
148
|
let targetPos = currentPos;
|
|
149
149
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
150
|
+
const isLive = this.playerController.isLive();
|
|
151
|
+
|
|
152
|
+
if (isLive) {
|
|
153
|
+
if (this.currentSeekType === SEEK_TYPE.REWIND) {
|
|
154
|
+
targetPos = Math.min(
|
|
155
|
+
currentPos + offset,
|
|
156
|
+
this.playerController.getSeekableDuration()
|
|
157
|
+
);
|
|
158
|
+
} else if (this.currentSeekType === SEEK_TYPE.FORWARD) {
|
|
159
|
+
targetPos = Math.max(0, currentPos - offset);
|
|
160
|
+
} else {
|
|
161
|
+
log_warning(
|
|
162
|
+
`TVSeekController: handleDelayedSeek - invalid seek type: ${this.currentSeekType}`
|
|
163
|
+
);
|
|
164
|
+
}
|
|
157
165
|
} else {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
166
|
+
if (this.currentSeekType === SEEK_TYPE.FORWARD) {
|
|
167
|
+
targetPos = Math.min(
|
|
168
|
+
currentPos + offset,
|
|
169
|
+
this.playerController.getSeekableDuration()
|
|
170
|
+
);
|
|
171
|
+
} else if (this.currentSeekType === SEEK_TYPE.REWIND) {
|
|
172
|
+
targetPos = Math.max(0, currentPos - offset);
|
|
173
|
+
} else {
|
|
174
|
+
log_warning(
|
|
175
|
+
`TVSeekController: handleDelayedSeek - invalid seek type: ${this.currentSeekType}`
|
|
176
|
+
);
|
|
177
|
+
}
|
|
161
178
|
}
|
|
162
179
|
|
|
163
180
|
log_debug(
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { act, renderHook } from "@testing-library/react-
|
|
1
|
+
import { act, renderHook } from "@testing-library/react-native";
|
|
2
2
|
import { playerManager } from "@applicaster/zapp-react-native-utils/appUtils/playerManager";
|
|
3
3
|
|
|
4
4
|
import { ON_HOLD_INTERVAL, SEEK_TYPE, SKIP_TIME_BASE } from "../const";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { View } from "react-native";
|
|
3
|
-
import { cleanup, renderHook } from "@testing-library/react-
|
|
3
|
+
import { cleanup, renderHook } from "@testing-library/react-native";
|
|
4
4
|
import configureStore from "redux-mock-store";
|
|
5
5
|
import { Provider } from "react-redux";
|
|
6
6
|
|
|
@@ -3,7 +3,10 @@ import memoizee from "memoizee";
|
|
|
3
3
|
import * as R from "ramda";
|
|
4
4
|
|
|
5
5
|
import { CellRendererResolver } from "@applicaster/zapp-react-native-ui-components/Components/CellRendererResolver";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
usePlugins,
|
|
8
|
+
useCellStyles,
|
|
9
|
+
} from "@applicaster/zapp-react-native-redux";
|
|
7
10
|
import { useDimensions } from "../layout";
|
|
8
11
|
import { useIsRTL } from "../../localizationUtils";
|
|
9
12
|
|
|
@@ -53,7 +56,8 @@ export function useCellResolver({
|
|
|
53
56
|
updateForInactiveScreens: false,
|
|
54
57
|
});
|
|
55
58
|
|
|
56
|
-
const
|
|
59
|
+
const plugins = usePlugins();
|
|
60
|
+
const cellStyles = useCellStyles();
|
|
57
61
|
const isRTL = useIsRTL();
|
|
58
62
|
|
|
59
63
|
const options = {
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
/// <reference types="@applicaster/applicaster-types" />
|
|
2
2
|
import * as React from "react";
|
|
3
3
|
|
|
4
|
+
import { coreLogger } from "@applicaster/zapp-react-native-utils/logger";
|
|
5
|
+
|
|
4
6
|
import { findComponentByType } from "@applicaster/zapp-react-native-utils/pluginUtils";
|
|
5
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
usePlugins,
|
|
9
|
+
useAppSelector,
|
|
10
|
+
selectComponents,
|
|
11
|
+
} from "@applicaster/zapp-react-native-redux";
|
|
6
12
|
|
|
7
13
|
type Decorator = (component: React.Component<any>) => React.Component<any>;
|
|
8
14
|
type Watcher = Array<any>;
|
|
@@ -15,9 +21,11 @@ export function useComponentResolver(
|
|
|
15
21
|
{ componentType, decorators }: Props,
|
|
16
22
|
watchers?: Watcher
|
|
17
23
|
): React.ComponentType<any> {
|
|
18
|
-
const
|
|
24
|
+
const plugins = usePlugins();
|
|
25
|
+
|
|
26
|
+
const components = useAppSelector(selectComponents);
|
|
19
27
|
|
|
20
|
-
|
|
28
|
+
const component = React.useMemo(
|
|
21
29
|
() =>
|
|
22
30
|
findComponentByType({
|
|
23
31
|
components,
|
|
@@ -27,4 +35,12 @@ export function useComponentResolver(
|
|
|
27
35
|
}),
|
|
28
36
|
watchers
|
|
29
37
|
);
|
|
38
|
+
|
|
39
|
+
if (!component) {
|
|
40
|
+
coreLogger.warn({
|
|
41
|
+
message: `useComponentResolver: Component of type ${componentType} not found`,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return component;
|
|
30
46
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { Provider } from "react-redux";
|
|
3
|
-
import { renderHook } from "@testing-library/react-
|
|
3
|
+
import { renderHook } from "@testing-library/react-native";
|
|
4
4
|
import configureStore from "redux-mock-store";
|
|
5
|
-
import thunk from "redux-thunk";
|
|
5
|
+
import { thunk } from "redux-thunk";
|
|
6
6
|
|
|
7
7
|
const mockStore = configureStore([thunk]);
|
|
8
8
|
|
|
@@ -1,16 +1,24 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { Provider } from "react-redux";
|
|
3
|
-
import { renderHook } from "@testing-library/react-
|
|
3
|
+
import { renderHook } from "@testing-library/react-native";
|
|
4
4
|
import configureStore from "redux-mock-store";
|
|
5
|
-
import thunk from "redux-thunk";
|
|
5
|
+
import { thunk } from "redux-thunk";
|
|
6
6
|
|
|
7
7
|
const mockStore = configureStore([thunk]);
|
|
8
8
|
|
|
9
9
|
import { useTargetScreenData } from "../useTargetScreenData";
|
|
10
10
|
|
|
11
11
|
describe("useTargetScreenData", function () {
|
|
12
|
-
const river_id_2 = {
|
|
13
|
-
|
|
12
|
+
const river_id_2 = {
|
|
13
|
+
id: "river_id_2",
|
|
14
|
+
type: "any",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const river_id_1 = {
|
|
18
|
+
id: "river_id_1",
|
|
19
|
+
type: "any",
|
|
20
|
+
};
|
|
21
|
+
|
|
14
22
|
const screenId = "river_id_2";
|
|
15
23
|
const entry = { id: "test", type: { value: "video" } };
|
|
16
24
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import {
|
|
2
|
+
import { useContentTypes } from "@applicaster/zapp-react-native-redux/hooks";
|
|
3
3
|
import * as R from "ramda";
|
|
4
4
|
import { appStore } from "@applicaster/zapp-react-native-redux/AppStore";
|
|
5
|
+
import { useRivers } from "../state";
|
|
5
6
|
|
|
6
7
|
export function getTargetScreenData(
|
|
7
8
|
entry: ZappEntry,
|
|
@@ -43,7 +44,8 @@ export function getTargetScreenDataFromEntry(entry: ZappEntry): ZappRiver {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
export const useTargetScreenData = (entry: ZappEntry) => {
|
|
46
|
-
const
|
|
47
|
+
const rivers = useRivers();
|
|
48
|
+
const contentTypes = useContentTypes();
|
|
47
49
|
|
|
48
50
|
return React.useMemo(
|
|
49
51
|
() => getTargetScreenData(entry, rivers, contentTypes),
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { renderHook, act } from "@testing-library/react-native";
|
|
2
|
+
import { useComponentScreenState } from "../useComponentScreenState";
|
|
3
|
+
import { useScreenContextV2 } from "@applicaster/zapp-react-native-utils/reactHooks/screen/useScreenContext";
|
|
4
|
+
|
|
5
|
+
import { create } from "zustand";
|
|
6
|
+
|
|
7
|
+
const createMockStore = () => create(() => ({}));
|
|
8
|
+
|
|
9
|
+
// Mock the external dependencies
|
|
10
|
+
jest.mock(
|
|
11
|
+
"@applicaster/zapp-react-native-utils/reactHooks/screen/useScreenContext",
|
|
12
|
+
() => ({
|
|
13
|
+
useScreenContextV2: jest.fn(),
|
|
14
|
+
})
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
const mockUseScreenContextV2 = useScreenContextV2 as jest.MockedFunction<
|
|
18
|
+
typeof useScreenContextV2
|
|
19
|
+
>;
|
|
20
|
+
|
|
21
|
+
describe("useComponentScreenState", () => {
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
jest.clearAllMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should return initial value when no state exists for componentId", () => {
|
|
27
|
+
const mockStore = createMockStore();
|
|
28
|
+
|
|
29
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
30
|
+
_componentStateStore: mockStore,
|
|
31
|
+
} as any);
|
|
32
|
+
|
|
33
|
+
const initialValue = "default-value";
|
|
34
|
+
const componentId = "test-component";
|
|
35
|
+
|
|
36
|
+
const { result } = renderHook(() =>
|
|
37
|
+
useComponentScreenState(componentId, initialValue)
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
expect(result.current[0]).toBe(initialValue);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should return stored value when state exists for componentId", () => {
|
|
44
|
+
const mockStore = createMockStore();
|
|
45
|
+
mockStore.setState({ "existing-component": "stored-value" });
|
|
46
|
+
|
|
47
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
48
|
+
_componentStateStore: mockStore,
|
|
49
|
+
} as any);
|
|
50
|
+
|
|
51
|
+
const { result } = renderHook(() =>
|
|
52
|
+
useComponentScreenState("existing-component", "default")
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
expect(result.current[0]).toBe("stored-value");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should update state when setter is called", () => {
|
|
59
|
+
const mockStore = createMockStore();
|
|
60
|
+
|
|
61
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
62
|
+
_componentStateStore: mockStore,
|
|
63
|
+
} as any);
|
|
64
|
+
|
|
65
|
+
const { result } = renderHook(() =>
|
|
66
|
+
useComponentScreenState("update-test", "initial")
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// Verify initial value
|
|
70
|
+
expect(result.current[0]).toBe("initial");
|
|
71
|
+
|
|
72
|
+
// Update the value
|
|
73
|
+
act(() => {
|
|
74
|
+
result.current[1]("updated-value");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Verify updated value
|
|
78
|
+
expect(result.current[0]).toBe("updated-value");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should handle different data types correctly", () => {
|
|
82
|
+
const mockStore = createMockStore();
|
|
83
|
+
|
|
84
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
85
|
+
_componentStateStore: mockStore,
|
|
86
|
+
} as any);
|
|
87
|
+
|
|
88
|
+
// Test with number
|
|
89
|
+
const { result: numberResult } = renderHook(() =>
|
|
90
|
+
useComponentScreenState<number>("number-test", 42)
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
expect(numberResult.current[0]).toBe(42);
|
|
94
|
+
|
|
95
|
+
// Test with boolean
|
|
96
|
+
const { result: boolResult } = renderHook(() =>
|
|
97
|
+
useComponentScreenState<boolean>("bool-test", true)
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
expect(boolResult.current[0]).toBe(true);
|
|
101
|
+
|
|
102
|
+
// Test with object
|
|
103
|
+
const testObj = { key: "value" };
|
|
104
|
+
|
|
105
|
+
const { result: objResult } = renderHook(() =>
|
|
106
|
+
useComponentScreenState<{ key: string }>("obj-test", testObj)
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
expect(objResult.current[0]).toEqual(testObj);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("should maintain separate states for different componentIds", () => {
|
|
113
|
+
const mockStore = createMockStore();
|
|
114
|
+
|
|
115
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
116
|
+
_componentStateStore: mockStore,
|
|
117
|
+
} as any);
|
|
118
|
+
|
|
119
|
+
const { result: firstResult } = renderHook(() =>
|
|
120
|
+
useComponentScreenState("first", "first-initial")
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const { result: secondResult } = renderHook(() =>
|
|
124
|
+
useComponentScreenState("second", "second-initial")
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
// Verify initial values are separate
|
|
128
|
+
expect(firstResult.current[0]).toBe("first-initial");
|
|
129
|
+
expect(secondResult.current[0]).toBe("second-initial");
|
|
130
|
+
|
|
131
|
+
// Update first component's state
|
|
132
|
+
act(() => {
|
|
133
|
+
firstResult.current[1]("first-updated");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// Verify only first component's state changed
|
|
137
|
+
expect(firstResult.current[0]).toBe("first-updated");
|
|
138
|
+
expect(secondResult.current[0]).toBe("second-initial");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("should return a memoized setter function", () => {
|
|
142
|
+
const mockStore = createMockStore();
|
|
143
|
+
|
|
144
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
145
|
+
_componentStateStore: mockStore,
|
|
146
|
+
} as any);
|
|
147
|
+
|
|
148
|
+
const { result, rerender } = renderHook(
|
|
149
|
+
({ id, initial }) => useComponentScreenState(id, initial),
|
|
150
|
+
{
|
|
151
|
+
initialProps: { id: "memo-test", initial: "initial" },
|
|
152
|
+
}
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const firstSetter = result.current[1];
|
|
156
|
+
|
|
157
|
+
// Rerender with same props
|
|
158
|
+
rerender({ id: "memo-test", initial: "initial" });
|
|
159
|
+
|
|
160
|
+
const secondSetter = result.current[1];
|
|
161
|
+
|
|
162
|
+
// Setter should be the same instance due to useCallback
|
|
163
|
+
expect(firstSetter).toBe(secondSetter);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("should update setter when componentId changes", () => {
|
|
167
|
+
const mockStore = createMockStore();
|
|
168
|
+
|
|
169
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
170
|
+
_componentStateStore: mockStore,
|
|
171
|
+
} as any);
|
|
172
|
+
|
|
173
|
+
const { result, rerender } = renderHook(
|
|
174
|
+
({ id, initial }) => useComponentScreenState(id, initial),
|
|
175
|
+
{
|
|
176
|
+
initialProps: { id: "old-id", initial: "initial" },
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
const firstSetter = result.current[1];
|
|
181
|
+
|
|
182
|
+
// Rerender with different componentId
|
|
183
|
+
rerender({ id: "new-id", initial: "initial" });
|
|
184
|
+
|
|
185
|
+
const secondSetter = result.current[1];
|
|
186
|
+
|
|
187
|
+
// Setter should be different because componentId changed
|
|
188
|
+
expect(firstSetter).not.toBe(secondSetter);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("should call store.setState with correct parameters", () => {
|
|
192
|
+
const mockStore = createMockStore();
|
|
193
|
+
const setStateSpy = jest.spyOn(mockStore, "setState");
|
|
194
|
+
|
|
195
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
196
|
+
_componentStateStore: mockStore,
|
|
197
|
+
} as any);
|
|
198
|
+
|
|
199
|
+
const { result } = renderHook(() =>
|
|
200
|
+
useComponentScreenState("state-test", "initial")
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
const newValue = "new-value";
|
|
204
|
+
|
|
205
|
+
act(() => {
|
|
206
|
+
result.current[1](newValue);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
expect(setStateSpy).toHaveBeenCalledWith({
|
|
210
|
+
"state-test": newValue,
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("should handle falsy initial values correctly", () => {
|
|
215
|
+
const mockStore = createMockStore();
|
|
216
|
+
|
|
217
|
+
mockUseScreenContextV2.mockReturnValue({
|
|
218
|
+
_componentStateStore: mockStore,
|
|
219
|
+
} as any);
|
|
220
|
+
|
|
221
|
+
// Test with falsy values
|
|
222
|
+
const { result: nullResult } = renderHook(() =>
|
|
223
|
+
useComponentScreenState("null-test", null)
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
expect(nullResult.current[0]).toBeNull();
|
|
227
|
+
|
|
228
|
+
const { result: falseResult } = renderHook(() =>
|
|
229
|
+
useComponentScreenState("false-test", false)
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
expect(falseResult.current[0]).toBe(false);
|
|
233
|
+
|
|
234
|
+
const { result: zeroResult } = renderHook(() =>
|
|
235
|
+
useComponentScreenState("zero-test", 0)
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
expect(zeroResult.current[0]).toBe(0);
|
|
239
|
+
|
|
240
|
+
const { result: emptyStrResult } = renderHook(() =>
|
|
241
|
+
useComponentScreenState("empty-test", "")
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
expect(emptyStrResult.current[0]).toBe("");
|
|
245
|
+
});
|
|
246
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { useScreenContextV2 } from "@applicaster/zapp-react-native-utils/reactHooks/screen/useScreenContext";
|
|
3
|
+
import { useStore } from "zustand";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A custom hook that provides persistent state storage across component re-mounts
|
|
7
|
+
* using the screen's component state store.
|
|
8
|
+
*
|
|
9
|
+
* @see {@link DOCS/adr/0010-useComponentScreenState.md} - ADR explaining why useComponentScreenState is used for List/Hero/Grid/Gallery components
|
|
10
|
+
*
|
|
11
|
+
* The state is shared across all components using the same componentId within the same screen context.
|
|
12
|
+
* If no value exists for the componentId, the initial value is returned as a fallback but is not persisted in the store.
|
|
13
|
+
* The initial value must be explicitly set using the returned setter function to persist it.
|
|
14
|
+
*
|
|
15
|
+
* @template T - The type of value being stored
|
|
16
|
+
* @param componentId - Unique identifier for this state in the component state store
|
|
17
|
+
* @param initialValue - The default value if no value exists for the componentId
|
|
18
|
+
* @returns A tuple containing the current value and a memoized setter function
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* const [count, setCount] = useComponentScreenState<number>('counter', 0);
|
|
23
|
+
* const [name, setName] = useComponentScreenState<string>('username', 'Anonymous');
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export const useComponentScreenState = <T = unknown>(
|
|
27
|
+
componentId: string,
|
|
28
|
+
initialValue: T
|
|
29
|
+
): [T, (value: T) => void] => {
|
|
30
|
+
const store = useScreenContextV2()._componentStateStore;
|
|
31
|
+
|
|
32
|
+
const value = useStore(
|
|
33
|
+
store,
|
|
34
|
+
(state) => (state[componentId] as T | undefined) ?? initialValue
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const setValue = React.useCallback(
|
|
38
|
+
(nextValue: T) => {
|
|
39
|
+
store.setState({ [componentId]: nextValue });
|
|
40
|
+
},
|
|
41
|
+
[componentId, store]
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
return [value, setValue] as const;
|
|
45
|
+
};
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
|
|
3
|
+
type WithOptionalCleanup = {
|
|
4
|
+
cleanup?: () => void;
|
|
5
|
+
};
|
|
6
|
+
|
|
3
7
|
/**
|
|
4
8
|
* returns a ref with an initial value that is lazily evaluated once
|
|
5
9
|
* */
|
|
@@ -10,5 +14,11 @@ export const useRefWithInitialValue = <T>(initialValueGetter: () => T) => {
|
|
|
10
14
|
ref.current = initialValueGetter();
|
|
11
15
|
}
|
|
12
16
|
|
|
17
|
+
React.useEffect(() => {
|
|
18
|
+
return () => {
|
|
19
|
+
(ref.current as unknown as WithOptionalCleanup)?.cleanup?.();
|
|
20
|
+
};
|
|
21
|
+
}, []);
|
|
22
|
+
|
|
13
23
|
return ref;
|
|
14
24
|
};
|