forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoneyRequestReportActionsList.tsx
More file actions
760 lines (676 loc) · 38.2 KB
/
MoneyRequestReportActionsList.tsx
File metadata and controls
760 lines (676 loc) · 38.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/* eslint-disable rulesdir/prefer-early-return */
import {useIsFocused, useRoute} from '@react-navigation/native';
import stableReportSelector from '@selectors/stableReportSelector';
import isEmpty from 'lodash/isEmpty';
import React, {useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState} from 'react';
import type {LayoutChangeEvent, ListRenderItemInfo, NativeScrollEvent, NativeSyntheticEvent} from 'react-native';
// eslint-disable-next-line no-restricted-imports
import {DeviceEventEmitter, InteractionManager, View} from 'react-native';
import FlatListWithScrollKey from '@components/FlatList/FlatListWithScrollKey';
import {usePersonalDetails} from '@components/OnyxListItemProvider';
import ScrollView from '@components/ScrollView';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useLoadReportActions from '@hooks/useLoadReportActions';
import useLocalize from '@hooks/useLocalize';
import useNetworkWithOfflineStatus from '@hooks/useNetworkWithOfflineStatus';
import useNewTransactions from '@hooks/useNewTransactions';
import useOnyx from '@hooks/useOnyx';
import usePaginatedReportActions from '@hooks/usePaginatedReportActions';
import useParentReportAction from '@hooks/useParentReportAction';
import usePrevious from '@hooks/usePrevious';
import useReportIsArchived from '@hooks/useReportIsArchived';
import useReportScrollManager from '@hooks/useReportScrollManager';
import useReportTransactionsCollection from '@hooks/useReportTransactionsCollection';
import useResponsiveLayoutOnWideRHP from '@hooks/useResponsiveLayoutOnWideRHP';
import useScrollToEndOnNewMessageReceived from '@hooks/useScrollToEndOnNewMessageReceived';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import {isConsecutiveChronosAutomaticTimerAction} from '@libs/ChronosUtils';
import DateUtils from '@libs/DateUtils';
import {hasDeferredWriteForReport} from '@libs/deferredLayoutWrite';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import {getAllNonDeletedTransactions, isActionVisibleOnMoneyRequestReport} from '@libs/MoneyRequestReportUtils';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackRouteProp} from '@libs/Navigation/PlatformStackNavigation/types';
import type {ReportsSplitNavigatorParamList} from '@libs/Navigation/types';
import {
getFilteredReportActionsForReportView,
getFirstVisibleReportActionID,
getOneTransactionThreadReportID,
hasNextActionMadeBySameActor,
isCurrentActionUnread,
isDeletedParentAction,
isIOUActionMatchingTransactionList,
isMoneyRequestAction,
isReportActionVisible,
wasMessageReceivedWhileOffline,
} from '@libs/ReportActionsUtils';
import {canUserPerformWriteAction, chatIncludesChronosWithID, getOriginalReportID, getReportLastVisibleActionCreated, isHarvestCreatedExpenseReport, isUnread} from '@libs/ReportUtils';
import markOpenReportEnd from '@libs/telemetry/markOpenReportEnd';
import type {SkeletonSpanReasonAttributes} from '@libs/telemetry/useSkeletonSpan';
import Visibility from '@libs/Visibility';
import isSearchTopmostFullScreenRoute from '@navigation/helpers/isSearchTopmostFullScreenRoute';
import FloatingMessageCounter from '@pages/inbox/report/FloatingMessageCounter';
import getInitialNumToRender from '@pages/inbox/report/getInitialNumReportActionsToRender';
import ReportActionIndexContext from '@pages/inbox/report/ReportActionIndexContext';
import ReportActionsListItemRenderer from '@pages/inbox/report/ReportActionsListItemRenderer';
import {getUnreadMarkerReportAction} from '@pages/inbox/report/shouldDisplayNewMarkerOnReportAction';
import useReportUnreadMessageScrollTracking from '@pages/inbox/report/useReportUnreadMessageScrollTracking';
import {ActionListContext} from '@pages/inbox/ReportScreenContext';
import variables from '@styles/variables';
import {getOlderActions, openReport, readNewestAction, subscribeToNewActionEvent} from '@userActions/Report';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
import type * as OnyxTypes from '@src/types/onyx';
import MoneyRequestReportTransactionList from './MoneyRequestReportTransactionList';
import MoneyRequestViewReportFields from './MoneyRequestViewReportFields';
import ReportActionsListLoadingSkeleton from './ReportActionsListLoadingSkeleton';
import SearchMoneyRequestReportEmptyState from './SearchMoneyRequestReportEmptyState';
import SelectionToolbar from './SelectionToolbar';
/**
* In this view we are not handling the special single transaction case, we're just handling the report
*/
const EmptyParentReportActionForTransactionThread = undefined;
// Amount of time to wait until all list items should be rendered and scrollToEnd will behave well
const DELAY_FOR_SCROLLING_TO_END = 100;
// The server page size for report actions is ~50. Gaps from IOU prioritization only happen
// when the initial load is truncated, so skip backfill for smaller reports.
const BACKFILL_MIN_ACTIONS_THRESHOLD = 50;
type MoneyRequestReportListProps = {
/** Callback executed on layout */
onLayout?: (event: LayoutChangeEvent) => void;
};
function MoneyRequestReportActionsList({onLayout}: MoneyRequestReportListProps) {
const styles = useThemeStyles();
const {translate, getLocalDateFromDatetime} = useLocalize();
const {isOffline, lastOfflineAt, lastOnlineAt} = useNetworkWithOfflineStatus();
const reportScrollManager = useReportScrollManager();
const lastMessageTime = useRef<string | null>(null);
const didLayout = useRef(false);
const [isVisible, setIsVisible] = useState(Visibility.isVisible);
const isFocused = useIsFocused();
const route = useRoute<PlatformStackRouteProp<ReportsSplitNavigatorParamList, typeof SCREENS.REPORT>>();
const reportIDFromRoute = route.params.reportID;
// Self-subscribe to report, policy, metadata, actions, transactions
// report is guaranteed to exist — callers only render this component when report is loaded
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportIDFromRoute}`) as unknown as [OnyxTypes.Report];
const [reportStable] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportIDFromRoute}`, {selector: stableReportSelector});
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${getNonEmptyStringOnyxID(report?.policyID)}`);
const [reportLoadingState] = useOnyx(`${ONYXKEYS.COLLECTION.RAM_ONLY_REPORT_LOADING_STATE}${reportIDFromRoute}`);
const [reportPaginationState] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_PAGINATION_STATE}${reportIDFromRoute}`);
const reportID = report?.reportID;
const {reportActions: unfilteredReportActions, hasNewerActions, hasOlderActions} = usePaginatedReportActions(reportID, route?.params?.reportActionID);
const reportActions = useMemo(() => getFilteredReportActionsForReportView(unfilteredReportActions), [unfilteredReportActions]);
const allReportTransactions = useReportTransactionsCollection(reportIDFromRoute);
const reportTransactions = useMemo(() => getAllNonDeletedTransactions(allReportTransactions, reportActions, isOffline, true), [allReportTransactions, reportActions, isOffline]);
const transactions = useMemo(
() => reportTransactions?.filter((transaction) => isOffline || transaction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) ?? [],
[reportTransactions, isOffline],
);
const hasPendingDeletionTransaction = useMemo(
() => Object.values(allReportTransactions ?? {}).some((transaction) => transaction?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE),
[allReportTransactions],
);
const newTransactions = useNewTransactions(reportLoadingState?.hasOnceLoadedReportActions, reportTransactions);
const showReportActionsLoadingState = reportLoadingState?.isLoadingInitialReportActions && !reportLoadingState?.hasOnceLoadedReportActions;
const reportTransactionIDs = useMemo(() => transactions.map((transaction) => transaction.transactionID), [transactions]);
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(report?.chatReportID)}`);
const linkedReportActionID = route?.params?.reportActionID;
const parentReportAction = useParentReportAction(report);
const personalDetails = usePersonalDetails();
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED);
const [betas] = useOnyx(ONYXKEYS.BETAS);
// reportActions is passed as an array because it's sorted chronologically for FlatList rendering and pagination.
// However, getOriginalReportID expects the Onyx object format (keyed by reportActionID) for efficient lookups.
const reportActionsObject = useMemo(() => {
const obj: OnyxTypes.ReportActions = {};
for (const action of reportActions) {
obj[action.reportActionID] = action;
}
return obj;
}, [reportActions]);
const transactionThreadReportID = getOneTransactionThreadReportID(report, chatReport, reportActions ?? [], false, reportTransactionIDs);
const firstVisibleReportActionID = useMemo(() => getFirstVisibleReportActionID(reportActions, isOffline), [reportActions, isOffline]);
const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`);
const {accountID: currentUserAccountID} = useCurrentUserPersonalDetails();
const isReportArchived = useReportIsArchived(reportID);
const canPerformWriteAction = canUserPerformWriteAction(report, isReportArchived);
const [visibleReportActionsData] = useOnyx(ONYXKEYS.DERIVED.VISIBLE_REPORT_ACTIONS);
const {shouldUseNarrowLayout} = useResponsiveLayoutOnWideRHP();
const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${getNonEmptyStringOnyxID(reportID)}`);
const shouldShowHarvestCreatedAction = isHarvestCreatedExpenseReport(reportNameValuePairs?.origin, reportNameValuePairs?.originalID);
const [enableScrollToEnd, setEnableScrollToEnd] = useState<boolean>(false);
const [lastActionEventId, setLastActionEventId] = useState<string>('');
// We are reversing actions because in this View we are starting at the top and don't use Inverted list
const visibleReportActions = useMemo(() => {
const filteredActions = reportActions.filter((reportAction) => {
const isActionVisibleOnMoneyReport = isActionVisibleOnMoneyRequestReport(reportAction, shouldShowHarvestCreatedAction);
if (!isActionVisibleOnMoneyReport) {
return false;
}
const passesOfflineCheck = isOffline || isDeletedParentAction(reportAction) || reportAction.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || reportAction.errors;
if (!passesOfflineCheck) {
return false;
}
const actionReportID = reportAction.reportID ?? reportID;
if (!isReportActionVisible(reportAction, actionReportID, canPerformWriteAction, visibleReportActionsData)) {
return false;
}
if (!isIOUActionMatchingTransactionList(reportAction, reportTransactionIDs)) {
return false;
}
return true;
});
return filteredActions.toReversed();
}, [reportActions, isOffline, canPerformWriteAction, reportTransactionIDs, shouldShowHarvestCreatedAction, visibleReportActionsData, reportID]);
const shouldShowOpenReportLoadingSkeleton = !isOffline && !!showReportActionsLoadingState && visibleReportActions.length === 0;
const skeletonReasonAttributes: SkeletonSpanReasonAttributes = {
context: 'MoneyRequestReportActionsList',
isOffline,
showReportActionsLoadingState: !!showReportActionsLoadingState,
};
useEffect(() => {
if (!shouldShowOpenReportLoadingSkeleton || !report) {
return;
}
markOpenReportEnd(report, {warm: false});
}, [report, shouldShowOpenReportLoadingSkeleton]);
const lastAction = visibleReportActions.at(-1);
const {scrollOffsetRef} = useContext(ActionListContext);
const scrollingVerticalBottomOffset = useRef(0);
const scrollingVerticalTopOffset = useRef(0);
const wrapperViewRef = useRef<View>(null);
const readActionSkipped = useRef(false);
const lastVisibleActionCreated = getReportLastVisibleActionCreated(report, transactionThreadReport);
const hasNewestReportAction = lastAction?.created === lastVisibleActionCreated;
const userActiveSince = useRef<string>(DateUtils.getDBTime());
const reportActionIDs = useMemo(() => {
return reportActions?.map((action) => action.reportActionID) ?? [];
}, [reportActions]);
const {loadOlderChats, loadNewerChats} = useLoadReportActions({
reportID,
reportActions,
allReportActionIDs: reportActionIDs,
transactionThreadReport,
hasOlderActions,
hasNewerActions,
newestFetchedReportActionID: reportPaginationState?.newestFetchedReportActionID,
});
const hasFinishedInitialLoad = reportLoadingState?.isLoadingInitialReportActions === false;
const prevNewestFetchedIDRef = useRef<string | undefined>(undefined);
useEffect(() => {
if (hasFinishedInitialLoad && hasNewerActions && reportActions.length > 0 && !isOffline && !reportLoadingState?.isLoadingNewerReportActions) {
// Safety guard: if the cursor hasn't advanced since the last call, the server
// isn't returning new data. Stop to prevent an infinite request loop.
const currentCursor = reportPaginationState?.newestFetchedReportActionID;
if (prevNewestFetchedIDRef.current !== undefined && prevNewestFetchedIDRef.current === currentCursor) {
return;
}
prevNewestFetchedIDRef.current = currentCursor;
loadNewerChats(false);
}
}, [
hasFinishedInitialLoad,
reportActions.length,
hasNewerActions,
isOffline,
reportLoadingState?.isLoadingNewerReportActions,
reportPaginationState?.newestFetchedReportActionID,
loadNewerChats,
]);
// Backfill loop: the backend prioritizes IOU actions in OpenReport/GetNewerActions for money
// request reports, which can leave non-IOU chat messages in a gap between the IOU-biased cursor
// and older messages. After auto-pagination finishes, walk backwards from the IOU cursor using
// getOlderActions. Each response advances oldestFetchedReportActionID so the next call picks up
// where the previous one left off, until the cursor stops advancing (gap filled).
const prevBackfillCursorRef = useRef<string | undefined>(undefined);
const isBackfillingRef = useRef(false);
const prevBackfillReportIDRef = useRef(reportID);
if (prevBackfillReportIDRef.current !== reportID) {
prevBackfillReportIDRef.current = reportID;
prevBackfillCursorRef.current = undefined;
isBackfillingRef.current = false;
}
useEffect(() => {
if (!hasFinishedInitialLoad || isOffline || hasNewerActions || reportLoadingState?.isLoadingNewerReportActions || reportLoadingState?.isLoadingOlderReportActions) {
return;
}
if (!isBackfillingRef.current) {
const hasIOUActions = reportActions.some((action) => isMoneyRequestAction(action));
if (!hasIOUActions || reportActions.length < BACKFILL_MIN_ACTIONS_THRESHOLD || !reportPaginationState?.newestFetchedReportActionID) {
return;
}
}
const cursor = isBackfillingRef.current ? reportPaginationState?.oldestFetchedReportActionID : reportPaginationState?.newestFetchedReportActionID;
if (!cursor) {
return;
}
if (prevBackfillCursorRef.current === cursor) {
return;
}
isBackfillingRef.current = true;
prevBackfillCursorRef.current = cursor;
const handle = InteractionManager.runAfterInteractions(() => getOlderActions(reportID, cursor));
return () => handle.cancel();
}, [
hasFinishedInitialLoad,
isOffline,
hasNewerActions,
reportLoadingState?.isLoadingNewerReportActions,
reportLoadingState?.isLoadingOlderReportActions,
reportPaginationState?.newestFetchedReportActionID,
reportPaginationState?.oldestFetchedReportActionID,
reportActions,
reportID,
]);
const onStartReached = useCallback(() => {
if (!isSearchTopmostFullScreenRoute()) {
loadOlderChats(false);
return;
}
InteractionManager.runAfterInteractions(() => requestAnimationFrame(() => loadOlderChats(false)));
}, [loadOlderChats]);
const onEndReached = useCallback(() => {
loadNewerChats(false);
}, [loadNewerChats]);
const prevUnreadMarkerReportActionID = useRef<string | null>(null);
const visibleActionsMap = useMemo(() => {
return visibleReportActions.reduce((actionsMap, reportAction) => {
Object.assign(actionsMap, {
[reportAction.reportActionID]: reportAction,
});
return actionsMap;
}, {} as OnyxTypes.ReportActions);
}, [visibleReportActions]);
const prevVisibleActionsMap = usePrevious(visibleActionsMap);
const reportLastReadTime = report?.lastReadTime ?? '';
/**
* The timestamp for the unread marker.
*
* This should ONLY be updated when the user
* - switches reports
* - marks a message as read/unread
* - reads a new message as it is received
*/
const [unreadMarkerTime, setUnreadMarkerTime] = useState(reportLastReadTime);
useEffect(() => {
setUnreadMarkerTime(reportLastReadTime);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [report?.reportID]);
useEffect(() => {
const unsubscribe = Visibility.onVisibilityChange(() => {
setIsVisible(Visibility.isVisible());
});
return unsubscribe;
}, []);
useEffect(() => {
if (!isFocused) {
return;
}
if (isUnread(report, transactionThreadReport, isReportArchived) || (lastAction && isCurrentActionUnread(report, lastAction, visibleReportActions))) {
// On desktop, when the notification center is displayed, isVisible will return false.
// Currently, there's no programmatic way to dismiss the notification center panel.
// To handle this, we use the 'referrer' parameter to check if the current navigation is triggered from a notification.
const isFromNotification = route?.params?.referrer === CONST.REFERRER.NOTIFICATION;
if ((isVisible || isFromNotification) && scrollingVerticalBottomOffset.current < CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD) {
readNewestAction(report?.reportID, !!reportLoadingState?.hasOnceLoadedReportActions);
if (isFromNotification) {
Navigation.setParams({referrer: undefined});
}
} else {
readActionSkipped.current = true;
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [report?.lastVisibleActionCreated, transactionThreadReport?.lastVisibleActionCreated, report?.reportID, isVisible, reportLoadingState?.hasOnceLoadedReportActions]);
useEffect(() => {
if (!isVisible || !isFocused) {
if (!lastMessageTime.current) {
lastMessageTime.current = lastAction?.created ?? '';
}
return;
}
// In case the user read new messages (after being inactive) with other device we should
// show marker based on report.lastReadTime
const newMessageTimeReference = lastMessageTime.current && report?.lastReadTime && lastMessageTime.current > report?.lastReadTime ? userActiveSince.current : report?.lastReadTime;
lastMessageTime.current = null;
const hasNewMessagesInView = scrollingVerticalBottomOffset.current < CONST.REPORT.ACTIONS.ACTION_VISIBLE_THRESHOLD;
const hasUnreadReportAction = reportActions.some(
(reportAction) => newMessageTimeReference && newMessageTimeReference < reportAction.created && reportAction.actorAccountID !== currentUserAccountID,
);
if (!hasNewMessagesInView || !hasUnreadReportAction) {
return;
}
readNewestAction(report?.reportID, !!reportLoadingState?.hasOnceLoadedReportActions);
userActiveSince.current = DateUtils.getDBTime();
// This effect logic to `mark as read` will only run when the report focused has new messages and the App visibility
// is changed to visible(meaning user switched to app/web, while user was previously using different tab or application).
// We will mark the report as read in the above case which marks the LHN report item as read while showing the new message
// marker for the chat messages received while the user wasn't focused on the report or on another browser tab for web.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isFocused, isVisible, reportLoadingState?.hasOnceLoadedReportActions]);
/**
* The index of the earliest message that was received while offline
*/
const earliestReceivedOfflineMessageIndex = useMemo(() => {
const lastIndex = reportActions.findLastIndex((action) => {
return wasMessageReceivedWhileOffline(action, isOffline, lastOfflineAt.current, lastOnlineAt.current, getLocalDateFromDatetime);
});
// The last index in the list is the earliest message that was received while offline
return lastIndex > -1 ? lastIndex : undefined;
}, [getLocalDateFromDatetime, isOffline, lastOfflineAt, lastOnlineAt, reportActions]);
/**
* The reportActionID the unread marker should display above
*/
const [unreadMarkerReportActionID, unreadMarkerReportActionIndex] = getUnreadMarkerReportAction({
visibleReportActions,
earliestReceivedOfflineMessageIndex,
currentUserAccountID,
prevSortedVisibleReportActionsObjects: prevVisibleActionsMap,
unreadMarkerTime,
scrollingVerticalOffset: scrollingVerticalBottomOffset.current,
prevUnreadMarkerReportActionID: prevUnreadMarkerReportActionID.current,
isOffline,
isReversed: true,
});
prevUnreadMarkerReportActionID.current = unreadMarkerReportActionID;
const {isFloatingMessageCounterVisible, setIsFloatingMessageCounterVisible, trackVerticalScrolling, onViewableItemsChanged} = useReportUnreadMessageScrollTracking({
reportID: report?.reportID ?? reportIDFromRoute ?? '',
currentVerticalScrollingOffsetRef: scrollingVerticalBottomOffset,
readActionSkippedRef: readActionSkipped,
unreadMarkerReportActionIndex,
isInverted: false,
hasNewerActions,
onTrackScrolling: (event: NativeSyntheticEvent<NativeScrollEvent>) => {
const {layoutMeasurement, contentSize, contentOffset} = event.nativeEvent;
const fullContentHeight = contentSize.height;
/**
* Count the diff between current scroll position and the bottom of the list.
* Diff == (height of all items in the list) - (height of the layout with the list) - (how far user scrolled)
*/
scrollingVerticalBottomOffset.current = fullContentHeight - layoutMeasurement.height - contentOffset.y;
scrollOffsetRef.current = scrollingVerticalBottomOffset.current;
// We additionally track the top offset to be able to scroll to the new transaction when it's added
scrollingVerticalTopOffset.current = contentOffset.y;
},
hasOnceLoadedReportActions: !!reportLoadingState?.hasOnceLoadedReportActions,
});
useScrollToEndOnNewMessageReceived({
sizeChangeType: 'grewFromReportActions',
scrollOffsetRef,
lastActionID: lastAction?.reportActionID,
visibleActionsLength: visibleReportActions.length,
reportActionsLength: reportActions.length,
hasNewestReportAction,
setIsFloatingMessageCounterVisible,
scrollToEnd: reportScrollManager.scrollToEnd,
resetKey: report.reportID,
});
/**
* Subscribe to read/unread events and update our unreadMarkerTime
*/
useEffect(() => {
const unreadActionSubscription = DeviceEventEmitter.addListener(`unreadAction_${report?.reportID}`, (newLastReadTime: string) => {
setUnreadMarkerTime(newLastReadTime);
userActiveSince.current = DateUtils.getDBTime();
});
const readNewestActionSubscription = DeviceEventEmitter.addListener(`readNewestAction_${report?.reportID}`, (newLastReadTime: string) => {
setUnreadMarkerTime(newLastReadTime);
});
return () => {
unreadActionSubscription.remove();
readNewestActionSubscription.remove();
};
}, [report?.reportID]);
/**
* When the user reads a new message as it is received, we'll push the unreadMarkerTime down to the timestamp of
* the latest report action. When new report actions are received and the user is not viewing them (they're above
* the MSG_VISIBLE_THRESHOLD), the unread marker will display over those new messages rather than the initial
* lastReadTime.
*/
useLayoutEffect(() => {
if (unreadMarkerReportActionID) {
return;
}
const mostRecentReportActionCreated = lastAction?.created ?? '';
if (mostRecentReportActionCreated <= unreadMarkerTime) {
return;
}
setUnreadMarkerTime(mostRecentReportActionCreated);
}, [lastAction?.created, unreadMarkerReportActionID, unreadMarkerTime]);
const scrollToBottomForCurrentUserAction = useCallback(
(isFromCurrentUser: boolean, reportAction?: OnyxTypes.ReportAction) => {
InteractionManager.runAfterInteractions(() => {
setIsFloatingMessageCounterVisible(false);
// If a new comment is added from the current user, scroll to the bottom, otherwise leave the user position unchanged
if (!isFromCurrentUser || reportAction?.actionName !== CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT) {
return;
}
// We want to scroll to the end of the list where the newest message is
// however scrollToEnd will not work correctly with items of variable sizes without `getItemLayout` - so we need to delay the scroll until every item rendered
const index = visibleReportActions.findIndex((item) => item.reportActionID === reportAction?.reportActionID);
if (index !== -1) {
setTimeout(() => {
reportScrollManager.scrollToEnd();
}, DELAY_FOR_SCROLLING_TO_END);
} else {
setEnableScrollToEnd(true);
setLastActionEventId(reportAction?.reportActionID);
}
});
},
[reportScrollManager, setIsFloatingMessageCounterVisible, visibleReportActions],
);
useEffect(() => {
if (!report?.reportID) {
return;
}
// This callback is triggered when a new action arrives via Pusher and the event is emitted from Report.ts. 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.
const unsubscribe = subscribeToNewActionEvent(report.reportID, scrollToBottomForCurrentUserAction);
return () => {
if (!unsubscribe) {
return;
}
unsubscribe();
};
// This effect handles subscribing to events, so we only want to run it on mount, and in case reportID changes
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [report?.reportID]);
useEffect(() => {
const index = visibleReportActions.findIndex((item) => item.reportActionID === lastActionEventId);
if (enableScrollToEnd && index !== -1) {
setTimeout(() => {
reportScrollManager.scrollToEnd();
}, DELAY_FOR_SCROLLING_TO_END);
setEnableScrollToEnd(false);
}
}, [visibleReportActions, lastActionEventId, enableScrollToEnd, reportScrollManager]);
const renderItem = useCallback(
({item: reportAction, index}: ListRenderItemInfo<OnyxTypes.ReportAction>) => {
const displayAsGroup =
!isConsecutiveChronosAutomaticTimerAction(visibleReportActions, index, chatIncludesChronosWithID(reportAction?.reportID), isOffline) &&
hasNextActionMadeBySameActor(visibleReportActions, index, isOffline);
const originalReportID = getOriginalReportID(reportStable?.reportID, reportAction, reportActionsObject);
return (
<ReportActionIndexContext.Provider value={index}>
<ReportActionsListItemRenderer
reportAction={reportAction}
parentReportAction={parentReportAction}
parentReportActionForTransactionThread={EmptyParentReportActionForTransactionThread}
report={reportStable}
transactionThreadReport={transactionThreadReport}
displayAsGroup={displayAsGroup}
shouldDisplayNewMarker={reportAction.reportActionID === unreadMarkerReportActionID}
shouldDisplayReplyDivider={visibleReportActions.length > 1}
isFirstVisibleReportAction={firstVisibleReportActionID === reportAction.reportActionID}
shouldHideThreadDividerLine
linkedReportActionID={linkedReportActionID}
personalDetails={personalDetails}
originalReportID={originalReportID}
isReportArchived={isReportArchived}
isHarvestCreatedExpenseReport={shouldShowHarvestCreatedAction}
/>
</ReportActionIndexContext.Provider>
);
},
[
visibleReportActions,
reportActionsObject,
parentReportAction,
reportStable,
isOffline,
transactionThreadReport,
unreadMarkerReportActionID,
firstVisibleReportActionID,
linkedReportActionID,
personalDetails,
isReportArchived,
shouldShowHarvestCreatedAction,
],
);
const scrollToBottomAndMarkReportAsRead = useCallback(() => {
setIsFloatingMessageCounterVisible(false);
if (!hasNewestReportAction) {
openReport({reportID, introSelected, betas});
reportScrollManager.scrollToEnd();
return;
}
reportScrollManager.scrollToEnd();
readActionSkipped.current = false;
readNewestAction(reportID, !!reportLoadingState?.hasOnceLoadedReportActions);
}, [setIsFloatingMessageCounterVisible, hasNewestReportAction, reportScrollManager, reportID, reportLoadingState?.hasOnceLoadedReportActions, introSelected, betas]);
const scrollToNewTransaction = useCallback(
(pageY: number) => {
wrapperViewRef.current?.measureInWindow((x, y, w, height) => {
// If the new transaction is already visible, we don't need to scroll to it
if (pageY > 0 && pageY < height) {
return;
}
reportScrollManager.scrollToOffset(scrollingVerticalTopOffset.current + pageY - variables.scrollToNewTransactionOffset);
});
},
[reportScrollManager],
);
/**
* Runs when the FlatList finishes laying out
*/
const recordTimeToMeasureItemLayout = useCallback(() => {
if (didLayout.current || !report) {
return;
}
didLayout.current = true;
markOpenReportEnd(report, {warm: !shouldShowOpenReportLoadingSkeleton});
}, [report, shouldShowOpenReportLoadingSkeleton]);
// Wrapped into useCallback to stabilize children re-renders
const keyExtractor = useCallback((item: OnyxTypes.ReportAction) => item.reportActionID, []);
const {windowHeight} = useWindowDimensions();
/**
* Calculates the ideal number of report actions to render in the first render, based on the screen height and on
* the height of the smallest report action possible.
*/
const initialNumToRender = useMemo((): number | undefined => {
const minimumReportActionHeight = styles.chatItem.paddingTop + styles.chatItem.paddingBottom + variables.fontSizeNormalHeight;
const availableHeight = windowHeight - (CONST.CHAT_FOOTER_MIN_HEIGHT + variables.contentHeaderHeight);
const numToRender = Math.ceil(availableHeight / minimumReportActionHeight);
if (linkedReportActionID) {
return getInitialNumToRender(numToRender);
}
return numToRender || undefined;
}, [styles.chatItem.paddingBottom, styles.chatItem.paddingTop, windowHeight, linkedReportActionID]);
const isReportEmpty = isEmpty(visibleReportActions) && isEmpty(transactions) && !showReportActionsLoadingState;
// hasDeferredWriteForReport is non-reactive (reads a module-level Map, not tracked by React).
// This is intentional: we only check on the initial render after the RHP dismisses.
// Once the deferred write flushes and createTransaction runs, Onyx updates make
// transactions non-empty, which drives the transition away from the skeleton.
// Scoping to `report.reportID` ensures an unrelated submit flow's pending dismiss doesn't keep
// *this* report stuck on the skeleton.
const isAwaitingDeferredTransaction = isReportEmpty && hasDeferredWriteForReport(CONST.DEFERRED_LAYOUT_WRITE_KEYS.DISMISS_MODAL, report?.reportID);
const showEmptyState = isReportEmpty && !isAwaitingDeferredTransaction;
if (!report) {
return null;
}
return (
<View
style={[styles.flex1]}
ref={wrapperViewRef}
>
<SelectionToolbar
reportID={reportIDFromRoute}
transactions={transactions}
reportActions={reportActions}
/>
<View style={[styles.flex1, styles.justifyContentEnd, styles.overflowHidden]}>
<FloatingMessageCounter
hasNewMessages={!!unreadMarkerReportActionID}
isActive={isFloatingMessageCounterVisible}
onClick={scrollToBottomAndMarkReportAsRead}
/>
{/* Exactly one of these three branches is active at a time:
1. isAwaitingDeferredTransaction — skeleton while dismiss-first creates the transaction
2. showEmptyState — genuinely empty report
3. !isReportEmpty — report has data, render the FlatList */}
{isAwaitingDeferredTransaction && <ReportActionsListLoadingSkeleton reasonAttributes={skeletonReasonAttributes} />}
{showEmptyState && (
<ScrollView contentContainerStyle={styles.flexGrow1}>
<MoneyRequestViewReportFields
report={report}
policy={policy}
/>
<SearchMoneyRequestReportEmptyState
report={report}
onLayout={onLayout}
policy={policy}
/>
</ScrollView>
)}
{!isReportEmpty && (
<FlatListWithScrollKey
initialNumToRender={initialNumToRender}
accessibilityLabel={translate('sidebarScreen.listOfChatMessages')}
testID="money-request-report-actions-list"
style={styles.overscrollBehaviorContain}
data={visibleReportActions}
renderItem={renderItem}
onViewableItemsChanged={onViewableItemsChanged}
keyExtractor={keyExtractor}
onLayout={recordTimeToMeasureItemLayout}
onEndReached={onEndReached}
onEndReachedThreshold={0.75}
onStartReached={onStartReached}
onStartReachedThreshold={0.75}
ListHeaderComponent={
<>
<MoneyRequestViewReportFields
report={report}
policy={policy}
/>
<MoneyRequestReportTransactionList
report={report}
onLayout={onLayout}
transactions={transactions}
newTransactions={newTransactions}
hasPendingDeletionTransaction={hasPendingDeletionTransaction}
reportActions={reportActions}
scrollToNewTransaction={scrollToNewTransaction}
policy={policy}
hasComments={visibleReportActions.length > 0}
isLoadingInitialReportActions={showReportActionsLoadingState}
/>
</>
}
keyboardShouldPersistTaps="handled"
onScroll={trackVerticalScrolling}
contentContainerStyle={[shouldUseNarrowLayout ? styles.pt4 : styles.pt3]}
ref={reportScrollManager.ref}
ListEmptyComponent={!isOffline && showReportActionsLoadingState ? <ReportActionsListLoadingSkeleton reasonAttributes={skeletonReasonAttributes} /> : undefined} // This skeleton component is only used for loading state, the empty state is handled by SearchMoneyRequestReportEmptyState
removeClippedSubviews={false}
initialScrollKey={linkedReportActionID}
/>
)}
</View>
</View>
);
}
export default MoneyRequestReportActionsList;