import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { ChevronRight, Shield } from 'lucide-react'; import { usePezkuwi } from '../contexts/PezkuwiContext'; import { useWallet } from '../contexts/WalletContext'; // Import useWallet import { formatBalance } from '@pezkuwi/lib/wallet'; const HeroSection: React.FC = () => { const { t } = useTranslation(); const { api, isApiReady } = usePezkuwi(); const { selectedAccount } = useWallet(); // Use selectedAccount from WalletContext const [stats, setStats] = useState({ activeProposals: 0, totalVoters: 0, tokensStaked: '0', trustScore: 0 }); useEffect(() => { const fetchStats = async () => { if (!api || !isApiReady) return; let currentTrustScore = 0; // Default if not fetched or no account if (selectedAccount?.address) { try { // Assuming pallet-staking-score has a storage item for trust scores // The exact query might need adjustment based on chain metadata const rawTrustScore = await api.query.stakingScore.trustScore(selectedAccount.address); // Assuming trustScore is a simple number or a wrapper around it currentTrustScore = rawTrustScore.isSome ? rawTrustScore.unwrap().toNumber() : 0; } catch (err) { if (import.meta.env.DEV) console.warn('Failed to fetch trust score:', err); currentTrustScore = 0; } } try { // Fetch active referenda let activeProposals = 0; try { const referendaCount = await api.query.referenda.referendumCount(); activeProposals = referendaCount.toNumber(); } catch (err) { if (import.meta.env.DEV) console.warn('Failed to fetch referenda:', err); } // Fetch total staked tokens let tokensStaked = '0'; try { const currentEra = await api.query.staking.currentEra(); if (currentEra.isSome) { const eraIndex = currentEra.unwrap().toNumber(); const totalStake = await api.query.staking.erasTotalStake(eraIndex); const formatted = formatBalance(totalStake.toString()); tokensStaked = `${formatted} HEZ`; } } catch (err) { if (import.meta.env.DEV) console.warn('Failed to fetch total stake:', err); } // Count total voters from conviction voting let totalVoters = 0; try { // Get all voting keys and count unique voters const votingKeys = await api.query.convictionVoting.votingFor.keys(); // Each key represents a unique (account, track) pair // Count unique accounts const uniqueAccounts = new Set(votingKeys.map(key => key.args[0].toString())); totalVoters = uniqueAccounts.size; } catch (err) { if (import.meta.env.DEV) console.warn('Failed to fetch voters:', err); } // Update stats setStats({ activeProposals, totalVoters, tokensStaked, trustScore: currentTrustScore }); if (import.meta.env.DEV) console.log('✅ Hero stats updated:', { activeProposals, totalVoters, tokensStaked, trustScore: currentTrustScore }); } catch (error) { if (import.meta.env.DEV) console.error('Failed to fetch hero stats:', error); } }; fetchStats(); }, [api, isApiReady, selectedAccount]); // Add selectedAccount to dependencies return (
{/* Background Image */}
DKstate Background
{/* Content */}
Digital Kurdistan State v1.0

PezkuwiChain

{t('hero.title', 'Blockchain Governance Platform')}

{t('hero.subtitle', 'Democratic and transparent governance with blockchain technology')}

{stats.activeProposals}
{t('hero.stats.activeProposals', 'Active Proposals')}
{stats.totalVoters.toLocaleString()}
{t('hero.stats.totalVoters', 'Total Voters')}
{stats.tokensStaked}
{t('hero.stats.tokensStaked', 'Tokens Staked')}
{stats.trustScore}%
{t('hero.stats.trustScore', 'Trust Score')}
); }; export default HeroSection;