@@ -6,6 +6,8 @@ import * as S from './TransactionItem.styles';
66import { BaseRow } from '@app/components/common/BaseRow/BaseRow' ;
77import { BaseCol } from '@app/components/common/BaseCol/BaseCol' ;
88import { useBitcoinRates } from '@app/hooks/useBitcoinRates' ;
9+ import { getCurrencyPrice } from '@app/utils/utils' ;
10+ import { CurrencyTypeEnum } from '@app/interfaces/interfaces' ;
911
1012function makeHexId ( length : number ) : string {
1113 const characters = 'abcdef0123456789' ;
@@ -16,63 +18,91 @@ function makeHexId(length: number): string {
1618 return result ;
1719}
1820
19- function convertBtcToUsd ( btcValue : string , btcPriceInUsd : number ) : string {
20- const btcAmount = parseFloat ( btcValue ) ;
21- if ( btcAmount < 1 ) {
22- const satoshis = Math . round ( btcAmount * 100000000 ) ;
23- const usdValue = ( satoshis / 100000000 ) * btcPriceInUsd ;
24- return usdValue . toFixed ( 2 ) ;
25- } else {
26- const wholeBtc = Math . floor ( btcAmount ) ;
27- const satoshis = Math . round ( ( btcAmount - wholeBtc ) * 100000000 ) ;
28- const usdValue = ( wholeBtc * btcPriceInUsd ) + ( satoshis / 100000000 * btcPriceInUsd ) ;
29- return usdValue . toFixed ( 2 ) ;
30- }
31- }
32-
33- export const TransactionItem : React . FC < WalletTransaction > = ( { witness_tx_id, date, output, value } ) => {
21+ export const TransactionItem : React . FC < WalletTransaction > = ( {
22+ witness_tx_id,
23+ date,
24+ output,
25+ value,
26+ } ) => {
3427 const { t } = useTranslation ( ) ;
3528 const [ transactionId , setTransactionId ] = useState < string | null > ( null ) ;
36- const [ usdValue , setUsdValue ] = useState < string > ( '' ) ;
29+
3730 const { rates, isLoading, error } = useBitcoinRates ( ) ;
3831
32+ // Effect to initialize the transaction ID when the component mounts
3933 useEffect ( ( ) => {
4034 if ( ! witness_tx_id ) {
4135 setTransactionId ( makeHexId ( 64 ) ) ;
4236 }
4337 } , [ witness_tx_id ] ) ;
4438
39+ // Parse 'value' as BTC amount
40+ const btcAmount = parseFloat ( value ) / 100000000 ;
41+
42+ const [ usdValue , setUsdValue ] = useState < number | null > ( null ) ;
43+
4544 useEffect ( ( ) => {
46- if ( ! isLoading && ! error && rates . length > 0 ) {
47- const btcPrice = rates [ rates . length - 1 ] . usd_value ; // Get the most recent BTC price
48- const usdValueCalculated = convertBtcToUsd ( value , btcPrice ) ;
49- setUsdValue ( usdValueCalculated ) ;
45+ if ( ! isLoading && rates . length > 0 && btcAmount ) {
46+ // Convert the transaction date to a timestamp
47+ const transactionDate = new Date ( date ) . getTime ( ) ;
48+
49+ // Find the rate closest to the transaction date
50+ let closestRate = rates [ 0 ] ;
51+ let minDiff = Math . abs ( rates [ 0 ] . date - transactionDate ) ;
52+
53+ for ( let i = 1 ; i < rates . length ; i ++ ) {
54+ const diff = Math . abs ( rates [ i ] . date - transactionDate ) ;
55+ if ( diff < minDiff ) {
56+ minDiff = diff ;
57+ closestRate = rates [ i ] ;
58+ }
59+ }
60+
61+ const rate = closestRate . usd_value ;
62+
63+ // Compute the USD value
64+ const usdAmount = btcAmount * rate ;
65+ setUsdValue ( usdAmount ) ;
5066 }
51- } , [ value , rates , isLoading , error ] ) ;
67+ } , [ isLoading , rates , btcAmount , date ] ) ;
68+
69+ // Handle potential errors and loading states
70+ if ( error ) {
71+ return < div > Error loading exchange rates: { error } </ div > ;
72+ }
5273
53- // Skip rendering if the value is zero
54- if ( parseFloat ( value ) === 0 ) {
55- return null ;
74+ if ( isLoading || usdValue === null ) {
75+ return < div > Loading...</ div > ;
5676 }
5777
5878 return (
5979 < S . TransactionCard >
6080 < BaseRow gutter = { [ 20 , 20 ] } wrap = { true } align = "middle" >
6181 < BaseCol span = { 24 } >
62- < S . Label > { t ( 'Transaction ID' ) } :</ S . Label >
63- < S . Address style = { { color : 'var(--text-main)' } } > { witness_tx_id ? witness_tx_id : transactionId } </ S . Address >
82+ < S . Label > { 'Transaction ID' } :</ S . Label >
83+ < S . Address style = { { color : 'var(--text-main)' } } >
84+ { witness_tx_id ? witness_tx_id : transactionId }
85+ </ S . Address >
6486 </ BaseCol >
6587 < BaseCol span = { 24 } >
66- < S . Label > { t ( 'Address' ) } :</ S . Label >
88+ < S . Label > { 'Address' } :</ S . Label >
6789 < S . Output > { output } </ S . Output >
6890 </ BaseCol >
6991 < BaseCol span = { 12 } >
70- < S . Label > { t ( 'Date' ) } :</ S . Label >
92+ < S . Label > { 'Date' } :</ S . Label >
7193 < S . DateText > { Dates . getDate ( date ) . format ( 'L LTS' ) } </ S . DateText >
7294 </ BaseCol >
7395 < BaseCol span = { 12 } style = { { textAlign : 'right' } } >
74- < S . Label > { t ( 'Value' ) } :</ S . Label >
75- { usdValue && < S . Value > ${ usdValue } </ S . Value > }
96+ < S . Label > { 'Value' } :</ S . Label >
97+ < S . Value >
98+ { getCurrencyPrice (
99+ usdValue . toLocaleString ( undefined , {
100+ minimumFractionDigits : 2 ,
101+ maximumFractionDigits : 2 ,
102+ } ) ,
103+ CurrencyTypeEnum . USD
104+ ) }
105+ </ S . Value >
76106 </ BaseCol >
77107 </ BaseRow >
78108 </ S . TransactionCard >
0 commit comments