Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/components/Lottie/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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, useEffect, useState} from 'react';
import React, {forwardRef, useContext, useEffect, useState} from 'react';
import {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';

Expand All @@ -30,11 +33,40 @@ function Lottie({source, webStyle, ...props}: Props, ref: ForwardedRef<LottieVie

const aspectRatioStyle = styles.aspectRatioLottie(source);

// If the image fails to load, app is in background state, animation file isn't ready, or the splash screen isn't hidden yet,
const browser = Browser.getBrowser();
const [hasNavigatedAway, setHasNavigatedAway] = React.useState(false);
const navigationContainerRef = useContext(NavigationContainerRefContext);
const navigator = useContext(NavigationContext);

useEffect(() => {
if (!browser || !navigationContainerRef || !navigator) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check browser?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the issue only occurs in browsers, it seems unnecessary to apply this fix to native platforms.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let change the PR status

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)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition !isSideModalNavigator(targetRouteName) was introduced to exclude side modals on web/desktop platforms. However, the lack of an exception for small screens led to the issue here: #53208
Proposal: #53208 (comment)
PR: #55823

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 issue: https://github.com/Expensify/App/issues/34696
if (isError || appState.isBackground || !animationFile || splashScreenState !== CONST.BOOT_SPLASH_STATE.HIDDEN) {
// 2. heavy rendering, see issues: https://github.com/Expensify/App/issues/34696 and https://github.com/Expensify/App/issues/47273
if (hasNavigatedAway || isError || appState.isBackground || !animationFile || splashScreenState !== CONST.BOOT_SPLASH_STATE.HIDDEN) {
return <View style={[aspectRatioStyle, props.style]} />;
}

Expand Down