-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.tsx
113 lines (97 loc) · 4.44 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import {NavigationContainerRefContext, NavigationContext} from '@react-navigation/native';
import type {AnimationObject, LottieViewProps} from 'lottie-react-native';
import LottieView from 'lottie-react-native';
import type {ForwardedRef} from 'react';
import React, {forwardRef, useContext, useEffect, useState} from 'react';
import {InteractionManager, View} from 'react-native';
import type DotLottieAnimation from '@components/LottieAnimations/types';
import useAppState from '@hooks/useAppState';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import * as Browser from '@libs/Browser';
import isSideModalNavigator from '@libs/Navigation/isSideModalNavigator';
import CONST from '@src/CONST';
import {useSplashScreenStateContext} from '@src/SplashScreenStateContext';
type Props = {
source: DotLottieAnimation;
shouldLoadAfterInteractions?: boolean;
} & Omit<LottieViewProps, 'source'>;
function Lottie({source, webStyle, shouldLoadAfterInteractions, ...props}: Props, ref: ForwardedRef<LottieView>) {
const appState = useAppState();
const {splashScreenState} = useSplashScreenStateContext();
const styles = useThemeStyles();
const [isError, setIsError] = React.useState(false);
useNetwork({onReconnect: () => setIsError(false)});
const [animationFile, setAnimationFile] = useState<string | AnimationObject | {uri: string}>();
const [isInteractionComplete, setIsInteractionComplete] = useState(false);
useEffect(() => {
setAnimationFile(source.file);
}, [setAnimationFile, source.file]);
useEffect(() => {
if (!shouldLoadAfterInteractions) {
return;
}
const interactionTask = InteractionManager.runAfterInteractions(() => {
setIsInteractionComplete(true);
});
return () => {
interactionTask.cancel();
};
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);
const aspectRatioStyle = styles.aspectRatioLottie(source);
const browser = Browser.getBrowser();
const [hasNavigatedAway, setHasNavigatedAway] = React.useState(false);
const navigationContainerRef = useContext(NavigationContainerRefContext);
const navigator = useContext(NavigationContext);
useEffect(() => {
if (!browser || !navigationContainerRef || !navigator) {
return;
}
const unsubscribeNavigationFocus = navigator.addListener('focus', () => {
setHasNavigatedAway(false);
});
return unsubscribeNavigationFocus;
}, [browser, navigationContainerRef, navigator]);
useEffect(() => {
if (!browser || !navigationContainerRef || !navigator) {
return;
}
const unsubscribeNavigationBlur = navigator.addListener('blur', () => {
const state = navigationContainerRef.getRootState();
const targetRouteName = state?.routes?.[state?.index ?? 0]?.name;
if (!isSideModalNavigator(targetRouteName)) {
setHasNavigatedAway(true);
}
});
return unsubscribeNavigationBlur;
}, [browser, navigationContainerRef, navigator]);
// If the page navigates to another screen, the image fails to load, app is in background state, animation file isn't ready, or the splash screen isn't hidden yet,
// we'll just render an empty view as the fallback to prevent
// 1. memory leak, see issue: https://github.com/Expensify/App/issues/36645
// 2. heavy rendering, see issues: https://github.com/Expensify/App/issues/34696 and https://github.com/Expensify/App/issues/47273
// 3. lag on react navigation transitions, see issue: https://github.com/Expensify/App/issues/44812
if (
hasNavigatedAway ||
isError ||
appState.isBackground ||
!animationFile ||
splashScreenState !== CONST.BOOT_SPLASH_STATE.HIDDEN ||
(!isInteractionComplete && shouldLoadAfterInteractions)
) {
return <View style={[aspectRatioStyle, props.style]} />;
}
return (
<LottieView
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
source={animationFile}
ref={ref}
style={[aspectRatioStyle, props.style]}
webStyle={{...aspectRatioStyle, ...webStyle}}
onAnimationFailure={() => setIsError(true)}
/>
);
}
Lottie.displayName = 'Lottie';
export default forwardRef(Lottie);