Доделать донаты и статистику поддержки

This commit is contained in:
AidarKC
2026-11-12 12:15:35 +03:00
parent c97df82538
commit 2a72842a7a
16 changed files with 402 additions and 15 deletions
+27 -4
View File
@@ -1,5 +1,5 @@
import { t } from '../i18n/index.js';
import { readShineUserPda } from '../services/shine-user-pda-service.js';
import { authService } from '../state.js';
import { formatLamportsSol } from '../services/shine-support-amount.js';
export function supportStatsValues(stats = {}) {
@@ -10,7 +10,7 @@ export function supportStatsValues(stats = {}) {
}
/** Adds an independent asynchronous block; failure/Free accounts do not get fictitious statistics. */
export function attachSupportStats(parent, { login, solanaEndpoint, isActive = () => true }) {
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;';
@@ -21,11 +21,34 @@ export function attachSupportStats(parent, { login, solanaEndpoint, isActive = (
content.textContent = t('screens.support_phase2.loading');
section.append(title, content);
parent.append(section);
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 pda = await readShineUserPda({ login, solanaEndpoint });
const stats = await authService.getSupportStats(login);
if (!isActive() || !section.isConnected) return;
const values = supportStatsValues(pda.supportStats ?? {});
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.supportSentLamports,
supportReceived: stats.supportReceivedLamports,
daoDirect: stats.daoDirectLamports,
});
content.replaceChildren();
content.className = 'stack';
[t('screens.support_phase2.sent'), t('screens.support_phase2.received'), t('screens.support_phase2.shine')].forEach((label, idx) => {
+1 -1
View File
@@ -67,7 +67,7 @@ export function render({ navigate, chrome }) {
<button class="nav-row" type="button" data-self-profile-action="profiles"><span class="nav-row__label">${th('screens.profile_view.text_015')}</span><span class="nav-row__hint">${th('screens.profile_view.text_016')}</span></button>
</div>`;
attachSupportStats(body, { login: card.login || login, solanaEndpoint: state.entrySettings.solanaServer, isActive: () => !disposed });
attachSupportStats(body, { login: card.login || login, isActive: () => !disposed });
avatarPreview = attachProfileAvatarPreview(body.querySelector('.user-profile-avatar-slot'), card);
status.replaceChildren();
+1 -1
View File
@@ -67,7 +67,7 @@ export function render({ navigate, route, chrome }) {
].join(''),
});
attachSupportStats(body, { login: card.login, solanaEndpoint: state.entrySettings.solanaServer, isActive: () => !disposed });
attachSupportStats(body, { login: card.login, isActive: () => !disposed });
syncRelationTile();
avatarPreview = attachProfileAvatarPreview(body.querySelector('.user-profile-avatar-slot'), card);
+6
View File
@@ -1598,6 +1598,12 @@ export class AuthService {
return response.payload || {};
}
async getSupportStats(login) {
const response = await this.ws.request('GetSupportStats', { login: String(login || '').trim() });
if (response.status !== 200) throw opError('GetSupportStats', response);
return response.payload || {};
}
async getSupportTotals(channel) {
const payload = {
channelRootBlockNumber: Number(channel?.channelRootBlockNumber),
+11 -1
View File
@@ -8,9 +8,10 @@ function file(path) { return readFileSync(new URL(path, import.meta.url), 'utf8'
test('three displayed counters: full sent, net received, sent/10 + direct', () => {
assert.deepEqual(supportStatsValues({ supportSent: 1_000_000_000n, supportReceived: 900_000_000n, daoDirect: 200_000_000n }), [1_000_000_000n, 900_000_000n, 300_000_000n]);
assert.deepEqual(supportStatsValues({}), [0n, 0n, 0n]);
assert.deepEqual(supportStatsValues({ supportSent: '10000000000000000000', supportReceived: '9000000000000000000', daoDirect: '250000000000000000' }), [10000000000000000000n, 9000000000000000000n, 1250000000000000000n]);
});
test('profile views attach counters from Solana PDA and support user action', () => {
test('profile views attach counters from server cache and support user action', () => {
assert.match(file('../js/pages/profile-view.js'), /attachSupportStats\(body/);
const other = file('../js/pages/user-profile-view.js');
assert.match(other, /attachSupportStats\(body/);
@@ -41,3 +42,12 @@ test('shared modal protects direct and tracked workflows', () => {
assert.match(src, /window\.confirm\(t\('screens\.support_phase2\.direct_warning'\)\)/);
assert.match(src, /amount % 10n/);
});
test('profile statistics use server API without Solana RPC', () => {
const component = file('../js/components/support-stats.js');
const auth = file('../js/services/auth-service.js');
assert.match(component, /authService\.getSupportStats\(login\)/);
assert.doesNotMatch(component, /readShineUserPda/);
assert.match(component, /if \(!stats\.ready\)/);
assert.match(auth, /this\.ws\.request\('GetSupportStats'/);
});