Skip to content

Commit 6125c38

Browse files
committed
Get rid of full list re-render when marking messages as unread
1 parent 3be5e69 commit 6125c38

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/pages/home/report/ReportActionsList.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ function ReportActionsList({
114114
const readActionSkipped = useRef(false);
115115
const reportActionSize = useRef(sortedReportActions.length);
116116

117-
// Considering that renderItem is enclosed within a useCallback, marking it as "read" twice will retain the value as "true," preventing the useCallback from re-executing.
118-
// However, if we create and listen to an object, it will lead to a new useCallback execution.
119-
const [messageManuallyMarked, setMessageManuallyMarked] = useState({read: false});
117+
// This state is used to force a re-render when the user manually marks a message as unread
118+
// by using a timestamp you can force re-renders without having to worry about if another message was marked as unread before
119+
const [messageManuallyMarkedUnread, setMessageManuallyMarkedUnread] = useState(0);
120120
const [isFloatingMessageCounterVisible, setIsFloatingMessageCounterVisible] = useState(false);
121121
const animatedStyles = useAnimatedStyle(() => ({
122122
opacity: opacity.value,
@@ -163,15 +163,14 @@ function ReportActionsList({
163163

164164
useEffect(() => {
165165
const didManuallyMarkReportAsUnread = report.lastReadTime < DateUtils.getDBTime() && ReportUtils.isUnread(report);
166-
if (!didManuallyMarkReportAsUnread) {
167-
setMessageManuallyMarked({read: false});
168-
return;
166+
if (didManuallyMarkReportAsUnread) {
167+
// Clearing the current unread marker so that it can be recalculated
168+
currentUnreadMarker.current = null;
169+
setMessageManuallyMarkedUnread(new Date().getTime());
170+
} else {
171+
setMessageManuallyMarkedUnread(0);
169172
}
170173

171-
// Clearing the current unread marker so that it can be recalculated
172-
currentUnreadMarker.current = null;
173-
setMessageManuallyMarked({read: true});
174-
175174
// We only care when a new lastReadTime is set in the report
176175
// eslint-disable-next-line react-hooks/exhaustive-deps
177176
}, [report.lastReadTime]);
@@ -230,7 +229,7 @@ function ReportActionsList({
230229
const isCurrentMessageUnread = isMessageUnread(reportAction, report.lastReadTime);
231230
shouldDisplayNewMarker = isCurrentMessageUnread && !isMessageUnread(nextMessage, report.lastReadTime);
232231

233-
if (!messageManuallyMarked.read) {
232+
if (!messageManuallyMarkedUnread) {
234233
shouldDisplayNewMarker = shouldDisplayNewMarker && reportAction.actorAccountID !== Report.getCurrentUserAccountID();
235234
}
236235
const canDisplayMarker = scrollingVerticalOffset.current < MSG_VISIBLE_THRESHOLD ? reportAction.created < userActiveSince.current : true;
@@ -272,7 +271,7 @@ function ReportActionsList({
272271
/>
273272
);
274273
},
275-
[report, hasOutstandingIOU, sortedReportActions, mostRecentIOUReportActionID, messageManuallyMarked],
274+
[report, hasOutstandingIOU, sortedReportActions, mostRecentIOUReportActionID, messageManuallyMarkedUnread],
276275
);
277276

278277
// Native mobile does not render updates flatlist the changes even though component did update called.

0 commit comments

Comments
 (0)