1
+ import { useQuery } from '@tanstack/react-query'
1
2
import { abs } from 'biggystring'
2
3
import {
3
4
EdgeAccount ,
@@ -9,6 +10,7 @@ import {
9
10
EdgeTxSwap
10
11
} from 'edge-core-js'
11
12
import * as React from 'react'
13
+ import { useMemo } from 'react'
12
14
import { View } from 'react-native'
13
15
import FastImage from 'react-native-fast-image'
14
16
import IonIcon from 'react-native-vector-icons/Ionicons'
@@ -34,8 +36,10 @@ import { getExchangeDenom } from '../../selectors/DenominationSelectors'
34
36
import { useSelector } from '../../types/reactRedux'
35
37
import { EdgeAppSceneProps } from '../../types/routerTypes'
36
38
import { getCurrencyCodeWithAccount } from '../../util/CurrencyInfoHelpers'
39
+ import { getEdgeTxSwapFromReportTxInfo } from '../../util/getEdgeTxSwapFromReportTxInfo'
37
40
import { matchJson } from '../../util/matchJson'
38
41
import { getMemoTitle } from '../../util/memoUtils'
42
+ import { fetchGetTxInfo } from '../../util/reportsServer'
39
43
import {
40
44
convertCurrencyFromExchangeRates ,
41
45
convertNativeToExchange ,
@@ -86,6 +90,37 @@ const TransactionDetailsComponent = (props: Props) => {
86
90
const styles = getStyles ( theme )
87
91
const iconColor = useIconColor ( { pluginId : currencyInfo . pluginId , tokenId } )
88
92
93
+ const transactionSwapData =
94
+ convertActionToSwapData ( account , transaction ) ?? transaction . swapData
95
+
96
+ // Query for transaction info from reports server only if the transaction is
97
+ // a receive (we need to get potential swap data) or the transaction has
98
+ // swap data (we need to get the status)
99
+ const shouldShowTradeDetails =
100
+ ! transaction . isSend || transactionSwapData != null
101
+ const { data : reportsTxInfo , isLoading : isReportsTxInfoLoading } = useQuery ( {
102
+ queryKey : [ 'txInfo' , transaction . txid ] ,
103
+ queryFn : async ( ) => {
104
+ return await fetchGetTxInfo ( transaction )
105
+ } ,
106
+ staleTime : query =>
107
+ // Only cache if the status has resolved, otherwise we'll always consider
108
+ // the data to be stale:
109
+ [ 'processing' , 'pending' , undefined ] . includes ( query . state . data ?. status )
110
+ ? 0 // No cache
111
+ : Infinity , // Cache forever
112
+ enabled : shouldShowTradeDetails ,
113
+ retry : false
114
+ } )
115
+
116
+ const swapDataFromReports = useMemo (
117
+ ( ) =>
118
+ reportsTxInfo == null
119
+ ? null
120
+ : getEdgeTxSwapFromReportTxInfo ( wallet , transaction , reportsTxInfo ) ,
121
+ [ reportsTxInfo , transaction , wallet ]
122
+ )
123
+
89
124
// Choose a default category based on metadata or the txAction
90
125
const {
91
126
action,
@@ -96,8 +131,7 @@ const TransactionDetailsComponent = (props: Props) => {
96
131
savedData
97
132
} = getTxActionDisplayInfo ( transaction , account , wallet )
98
133
99
- const swapData =
100
- convertActionToSwapData ( account , transaction ) ?? transaction . swapData
134
+ const swapData = transactionSwapData ?? swapDataFromReports
101
135
102
136
const thumbnailPath =
103
137
useContactThumbnail ( mergedData . name ) ?? pluginIdIcons [ iconPluginId ?? '' ]
@@ -546,7 +580,11 @@ const TransactionDetailsComponent = (props: Props) => {
546
580
< SwapDetailsCard
547
581
swapData = { swapData }
548
582
transaction = { transaction }
549
- sourceWallet = { wallet }
583
+ sourceWallet = {
584
+ swapData . payoutWalletId === wallet . id ? undefined : wallet
585
+ }
586
+ reportsTxInfo = { reportsTxInfo }
587
+ isReportsTxInfoLoading = { isReportsTxInfoLoading }
550
588
/>
551
589
) }
552
590
</ EdgeAnim >
@@ -591,6 +629,7 @@ const TransactionDetailsComponent = (props: Props) => {
591
629
) }
592
630
/>
593
631
</ EdgeAnim >
632
+
594
633
< EdgeAnim enter = { { type : 'fadeInDown' , distance : 140 } } >
595
634
< ButtonsView
596
635
layout = "column"
0 commit comments