11import { useIsFocused } from '@react-navigation/native' ;
22import { useEffect , useEffectEvent , useMemo } from 'react' ;
33import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails' ;
4+ import useLocalize from '@hooks/useLocalize' ;
45import useNetwork from '@hooks/useNetwork' ;
56import useOnyx from '@hooks/useOnyx' ;
67import { search } from '@libs/actions/Search' ;
78import { getIOUActionForTransactionID } from '@libs/ReportActionsUtils' ;
89import { buildQueryStringFromFilterFormValues , buildSearchQueryJSON } from '@libs/SearchQueryUtils' ;
9- import { getAmount , getCreated , getCurrency , getMerchant } from '@libs/TransactionUtils' ;
10+ import { getAmount , getCreated , getCurrency , getMerchantName } from '@libs/TransactionUtils' ;
1011import CONST from '@src/CONST' ;
1112import ONYXKEYS from '@src/ONYXKEYS' ;
1213import type { Report , ReportAction , Transaction } from '@src/types/onyx' ;
@@ -52,6 +53,7 @@ type RecentlyAddedExpense = {
5253function useRecentlyAddedData ( ) : { transactions : RecentlyAddedExpense [ ] } {
5354 const { accountID} = useCurrentUserPersonalDetails ( ) ;
5455 const { isOffline} = useNetwork ( ) ;
56+ const { translate} = useLocalize ( ) ;
5557 const isFocused = useIsFocused ( ) ;
5658
5759 const query = useMemo (
@@ -114,6 +116,8 @@ function useRecentlyAddedData(): {transactions: RecentlyAddedExpense[]} {
114116 }
115117
116118 const reportOwnerByReportID = new Map < string , number | undefined > ( ) ;
119+ const reportTypeByReportID = new Map < string , string | undefined > ( ) ;
120+ const reportChatTypeByReportID = new Map < string , string | undefined > ( ) ;
117121 const snapshotTransactions : Transaction [ ] = [ ] ;
118122 const snapshotReportActions : ReportAction [ ] = [ ] ;
119123 // Snapshot data is a keyed record where the key prefix determines the value type.
@@ -128,6 +132,8 @@ function useRecentlyAddedData(): {transactions: RecentlyAddedExpense[]} {
128132 const report = value as Report ;
129133 if ( report ?. reportID ) {
130134 reportOwnerByReportID . set ( report . reportID , report . ownerAccountID ) ;
135+ reportTypeByReportID . set ( report . reportID , report . type ) ;
136+ reportChatTypeByReportID . set ( report . reportID , report . chatType ) ;
131137 }
132138 continue ;
133139 }
@@ -164,17 +170,28 @@ function useRecentlyAddedData(): {transactions: RecentlyAddedExpense[]} {
164170 return firstKey < secondKey ? 1 : - 1 ;
165171 } )
166172 . slice ( 0 , CONST . HOME . SECTION_VISIBLE_LIMIT )
167- . map ( ( transaction ) => ( {
168- transactionID : transaction . transactionID ,
169- reportID : transaction . reportID ,
170- created : getCreated ( transaction ) ,
171- merchant : getMerchant ( transaction ) ,
172- amount : getAmount ( transaction ) ,
173- currency : getCurrency ( transaction ) ,
174- threadReportID : getIOUActionForTransactionID ( snapshotReportActions , transaction . transactionID ) ?. childReportID ,
175- transaction,
176- } ) ) ;
177- } , [ snapshotData , accountID , insertedByTransactionID ] ) ;
173+ . map ( ( transaction ) => {
174+ const reportType = reportTypeByReportID . get ( transaction . reportID ) ;
175+ const isFromExpenseReport = reportType === CONST . REPORT . TYPE . EXPENSE ;
176+ // Self-DM and unreported (tracked) expenses support signed amounts like expense reports, so their
177+ // sign must be preserved too. Without this, a self-DM credit/refund is collapsed to its absolute
178+ // value and loses its negative sign.
179+ const isFromTrackedExpense =
180+ transaction . reportID === CONST . REPORT . UNREPORTED_REPORT_ID || reportChatTypeByReportID . get ( transaction . reportID ) === CONST . REPORT . CHAT_TYPE . SELF_DM ;
181+ return {
182+ transactionID : transaction . transactionID ,
183+ reportID : transaction . reportID ,
184+ created : getCreated ( transaction ) ,
185+ merchant : getMerchantName ( transaction , translate ) ,
186+ // Expense-report, self-DM, and tracked transactions are stored with an inverted sign, so the
187+ // displayed amount must be negated for them (mirrors the Search transaction list).
188+ amount : getAmount ( transaction , isFromExpenseReport , isFromTrackedExpense ) ,
189+ currency : getCurrency ( transaction ) ,
190+ threadReportID : getIOUActionForTransactionID ( snapshotReportActions , transaction . transactionID ) ?. childReportID ,
191+ transaction,
192+ } ;
193+ } ) ;
194+ } , [ snapshotData , accountID , insertedByTransactionID , translate ] ) ;
178195
179196 return { transactions} ;
180197}
0 commit comments