SHA256
51 lines
2.3 KiB
JavaScript
51 lines
2.3 KiB
JavaScript
import { t } from '../i18n/index.js';
|
|
import { readShineUserPda } from '../services/shine-user-pda-service.js';
|
|
import { formatLamportsSol } 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, sent / 10n + daoDirect];
|
|
}
|
|
|
|
/** Adds an independent asynchronous block; failure/Free accounts do not get fictitious statistics. */
|
|
export function attachSupportStats(parent, { login, solanaEndpoint, 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);
|
|
parent.append(section);
|
|
const refresh = async () => {
|
|
try {
|
|
const pda = await readShineUserPda({ login, solanaEndpoint });
|
|
if (!isActive() || !section.isConnected) return;
|
|
const values = supportStatsValues(pda.supportStats ?? {});
|
|
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 = `${formatLamportsSol(values[idx])} SOL`;
|
|
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 };
|
|
}
|