From ed82458bd1b324aa4e440eb485cce5e49dd1c629 Mon Sep 17 00:00:00 2001
From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com>
Date: Tue, 4 Jul 2023 10:57:40 +0100
Subject: [PATCH 1/8] Migrate ReportActionsView.js to function component
---
src/pages/home/report/ReportActionsView.js | 436 +++++++++++----------
1 file changed, 221 insertions(+), 215 deletions(-)
diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js
index f3c9e67d79db..69f7278ef46e 100755
--- a/src/pages/home/report/ReportActionsView.js
+++ b/src/pages/home/report/ReportActionsView.js
@@ -1,4 +1,4 @@
-import React from 'react';
+import React, {useRef, useState, useEffect, useContext, useCallback} from 'react';
import PropTypes from 'prop-types';
import _ from 'underscore';
import lodashGet from 'lodash/get';
@@ -34,6 +34,7 @@ const propTypes = {
reportActions: PropTypes.arrayOf(PropTypes.shape(reportActionPropTypes)),
/** Whether the composer is full size */
+ /* eslint-disable-next-line react/no-unused-prop-types */
isComposerFullSize: PropTypes.bool.isRequired,
/** Information about the network */
@@ -57,240 +58,192 @@ const defaultProps = {
policy: null,
};
-class ReportActionsView extends React.Component {
- constructor(props) {
- super(props);
+function ReportActionsView(props) {
+ const context = useContext(ReportScreenContext);
- this.didLayout = false;
- this.didSubscribeToReportTypingEvents = false;
- this.unsubscribeVisibilityListener = null;
- this.hasCachedActions = _.size(props.reportActions) > 0;
+ const didLayout = useRef(false);
+ const didSubscribeToReportTypingEvents = useRef(false);
+ const unsubscribeVisibilityListener = useRef(null);
+ const hasCachedActions = useRef(_.size(props.reportActions) > 0);
- this.reactionListRef = React.createRef();
- this.state = {
- isFloatingMessageCounterVisible: false,
- newMarkerReportActionID: ReportUtils.getNewMarkerReportActionID(this.props.report, props.reportActions),
- };
+ const [isFloatingMessageCounterVisible, setIsFloatingMessageCounterVisible] = useState(false);
+ const [newMarkerReportActionID, setNewMarkerReportActionID] = useState(ReportUtils.getNewMarkerReportActionID(props.report, props.reportActions));
- this.currentScrollOffset = 0;
- this.mostRecentIOUReportActionID = ReportActionsUtils.getMostRecentIOURequestActionID(props.reportActions);
- this.trackScroll = this.trackScroll.bind(this);
- this.toggleFloatingMessageCounter = this.toggleFloatingMessageCounter.bind(this);
- this.loadMoreChats = this.loadMoreChats.bind(this);
- this.recordTimeToMeasureItemLayout = this.recordTimeToMeasureItemLayout.bind(this);
- this.scrollToBottomAndMarkReportAsRead = this.scrollToBottomAndMarkReportAsRead.bind(this);
- this.openReportIfNecessary = this.openReportIfNecessary.bind(this);
- }
+ const currentScrollOffset = useRef(0);
+ const mostRecentIOUReportActionID = useRef(ReportActionsUtils.getMostRecentIOURequestActionID(props.reportActions));
+
+ const unsubscribeFromNewActionEvent = useRef(null);
+
+ const prevReportActionsRef = useRef(props.reportActions);
+ const prevReportRef = useRef(props.report);
+ const prevNetworkRef = useRef(props.network);
+ const prevIsSmallScreenWidthRef = useRef(props.isSmallScreenWidth);
- componentDidMount() {
- this.unsubscribeVisibilityListener = Visibility.onVisibilityChange(() => {
- if (!this.isReportFullyVisible()) {
+ /**
+ * @returns {Boolean}
+ */
+ const isReportFullyVisible = useCallback(() => getIsReportFullyVisible(props.isFocused), [props.isFocused]);
+
+ const openReportIfNecessary = useCallback(() => {
+ // If the report is optimistic (AKA not yet created) we don't need to call openReport again
+ if (props.report.isOptimisticReport) {
+ return;
+ }
+
+ Report.openReport(props.report.reportID);
+ }, [props.report]);
+
+ useEffect(() => {
+ unsubscribeVisibilityListener.current = Visibility.onVisibilityChange(() => {
+ if (!isReportFullyVisible()) {
return;
}
-
// If the app user becomes active and they have no unread actions we clear the new marker to sync their device
// e.g. they could have read these messages on another device and only just become active here
- const hasUnreadActions = ReportUtils.isUnread(this.props.report);
+ const hasUnreadActions = ReportUtils.isUnread(props.report);
if (!hasUnreadActions) {
- this.setState({newMarkerReportActionID: ''});
+ setNewMarkerReportActionID('');
}
});
- this.openReportIfNecessary();
+ openReportIfNecessary();
// This callback is triggered when a new action arrives via Pusher and the event is emitted from Report.js. This allows us to maintain
// a single source of truth for the "new action" event instead of trying to derive that a new action has appeared from looking at props.
- this.unsubscribeFromNewActionEvent = Report.subscribeToNewActionEvent(this.props.report.reportID, (isFromCurrentUser, newActionID) => {
- const isNewMarkerReportActionIDSet = !_.isEmpty(this.state.newMarkerReportActionID);
+ unsubscribeFromNewActionEvent.current = Report.subscribeToNewActionEvent(props.report.reportID, (isFromCurrentUser, newActionID) => {
+ const isNewMarkerReportActionIDSet = !_.isEmpty(newMarkerReportActionID);
// If a new comment is added and it's from the current user scroll to the bottom otherwise leave the user positioned where
// they are now in the list.
if (isFromCurrentUser) {
- ReportScrollManager.scrollToBottom(this.context.flatListRef);
-
+ ReportScrollManager.scrollToBottom(context.flatListRef);
// If the current user sends a new message in the chat we clear the new marker since they have "read" the report
- this.setState({newMarkerReportActionID: ''});
- } else if (this.isReportFullyVisible()) {
+ setNewMarkerReportActionID('');
+ } else if (isReportFullyVisible()) {
// We use the scroll position to determine whether the report should be marked as read and the new line indicator reset.
// If the user is scrolled up and no new line marker is set we will set it otherwise we will do nothing so the new marker
// stays in it's previous position.
- if (this.currentScrollOffset === 0) {
- Report.readNewestAction(this.props.report.reportID);
- this.setState({newMarkerReportActionID: ''});
+ if (currentScrollOffset.current === 0) {
+ Report.readNewestAction(props.report.reportID);
+ setNewMarkerReportActionID('');
} else if (!isNewMarkerReportActionIDSet) {
- this.setState({newMarkerReportActionID: newActionID});
+ // The report is not in view and we received a comment from another user while the new marker is not set
+ // so we will set the new marker now.
+ setNewMarkerReportActionID(newActionID);
}
} else if (!isNewMarkerReportActionIDSet) {
- // The report is not in view and we received a comment from another user while the new marker is not set
- // so we will set the new marker now.
- this.setState({newMarkerReportActionID: newActionID});
+ setNewMarkerReportActionID(newActionID);
}
});
- }
-
- shouldComponentUpdate(nextProps, nextState) {
- if (!_.isEqual(nextProps.reportActions, this.props.reportActions)) {
- this.mostRecentIOUReportActionID = ReportActionsUtils.getMostRecentIOURequestActionID(nextProps.reportActions);
- return true;
- }
-
- if (lodashGet(nextProps.network, 'isOffline') !== lodashGet(this.props.network, 'isOffline')) {
- return true;
- }
-
- if (nextProps.report.isLoadingMoreReportActions !== this.props.report.isLoadingMoreReportActions) {
- return true;
- }
-
- if (nextProps.report.isLoadingReportActions !== this.props.report.isLoadingReportActions) {
- return true;
- }
-
- if (nextProps.report.lastReadTime !== this.props.report.lastReadTime) {
- return true;
- }
- if (nextState.isFloatingMessageCounterVisible !== this.state.isFloatingMessageCounterVisible) {
- return true;
- }
-
- if (nextState.newMarkerReportActionID !== this.state.newMarkerReportActionID) {
- return true;
- }
-
- if (this.props.isSmallScreenWidth !== nextProps.isSmallScreenWidth) {
- return true;
- }
-
- if (lodashGet(this.props.report, 'hasOutstandingIOU') !== lodashGet(nextProps.report, 'hasOutstandingIOU')) {
- return true;
- }
-
- if (this.props.isComposerFullSize !== nextProps.isComposerFullSize) {
- return true;
- }
-
- if (lodashGet(this.props.report, 'statusNum') !== lodashGet(nextProps.report, 'statusNum') || lodashGet(this.props.report, 'stateNum') !== lodashGet(nextProps.report, 'stateNum')) {
- return true;
- }
-
- if (lodashGet(this.props, 'policy.avatar') !== lodashGet(nextProps, 'policy.avatar')) {
- return true;
- }
-
- if (lodashGet(this.props, 'policy.name') !== lodashGet(nextProps, 'policy.name')) {
- return true;
- }
+ return () => {
+ if (unsubscribeVisibilityListener.current) {
+ unsubscribeVisibilityListener.current();
+ }
- return !_.isEqual(lodashGet(this.props.report, 'icons', []), lodashGet(nextProps.report, 'icons', []));
- }
+ if (unsubscribeFromNewActionEvent.current) {
+ unsubscribeFromNewActionEvent.current();
+ }
- componentDidUpdate(prevProps) {
- const isReportFullyVisible = this.isReportFullyVisible();
+ Report.unsubscribeFromReportChannel(props.report.reportID);
+ };
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
+ useEffect(() => {
+ const prevNetwork = prevNetworkRef.current;
// When returning from offline to online state we want to trigger a request to OpenReport which
// will fetch the reportActions data and mark the report as read. If the report is not fully visible
// then we call ReconnectToReport which only loads the reportActions data without marking the report as read.
- const wasNetworkChangeDetected = lodashGet(prevProps.network, 'isOffline') && !lodashGet(this.props.network, 'isOffline');
+ const wasNetworkChangeDetected = lodashGet(prevNetwork, 'isOffline') && !lodashGet(props.network, 'isOffline');
if (wasNetworkChangeDetected) {
- if (isReportFullyVisible) {
- this.openReportIfNecessary();
+ if (isReportFullyVisible()) {
+ openReportIfNecessary();
} else {
- Report.reconnect(this.props.report.reportID);
+ Report.reconnect(props.report.reportID);
}
}
+ // update ref with current network state
+ prevNetworkRef.current = props.network;
+ }, [props.network, props.report, isReportFullyVisible, openReportIfNecessary]);
+ useEffect(() => {
+ const prevIsSmallScreenWidth = prevIsSmallScreenWidthRef.current;
// If the view is expanded from mobile to desktop layout
// we update the new marker position, mark the report as read, and fetch new report actions
- const didScreenSizeIncrease = prevProps.isSmallScreenWidth && !this.props.isSmallScreenWidth;
- const didReportBecomeVisible = isReportFullyVisible && didScreenSizeIncrease;
+ const didScreenSizeIncrease = prevIsSmallScreenWidth && !props.isSmallScreenWidth;
+ const didReportBecomeVisible = isReportFullyVisible() && didScreenSizeIncrease;
if (didReportBecomeVisible) {
- this.setState({
- newMarkerReportActionID: ReportUtils.isUnread(this.props.report) ? ReportUtils.getNewMarkerReportActionID(this.props.report, this.props.reportActions) : '',
- });
- this.openReportIfNecessary();
+ setNewMarkerReportActionID(ReportUtils.isUnread(props.report) ? ReportUtils.getNewMarkerReportActionID(props.report, props.reportActions) : '');
+ openReportIfNecessary();
}
+ // update ref with current state
+ prevIsSmallScreenWidthRef.current = props.isSmallScreenWidth;
+ }, [props.isSmallScreenWidth, props.report, props.reportActions, isReportFullyVisible, openReportIfNecessary]);
+ useEffect(() => {
+ const prevReportActions = prevReportActionsRef.current;
// If the report is unread, we want to check if the number of actions has decreased. If so, then it seems that one of them was deleted. In this case, if the deleted action was the
// one marking the unread point, we need to recalculate which action should be the unread marker.
- if (ReportUtils.isUnread(this.props.report) && prevProps.reportActions.length > this.props.reportActions.length) {
- this.setState({
- newMarkerReportActionID: ReportUtils.getNewMarkerReportActionID(this.props.report, this.props.reportActions),
- });
- }
+ if (prevReportActions && ReportUtils.isUnread(props.report) && prevReportActions.length > props.report.length)
+ setNewMarkerReportActionID(ReportUtils.getNewMarkerReportActionID(props.report, props.reportActions));
+
+ prevReportActionsRef.current = props.reportActions;
+ }, [props.report, props.reportActions]);
+ useEffect(() => {
// If the last unread message was deleted, remove the *New* green marker and the *New Messages* notification at scroll just as the deletion starts.
if (
- ReportUtils.isUnread(this.props.report) &&
- this.props.reportActions.length > 0 &&
- this.props.reportActions[0].pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE &&
- !this.props.network.isOffline
- ) {
- const reportActionsWithoutPendingOne = lodashCloneDeep(this.props.reportActions);
- reportActionsWithoutPendingOne.shift();
- const newMarkerReportActionID = ReportUtils.getNewMarkerReportActionID(this.props.report, reportActionsWithoutPendingOne);
- if (newMarkerReportActionID !== this.state.newMarkerReportActionID) {
- this.setState({newMarkerReportActionID});
- }
+ !(
+ ReportUtils.isUnread(props.report) &&
+ props.reportActions.length > 0 &&
+ props.reportActions[0].pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE &&
+ !props.network.isOffline
+ )
+ )
+ return;
+ const reportActionsWithoutPendingOne = lodashCloneDeep(props.reportActions);
+ reportActionsWithoutPendingOne.shift();
+ if (newMarkerReportActionID !== ReportUtils.getNewMarkerReportActionID(props.report, reportActionsWithoutPendingOne)) {
+ setNewMarkerReportActionID(ReportUtils.getNewMarkerReportActionID(props.report, reportActionsWithoutPendingOne));
}
+ }, [props.report, props.reportActions, props.network, newMarkerReportActionID]);
+ useEffect(() => {
+ const prevReport = prevReportRef.current;
// Checks to see if a report comment has been manually "marked as unread". All other times when the lastReadTime
// changes it will be because we marked the entire report as read.
- const didManuallyMarkReportAsUnread = prevProps.report.lastReadTime !== this.props.report.lastReadTime && ReportUtils.isUnread(this.props.report);
+ const didManuallyMarkReportAsUnread = prevReport && prevReport.lastReadTime !== props.report.lastReadTime && ReportUtils.isUnread(props.report);
if (didManuallyMarkReportAsUnread) {
- this.setState({newMarkerReportActionID: ReportUtils.getNewMarkerReportActionID(this.props.report, this.props.reportActions)});
+ setNewMarkerReportActionID(ReportUtils.getNewMarkerReportActionID(props.report, props.reportActions));
}
+ // update ref with current report
+ prevReportRef.current = props.report;
+ }, [props.report, props.reportActions]);
+ useEffect(() => {
// Ensures subscription event succeeds when the report/workspace room is created optimistically.
// Check if the optimistic `OpenReport` or `AddWorkspaceRoom` has succeeded by confirming
// any `pendingFields.createChat` or `pendingFields.addWorkspaceRoom` fields are set to null.
// Existing reports created will have empty fields for `pendingFields`.
- const didCreateReportSuccessfully = !this.props.report.pendingFields || (!this.props.report.pendingFields.addWorkspaceRoom && !this.props.report.pendingFields.createChat);
- if (!this.didSubscribeToReportTypingEvents && didCreateReportSuccessfully) {
- Report.subscribeToReportTypingEvents(this.props.report.reportID);
- this.didSubscribeToReportTypingEvents = true;
+ const didCreateReportSuccessfully = !props.report.pendingFields || (!props.report.pendingFields.addWorkspaceRoom && !props.report.pendingFields.createChat);
+ if (!didSubscribeToReportTypingEvents && didCreateReportSuccessfully) {
+ Report.subscribeToReportTypingEvents(props.report.reportID);
+ didSubscribeToReportTypingEvents.current = true;
}
- }
-
- componentWillUnmount() {
- if (this.unsubscribeVisibilityListener) {
- this.unsubscribeVisibilityListener();
- }
-
- if (this.unsubscribeFromNewActionEvent) {
- this.unsubscribeFromNewActionEvent();
- }
-
- Report.unsubscribeFromReportChannel(this.props.report.reportID);
- }
-
- /**
- * @returns {Boolean}
- */
- isReportFullyVisible() {
- return getIsReportFullyVisible(this.props.isFocused);
- }
-
- // If the report is optimistic (AKA not yet created) we don't need to call openReport again
- openReportIfNecessary() {
- if (this.props.report.isOptimisticReport) {
- return;
- }
-
- Report.openReport(this.props.report.reportID);
- }
+ }, [props.report, didSubscribeToReportTypingEvents]);
/**
* Retrieves the next set of report actions for the chat once we are nearing the end of what we are currently
* displaying.
*/
- loadMoreChats() {
+ const loadMoreChats = useCallback(() => {
// Only fetch more if we are not already fetching so that we don't initiate duplicate requests.
- if (this.props.report.isLoadingMoreReportActions) {
+ if (props.report.isLoadingMoreReportActions) {
return;
}
- const oldestReportAction = _.last(this.props.reportActions);
+ const oldestReportAction = _.last(props.reportActions);
// Don't load more chats if we're already at the beginning of the chat history
if (oldestReportAction.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) {
@@ -298,47 +251,51 @@ class ReportActionsView extends React.Component {
}
// Retrieve the next REPORT.ACTIONS.LIMIT sized page of comments
- Report.readOldestAction(this.props.report.reportID, oldestReportAction.reportActionID);
- }
+ Report.readOldestAction(props.report.reportID, oldestReportAction.reportActionID);
+ }, [props.report, props.reportActions]);
- scrollToBottomAndMarkReportAsRead() {
- ReportScrollManager.scrollToBottom(this.context.flatListRef);
- Report.readNewestAction(this.props.report.reportID);
- }
+ const scrollToBottomAndMarkReportAsRead = useCallback(() => {
+ console.log('hello');
+ ReportScrollManager.scrollToBottom(context.flatListRef);
+ Report.readNewestAction(props.report.reportID);
+ }, [props.report]);
/**
* Show/hide the new floating message counter when user is scrolling back/forth in the history of messages.
*/
- toggleFloatingMessageCounter() {
- if (this.currentScrollOffset < -200 && !this.state.isFloatingMessageCounterVisible) {
- this.setState({isFloatingMessageCounterVisible: true});
+ const toggleFloatingMessageCounter = useCallback(() => {
+ if (currentScrollOffset.current < -200 && !isFloatingMessageCounterVisible) {
+ setIsFloatingMessageCounterVisible(true);
}
- if (this.currentScrollOffset > -200 && this.state.isFloatingMessageCounterVisible) {
- this.setState({isFloatingMessageCounterVisible: false});
+ if (currentScrollOffset.current > -200 && isFloatingMessageCounterVisible) {
+ setIsFloatingMessageCounterVisible(false);
}
- }
+ }, [currentScrollOffset, isFloatingMessageCounterVisible]);
/**
* keeps track of the Scroll offset of the main messages list
*
* @param {Object} {nativeEvent}
*/
- trackScroll({nativeEvent}) {
- this.currentScrollOffset = -nativeEvent.contentOffset.y;
- this.toggleFloatingMessageCounter();
- }
+ const trackScroll = useCallback(
+ ({nativeEvent}) => {
+ currentScrollOffset.current = -nativeEvent.contentOffset.y;
+ toggleFloatingMessageCounter();
+ },
+ [currentScrollOffset, toggleFloatingMessageCounter],
+ );
/**
* Runs when the FlatList finishes laying out
*/
- recordTimeToMeasureItemLayout() {
- if (this.didLayout) {
+ const recordTimeToMeasureItemLayout = useCallback(() => {
+ if (didLayout.current) {
return;
}
- this.didLayout = true;
- Timing.end(CONST.TIMING.SWITCH_REPORT, this.hasCachedActions ? CONST.TIMING.WARM : CONST.TIMING.COLD);
+ didLayout.current = true;
+ Timing.end(CONST.TIMING.SWITCH_REPORT, hasCachedActions.current ? CONST.TIMING.WARM : CONST.TIMING.COLD);
// Capture the init measurement only once not per each chat switch as the value gets overwritten
if (!ReportActionsView.initMeasured) {
@@ -347,41 +304,90 @@ class ReportActionsView extends React.Component {
} else {
Performance.markEnd(CONST.TIMING.SWITCH_REPORT);
}
- }
+ }, [didLayout, hasCachedActions]);
- render() {
- // Comments have not loaded at all yet do nothing
- if (!_.size(this.props.reportActions)) {
- return null;
- }
- return (
- <>
-
-
-
-
- >
- );
+ // Comments have not loaded at all yet do nothing
+ if (!_.size(props.reportActions)) {
+ return null;
}
+
+ return (
+ <>
+
+
+
+
+ >
+ );
}
ReportActionsView.propTypes = propTypes;
ReportActionsView.defaultProps = defaultProps;
-ReportActionsView.contextType = ReportScreenContext;
+ReportActionsView.displayName = 'ReportActionsView';
+
+function arePropsEqual(oldProps, newProps) {
+ if (!_.isEqual(oldProps.reportActions, newProps.reportActions)) {
+ return false;
+ }
+
+ if (lodashGet(oldProps.network, 'isOffline') !== lodashGet(newProps.network, 'isOffline')) {
+ return false;
+ }
+
+ if (oldProps.report.isLoadingMoreReportActions !== newProps.report.isLoadingMoreReportActions) {
+ return false;
+ }
+
+ if (oldProps.report.isLoadingReportActions !== newProps.report.isLoadingReportActions) {
+ return false;
+ }
+
+ if (oldProps.report.lastReadTime !== newProps.report.lastReadTime) {
+ return false;
+ }
+
+ if (newProps.isSmallScreenWidth !== oldProps.isSmallScreenWidth) {
+ return false;
+ }
+
+ if (lodashGet(newProps.report, 'hasOutstandingIOU') !== lodashGet(oldProps.report, 'hasOutstandingIOU')) {
+ return false;
+ }
+
+ if (newProps.isComposerFullSize !== oldProps.isComposerFullSize) {
+ return false;
+ }
+
+ if (lodashGet(newProps.report, 'statusNum') !== lodashGet(oldProps.report, 'statusNum') || lodashGet(newProps.report, 'stateNum') !== lodashGet(oldProps.report, 'stateNum')) {
+ return false;
+ }
+
+ if (lodashGet(newProps, 'policy.avatar') !== lodashGet(oldProps, 'policy.avatar')) {
+ return false;
+ }
+
+ if (lodashGet(newProps, 'policy.name') !== lodashGet(oldProps, 'policy.name')) {
+ return false;
+ }
+
+ return _.isEqual(lodashGet(newProps.report, 'icons', []), lodashGet(oldProps.report, 'icons', []));
+}
+
+const MemoizedReportActionsView = React.memo(ReportActionsView, arePropsEqual);
-export default compose(Performance.withRenderTrace({id: ' rendering'}), withWindowDimensions, withNavigationFocus, withLocalize, withNetwork())(ReportActionsView);
+export default compose(Performance.withRenderTrace({id: ' rendering'}), withWindowDimensions, withNavigationFocus, withLocalize, withNetwork())(MemoizedReportActionsView);
From 1b9f172a843093fd658753cc5d74b529b7fae791 Mon Sep 17 00:00:00 2001
From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com>
Date: Tue, 4 Jul 2023 18:02:19 +0100
Subject: [PATCH 2/8] include context.flatListRef missing dependency in
useCallback
---
src/pages/home/report/ReportActionsView.js | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js
index 69f7278ef46e..cf2883d46796 100755
--- a/src/pages/home/report/ReportActionsView.js
+++ b/src/pages/home/report/ReportActionsView.js
@@ -255,10 +255,9 @@ function ReportActionsView(props) {
}, [props.report, props.reportActions]);
const scrollToBottomAndMarkReportAsRead = useCallback(() => {
- console.log('hello');
ReportScrollManager.scrollToBottom(context.flatListRef);
Report.readNewestAction(props.report.reportID);
- }, [props.report]);
+ }, [props.report, context.flatListRef]);
/**
* Show/hide the new floating message counter when user is scrolling back/forth in the history of messages.
From 3c53108776870175f1dcdb216795da65343261e9 Mon Sep 17 00:00:00 2001
From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com>
Date: Wed, 5 Jul 2023 11:09:35 +0100
Subject: [PATCH 3/8] using useMemo in isReportFullyVisible instead of
useCallback, and replace ReportScrollManager with useReportScrollManager hook
---
src/pages/home/report/ReportActionsView.js | 22 ++++++++++++----------
1 file changed, 12 insertions(+), 10 deletions(-)
diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js
index cf2883d46796..67369e736de2 100755
--- a/src/pages/home/report/ReportActionsView.js
+++ b/src/pages/home/report/ReportActionsView.js
@@ -1,4 +1,4 @@
-import React, {useRef, useState, useEffect, useContext, useCallback} from 'react';
+import React, {useRef, useState, useEffect, useContext, useCallback, useMemo} from 'react';
import PropTypes from 'prop-types';
import _ from 'underscore';
import lodashGet from 'lodash/get';
@@ -10,7 +10,7 @@ import Timing from '../../../libs/actions/Timing';
import CONST from '../../../CONST';
import compose from '../../../libs/compose';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions';
-import * as ReportScrollManager from '../../../libs/ReportScrollManager';
+import useReportScrollManager from '../../../hooks/useReportScrollManager';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import Performance from '../../../libs/Performance';
import {withNetwork} from '../../../components/OnyxProvider';
@@ -61,6 +61,8 @@ const defaultProps = {
function ReportActionsView(props) {
const context = useContext(ReportScreenContext);
+ const reportScrollManager = useReportScrollManager();
+
const didLayout = useRef(false);
const didSubscribeToReportTypingEvents = useRef(false);
const unsubscribeVisibilityListener = useRef(null);
@@ -82,7 +84,7 @@ function ReportActionsView(props) {
/**
* @returns {Boolean}
*/
- const isReportFullyVisible = useCallback(() => getIsReportFullyVisible(props.isFocused), [props.isFocused]);
+ const isReportFullyVisible = useMemo(() => getIsReportFullyVisible(props.isFocused), [props.isFocused]);
const openReportIfNecessary = useCallback(() => {
// If the report is optimistic (AKA not yet created) we don't need to call openReport again
@@ -95,7 +97,7 @@ function ReportActionsView(props) {
useEffect(() => {
unsubscribeVisibilityListener.current = Visibility.onVisibilityChange(() => {
- if (!isReportFullyVisible()) {
+ if (!isReportFullyVisible) {
return;
}
// If the app user becomes active and they have no unread actions we clear the new marker to sync their device
@@ -116,10 +118,10 @@ function ReportActionsView(props) {
// If a new comment is added and it's from the current user scroll to the bottom otherwise leave the user positioned where
// they are now in the list.
if (isFromCurrentUser) {
- ReportScrollManager.scrollToBottom(context.flatListRef);
+ reportScrollManager.scrollToBottom(context.flatListRef);
// If the current user sends a new message in the chat we clear the new marker since they have "read" the report
setNewMarkerReportActionID('');
- } else if (isReportFullyVisible()) {
+ } else if (isReportFullyVisible) {
// We use the scroll position to determine whether the report should be marked as read and the new line indicator reset.
// If the user is scrolled up and no new line marker is set we will set it otherwise we will do nothing so the new marker
// stays in it's previous position.
@@ -157,7 +159,7 @@ function ReportActionsView(props) {
// then we call ReconnectToReport which only loads the reportActions data without marking the report as read.
const wasNetworkChangeDetected = lodashGet(prevNetwork, 'isOffline') && !lodashGet(props.network, 'isOffline');
if (wasNetworkChangeDetected) {
- if (isReportFullyVisible()) {
+ if (isReportFullyVisible) {
openReportIfNecessary();
} else {
Report.reconnect(props.report.reportID);
@@ -172,7 +174,7 @@ function ReportActionsView(props) {
// If the view is expanded from mobile to desktop layout
// we update the new marker position, mark the report as read, and fetch new report actions
const didScreenSizeIncrease = prevIsSmallScreenWidth && !props.isSmallScreenWidth;
- const didReportBecomeVisible = isReportFullyVisible() && didScreenSizeIncrease;
+ const didReportBecomeVisible = isReportFullyVisible && didScreenSizeIncrease;
if (didReportBecomeVisible) {
setNewMarkerReportActionID(ReportUtils.isUnread(props.report) ? ReportUtils.getNewMarkerReportActionID(props.report, props.reportActions) : '');
openReportIfNecessary();
@@ -255,9 +257,9 @@ function ReportActionsView(props) {
}, [props.report, props.reportActions]);
const scrollToBottomAndMarkReportAsRead = useCallback(() => {
- ReportScrollManager.scrollToBottom(context.flatListRef);
+ reportScrollManager.scrollToBottom(context.flatListRef);
Report.readNewestAction(props.report.reportID);
- }, [props.report, context.flatListRef]);
+ }, [reportScrollManager, props.report, context.flatListRef]);
/**
* Show/hide the new floating message counter when user is scrolling back/forth in the history of messages.
From 22ecb15804ea7da0abe4aa966a1eba801067033e Mon Sep 17 00:00:00 2001
From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com>
Date: Wed, 5 Jul 2023 11:51:57 +0100
Subject: [PATCH 4/8] remove context.flatListRef arg from
reportScrollManager.scrollToBottom()
---
src/pages/home/report/ReportActionsView.js | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js
index 67369e736de2..fc525efb162d 100755
--- a/src/pages/home/report/ReportActionsView.js
+++ b/src/pages/home/report/ReportActionsView.js
@@ -118,7 +118,7 @@ function ReportActionsView(props) {
// If a new comment is added and it's from the current user scroll to the bottom otherwise leave the user positioned where
// they are now in the list.
if (isFromCurrentUser) {
- reportScrollManager.scrollToBottom(context.flatListRef);
+ reportScrollManager.scrollToBottom();
// If the current user sends a new message in the chat we clear the new marker since they have "read" the report
setNewMarkerReportActionID('');
} else if (isReportFullyVisible) {
@@ -257,9 +257,9 @@ function ReportActionsView(props) {
}, [props.report, props.reportActions]);
const scrollToBottomAndMarkReportAsRead = useCallback(() => {
- reportScrollManager.scrollToBottom(context.flatListRef);
+ reportScrollManager.scrollToBottom();
Report.readNewestAction(props.report.reportID);
- }, [reportScrollManager, props.report, context.flatListRef]);
+ }, [reportScrollManager, props.report]);
/**
* Show/hide the new floating message counter when user is scrolling back/forth in the history of messages.
From 963b9e41ce2d5d9d134ac67bedaa902362118845 Mon Sep 17 00:00:00 2001
From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com>
Date: Wed, 5 Jul 2023 13:00:20 +0100
Subject: [PATCH 5/8] remove ReportScrollManager files as we don't need it
anymore
---
src/libs/ReportScrollManager/index.js | 30 --------------------
src/libs/ReportScrollManager/index.native.js | 28 ------------------
2 files changed, 58 deletions(-)
delete mode 100644 src/libs/ReportScrollManager/index.js
delete mode 100644 src/libs/ReportScrollManager/index.native.js
diff --git a/src/libs/ReportScrollManager/index.js b/src/libs/ReportScrollManager/index.js
deleted file mode 100644
index 1dd9922b33b8..000000000000
--- a/src/libs/ReportScrollManager/index.js
+++ /dev/null
@@ -1,30 +0,0 @@
-/**
- * Scroll to the provided index. On non-native implementations we do not want to scroll when we are scrolling because
- * we are editing a comment.
- *
- * @param {Object} ref
- * @param {Object} index
- * @param {Boolean} isEditing
- */
-function scrollToIndex(ref, index, isEditing) {
- if (!ref.current || isEditing) {
- return;
- }
-
- ref.current.scrollToIndex(index);
-}
-
-/**
- * Scroll to the bottom of the flatlist.
- *
- * @param {Object} ref
- */
-function scrollToBottom(ref) {
- if (!ref.current) {
- return;
- }
-
- ref.current.scrollToOffset({animated: false, offset: 0});
-}
-
-export {scrollToIndex, scrollToBottom};
diff --git a/src/libs/ReportScrollManager/index.native.js b/src/libs/ReportScrollManager/index.native.js
deleted file mode 100644
index 570d3a72acac..000000000000
--- a/src/libs/ReportScrollManager/index.native.js
+++ /dev/null
@@ -1,28 +0,0 @@
-/**
- * Scroll to the provided index.
- *
- * @param {Object} ref
- * @param {Object} index
- */
-function scrollToIndex(ref, index) {
- if (!ref.current) {
- return;
- }
-
- ref.current.scrollToIndex(index);
-}
-
-/**
- * Scroll to the bottom of the flatlist.
- *
- * @param {Object} ref
- */
-function scrollToBottom(ref) {
- if (!ref.current) {
- return;
- }
-
- ref.current.scrollToOffset({animated: false, offset: 0});
-}
-
-export {scrollToIndex, scrollToBottom};
From 9810bcd488e39701436b160d90772c66aa6a2968 Mon Sep 17 00:00:00 2001
From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com>
Date: Wed, 5 Jul 2023 22:50:10 +0100
Subject: [PATCH 6/8] put each task performed when the component mounts into
it's own useEffect call
---
src/pages/home/report/ReportActionsView.js | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js
index fc525efb162d..6fe680edf1bb 100755
--- a/src/pages/home/report/ReportActionsView.js
+++ b/src/pages/home/report/ReportActionsView.js
@@ -107,9 +107,15 @@ function ReportActionsView(props) {
setNewMarkerReportActionID('');
}
});
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
+ useEffect(() => {
openReportIfNecessary();
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
+ useEffect(() => {
// This callback is triggered when a new action arrives via Pusher and the event is emitted from Report.js. This allows us to maintain
// a single source of truth for the "new action" event instead of trying to derive that a new action has appeared from looking at props.
unsubscribeFromNewActionEvent.current = Report.subscribeToNewActionEvent(props.report.reportID, (isFromCurrentUser, newActionID) => {
@@ -139,10 +145,6 @@ function ReportActionsView(props) {
});
return () => {
- if (unsubscribeVisibilityListener.current) {
- unsubscribeVisibilityListener.current();
- }
-
if (unsubscribeFromNewActionEvent.current) {
unsubscribeFromNewActionEvent.current();
}
@@ -202,8 +204,9 @@ function ReportActionsView(props) {
props.reportActions[0].pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE &&
!props.network.isOffline
)
- )
+ ) {
return;
+ }
const reportActionsWithoutPendingOne = lodashCloneDeep(props.reportActions);
reportActionsWithoutPendingOne.shift();
if (newMarkerReportActionID !== ReportUtils.getNewMarkerReportActionID(props.report, reportActionsWithoutPendingOne)) {
From 343a43ca1909568349bd7eafaa2b86e6911f8723 Mon Sep 17 00:00:00 2001
From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com>
Date: Thu, 6 Jul 2023 11:05:01 +0100
Subject: [PATCH 7/8] remove wraping all functions in useCallback, and remove
openReportIfNecessary from useEffect deps
---
src/pages/home/report/ReportActionsView.js | 40 ++++++++++------------
1 file changed, 19 insertions(+), 21 deletions(-)
diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js
index 6fe680edf1bb..564342bf3d25 100755
--- a/src/pages/home/report/ReportActionsView.js
+++ b/src/pages/home/report/ReportActionsView.js
@@ -1,4 +1,4 @@
-import React, {useRef, useState, useEffect, useContext, useCallback, useMemo} from 'react';
+import React, {useRef, useState, useEffect, useContext, useMemo} from 'react';
import PropTypes from 'prop-types';
import _ from 'underscore';
import lodashGet from 'lodash/get';
@@ -86,14 +86,14 @@ function ReportActionsView(props) {
*/
const isReportFullyVisible = useMemo(() => getIsReportFullyVisible(props.isFocused), [props.isFocused]);
- const openReportIfNecessary = useCallback(() => {
+ const openReportIfNecessary = () => {
// If the report is optimistic (AKA not yet created) we don't need to call openReport again
if (props.report.isOptimisticReport) {
return;
}
Report.openReport(props.report.reportID);
- }, [props.report]);
+ };
useEffect(() => {
unsubscribeVisibilityListener.current = Visibility.onVisibilityChange(() => {
@@ -169,7 +169,8 @@ function ReportActionsView(props) {
}
// update ref with current network state
prevNetworkRef.current = props.network;
- }, [props.network, props.report, isReportFullyVisible, openReportIfNecessary]);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [props.network, props.report, isReportFullyVisible]);
useEffect(() => {
const prevIsSmallScreenWidth = prevIsSmallScreenWidthRef.current;
@@ -183,7 +184,8 @@ function ReportActionsView(props) {
}
// update ref with current state
prevIsSmallScreenWidthRef.current = props.isSmallScreenWidth;
- }, [props.isSmallScreenWidth, props.report, props.reportActions, isReportFullyVisible, openReportIfNecessary]);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [props.isSmallScreenWidth, props.report, props.reportActions, isReportFullyVisible]);
useEffect(() => {
const prevReportActions = prevReportActionsRef.current;
@@ -242,7 +244,7 @@ function ReportActionsView(props) {
* Retrieves the next set of report actions for the chat once we are nearing the end of what we are currently
* displaying.
*/
- const loadMoreChats = useCallback(() => {
+ const loadMoreChats = () => {
// Only fetch more if we are not already fetching so that we don't initiate duplicate requests.
if (props.report.isLoadingMoreReportActions) {
return;
@@ -257,17 +259,17 @@ function ReportActionsView(props) {
// Retrieve the next REPORT.ACTIONS.LIMIT sized page of comments
Report.readOldestAction(props.report.reportID, oldestReportAction.reportActionID);
- }, [props.report, props.reportActions]);
+ };
- const scrollToBottomAndMarkReportAsRead = useCallback(() => {
+ const scrollToBottomAndMarkReportAsRead = () => {
reportScrollManager.scrollToBottom();
Report.readNewestAction(props.report.reportID);
- }, [reportScrollManager, props.report]);
+ };
/**
* Show/hide the new floating message counter when user is scrolling back/forth in the history of messages.
*/
- const toggleFloatingMessageCounter = useCallback(() => {
+ const toggleFloatingMessageCounter = () => {
if (currentScrollOffset.current < -200 && !isFloatingMessageCounterVisible) {
setIsFloatingMessageCounterVisible(true);
}
@@ -275,25 +277,21 @@ function ReportActionsView(props) {
if (currentScrollOffset.current > -200 && isFloatingMessageCounterVisible) {
setIsFloatingMessageCounterVisible(false);
}
- }, [currentScrollOffset, isFloatingMessageCounterVisible]);
+ };
/**
* keeps track of the Scroll offset of the main messages list
*
* @param {Object} {nativeEvent}
*/
- const trackScroll = useCallback(
- ({nativeEvent}) => {
- currentScrollOffset.current = -nativeEvent.contentOffset.y;
- toggleFloatingMessageCounter();
- },
- [currentScrollOffset, toggleFloatingMessageCounter],
- );
-
+ const trackScroll = ({nativeEvent}) => {
+ currentScrollOffset.current = -nativeEvent.contentOffset.y;
+ toggleFloatingMessageCounter();
+ };
/**
* Runs when the FlatList finishes laying out
*/
- const recordTimeToMeasureItemLayout = useCallback(() => {
+ const recordTimeToMeasureItemLayout = () => {
if (didLayout.current) {
return;
}
@@ -308,7 +306,7 @@ function ReportActionsView(props) {
} else {
Performance.markEnd(CONST.TIMING.SWITCH_REPORT);
}
- }, [didLayout, hasCachedActions]);
+ };
// Comments have not loaded at all yet do nothing
if (!_.size(props.reportActions)) {
From 2ca339edb9bb723d2f2bd0081932383d4c8e041c Mon Sep 17 00:00:00 2001
From: Rayane Djouah <77965000+rayane-djouah@users.noreply.github.com>
Date: Thu, 6 Jul 2023 20:01:10 +0100
Subject: [PATCH 8/8] adding back unsubscribeVisibilityListener in useEffect
hook
---
src/pages/home/report/ReportActionsView.js | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js
index 564342bf3d25..4ca11ae543de 100755
--- a/src/pages/home/report/ReportActionsView.js
+++ b/src/pages/home/report/ReportActionsView.js
@@ -107,6 +107,12 @@ function ReportActionsView(props) {
setNewMarkerReportActionID('');
}
});
+ return () => {
+ if (!unsubscribeVisibilityListener.current) {
+ return;
+ }
+ unsubscribeVisibilityListener.current();
+ };
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);