11import { useIsFocused } from '@react-navigation/native' ;
2- import { useCallback , useMemo , useRef } from 'react' ;
32import type { OnyxEntry } from 'react-native-onyx' ;
43import { getNewerActions , getOlderActions } from '@userActions/Report' ;
54import CONST from '@src/CONST' ;
@@ -11,9 +10,6 @@ type UseLoadReportActionsArguments = {
1110 /** The id of the current report */
1211 reportID : string ;
1312
14- /** The id of the reportAction (if specific action was linked to */
15- reportActionID ?: string ;
16-
1713 /** The list of reportActions linked to the current report */
1814 reportActions : ReportAction [ ] ;
1915
@@ -34,121 +30,87 @@ type UseLoadReportActionsArguments = {
3430 * Provides reusable logic to get the functions for loading older/newer reportActions.
3531 * Used in the report displaying components
3632 */
37- function useLoadReportActions ( { reportID, reportActionID, reportActions, allReportActionIDs, transactionThreadReport, hasOlderActions, hasNewerActions} : UseLoadReportActionsArguments ) {
38- const didLoadOlderChats = useRef ( false ) ;
39- const didLoadNewerChats = useRef ( false ) ;
40-
33+ function useLoadReportActions ( { reportID, reportActions, allReportActionIDs, transactionThreadReport, hasOlderActions, hasNewerActions} : UseLoadReportActionsArguments ) {
4134 const { isOffline} = useNetwork ( ) ;
4235 const isFocused = useIsFocused ( ) ;
36+ const newestReportAction = reportActions ?. at ( 0 ) ;
37+ const oldestReportAction = reportActions ?. at ( - 1 ) ;
38+
39+ const isTransactionThreadReport = ! isEmptyObject ( transactionThreadReport ) ;
40+
41+ let currentReportNewestAction = null ;
42+ let currentReportOldestAction = null ;
43+ let transactionThreadNewestAction = null ;
44+ let transactionThreadOldestAction = null ;
45+
46+ const allReportActionIDsSet = new Set ( allReportActionIDs ) ;
47+
48+ for ( const action of reportActions ) {
49+ // Determine which report this action belongs to
50+ const isCurrentReport = allReportActionIDsSet . has ( action . reportActionID ) ;
51+ const targetReportID = isCurrentReport ? reportID : transactionThreadReport ?. reportID ;
4352
44- const newestReportAction = useMemo ( ( ) => reportActions ?. at ( 0 ) , [ reportActions ] ) ;
45- const oldestReportAction = useMemo ( ( ) => reportActions ?. at ( - 1 ) , [ reportActions ] ) ;
46-
47- // Track oldest/newest actions per report in a single pass
48- const { currentReportOldest, currentReportNewest, transactionThreadOldest, transactionThreadNewest} = useMemo ( ( ) => {
49- let currentReportNewestAction = null ;
50- let currentReportOldestAction = null ;
51- let transactionThreadNewestAction = null ;
52- let transactionThreadOldestAction = null ;
53-
54- const allReportActionIDsSet = new Set ( allReportActionIDs ) ;
55-
56- for ( const action of reportActions ) {
57- // Determine which report this action belongs to
58- const isCurrentReport = allReportActionIDsSet . has ( action . reportActionID ) ;
59- const targetReportID = isCurrentReport ? reportID : transactionThreadReport ?. reportID ;
60-
61- // Track newest/oldest per report
62- if ( targetReportID === reportID ) {
63- // Newest = first matching action we encounter
64- if ( ! currentReportNewestAction ) {
65- currentReportNewestAction = action ;
66- }
67- // Oldest = last matching action we encounter
68- currentReportOldestAction = action ;
69- } else if ( ! isEmptyObject ( transactionThreadReport ) && transactionThreadReport ?. reportID === targetReportID ) {
70- // Same logic for transaction thread
71- if ( ! transactionThreadNewestAction ) {
72- transactionThreadNewestAction = action ;
73- }
74- transactionThreadOldestAction = action ;
53+ // Track newest/oldest per report
54+ if ( targetReportID === reportID ) {
55+ // Newest = first matching action we encounter
56+ if ( ! currentReportNewestAction ) {
57+ currentReportNewestAction = action ;
7558 }
59+ // Oldest = last matching action we encounter
60+ currentReportOldestAction = action ;
61+ } else if ( isTransactionThreadReport && transactionThreadReport ?. reportID === targetReportID ) {
62+ // Same logic for transaction thread
63+ if ( ! transactionThreadNewestAction ) {
64+ transactionThreadNewestAction = action ;
65+ }
66+ transactionThreadOldestAction = action ;
7667 }
77-
78- return {
79- currentReportOldest : currentReportOldestAction ,
80- currentReportNewest : currentReportNewestAction ,
81- transactionThreadOldest : transactionThreadOldestAction ,
82- transactionThreadNewest : transactionThreadNewestAction ,
83- } ;
84- } , [ reportActions , allReportActionIDs , reportID , transactionThreadReport ] ) ;
68+ }
8569
8670 /**
8771 * Retrieves the next set of reportActions for the chat once we are nearing the end of what we are currently
8872 * displaying.
8973 */
90- const loadOlderChats = useCallback (
91- ( force = false ) => {
92- // Only fetch more if we are neither already fetching (so that we don't initiate duplicate requests) nor offline.
93- if ( ! force && isOffline ) {
94- return ;
95- }
96-
97- // Don't load more reportActions if we're already at the beginning of the chat history
98- if ( ! oldestReportAction || ! hasOlderActions ) {
99- return ;
100- }
74+ const loadOlderChats = ( force = false ) => {
75+ // Only fetch more if we are neither already fetching (so that we don't initiate duplicate requests) nor offline.
76+ if ( ! force && isOffline ) {
77+ return ;
78+ }
10179
102- didLoadOlderChats . current = true ;
80+ // Don't load more reportActions if we're already at the beginning of the chat history
81+ if ( ! oldestReportAction || ! hasOlderActions ) {
82+ return ;
83+ }
10384
104- if ( ! isEmptyObject ( transactionThreadReport ) ) {
105- getOlderActions ( reportID , currentReportOldest ?. reportActionID ) ;
106- getOlderActions ( transactionThreadReport . reportID , transactionThreadOldest ?. reportActionID ) ;
107- } else {
108- getOlderActions ( reportID , currentReportOldest ?. reportActionID ) ;
109- }
110- } ,
111- [ isOffline , oldestReportAction , hasOlderActions , transactionThreadReport , reportID , currentReportOldest ?. reportActionID , transactionThreadOldest ?. reportActionID ] ,
112- ) ;
113-
114- const loadNewerChats = useCallback (
115- ( force = false ) => {
116- if (
117- ! force &&
118- ( ! reportActionID ||
119- ! isFocused ||
120- ! newestReportAction ||
121- ! hasNewerActions ||
122- isOffline ||
123- // If there was an error only try again once on initial mount. We should also still load
124- // more in case we have cached messages.
125- didLoadNewerChats . current ||
126- newestReportAction . pendingAction === CONST . RED_BRICK_ROAD_PENDING_ACTION . DELETE )
127- ) {
128- return ;
129- }
85+ if ( isTransactionThreadReport ) {
86+ getOlderActions ( reportID , currentReportOldestAction ?. reportActionID ) ;
87+ getOlderActions ( transactionThreadReport ?. reportID , transactionThreadOldestAction ?. reportActionID ) ;
88+ } else {
89+ getOlderActions ( reportID , currentReportOldestAction ?. reportActionID ) ;
90+ }
91+ } ;
13092
131- didLoadNewerChats . current = true ;
93+ const loadNewerChats = ( force = false ) => {
94+ if (
95+ ! force &&
96+ ( ! isFocused ||
97+ ! newestReportAction ||
98+ ! hasNewerActions ||
99+ isOffline ||
100+ // If there was an error only try again once on initial mount. We should also still load
101+ // more in case we have cached messages.
102+ newestReportAction . pendingAction === CONST . RED_BRICK_ROAD_PENDING_ACTION . DELETE )
103+ ) {
104+ return ;
105+ }
132106
133- if ( ! isEmptyObject ( transactionThreadReport ) ) {
134- getNewerActions ( reportID , currentReportNewest ?. reportActionID ) ;
135- getNewerActions ( transactionThreadReport . reportID , transactionThreadNewest ?. reportActionID ) ;
136- } else if ( newestReportAction ) {
137- getNewerActions ( reportID , newestReportAction . reportActionID ) ;
138- }
139- } ,
140- [
141- reportActionID ,
142- isFocused ,
143- newestReportAction ,
144- hasNewerActions ,
145- isOffline ,
146- transactionThreadReport ,
147- reportID ,
148- currentReportNewest ?. reportActionID ,
149- transactionThreadNewest ?. reportActionID ,
150- ] ,
151- ) ;
107+ if ( isTransactionThreadReport ) {
108+ getNewerActions ( reportID , currentReportNewestAction ?. reportActionID ) ;
109+ getNewerActions ( transactionThreadReport . reportID , transactionThreadNewestAction ?. reportActionID ) ;
110+ } else if ( newestReportAction ) {
111+ getNewerActions ( reportID , newestReportAction . reportActionID ) ;
112+ }
113+ } ;
152114
153115 return {
154116 loadOlderChats,
0 commit comments