Skip to content

Commit

Permalink
Merge pull request Expensify#27642 from staszekscp/remove-visibilityL…
Browse files Browse the repository at this point in the history
…isteners-fix

Remove visibilityListeners when report gets unfocused
  • Loading branch information
danieldoglas authored Sep 19, 2023
2 parents 1c41575 + 55e9907 commit 3bffd67
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/pages/home/ReportScreen.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {useRef, useState, useEffect, useMemo, useCallback} from 'react';
import {withOnyx} from 'react-native-onyx';
import {useFocusEffect} from '@react-navigation/native';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import lodashGet from 'lodash/get';
Expand Down Expand Up @@ -259,26 +260,28 @@ function ReportScreen({
[route],
);

useEffect(() => {
const unsubscribeVisibilityListener = Visibility.onVisibilityChange(() => {
const isTopMostReportID = Navigation.getTopmostReportId() === getReportID(route);
// If the report is not fully visible (AKA on small screen devices and LHR is open) or the report is optimistic (AKA not yet created)
// we don't need to call openReport
if (!getIsReportFullyVisible(isTopMostReportID) || report.isOptimisticReport) {
return;
}

Report.openReport(report.reportID);
});
useFocusEffect(
useCallback(() => {
const unsubscribeVisibilityListener = Visibility.onVisibilityChange(() => {
const isTopMostReportID = Navigation.getTopmostReportId() === getReportID(route);
// If the report is not fully visible (AKA on small screen devices and LHR is open) or the report is optimistic (AKA not yet created)
// we don't need to call openReport
if (!getIsReportFullyVisible(isTopMostReportID) || report.isOptimisticReport) {
return;
}

Report.openReport(report.reportID);
});

return () => unsubscribeVisibilityListener();
// The effect should run only on the first focus to attach listener
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []),
);

useEffect(() => {
fetchReportIfNeeded();
ComposerActions.setShouldShowComposeInput(true);
return () => {
if (!unsubscribeVisibilityListener) {
return;
}
unsubscribeVisibilityListener();
};
// I'm disabling the warning, as it expects to use exhaustive deps, even though we want this useEffect to run only on the first render.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down

0 comments on commit 3bffd67

Please sign in to comment.