import { useState, useEffect } from 'react';
import { useTransactions } from '@sunnyside-io/privacy-boost-react';
function TransactionHistory() {
const { transactions, isLoading, fetchHistory } = useTransactions();
const [filter, setFilter] = useState<string>('all');
useEffect(() => {
if (filter === 'all') {
fetchHistory({ limit: 50 });
} else {
fetchHistory({ txType: filter, limit: 50 });
}
}, [filter, fetchHistory]);
const formatAmount = (amount: bigint, decimals = 18) => {
return (Number(amount) / 10 ** decimals).toFixed(4);
};
const formatDate = (timestamp: number) => {
return new Date(timestamp).toLocaleDateString();
};
return (
<div>
{/* Filter Tabs */}
<div className="tabs">
{['all', 'deposit', 'withdraw', 'transfer'].map((type) => (
<button
key={type}
onClick={() => setFilter(type)}
className={filter === type ? 'active' : ''}
>
{type.charAt(0).toUpperCase() + type.slice(1)}
</button>
))}
</div>
{/* Loading State */}
{isLoading && <div>Loading transactions...</div>}
{/* Transaction List */}
{!isLoading && transactions.length === 0 && (
<p>No transactions found</p>
)}
<table>
<thead>
<tr>
<th>Type</th>
<th>Amount</th>
<th>Status</th>
<th>Date</th>
</tr>
</thead>
<tbody>
{transactions.map((tx) => (
<tr key={tx.txHash}>
<td>
<span className={`type-${tx.type}`}>
{tx.type}
</span>
{tx.direction === 'incoming' && ' (received)'}
</td>
<td>{tx.amount ? formatAmount(tx.amount) : '-'}</td>
<td>
<span className={`status-${tx.status}`}>
{tx.status}
</span>
</td>
<td>{formatDate(tx.createdAt)}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}