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

Remove visibilityListeners when report gets unfocused #27642

Merged
Merged
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
34 changes: 20 additions & 14 deletions src/pages/home/ReportScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import DragAndDropProvider from '../../components/DragAndDrop/Provider';
import usePrevious from '../../hooks/usePrevious';
import CONST from '../../CONST';
import withCurrentReportID, {withCurrentReportIDPropTypes, withCurrentReportIDDefaultProps} from '../../components/withCurrentReportID';
import {useIsFocused} from '@react-navigation/native';

const propTypes = {
/** Navigation route context info provided by react navigation */
Expand Down Expand Up @@ -148,8 +149,10 @@ function ReportScreen({
}) {
const firstRenderRef = useRef(true);
const flatListRef = useRef();
const unsubscribeVisibilityListener = useRef();
const reactionListRef = useRef();
const prevReport = usePrevious(report);
const isFocused = useIsFocused();

const [skeletonViewContainerHeight, setSkeletonViewContainerHeight] = useState(0);
const [isBannerVisible, setIsBannerVisible] = useState(true);
Expand Down Expand Up @@ -260,25 +263,28 @@ function ReportScreen({
);

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) {
if (isFocused) {
unsubscribeVisibilityListener.current = 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);
});
} else {
if (!unsubscribeVisibilityListener.current) {
return;
}
unsubscribeVisibilityListener.current();
}
}, [isFocused]);
Copy link
Member

Choose a reason for hiding this comment

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

let's use useFocusEffect hook instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I'm changing that right now


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

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