Skip to content

Commit 083d5a5

Browse files
committed
finally moved sats conversion to usd to the front-end/panel. It has been tested and it works.
1 parent 65692eb commit 083d5a5

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

src/components/relay-dashboard/transactions/TransactionItem/TransactionItem.tsx

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import React, {useEffect, useState} from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { useTranslation } from 'react-i18next';
33
import { WalletTransaction } from '@app/api/activity.api';
44
import { Dates } from '@app/constants/Dates';
5-
import { formatSatsWithCommas, getSatsCurrency } from '@app/utils/utils';
6-
import { CurrencyTypeEnum } from '@app/interfaces/interfaces';
75
import * as S from './TransactionItem.styles';
86
import { BaseRow } from '@app/components/common/BaseRow/BaseRow';
97
import { BaseCol } from '@app/components/common/BaseCol/BaseCol';
8+
import { useBitcoinRates } from '@app/hooks/useBitcoinRates';
9+
1010
function makeHexId(length: number): string {
1111
const characters = 'abcdef0123456789';
1212
let result = '';
@@ -16,27 +16,51 @@ function makeHexId(length: number): string {
1616
return result;
1717
}
1818

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+
1933
export const TransactionItem: React.FC<WalletTransaction> = ({ witness_tx_id, date, output, value }) => {
2034
const { t } = useTranslation();
2135
const [transactionId, setTransactionId] = useState<string | null>(null);
36+
const [usdValue, setUsdValue] = useState<string>('');
37+
const { rates, isLoading, error } = useBitcoinRates();
2238

23-
// Effect to initialize the transaction ID when the component mounts
2439
useEffect(() => {
2540
if (!witness_tx_id) {
2641
setTransactionId(makeHexId(64));
2742
}
2843
}, [witness_tx_id]);
2944

30-
const numericValue = parseFloat(value);
31-
45+
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);
50+
}
51+
}, [value, rates, isLoading, error]);
3252

53+
// Skip rendering if the value is zero
54+
if (parseFloat(value) === 0) {
55+
return null;
56+
}
3357

3458
return (
3559
<S.TransactionCard>
3660
<BaseRow gutter={[20, 20]} wrap={true} align="middle">
3761
<BaseCol span={24}>
3862
<S.Label>{t('Transaction ID')}:</S.Label>
39-
<S.Address style={{ color: 'var(--text-main)' }}> {witness_tx_id ? witness_tx_id : transactionId}</S.Address>
63+
<S.Address style={{ color: 'var(--text-main)' }}> {witness_tx_id ? witness_tx_id : transactionId}</S.Address>
4064
</BaseCol>
4165
<BaseCol span={24}>
4266
<S.Label>{t('Address')}:</S.Label>
@@ -48,7 +72,7 @@ export const TransactionItem: React.FC<WalletTransaction> = ({ witness_tx_id, da
4872
</BaseCol>
4973
<BaseCol span={12} style={{ textAlign: 'right' }}>
5074
<S.Label>{t('Value')}:</S.Label>
51-
<S.Value>{getSatsCurrency(formatSatsWithCommas(numericValue), CurrencyTypeEnum.SATS)}</S.Value>
75+
{usdValue && <S.Value>${usdValue}</S.Value>}
5276
</BaseCol>
5377
</BaseRow>
5478
</S.TransactionCard>

0 commit comments

Comments
 (0)