import React, { useState, useEffect } from 'react'; import { useParams, Link } from 'react-router-dom'; import { ArrowRight, Clock } from 'lucide-react'; import axios from 'axios'; import { format } from 'date-fns'; const API_URL = process.env.REACT_APP_API_URL || ''; function TransactionDetail() { const { txid } = useParams(); const [tx, setTx] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(''); useEffect(() => { fetchTransaction(); }, [txid]); const fetchTransaction = async () => { setLoading(true); setError(''); try { const response = await axios.get(`${API_URL}/api/tx/${txid}`); setTx(response.data); } catch (err) { setError(err.response?.data?.error || 'Transaction not found'); } finally { setLoading(false); } }; if (loading) { return (
); } if (error) { return
{error}
; } if (!tx) return null; // Use backend-calculated totals if available, otherwise calculate const totalInput = tx.totalInput !== undefined ? tx.totalInput : tx.vin?.reduce((sum, input) => sum + (input.value || 0), 0) || 0; const totalOutput = tx.totalOutput !== undefined ? tx.totalOutput : tx.vout?.reduce((sum, output) => sum + (output.value || 0), 0) || 0; const fee = tx.fees !== undefined ? tx.fees : (totalInput - totalOutput); return (

Transaction Details

{/* Transaction Info */}

Transaction Information

Transaction ID
{tx.txid}
{tx.blockhash && (
Block Hash
{tx.blockhash}
)}
{tx.confirmations !== undefined && (
Confirmations
{tx.confirmations}
)} {tx.time && (
Time
{format(new Date(tx.time * 1000), 'PPpp')}
)} {tx.size && (
Size
{tx.size} bytes
)} {fee > 0 && (
Fee
{fee.toFixed(8)} MAZA
)}
{/* Inputs and Outputs */}
{/* Inputs */}

Inputs ({tx.vin?.length || 0})

{tx.vin && tx.vin.length > 0 ? ( <>
{tx.vin.map((input, index) => (
{input.coinbase || input.isCoinbase ? (
Coinbase (Newly Generated Coins)
) : ( <> {input.txid && ( {input.txid}:{input.vout} )} {input.value !== undefined && input.value !== null ? ( <>
{input.value.toFixed(8)} MAZA
{input.address && input.address !== 'Unknown' && ( {input.address} )} ) : input.error ? (
{input.error}
) : (
Amount unknown
)} )}
))}
{totalInput > 0 && (
Total Input: {totalInput.toFixed(8)} MAZA
)} ) : (
No inputs
)}
{/* Outputs */}

Outputs ({tx.vout?.length || 0})

{tx.vout && tx.vout.length > 0 ? ( <>
{tx.vout.map((output, index) => (
{output.value.toFixed(8)} MAZA
{output.scriptPubKey?.addresses?.map((address, i) => ( {address} ))}
))}
{totalOutput > 0 && (
Total Output: {totalOutput.toFixed(8)} MAZA
)} ) : (
No outputs
)}
); } export default TransactionDetail;