@applicaster/zapp-react-native-ui-components 13.0.0-alpha.5428512514 → 13.0.0-alpha.5771418585

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 (70) hide show
  1. package/Components/BaseFocusable/index.ios.ts +1 -1
  2. package/Components/BaseFocusable/index.tsx +1 -1
  3. package/Components/Cell/Cell.tsx +7 -3
  4. package/Components/Cell/CellWithFocusable.ios.tsx +126 -0
  5. package/Components/Cell/CellWithFocusable.tsx +43 -59
  6. package/Components/Cell/TvOSCellComponent.tsx +0 -4
  7. package/Components/Cell/__tests__/CellWIthFocusable.test.js +13 -6
  8. package/Components/ErrorScreen/index.tsx +6 -6
  9. package/Components/Focusable/Focusable.tsx +1 -0
  10. package/Components/Focusable/FocusableTvOS.tsx +1 -1
  11. package/Components/Focusable/Touchable.tsx +19 -20
  12. package/Components/FocusableList/index.tsx +0 -1
  13. package/Components/MasterCell/DefaultComponents/BorderContainerView/__tests__/index.test.tsx +66 -0
  14. package/Components/MasterCell/DefaultComponents/BorderContainerView/index.tsx +4 -1
  15. package/Components/MasterCell/DefaultComponents/Image/Image.ios.tsx +6 -12
  16. package/Components/MasterCell/DefaultComponents/ImageBorderContainer/__tests__/index.test.ts +93 -0
  17. package/Components/MasterCell/DefaultComponents/LiveImage/index.tsx +2 -2
  18. package/Components/MasterCell/DefaultComponents/SecondaryImage/utils.ts +1 -1
  19. package/Components/MasterCell/DefaultComponents/__tests__/image.test.js +1 -1
  20. package/Components/MasterCell/hooks/useAsyncRendering/MasterCellAsyncRenderManager.ts +2 -2
  21. package/Components/MasterCell/hooks/useAsyncRendering/index.ts +2 -2
  22. package/Components/MasterCell/utils/index.ts +1 -1
  23. package/Components/ModalComponent/Button/index.tsx +6 -6
  24. package/Components/ModalComponent/Header/index.tsx +3 -3
  25. package/Components/OfflineHandler/NotificationView/NotificationView.lg.tsx +2 -2
  26. package/Components/OfflineHandler/NotificationView/NotificationView.samsung.tsx +2 -2
  27. package/Components/PlayerContainer/PlayerContainer.tsx +11 -15
  28. package/Components/PlayerContainer/PlayerContainerContext.tsx +9 -0
  29. package/Components/PlayerContainer/ProgramInfo/index.tsx +2 -2
  30. package/Components/River/ComponentsMap/ComponentsMap.tsx +39 -65
  31. package/Components/River/ComponentsMap/hooks/useLoadingState.ts +78 -51
  32. package/Components/River/RiverFooter.tsx +39 -9
  33. package/Components/River/RiverItem.tsx +37 -2
  34. package/Components/River/__tests__/__snapshots__/componentsMap.test.js.snap +100 -31
  35. package/Components/River/__tests__/componentsMap.test.js +17 -5
  36. package/Components/Screen/hooks.ts +56 -0
  37. package/Components/Screen/index.tsx +16 -42
  38. package/Components/Tabs/Tab.tsx +11 -9
  39. package/Components/TextInputTv/index.tsx +4 -3
  40. package/Components/Transitioner/AnimationManager.js +8 -8
  41. package/Components/Transitioner/Scene.tsx +53 -24
  42. package/Components/Transitioner/Transitioner.tsx +3 -3
  43. package/Components/Transitioner/__tests__/__snapshots__/Scene.test.js.snap +59 -43
  44. package/Components/Transitioner/__tests__/__snapshots__/transitioner.test.js.snap +2 -2
  45. package/Components/Transitioner/index.js +8 -4
  46. package/Components/VideoLive/LiveImageManager.ts +27 -1
  47. package/Components/VideoLive/PlayerLiveImageComponent.tsx +32 -24
  48. package/Components/VideoLive/__tests__/PlayerLiveImageComponent.test.tsx +51 -1
  49. package/Components/VideoLive/__tests__/__snapshots__/PlayerLiveImageComponent.test.tsx.snap +0 -5
  50. package/Components/VideoModal/ModalAnimation/AnimatedScrollModal.tsx +16 -12
  51. package/Components/VideoModal/ModalAnimation/AnimatedVideoPlayerComponent.tsx +5 -1
  52. package/Components/VideoModal/ModalAnimation/AnimationComponent.tsx +15 -14
  53. package/Components/VideoModal/ModalAnimation/ModalAnimationContext.tsx +2 -3
  54. package/Components/VideoModal/ModalAnimation/utils.ts +2 -2
  55. package/Components/VideoModal/OpaqueLayer.tsx +33 -0
  56. package/Components/VideoModal/PlayerDetails.tsx +5 -5
  57. package/Components/VideoModal/PlayerWrapper.tsx +17 -36
  58. package/Components/VideoModal/VideoModal.tsx +14 -23
  59. package/Components/VideoModal/__tests__/PlayerWrapper.test.tsx +1 -1
  60. package/Components/VideoModal/__tests__/__snapshots__/PlayerWrapper.test.tsx.snap +0 -90
  61. package/Components/VideoModal/hooks/__tests__/useDelayedPlayerDetails.test.ts +89 -0
  62. package/Components/VideoModal/hooks/index.ts +7 -0
  63. package/Components/VideoModal/hooks/useDelayedPlayerDetails.ts +49 -0
  64. package/Components/VideoModal/hooks/utils/__tests__/showDetails.test.ts +91 -0
  65. package/Components/VideoModal/hooks/utils/index.ts +33 -0
  66. package/Components/VideoModal/utils.ts +1 -1
  67. package/Contexts/ScreenContext/index.tsx +3 -2
  68. package/Decorators/ZappPipesDataConnector/__tests__/Hero.js +1 -1
  69. package/Decorators/ZappPipesDataConnector/index.tsx +31 -1
  70. package/package.json +5 -5
@@ -17,7 +17,7 @@ const SECONDARY_IMAGE_PREFIX = "secondary_image";
17
17
 
18
18
  type ImageSizing = typeof IMAGE_SIZING_FIT | typeof IMAGE_SIZING_FILL;
19
19
 
20
- type ImagePosition = typeof IMAGE_POSITION[keyof typeof IMAGE_POSITION];
20
+ type ImagePosition = (typeof IMAGE_POSITION)[keyof typeof IMAGE_POSITION];
21
21
 
22
22
  export type DisplayMode =
23
23
  | typeof DISPLAY_MODE_FIXED
@@ -39,6 +39,6 @@ describe("image with no source", () => {
39
39
  );
40
40
 
41
41
  const testInstance = testRenderer.root;
42
- expect(testInstance.findByType(Image).props.source).toEqual({ uri: "" });
42
+ expect(testInstance.findByType(Image).props.source).toBeUndefined();
43
43
  });
44
44
  });
@@ -16,13 +16,13 @@ export class MasterCellAsyncRenderManager {
16
16
  }
17
17
 
18
18
  public emitAsyncElementRegistrate = () => {
19
- this.asyncElementRegistrate$.next();
19
+ this.asyncElementRegistrate$.next(null);
20
20
 
21
21
  return this.decrementAsyncElementRegistrationCount;
22
22
  };
23
23
 
24
24
  public emitAsyncElementLayout = () => {
25
- this.asyncElementLayout$.next();
25
+ this.asyncElementLayout$.next(null);
26
26
  };
27
27
 
28
28
  public decrementAsyncElementRegistrationCount = () => {
@@ -8,7 +8,7 @@ import { MasterCellAsyncRenderManager } from "./MasterCellAsyncRenderManager";
8
8
  const layoutMeasure = (viewRef, onMeasure) => {
9
9
  viewRef?.measureLayout?.(
10
10
  findNodeHandle(viewRef),
11
- (x, y, width, height) => {
11
+ (_x, _y, width, height) => {
12
12
  onMeasure({ width, height });
13
13
  },
14
14
  noop
@@ -16,7 +16,7 @@ const layoutMeasure = (viewRef, onMeasure) => {
16
16
  };
17
17
 
18
18
  const regularMeasure = (viewRef, onMeasure) => {
19
- viewRef?.measure?.((x, y, width, height) => {
19
+ viewRef?.measure?.((_x, _y, width, height) => {
20
20
  onMeasure({ width, height });
21
21
  });
22
22
  };
@@ -103,7 +103,7 @@ export const useFilterChildren = <
103
103
  item: any;
104
104
  pluginIdentifier: string;
105
105
  };
106
- }
106
+ },
107
107
  >(
108
108
  children: T[]
109
109
  ): T[] => {
@@ -3,8 +3,8 @@ import { StyleSheet, TouchableOpacity, View } from "react-native";
3
3
  import { Item, ItemProps } from "./Item";
4
4
  import { ItemIcon, ItemIconProps } from "./ItemIcon";
5
5
  import { ItemLabel, ItemLabelProps } from "./ItemLabel";
6
- import { defaultSelectedAsset } from "./assets";
7
6
  import * as assets from "./assets";
7
+ import { defaultSelectedAsset } from "./assets";
8
8
 
9
9
  type ButtonProps = {
10
10
  configuration: any;
@@ -92,18 +92,18 @@ export function Button({
92
92
  >
93
93
  <Item {...itemProps} focused={focused} selected={selected}>
94
94
  <View style={styles.label_icon_container}>
95
- {itemIconProps.asset && itemIconProps.asset.length > 0 && (
95
+ {itemIconProps.asset && itemIconProps.asset.length > 0 ? (
96
96
  <ItemIcon {...itemIconProps} />
97
- )}
98
- {itemLabelProps.label && (
97
+ ) : null}
98
+ {itemLabelProps.label ? (
99
99
  <ItemLabel
100
100
  {...itemLabelProps}
101
101
  focused={focused}
102
102
  selected={selected}
103
103
  />
104
- )}
104
+ ) : null}
105
105
  </View>
106
- {selected && <ItemIcon {...selectedItemIconProps} />}
106
+ {selected ? <ItemIcon {...selectedItemIconProps} /> : null}
107
107
  </Item>
108
108
  </TouchableOpacity>
109
109
  </View>
@@ -1,5 +1,5 @@
1
1
  import React, { useMemo } from "react";
2
- import { View, StyleSheet, LayoutChangeEvent } from "react-native";
2
+ import { LayoutChangeEvent, StyleSheet, View } from "react-native";
3
3
  import { Title, TitleProps } from "./Title";
4
4
  import { CloseButton, CloseButtonProps } from "./CloseButton";
5
5
  import { getCloseButtonProps, getTitleProps } from "./utils";
@@ -60,8 +60,8 @@ export function ModalHeader(props: Props) {
60
60
  width: width - buttonsContainerWidth,
61
61
  }}
62
62
  >
63
- {title && <Title {...titleProps} />}
64
- {summary && <Title {...summaryProps} />}
63
+ {title ? <Title {...titleProps} /> : null}
64
+ {summary ? <Title {...summaryProps} /> : null}
65
65
  </View>
66
66
  <View style={{ width: buttonsContainerWidth }}>
67
67
  <CloseButton {...closeButtonProps} />
@@ -90,7 +90,7 @@ export const NotificationView = (props: Props) => {
90
90
  return (
91
91
  <div onClick={onClose}>
92
92
  {children}
93
- {open && (
93
+ {open ? (
94
94
  <div style={styles.body}>
95
95
  <div style={styles.title}>
96
96
  <h2>{MSG}</h2>
@@ -98,7 +98,7 @@ export const NotificationView = (props: Props) => {
98
98
 
99
99
  <h4 style={styles.subtitle}>{EXTRA_MSG}</h4>
100
100
  </div>
101
- )}
101
+ ) : null}
102
102
  </div>
103
103
  );
104
104
  };
@@ -85,7 +85,7 @@ export const NotificationView = (props: Props) => {
85
85
  return (
86
86
  <div onClick={onClose}>
87
87
  {children}
88
- {shown && (
88
+ {shown ? (
89
89
  <div style={styles.body}>
90
90
  <div style={styles.title}>
91
91
  <h2>{title}</h2>
@@ -93,7 +93,7 @@ export const NotificationView = (props: Props) => {
93
93
 
94
94
  <h4 style={styles.subtitle}>{subtitle}</h4>
95
95
  </div>
96
- )}
96
+ ) : null}
97
97
  </div>
98
98
  );
99
99
  };
@@ -1,17 +1,17 @@
1
1
  import * as React from "react";
2
+ import { useEffect, useReducer } from "react";
2
3
  // @ts-ignore
3
- import { View, ViewStyle, TVMenuControl } from "react-native";
4
+ import { TVMenuControl, View, ViewStyle } from "react-native";
4
5
  import * as R from "ramda";
5
6
  import uuid from "uuid/v4";
6
7
  import { playerManager } from "@applicaster/zapp-react-native-utils/appUtils/playerManager";
7
8
  import {
8
9
  isApplePlatform,
9
- platformSelect,
10
10
  isTV,
11
+ platformSelect,
11
12
  } from "@applicaster/zapp-react-native-utils/reactUtils";
12
13
 
13
14
  import { TVEventHandlerComponent } from "@applicaster/zapp-react-native-tvos-ui-components/Components/TVEventHandlerComponent";
14
- import { useEffect, useReducer } from "react";
15
15
  import { usePrevious } from "@applicaster/zapp-react-native-utils/reactHooks/utils";
16
16
  import { usePickFromState } from "@applicaster/zapp-react-native-redux/hooks";
17
17
  import {
@@ -29,10 +29,10 @@ import {
29
29
  import { ProgramInfo } from "./ProgramInfo";
30
30
  import { AudioPlayer } from "../AudioPlayer";
31
31
  import {
32
- playerContainerLogger,
33
32
  log_debug,
34
33
  log_info,
35
34
  log_warning,
35
+ playerContainerLogger,
36
36
  } from "./logger";
37
37
  import { usePlayer } from "@applicaster/zapp-react-native-utils/appUtils/playerManager/usePlayer";
38
38
  import { useSetNavbarState } from "@applicaster/zapp-react-native-utils/reactHooks/screen/useScreenContext";
@@ -53,8 +53,8 @@ import { usePlayNextOverlay } from "@applicaster/zapp-react-native-utils/appUtil
53
53
  import { PlayNextState } from "@applicaster/zapp-react-native-utils/appUtils/playerManager/OverlayObserver/OverlaysObserver";
54
54
 
55
55
  import {
56
- useModalAnimationContext,
57
56
  PlayerAnimationStateEnum,
57
+ useModalAnimationContext,
58
58
  } from "@applicaster/zapp-react-native-ui-components/Components/VideoModal/ModalAnimation";
59
59
 
60
60
  type Props = {
@@ -380,7 +380,7 @@ const PlayerContainerComponent = (props: Props) => {
380
380
  }
381
381
  };
382
382
 
383
- const playerRemoteHandler = (component, event, isLanguageOverlayVisible) => {
383
+ const playerRemoteHandler = (event, isLanguageOverlayVisible) => {
384
384
  const { eventType } = event;
385
385
 
386
386
  if (!isLanguageOverlayVisible && eventType === "menu") {
@@ -623,12 +623,8 @@ const PlayerContainerComponent = (props: Props) => {
623
623
  <PlayerContainerContext.Consumer>
624
624
  {(context) => (
625
625
  <TVEventHandlerComponent
626
- tvEventHandler={(component, event) =>
627
- playerRemoteHandler(
628
- component,
629
- event,
630
- context.isLanguageOverlayVisible
631
- )
626
+ tvEventHandler={(_component, event) =>
627
+ playerRemoteHandler(event, context.isLanguageOverlayVisible)
632
628
  }
633
629
  >
634
630
  <FocusableGroup
@@ -687,10 +683,10 @@ const PlayerContainerComponent = (props: Props) => {
687
683
 
688
684
  {renderRestPlayers(restPlayerProps)}
689
685
 
690
- {state.error && <ErrorDisplay error={state.error} />}
686
+ {state.error ? <ErrorDisplay error={state.error} /> : null}
691
687
  </View>
692
688
  {/* Components container */}
693
- {isInlineTV && context.showComponentsContainer && (
689
+ {isInlineTV && context.showComponentsContainer ? (
694
690
  <View
695
691
  style={styles.inlineRiver}
696
692
  testID={"inline-river-component-wrapper"}
@@ -721,7 +717,7 @@ const PlayerContainerComponent = (props: Props) => {
721
717
  )}
722
718
  </ComponentFocusableWrapperView>
723
719
  </View>
724
- )}
720
+ ) : null}
725
721
  </View>
726
722
  </FocusableGroup>
727
723
  </TVEventHandlerComponent>
@@ -9,11 +9,14 @@ type ContextProps = {
9
9
  setIsLanguageOverlayVisible: (isVisible) => void;
10
10
  setShowComponentsContainer: (isVisible) => void;
11
11
  showComponentsContainer: boolean;
12
+ setIsSeekBarTouch: (isTouch) => void;
13
+ isSeekBarTouch: boolean;
12
14
  };
13
15
 
14
16
  export const PlayerContainerContext = createContext({
15
17
  ignoreOffsetContainer: false,
16
18
  isLanguageOverlayVisible: false,
19
+ isSeekBarTouch: false,
17
20
  } as ContextProps);
18
21
 
19
22
  type Props = {
@@ -40,6 +43,8 @@ export const PlayerContainerContextProvider = ({
40
43
  const [showComponentsContainer, setShowComponentsContainer] =
41
44
  React.useState(true);
42
45
 
46
+ const [isSeekBarTouch, setIsSeekBarTouch] = React.useState(false);
47
+
43
48
  const value = React.useMemo(
44
49
  () => ({
45
50
  ignoreOffsetContainer,
@@ -49,6 +54,8 @@ export const PlayerContainerContextProvider = ({
49
54
  setIsLanguageOverlayVisible,
50
55
  showComponentsContainer,
51
56
  setShowComponentsContainer: inline ? setShowComponentsContainer : null,
57
+ isSeekBarTouch,
58
+ setIsSeekBarTouch,
52
59
  }),
53
60
  [
54
61
  ignoreOffsetContainer,
@@ -58,6 +65,8 @@ export const PlayerContainerContextProvider = ({
58
65
  setIsLanguageOverlayVisible,
59
66
  showComponentsContainer,
60
67
  setShowComponentsContainer,
68
+ isSeekBarTouch,
69
+ setIsSeekBarTouch,
61
70
  ]
62
71
  );
63
72
 
@@ -114,12 +114,12 @@ function ProgramInfoComponent({
114
114
  return (
115
115
  <View style={container}>
116
116
  <View style={programStyles.programInfo.container}>
117
- {logo && (
117
+ {logo ? (
118
118
  <Image
119
119
  source={{ uri: logo }}
120
120
  style={programStyles.programInfo.logo}
121
121
  />
122
- )}
122
+ ) : null}
123
123
  {!isAudioContent && transportInfo}
124
124
  </View>
125
125
  </View>
@@ -70,21 +70,6 @@ function ComponentsMapComponent(props: Props) {
70
70
 
71
71
  const [flatListHeight, setFlatListHeight] = React.useState(null);
72
72
 
73
- const {
74
- isAnyLoading,
75
- isAllLoaded,
76
- waitForAllComponents,
77
- onLoadFinished,
78
- onLoadFailed,
79
- shouldShowLoadingError,
80
- isAnyLoaded,
81
- arePreviousComponentsLoaded,
82
- } = useLoadingState(feed?.entry?.length || components.length);
83
-
84
- const theme = useTheme();
85
-
86
- const logTimestamp = useProfilerLogging();
87
-
88
73
  const riverComponents = React.useMemo(() => {
89
74
  if (feed?.entry?.length < components.length) {
90
75
  return R.slice(0, feed?.entry?.length, components);
@@ -93,55 +78,42 @@ function ComponentsMapComponent(props: Props) {
93
78
  return components;
94
79
  }, [components, feed]);
95
80
 
81
+ const logTimestamp = useProfilerLogging();
82
+
83
+ const onLoadDone = React.useCallback(() => {
84
+ logTimestamp(riverId?.toString());
85
+ }, [logTimestamp]);
86
+
87
+ const { loadingState, onLoadFinished, onLoadFailed, shouldShowLoadingError } =
88
+ useLoadingState(riverComponents.length, onLoadDone);
89
+
90
+ const theme = useTheme();
91
+
96
92
  const renderRiverItem = React.useCallback(
97
93
  ({ item, index }) => {
98
- const readyToBeDisplayed = arePreviousComponentsLoaded(index);
99
-
100
- const styles = {
101
- display: !readyToBeDisplayed ? "none" : ("flex" as "none" | "flex"),
94
+ const riverItemProps = {
95
+ riverId,
96
+ item,
97
+ index,
98
+ isScreenWrappedInContainer,
99
+ feed,
100
+ groupId,
101
+ onLoadFailed,
102
+ onLoadFinished,
103
+ getStaticComponentFeed,
104
+ isLast: isLast(index, riverComponents.length),
105
+ loadingState,
102
106
  };
103
107
 
104
108
  return (
105
- <View style={styles}>
106
- <ScreenLoadingMeasurementsListItemWrapper index={index}>
107
- <RiverItem
108
- {...{
109
- readyToBeDisplayed,
110
- riverId,
111
- item,
112
- index,
113
- isScreenWrappedInContainer,
114
- feed,
115
- groupId,
116
- onLoadFailed,
117
- onLoadFinished,
118
- getStaticComponentFeed,
119
- isLast: isLast(index, riverComponents.length),
120
- }}
121
- />
122
- </ScreenLoadingMeasurementsListItemWrapper>
123
- </View>
109
+ <ScreenLoadingMeasurementsListItemWrapper index={index}>
110
+ <RiverItem {...riverItemProps} />
111
+ </ScreenLoadingMeasurementsListItemWrapper>
124
112
  );
125
113
  },
126
- [
127
- feed,
128
- getStaticComponentFeed,
129
- arePreviousComponentsLoaded,
130
- onLoadFailed,
131
- onLoadFinished,
132
- ]
114
+ [feed, getStaticComponentFeed, onLoadFailed, onLoadFinished]
133
115
  );
134
116
 
135
- const renderFooter = React.useCallback(() => {
136
- return (
137
- <RiverFooter
138
- visible={isAnyLoading}
139
- flatListHeight={flatListHeight}
140
- isAnyLoaded={isAnyLoaded || waitForAllComponents}
141
- />
142
- );
143
- }, [flatListHeight, isAnyLoading, isAnyLoaded]);
144
-
145
117
  const screenStyle = React.useMemo(
146
118
  () => ({
147
119
  paddingTop: ifEmptyUseFallback(
@@ -179,12 +151,6 @@ function ComponentsMapComponent(props: Props) {
179
151
 
180
152
  usePipesCacheReset(riverId, riverComponents);
181
153
 
182
- React.useEffect(() => {
183
- if (isAllLoaded) {
184
- logTimestamp(riverId?.toString());
185
- }
186
- }, [isAllLoaded]);
187
-
188
154
  const refreshControl = React.useMemo(
189
155
  () =>
190
156
  pullToRefreshEnabled ? (
@@ -280,6 +246,11 @@ function ComponentsMapComponent(props: Props) {
280
246
  }
281
247
  }, []);
282
248
 
249
+ const contentContainerStyle = React.useMemo(
250
+ () => (isScreenWrappedInContainer ? {} : screenStyle),
251
+ [isScreenWrappedInContainer, screenStyle]
252
+ );
253
+
283
254
  if (shouldShowLoadingError) {
284
255
  return <RiverError />;
285
256
  }
@@ -291,7 +262,7 @@ function ComponentsMapComponent(props: Props) {
291
262
  <View style={styles.container}>
292
263
  <ScreenLoadingMeasurements
293
264
  riverId={riverId}
294
- numberOfComponents={feed?.entry?.length || components.length}
265
+ numberOfComponents={riverComponents.length}
295
266
  >
296
267
  <ViewportTracker>
297
268
  <FlatList
@@ -312,10 +283,13 @@ function ComponentsMapComponent(props: Props) {
312
283
  keyExtractor={keyExtractor}
313
284
  renderItem={renderRiverItem}
314
285
  data={riverComponents}
315
- contentContainerStyle={
316
- isScreenWrappedInContainer ? {} : screenStyle
286
+ contentContainerStyle={contentContainerStyle}
287
+ ListFooterComponent={
288
+ <RiverFooter
289
+ flatListHeight={flatListHeight}
290
+ loadingState={loadingState}
291
+ />
317
292
  }
318
- ListFooterComponent={renderFooter}
319
293
  refreshControl={refreshControl}
320
294
  onScrollBeginDrag={onScrollBeginDrag}
321
295
  onScroll={onScroll}
@@ -1,50 +1,86 @@
1
1
  import * as React from "react";
2
- import * as R from "ramda";
3
-
4
- const allTrue = R.all(R.equals(true));
5
- const anyFalse = R.any(R.equals(false));
6
- const anyTrue = R.any(R.equals(true));
2
+ import { isNil, set, lensIndex, T, slice } from "ramda";
3
+ import { BehaviorSubject } from "rxjs";
4
+ import { useRefWithInitialValue } from "@applicaster/zapp-react-native-utils/reactHooks/state/useRefWithInitialValue";
7
5
 
8
6
  const reducer = (state, { payload }) => {
9
- if (!R.isNil(payload) && !state[payload]) {
10
- return R.set(R.lensIndex(payload), true)(state);
7
+ if (!isNil(payload) && !state[payload]) {
8
+ return set(lensIndex(payload), true)(state);
11
9
  }
12
10
 
13
11
  return state;
14
12
  };
15
13
 
16
- type Return = {
17
- isAnyLoading: boolean;
18
- isAllLoaded: boolean;
14
+ type LoadingState = {
15
+ index: number;
16
+ done: boolean;
19
17
  waitForAllComponents: boolean;
18
+ };
19
+
20
+ type Return = {
21
+ loadingState: BehaviorSubject<LoadingState>;
20
22
  onLoadFinished: (index: number) => void;
21
23
  onLoadFailed: ({ error, index }: { error: Error; index: number }) => void;
22
24
  shouldShowLoadingError: boolean;
23
- isAnyLoaded: boolean;
24
25
  arePreviousComponentsLoaded: (index: number) => boolean;
25
26
  };
26
27
 
27
- type Action = { payload: { index: number } };
28
-
29
- type Loaded = true;
30
- type Loading = false;
31
-
32
- type LoadingState = Array<Loaded | Loading>;
33
-
34
28
  // TODO: Take this value from Zapp configuration, when feature is added to GeneralScreen
35
29
  const SHOULD_FAIL_ON_COMPONENT_LOADING = false;
36
30
 
37
- export const useLoadingState = (count: number): Return => {
31
+ const createLoadingStateObservable = () =>
32
+ new BehaviorSubject<LoadingState>({
33
+ index: -1,
34
+ done: false,
35
+ waitForAllComponents: SHOULD_FAIL_ON_COMPONENT_LOADING,
36
+ });
37
+
38
+ export const useLoadingState = (
39
+ count: number,
40
+ onLoadDone: () => void
41
+ ): Return => {
42
+ const componentStateRef = React.useRef(new Array(count).fill(false));
38
43
  const [loadingError, setLoadingError] = React.useState(null);
39
44
 
40
- const [componentsState, dispatch] = React.useReducer<
41
- React.Reducer<LoadingState, Action>
42
- >(reducer, new Array(count).fill(false));
45
+ const loadingState = useRefWithInitialValue<BehaviorSubject<LoadingState>>(
46
+ createLoadingStateObservable
47
+ );
48
+
49
+ const arePreviousComponentsLoaded = React.useCallback((index) => {
50
+ if (index === 0) {
51
+ return true;
52
+ }
43
53
 
44
- const handleComponentLoaded = React.useCallback((index) => {
45
- dispatch({ payload: index });
54
+ const componentsBefore = slice(0, index, componentStateRef.current);
55
+
56
+ return componentsBefore.every(T);
46
57
  }, []);
47
58
 
59
+ const dispatch = React.useCallback(({ payload }) => {
60
+ const newState = reducer(componentStateRef.current, { payload });
61
+ componentStateRef.current = newState;
62
+ const isDone = arePreviousComponentsLoaded(count - 1);
63
+
64
+ const state = loadingState.current.getValue();
65
+
66
+ const newLoadingState = {
67
+ ...state,
68
+ index: state.index < payload ? payload : state.index,
69
+ done: isDone,
70
+ };
71
+
72
+ loadingState.current.next(newLoadingState);
73
+
74
+ if (isDone) {
75
+ onLoadDone();
76
+ }
77
+ }, []);
78
+
79
+ const handleComponentLoaded = React.useCallback(
80
+ (index) => dispatch({ payload: index }),
81
+ []
82
+ );
83
+
48
84
  const handleComponentLoadErrorWhenNeedToFail = React.useCallback(
49
85
  ({ error }) => {
50
86
  if (error !== loadingError) {
@@ -55,35 +91,26 @@ export const useLoadingState = (count: number): Return => {
55
91
  );
56
92
 
57
93
  const handleComponentLoadErrorWhenNoNeedToFail = React.useCallback(
58
- ({ index }) => {
59
- handleComponentLoaded(index);
60
- },
94
+ ({ index }) => handleComponentLoaded(index),
61
95
  []
62
96
  );
63
97
 
64
- const arePreviousComponentsLoaded = React.useCallback(
65
- (index) => {
66
- if (index === 0) {
67
- return true;
68
- }
69
-
70
- const componentsBefore = R.slice(0, index, componentsState);
71
-
72
- return allTrue(componentsBefore);
73
- },
74
- [componentsState]
98
+ return React.useMemo(
99
+ () => ({
100
+ loadingState: loadingState.current,
101
+ onLoadFinished: handleComponentLoaded,
102
+ onLoadFailed: SHOULD_FAIL_ON_COMPONENT_LOADING
103
+ ? handleComponentLoadErrorWhenNeedToFail
104
+ : handleComponentLoadErrorWhenNoNeedToFail,
105
+ shouldShowLoadingError: SHOULD_FAIL_ON_COMPONENT_LOADING && loadingError,
106
+ arePreviousComponentsLoaded,
107
+ }),
108
+ [
109
+ loadingError,
110
+ handleComponentLoaded,
111
+ handleComponentLoadErrorWhenNeedToFail,
112
+ handleComponentLoadErrorWhenNoNeedToFail,
113
+ arePreviousComponentsLoaded,
114
+ ]
75
115
  );
76
-
77
- return {
78
- isAnyLoading: anyFalse(componentsState),
79
- isAllLoaded: allTrue(componentsState),
80
- onLoadFinished: handleComponentLoaded,
81
- onLoadFailed: SHOULD_FAIL_ON_COMPONENT_LOADING
82
- ? handleComponentLoadErrorWhenNeedToFail
83
- : handleComponentLoadErrorWhenNoNeedToFail,
84
- shouldShowLoadingError: SHOULD_FAIL_ON_COMPONENT_LOADING && loadingError,
85
- isAnyLoaded: anyTrue(componentsState),
86
- waitForAllComponents: SHOULD_FAIL_ON_COMPONENT_LOADING,
87
- arePreviousComponentsLoaded,
88
- };
89
116
  };