'use client'; import { useState, useEffect, useImperativeHandle, forwardRef } from 'react'; import { useWallet } from '@solana/wallet-adapter-react'; import { WalletMultiButton } from '@solana/wallet-adapter-react-ui'; import { Connection, PublicKey } from '@solana/web3.js'; import Image from 'next/image'; import '@solana/wallet-adapter-react-ui/styles.css'; import { CLUSTER_URL } from '../shared'; import { DINO_TOKEN_ADDRESS } from '../constants'; export interface HeaderRef { refreshBalance: () => Promise; } const Header = forwardRef((props, ref) => { const { publicKey } = useWallet(); const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const [isScrolled, setIsScrolled] = useState(false); const [dinoBalance, setDinoBalance] = useState(null); const [isLoadingBalance, setIsLoadingBalance] = useState(false); const [showMobileWalletHelp, setShowMobileWalletHelp] = useState(false); const [mobileWalletReady, setMobileWalletReady] = useState(false); // Check if we're on mobile const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( typeof window !== 'undefined' ? navigator.userAgent : '' ); // Custom styling for WalletMultiButton const walletButtonStyle = { backgroundColor: 'transparent', border: 'none', borderRadius: '12px', padding: '8px 16px', fontSize: '14px', fontWeight: '600', transition: 'all 0.3s ease', boxShadow: '0 4px 6px -1px rgba(0, 0, 0, 0.1)', }; // Mobile-specific wallet button styling const mobileWalletButtonStyle = { ...walletButtonStyle, fontSize: '12px', padding: '6px 12px', }; const fetchDinoBalance = async () => { if (!publicKey) { setDinoBalance(null); return; } setIsLoadingBalance(true); try { const connection = new Connection(CLUSTER_URL); const tokenMint = new PublicKey(DINO_TOKEN_ADDRESS); // Get token accounts for the user const tokenAccounts = await connection.getParsedTokenAccountsByOwner( publicKey, { mint: tokenMint } ); if (tokenAccounts.value.length > 0) { const balance = tokenAccounts.value[0].account.data.parsed.info.tokenAmount.uiAmount; setDinoBalance(balance); } else { setDinoBalance(0); } } catch (error) { console.error('Error fetching DINO balance:', error); setDinoBalance(null); } finally { setIsLoadingBalance(false); } }; // Expose refreshBalance function to parent component useImperativeHandle(ref, () => ({ refreshBalance: fetchDinoBalance })); useEffect(() => { const handleScroll = () => { setIsScrolled(window.scrollY > 10); }; window.addEventListener('scroll', handleScroll); return () => window.removeEventListener('scroll', handleScroll); }, []); useEffect(() => { fetchDinoBalance(); }, [publicKey]); // Check mobile wallet readiness useEffect(() => { if (isMobile && typeof window !== 'undefined') { const checkWalletReadiness = () => { const phantom = (window as { phantom?: { solana?: { isConnected?: boolean } } }).phantom; if (phantom && phantom.solana) { console.log('Mobile Phantom wallet is ready'); setMobileWalletReady(true); } else { console.log('Mobile Phantom wallet not ready yet'); setMobileWalletReady(false); } }; // Check immediately checkWalletReadiness(); // Check periodically const interval = setInterval(checkWalletReadiness, 2000); // Check when page becomes visible/focused const handleVisibilityChange = () => { if (!document.hidden) { checkWalletReadiness(); } }; const handleFocus = () => { checkWalletReadiness(); }; document.addEventListener('visibilitychange', handleVisibilityChange); window.addEventListener('focus', handleFocus); return () => { clearInterval(interval); document.removeEventListener('visibilitychange', handleVisibilityChange); window.removeEventListener('focus', handleFocus); }; } }, [isMobile]); const handleBuyDino = () => { // Redirect to DexScreener - you can update this URL to the actual $DINO token page window.open('https://dexscreener.com/solana', '_blank'); }; const formatBalance = (balance: number | null) => { if (balance === null) return '0'; if (balance === 0) return '0'; if (balance < 0.0001) return '< 0.0001'; return balance.toFixed(4); }; return ( <>
{/* Logo and Title */}
DINO Token Icon

DINO

{/* Desktop Navigation Links */} {/* Buy $DINO Button and Wallet Connect - Desktop */}
{publicKey ? (
{/* DINO Balance Display */}
{isLoadingBalance ? (
Loading...
) : ( `${formatBalance(dinoBalance)} $DINO` )}
) : ( )}
{/* Mobile Menu Button */}
{/* Mobile wallet help message */} {!publicKey && (
Tap to connect your wallet
)} {/* Mobile wallet readiness indicator */} {isMobile && !publicKey && (
{mobileWalletReady ? 'Wallet Ready' : 'Checking Wallet...'}
)} {/* Mobile wallet help button */} {isMobile && !publicKey && ( )}
{/* Mobile Menu */} {isMobileMenuOpen && (
Support Whitepaper
Socials:
{publicKey && (
{isLoadingBalance ? (
Loading...
) : ( `${formatBalance(dinoBalance)} $DINO` )}
)}
)}
{/* Mobile Wallet Help Modal */} {showMobileWalletHelp && (

Mobile Wallet Help

If you get "Wallet not ready" error:

  1. Install Phantom from App Store (iOS) or Play Store (Android)
  2. Open the Phantom app and create/import a wallet
  3. Return to this page and refresh
  4. Try connecting again
Make sure your wallet app is open before connecting
)} ); }); Header.displayName = 'Header'; export default Header;