import React, { useState } from 'react'; import { Link } from 'react-router-dom'; import { useAuth } from '@/contexts/useAuth'; import { useForum } from '@/contexts/useForum'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { LogOut, Terminal, Wifi, WifiOff, AlertTriangle, CheckCircle, Key, RefreshCw, CircleSlash } 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'; const Header = () => { const { currentUser, isAuthenticated, verificationStatus, verifyOwnership, delegateKey, isDelegationValid, delegationTimeRemaining, isWalletAvailable } = useAuth(); const { isNetworkConnected, isRefreshing } = useForum(); const { toast } = useToast(); // 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); // Use sessionStorage to persist wizard state across navigation const getHasShownWizard = () => { try { return sessionStorage.getItem('hasShownWalletWizard') === 'true'; } catch { return false; } }; const setHasShownWizard = (value: boolean) => { try { sessionStorage.setItem('hasShownWalletWizard', value.toString()); } catch { // Fallback if sessionStorage is not available } }; // Auto-open wizard when wallet connects for the first time React.useEffect(() => { if (isConnected && !getHasShownWizard()) { setWalletWizardOpen(true); setHasShownWizard(true); } }, [isConnected]); const handleConnect = async () => { setWalletWizardOpen(true); }; const handleDisconnect = async () => { await disconnect(); setHasShownWizard(false); // Reset so wizard can show again on next connection toast({ title: "Wallet Disconnected", description: "Your wallet has been disconnected successfully.", }); }; const getAccountStatusText = () => { switch (verificationStatus) { case 'unverified': return 'Setup Required'; case 'verifying': return 'Verifying...'; case 'verified-none': return 'Read-Only Access'; case 'verified-owner': return isDelegationValid() ? 'Full Access' : 'Setup Key'; default: return 'Setup Account'; } }; const getAccountStatusIcon = () => { switch (verificationStatus) { case 'unverified': return ; case 'verifying': return ; case 'verified-none': return ; case 'verified-owner': return isDelegationValid() ? : ; default: return ; } }; const getAccountStatusVariant = () => { switch (verificationStatus) { case 'unverified': return 'destructive'; case 'verifying': return 'outline'; case 'verified-none': return 'secondary'; case 'verified-owner': return isDelegationValid() ? 'default' : 'outline'; default: return 'outline'; } }; return ( <>
OpChan
{isNetworkConnected ? ( <> WAKU: Connected ) : ( <> WAKU: Offline )}

{isNetworkConnected ? "Waku network connection active." : "Waku network connection lost."}

{isRefreshing &&

Refreshing data...

}
{!isConnected ? ( ) : (

Account Setup

Click to view and manage your wallet connection, verification status, and key delegation.

{currentUser?.ensName || `${address?.slice(0, 5)}...${address?.slice(-4)}`}

{currentUser?.ensName ? `${currentUser.ensName} (${address})` : address}

Disconnect Wallet
)}
{ toast({ title: "Setup Complete", description: "You can now use all OpChan features!", }); }} /> ); }; export default Header;