SHA256
155 lines
7.1 KiB
JavaScript
155 lines
7.1 KiB
JavaScript
import { t } from '../i18n/index.js';
|
||
import { authService } from '../state.js';
|
||
import { getSolanaCluster } from '../services/solana-rpc-gateway.js';
|
||
import { formatStars } from '../services/shine-support-amount.js';
|
||
|
||
export function supportStatsValues(stats = {}) {
|
||
const sent = BigInt(stats.supportSent ?? 0);
|
||
const received = BigInt(stats.supportReceived ?? 0);
|
||
const daoDirect = BigInt(stats.daoDirect ?? 0);
|
||
return [sent, received, daoDirect];
|
||
}
|
||
|
||
/** Adds an independent asynchronous block; failure/Free accounts do not get fictitious statistics. */
|
||
export function attachSupportStats(parent, { login, isActive = () => true }) {
|
||
const section = document.createElement('section');
|
||
section.className = 'card stack profile-support-stats';
|
||
section.style.cssText = 'margin-top:12px;gap:8px;';
|
||
const title = document.createElement('strong');
|
||
title.textContent = t('screens.support_phase2.statistics');
|
||
const content = document.createElement('div');
|
||
content.className = 'meta-muted';
|
||
content.textContent = t('screens.support_phase2.loading');
|
||
section.append(title, content);
|
||
const history = document.createElement('details');
|
||
history.className = 'profile-support-history';
|
||
const historySummary = document.createElement('summary');
|
||
historySummary.textContent = t('screens.support_phase2.history');
|
||
const historyRows = document.createElement('div');
|
||
historyRows.className = 'stack';
|
||
historyRows.style.cssText = 'gap:8px;margin-top:10px;';
|
||
history.append(historySummary, historyRows);
|
||
section.append(history);
|
||
parent.append(section);
|
||
let historyOffset = 0;
|
||
let loadingHistory = false;
|
||
let historyLoaded = false;
|
||
const historyLogin = String(login || '').trim().toLowerCase();
|
||
const loadHistory = async () => {
|
||
if (loadingHistory || !isActive() || !section.isConnected) return;
|
||
loadingHistory = true;
|
||
const note = document.createElement('div');
|
||
note.className = 'meta-muted';
|
||
note.textContent = t('screens.support_phase2.loading');
|
||
historyRows.append(note);
|
||
try {
|
||
const result = await authService.getSupportHistory(login, 20, historyOffset);
|
||
if (!isActive() || !section.isConnected) return;
|
||
note.remove();
|
||
if (!result.ready) {
|
||
historyRows.textContent = t('screens.support_phase2.loading');
|
||
return;
|
||
}
|
||
if (!historyOffset && !result.transfers?.length) {
|
||
historyRows.textContent = t('screens.support_phase2.no_history');
|
||
}
|
||
const more = historyRows.querySelector('[data-stars-more]');
|
||
more?.remove();
|
||
for (const transfer of result.transfers || []) {
|
||
const outgoing = transfer.senderLogin?.trim().toLowerCase() === historyLogin;
|
||
const cents = transfer.kind === 'DAO' || outgoing
|
||
? transfer.amountStarCents : transfer.recipientStarCents;
|
||
const row = document.createElement('div');
|
||
row.className = 'row';
|
||
row.style.cssText = 'display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:6px;';
|
||
const info = document.createElement('span');
|
||
info.textContent = transfer.kind === 'DAO'
|
||
? t('screens.support_phase2.history_dao')
|
||
: `${outgoing ? '→' : '←'} ${outgoing ? transfer.recipientLogin : transfer.senderLogin}`;
|
||
const value = document.createElement('strong');
|
||
value.textContent = `${outgoing ? '−' : '+'}${formatStars(cents)}`;
|
||
const detail = document.createElement('small');
|
||
detail.className = 'meta-muted';
|
||
const date = transfer.blockTime ? new Date(Number(transfer.blockTime) * 1000).toLocaleString() : '';
|
||
const lamports = BigInt(transfer.amountLamports || '0');
|
||
const sol = `${lamports / 1_000_000_000n}.${String(lamports % 1_000_000_000n).padStart(9, '0').replace(/0+$/, '') || '0'} SOL`;
|
||
detail.textContent = `${date} · ${sol}`;
|
||
const explorer = document.createElement('a');
|
||
const sig = String(transfer.signature || '');
|
||
if (/^[1-9A-HJ-NP-Za-km-z]{64,90}$/.test(sig)) {
|
||
const network = getSolanaCluster();
|
||
explorer.href = `https://explorer.solana.com/tx/${sig}${network && network !== 'mainnet-beta' ? `?cluster=${encodeURIComponent(network)}` : ''}`;
|
||
explorer.target = '_blank'; explorer.rel = 'noopener noreferrer';
|
||
explorer.textContent = t('screens.support_phase2.transaction');
|
||
row.append(info, value, detail, explorer);
|
||
} else row.append(info, value, detail);
|
||
historyRows.append(row);
|
||
}
|
||
historyOffset += (result.transfers || []).length;
|
||
if (result.hasMore) {
|
||
const btn = document.createElement('button');
|
||
btn.className = 'text-btn'; btn.type = 'button';
|
||
btn.dataset.starsMore = '1';
|
||
btn.textContent = t('screens.support_phase2.more');
|
||
btn.addEventListener('click', () => void loadHistory());
|
||
historyRows.append(btn);
|
||
}
|
||
historyLoaded = true;
|
||
} catch {
|
||
if (isActive() && section.isConnected) note.textContent = t('screens.support_phase2.history_unavailable');
|
||
} finally { loadingHistory = false; }
|
||
};
|
||
history.addEventListener('toggle', () => {
|
||
if (history.open && !historyLoaded) void loadHistory();
|
||
});
|
||
let pendingRefresh = null;
|
||
const scheduleRetry = () => {
|
||
if (pendingRefresh !== null) return;
|
||
pendingRefresh = window.setTimeout(() => {
|
||
pendingRefresh = null;
|
||
if (isActive() && section.isConnected) void refresh();
|
||
}, 10_000);
|
||
};
|
||
const refresh = async () => {
|
||
try {
|
||
const stats = await authService.getSupportStats(login);
|
||
if (!isActive() || !section.isConnected) return;
|
||
if (!stats.ready) {
|
||
// Первичная индексация ещё идёт. Повторим запрос к кешу без RPC Solana.
|
||
content.textContent = t('screens.support_phase2.loading');
|
||
scheduleRetry();
|
||
return;
|
||
}
|
||
if (!stats.available) {
|
||
// Free-аккаунты не имеют Paid PDA, поэтому блок статистики не показываем.
|
||
section.remove();
|
||
return;
|
||
}
|
||
const values = supportStatsValues({
|
||
supportSent: stats.supportSentStarCents,
|
||
supportReceived: stats.supportReceivedStarCents,
|
||
daoDirect: stats.daoDirectStarCents,
|
||
});
|
||
content.replaceChildren();
|
||
content.className = 'stack';
|
||
[t('screens.support_phase2.sent'), t('screens.support_phase2.received'), t('screens.support_phase2.shine')].forEach((label, idx) => {
|
||
const row = document.createElement('div');
|
||
row.className = 'row';
|
||
row.style.cssText = 'display:flex;justify-content:space-between;gap:8px;';
|
||
const name = document.createElement('span');
|
||
const amount = document.createElement('strong');
|
||
name.textContent = label;
|
||
amount.textContent = formatStars(values[idx]);
|
||
row.append(name, amount);
|
||
content.append(row);
|
||
});
|
||
} catch {
|
||
if (!isActive() || !section.isConnected) return;
|
||
// A missing Paid PDA means Free, but network errors must not be displayed as zero.
|
||
content.textContent = t('screens.support_phase2.unavailable');
|
||
}
|
||
};
|
||
void refresh();
|
||
return { element: section, refresh };
|
||
}
|