Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Performance and heat issues on the 'Troubleshoot' page #38093

Merged
merged 6 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions src/components/Lottie/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {useIsFocused} from '@react-navigation/native';
import type {LottieViewProps} from 'lottie-react-native';
import LottieView from 'lottie-react-native';
import type {ForwardedRef} from 'react';
import React, {forwardRef} 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';

Expand All @@ -15,12 +17,17 @@ function Lottie({source, webStyle, ...props}: Props, ref: ForwardedRef<LottieVie
const styles = useThemeStyles();
const [isError, setIsError] = React.useState(false);

const appState = useAppState();
const isFocused = useIsFocused();

useNetwork({onReconnect: () => setIsError(false)});

const aspectRatioStyle = styles.aspectRatioLottie(source);

// If the image fails to load, we'll just render an empty view
if (isError) {
// If the image fails to load or app is in background state, we'll just render an empty view
// using the fallback in case of a Lottie error or (isFocused || isBackground) to prevent
// memory leaks, see issue: https://github.com/Expensify/App/issues/36645
if (isError || !isFocused || appState.isBackground) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry @ikevin127 I think we need to remove isFocus. Otherwise, in Web the Lottie animation of About page is hidden when we open Troubleshoot page
Screenshot 2024-03-12 at 20 58 52

Copy link
Contributor Author

@ikevin127 ikevin127 Mar 12, 2024

Choose a reason for hiding this comment

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

@hoangzinh Good catch, removed it.

From my tests I could not see any difference in performance with the additional !isFocused, so removing it does not affect the performance of the proposed fix any way 👍

return <View style={aspectRatioStyle} />;
}

Expand Down
33 changes: 33 additions & 0 deletions src/hooks/useAppState/index.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import type {AppStateStatus} from 'react-native';
import {AppState} from 'react-native';

type AppStateType = {
isForeground: boolean;
isInactive: boolean;
isBackground: boolean;
};

function useAppState() {
const [appState, setAppState] = React.useState<AppStateType>({
isForeground: AppState.currentState === 'active',
isInactive: AppState.currentState === 'inactive',
isBackground: AppState.currentState === 'background',
});

React.useEffect(() => {
function handleAppStateChange(nextAppState: AppStateStatus) {
setAppState({
isForeground: nextAppState === 'active',
isInactive: nextAppState === 'inactive',
isBackground: nextAppState === 'background',
});
}
const subscription = AppState.addEventListener('change', handleAppStateChange);
return () => subscription.remove();
}, []);

return appState;
}

export default useAppState;
4 changes: 4 additions & 0 deletions src/hooks/useAppState/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function useAppState() {
// Since there's no AppState in web, we'll always return isForeground as true
return {isForeground: true, isInactive: false, isBackground: false};
}
Loading