1+ import { useQuery } from '@tanstack/react-query'
12import { abs } from 'biggystring'
23import type {
34 EdgeAccount ,
@@ -8,6 +9,7 @@ import type {
89 EdgeTxSwap
910} from 'edge-core-js'
1011import * as React from 'react'
12+ import { useMemo } from 'react'
1113import { View } from 'react-native'
1214import FastImage from 'react-native-fast-image'
1315import IonIcon from 'react-native-vector-icons/Ionicons'
@@ -36,6 +38,11 @@ import type { EdgeAppSceneProps } from '../../types/routerTypes'
3638import { getCurrencyCodeWithAccount } from '../../util/CurrencyInfoHelpers'
3739import { matchJson } from '../../util/matchJson'
3840import { getMemoTitle } from '../../util/memoUtils'
41+ import {
42+ queryReportsTxInfo ,
43+ toEdgeTxActionSwap ,
44+ toEdgeTxSwap
45+ } from '../../util/reportsServer'
3946import {
4047 convertNativeToExchange ,
4148 darkenHexColor ,
@@ -86,6 +93,55 @@ export const TransactionDetailsComponent: React.FC<Props> = props => {
8693 const styles = getStyles ( theme )
8794 const iconColor = useIconColor ( { pluginId : currencyInfo . pluginId , tokenId } )
8895
96+ const transactionSwapData = ( ) : EdgeTxSwap | undefined =>
97+ convertActionToSwapData ( account , transaction ) ?? transaction . swapData
98+
99+ // Query for transaction info from reports server only if the transaction is
100+ // a receive (we need to get potential swap data) or the transaction has
101+ // swap data (we need to get the status)
102+ const shouldShowTradeDetails =
103+ ! transaction . isSend || transactionSwapData ( ) != null
104+ const { data : reportsTxInfo , isLoading : isReportsTxInfoLoading } = useQuery ( {
105+ queryKey : [ 'txInfo' , transaction . txid ] ,
106+ queryFn : async ( ) => {
107+ return await queryReportsTxInfo ( wallet , transaction )
108+ } ,
109+ staleTime : query =>
110+ // Only cache if the status has resolved, otherwise we'll always consider
111+ // the data to be stale:
112+ [ 'processing' , 'pending' , undefined ] . includes (
113+ query . state . data ?. swapInfo . status
114+ )
115+ ? 0 // No cache
116+ : Infinity , // Cache forever
117+ enabled : shouldShowTradeDetails ,
118+ retry : false
119+ } )
120+
121+ const swapDataFromReports = useMemo (
122+ ( ) =>
123+ reportsTxInfo == null
124+ ? undefined
125+ : toEdgeTxSwap ( account , wallet , transaction , reportsTxInfo ) ,
126+ [ account , reportsTxInfo , transaction , wallet ]
127+ )
128+
129+ const edgeTxActionSwapFromReports = useMemo ( ( ) => {
130+ if ( reportsTxInfo == null ) return
131+ return toEdgeTxActionSwap ( account , transaction , reportsTxInfo )
132+ } , [ account , reportsTxInfo , transaction ] )
133+
134+ // Update the transaction object with saveAction data from reports server:
135+ if (
136+ edgeTxActionSwapFromReports != null &&
137+ transaction . savedAction !== edgeTxActionSwapFromReports
138+ ) {
139+ transaction . savedAction = edgeTxActionSwapFromReports
140+ transaction . assetAction = {
141+ assetActionType : 'swap'
142+ }
143+ }
144+
89145 // Choose a default category based on metadata or the txAction
90146 const {
91147 action,
@@ -96,8 +152,7 @@ export const TransactionDetailsComponent: React.FC<Props> = props => {
96152 savedData
97153 } = getTxActionDisplayInfo ( transaction , account , wallet )
98154
99- const swapData =
100- convertActionToSwapData ( account , transaction ) ?? transaction . swapData
155+ const swapData = transactionSwapData ( ) ?? swapDataFromReports
101156
102157 const thumbnailPath =
103158 useContactThumbnail ( mergedData . name ) ?? pluginIdIcons [ iconPluginId ?? '' ]
@@ -610,7 +665,11 @@ export const TransactionDetailsComponent: React.FC<Props> = props => {
610665 < SwapDetailsCard
611666 swapData = { swapData }
612667 transaction = { transaction }
613- sourceWallet = { wallet }
668+ sourceWallet = {
669+ swapData . payoutWalletId === wallet . id ? undefined : wallet
670+ }
671+ reportsTxInfo = { reportsTxInfo ?? undefined }
672+ isReportsTxInfoLoading = { isReportsTxInfoLoading }
614673 />
615674 ) }
616675 </ EdgeAnim >
0 commit comments