import React, { useState } from 'react'; import { Link, useLocation } from 'react-router-dom'; import { useAuth, useNetworkStatus } from '@/hooks'; import { useAuth as useAuthContext } from '@/contexts/useAuth'; import { EVerificationStatus } from '@/types/identity'; import { useForum } from '@/contexts/useForum'; import { localDatabase } from '@/lib/database/LocalDatabase'; import { DelegationFullStatus } from '@/lib/delegation'; import { Button } from '@/components/ui/button'; import { LogOut, Terminal, AlertTriangle, CheckCircle, Key, CircleSlash, Home, Grid3X3, User, } from 'lucide-react'; import { Tooltip, TooltipContent, TooltipTrigger, } from '@/components/ui/tooltip'; import { useToast } from '@/components/ui/use-toast'; import { useAppKitAccount, useDisconnect } from '@reown/appkit/react'; import { WalletWizard } from '@/components/ui/wallet-wizard'; import { useUserDisplay } from '@/hooks'; const Header = () => { const { verificationStatus } = useAuth(); const { getDelegationStatus } = useAuthContext(); const [delegationInfo, setDelegationInfo] = useState(null); const networkStatus = useNetworkStatus(); const location = useLocation(); const { toast } = useToast(); const forum = useForum(); // Use AppKit hooks for multi-chain support const bitcoinAccount = useAppKitAccount({ namespace: 'bip122' }); const ethereumAccount = useAppKitAccount({ namespace: 'eip155' }); const { disconnect } = useDisconnect(); // Determine which account is connected const isBitcoinConnected = bitcoinAccount.isConnected; const isEthereumConnected = ethereumAccount.isConnected; const isConnected = isBitcoinConnected || isEthereumConnected; const address = isConnected ? isBitcoinConnected ? bitcoinAccount.address : ethereumAccount.address : undefined; const [walletWizardOpen, setWalletWizardOpen] = useState(false); // ✅ Get display name from enhanced hook const { displayName } = useUserDisplay(address || ''); // Load delegation status React.useEffect(() => { getDelegationStatus().then(setDelegationInfo).catch(console.error); }, [getDelegationStatus]); // Use LocalDatabase to persist wizard state across navigation const getHasShownWizard = async (): Promise => { try { const value = await localDatabase.loadUIState('hasShownWalletWizard'); return value === true; } catch { return false; } }; const setHasShownWizard = async (value: boolean): Promise => { try { await localDatabase.storeUIState('hasShownWalletWizard', value); } catch (e) { console.error('Failed to store wizard state', e); } }; // Auto-open wizard when wallet connects for the first time React.useEffect(() => { if (isConnected) { getHasShownWizard().then(hasShown => { if (!hasShown) { setWalletWizardOpen(true); setHasShownWizard(true).catch(console.error); } }); } }, [isConnected]); const handleConnect = async () => { setWalletWizardOpen(true); }; const handleDisconnect = async () => { await disconnect(); await setHasShownWizard(false); // Reset so wizard can show again on next connection toast({ title: 'Wallet Disconnected', description: 'Your wallet has been disconnected successfully.', }); }; const getAccountStatusText = () => { if (!isConnected) return 'Connect Wallet'; if (verificationStatus === EVerificationStatus.ENS_ORDINAL_VERIFIED) { return delegationInfo?.isValid ? 'Ready to Post' : 'Delegation Expired'; } else if (verificationStatus === EVerificationStatus.WALLET_CONNECTED) { return 'Verified (Read-only)'; } else { return 'Verify Wallet'; } }; const getStatusColor = () => { if (!isConnected) return 'text-red-400'; if ( verificationStatus === EVerificationStatus.ENS_ORDINAL_VERIFIED && delegationInfo?.isValid ) { return 'text-green-400'; } else if (verificationStatus === EVerificationStatus.WALLET_CONNECTED) { return 'text-yellow-400'; } else if ( verificationStatus === EVerificationStatus.ENS_ORDINAL_VERIFIED ) { return 'text-orange-400'; } else { return 'text-red-400'; } }; const getStatusIcon = () => { if (!isConnected) return ; if ( verificationStatus === EVerificationStatus.ENS_ORDINAL_VERIFIED && delegationInfo?.isValid ) { return ; } else if (verificationStatus === EVerificationStatus.WALLET_CONNECTED) { return ; } else if ( verificationStatus === EVerificationStatus.ENS_ORDINAL_VERIFIED ) { return ; } else { return ; } }; return (
{/* Logo and Navigation */}
opchan
{/* Right side - Status and User */}
{/* Network Status */}
{networkStatus.getStatusMessage()} {forum.lastSync && ( Last updated{' '} {new Date(forum.lastSync).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit', })} {forum.isSyncing ? ' • syncing…' : ''} )}
{/* User Status */} {isConnected ? (
{getStatusIcon()}
{displayName}
{getAccountStatusText()}
Address: {address?.slice(0, 8)}...
Status: {getAccountStatusText()}
{delegationInfo?.timeRemaining && (
Delegation: {delegationInfo.timeRemaining} remaining
)}
) : ( )}
{/* Wallet Wizard */} { setWalletWizardOpen(false); toast({ title: 'Setup Complete', description: 'Your wallet is ready to use!', }); }} />
); }; export default Header;