Skip to content

Commit bd70f71

Browse files
committed
fix: Book travel - Book travel animation becomes blank while RHP is dismissed
1 parent 821edc5 commit bd70f71

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/components/Lottie/index.tsx

+21-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {NavigationContainerRefContext, NavigationContext} from '@react-navigatio
22
import type {AnimationObject, LottieViewProps} from 'lottie-react-native';
33
import LottieView from 'lottie-react-native';
44
import type {ForwardedRef} from 'react';
5-
import React, {forwardRef, useContext, useEffect, useState} from 'react';
5+
import React, {forwardRef, useContext, useEffect, useRef, useState} from 'react';
66
import {InteractionManager, View} from 'react-native';
77
import type DotLottieAnimation from '@components/LottieAnimations/types';
88
import useAppState from '@hooks/useAppState';
@@ -18,7 +18,8 @@ type Props = {
1818
shouldLoadAfterInteractions?: boolean;
1919
} & Omit<LottieViewProps, 'source'>;
2020

21-
function Lottie({source, webStyle, shouldLoadAfterInteractions, ...props}: Props, ref: ForwardedRef<LottieView>) {
21+
function Lottie({source, webStyle, shouldLoadAfterInteractions, ...props}: Props, forwardedRef: ForwardedRef<LottieView>) {
22+
const animationRef = useRef<LottieView | null>(null);
2223
const appState = useAppState();
2324
const {splashScreenState} = useSplashScreenStateContext();
2425
const styles = useThemeStyles();
@@ -79,19 +80,20 @@ function Lottie({source, webStyle, shouldLoadAfterInteractions, ...props}: Props
7980
return unsubscribeNavigationBlur;
8081
}, [browser, navigationContainerRef, navigator]);
8182

83+
// If user is being navigated away, let pause the animation to prevent memory leak.
84+
useEffect(() => {
85+
if (!animationRef.current || !hasNavigatedAway) {
86+
return;
87+
}
88+
animationRef?.current?.pause();
89+
}, [hasNavigatedAway]);
90+
8291
// 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,
8392
// we'll just render an empty view as the fallback to prevent
8493
// 1. memory leak, see issue: https://github.com/Expensify/App/issues/36645
8594
// 2. heavy rendering, see issues: https://github.com/Expensify/App/issues/34696 and https://github.com/Expensify/App/issues/47273
8695
// 3. lag on react navigation transitions, see issue: https://github.com/Expensify/App/issues/44812
87-
if (
88-
hasNavigatedAway ||
89-
isError ||
90-
appState.isBackground ||
91-
!animationFile ||
92-
splashScreenState !== CONST.BOOT_SPLASH_STATE.HIDDEN ||
93-
(!isInteractionComplete && shouldLoadAfterInteractions)
94-
) {
96+
if (isError || appState.isBackground || !animationFile || splashScreenState !== CONST.BOOT_SPLASH_STATE.HIDDEN || (!isInteractionComplete && shouldLoadAfterInteractions)) {
9597
return <View style={[aspectRatioStyle, props.style]} />;
9698
}
9799

@@ -100,7 +102,15 @@ function Lottie({source, webStyle, shouldLoadAfterInteractions, ...props}: Props
100102
// eslint-disable-next-line react/jsx-props-no-spreading
101103
{...props}
102104
source={animationFile}
103-
ref={ref}
105+
ref={(ref) => {
106+
if (typeof forwardedRef === 'function') {
107+
forwardedRef(ref);
108+
} else if (forwardedRef && 'current' in forwardedRef) {
109+
// eslint-disable-next-line no-param-reassign
110+
forwardedRef.current = ref;
111+
}
112+
animationRef.current = ref;
113+
}}
104114
style={[aspectRatioStyle, props.style]}
105115
webStyle={{...aspectRatioStyle, ...webStyle}}
106116
onAnimationFailure={() => setIsError(true)}

0 commit comments

Comments
 (0)