Connect wallet transaction pagination to server API

This commit is contained in:
Unheard0840
2026-08-29 17:48:19 +02:00
parent e56c9fde81
commit 83070ce195
+105 -27
View File
@@ -1477,41 +1477,115 @@
}); });
} }
function getVisibleTransactionCount(wallet) { async function showMoreTransactions(encodedAddress) {
const total = getWalletTransactions(wallet).length; const address =
const current = visibleTransactionCounts.get(wallet.address); decodeURIComponent(
encodedAddress,
);
return Number.isFinite(current) && current > 0 const wallet =
? Math.min(current, total) wallets.find(
: Math.min(50, total); (item) =>
} item.address === address,
);
function showMoreTransactions(encodedAddress) {
const address = decodeURIComponent(encodedAddress);
const wallet = wallets.find(
(item) => item.address === address,
);
if (!wallet) { if (!wallet) {
return; return;
} }
const total = getWalletTransactions(wallet).length; const current =
const current = getVisibleTransactionCount(wallet); getWalletTransactions(wallet).length;
visibleTransactionCounts.set( const total =
address, Number(wallet.transactionCount) ||
Math.min(current + 50, total), current;
);
renderWallets(); if (current >= total) {
return;
}
const button =
document.querySelector(
`[data-show-more-address="${encodeURIComponent(address)}"]`,
);
if (button) {
button.disabled = true;
button.textContent = 'Loading...';
}
try {
const response =
await fetch(
`/api/addresses/transactions?address=${encodeURIComponent(address)}&offset=${current}&limit=50`,
{
credentials:
'same-origin',
},
);
if (
response.status === 401
) {
showLogin();
return;
}
if (!response.ok) {
throw new Error(
`HTTP ${response.status}`,
);
}
const data =
await response.json();
const transactions =
Array.isArray(
data.transactions,
)
? data.transactions
: [];
wallet.history = [
...getWalletTransactions(wallet),
...transactions,
];
wallet.transactionCount =
Number(data.total) ||
wallet.history.length;
wallet.transactionOffset =
current;
renderWallets();
} catch (error) {
console.error(
'Failed to load more transactions:',
error,
);
renderWallets();
}
} }
function renderWalletContent(wallet, transactionCount) { function renderWalletContent(
const transactions = getWalletTransactions(wallet); wallet,
const visibleCount = getVisibleTransactionCount(wallet); transactionCount,
const visibleTransactions = transactions.slice(0, visibleCount); ) {
const hasMore = visibleCount < transactions.length; const transactions =
getWalletTransactions(wallet);
const visibleCount =
transactions.length;
const total =
Number(wallet.transactionCount) ||
transactions.length;
const hasMore =
visibleCount < total;
return ` return `
<div class="wallet-content"> <div class="wallet-content">
@@ -1521,7 +1595,10 @@
${ ${
transactions.length transactions.length
? renderTransactions(wallet, visibleTransactions) ? renderTransactions(
wallet,
transactions,
)
: ` : `
<div class="empty"> <div class="empty">
No transactions found for this wallet. No transactions found for this wallet.
@@ -1535,10 +1612,11 @@
<button <button
class="button" class="button"
type="button" type="button"
data-show-more-address="${encodeURIComponent(wallet.address)}"
onclick="showMoreTransactions('${encodeURIComponent(wallet.address)}')" onclick="showMoreTransactions('${encodeURIComponent(wallet.address)}')"
style="width: 100%; margin-top: 14px;" style="width: 100%; margin-top: 14px;"
> >
Show more (${visibleCount} of ${transactions.length}) Show more (${visibleCount} of ${total})
</button> </button>
` `
: '' : ''