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 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
8 changes: 6 additions & 2 deletions src/components/Lottie/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 @@ -12,15 +13,18 @@ type Props = {
} & Omit<LottieViewProps, 'source'>;

function Lottie({source, webStyle, ...props}: Props, ref: ForwardedRef<LottieView>) {
const appState = useAppState();
const styles = useThemeStyles();
const [isError, setIsError] = React.useState(false);

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 appState.isBackground to prevent
// memory leak, see issue: https://github.com/Expensify/App/issues/36645
if (isError || appState.isBackground) {
return <View style={aspectRatioStyle} />;
}

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

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;
8 changes: 8 additions & 0 deletions src/hooks/useAppState/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type AppStateType from './types';

function useAppState(): AppStateType {
// Since there's no AppState in web, we'll always return isForeground as true
return {isForeground: true, isInactive: false, isBackground: false};
}

export default useAppState;
7 changes: 7 additions & 0 deletions src/hooks/useAppState/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type AppStateType = {
isForeground: boolean;
isInactive: boolean;
isBackground: boolean;
};

export default AppStateType;
Loading