import { useState, useEffect } from 'react' import { useNostrAuth } from '@/hooks/useNostrAuth' import { ConnectedUserMenu } from './ConnectedUserMenu' import { RecoveryStep } from './CreateAccountModalSteps' import { UnlockAccountModal } from './UnlockAccountModal' import type { NostrProfile } from '@/types/nostr' function ConnectForm({ onCreateAccount, onUnlock, loading, error, }: { onCreateAccount: () => void onUnlock: () => void loading: boolean error: string | null }) { return (
{error &&

{error}

}
) } function useAutoConnect(accountExists: boolean | null, pubkey: string | null, showRecoveryStep: boolean, showUnlockModal: boolean, connect: () => Promise) { useEffect(() => { if (accountExists === true && !pubkey && !showRecoveryStep && !showUnlockModal) { void connect() } }, [accountExists, pubkey, showRecoveryStep, showUnlockModal, connect]) } function ConnectedState({ pubkey, profile, loading, disconnect }: { pubkey: string; profile: NostrProfile | null; loading: boolean; disconnect: () => Promise }) { return ( { void disconnect() }} loading={loading} /> ) } function UnlockState({ loading, error, onUnlock, onClose }: { loading: boolean; error: string | null; onUnlock: () => void; onClose: () => void }) { return ( <> {}} onUnlock={onUnlock} loading={loading} error={error} /> ) } function DisconnectedState({ loading, error, showUnlockModal, setShowUnlockModal, onCreateAccount, }: { loading: boolean error: string | null showUnlockModal: boolean setShowUnlockModal: (show: boolean) => void onCreateAccount: () => void }) { return ( <> setShowUnlockModal(true)} loading={loading} error={error} /> {showUnlockModal && ( setShowUnlockModal(false)} onClose={() => setShowUnlockModal(false)} /> )} ) } export function ConnectButton() { const { connected, pubkey, profile, loading, error, connect, disconnect, accountExists, isUnlocked } = useNostrAuth() const [showRecoveryStep, setShowRecoveryStep] = useState(false) const [showUnlockModal, setShowUnlockModal] = useState(false) const [recoveryPhrase, setRecoveryPhrase] = useState([]) const [npub, setNpub] = useState('') const [creatingAccount, setCreatingAccount] = useState(false) const [createError, setCreateError] = useState(null) useAutoConnect(accountExists, pubkey, false, showUnlockModal, connect) const handleCreateAccount = async () => { setCreatingAccount(true) setCreateError(null) try { const { nostrAuthService } = await import('@/lib/nostrAuth') const result = await nostrAuthService.createAccount() setRecoveryPhrase(result.recoveryPhrase) setNpub(result.npub) setShowRecoveryStep(true) } catch (e) { setCreateError(e instanceof Error ? e.message : 'Failed to create account') } finally { setCreatingAccount(false) } } const handleRecoveryContinue = () => { setShowRecoveryStep(false) setShowUnlockModal(true) } const handleUnlockSuccess = () => { setShowUnlockModal(false) setRecoveryPhrase([]) setNpub('') } if (connected && pubkey && isUnlocked) { return } if (accountExists === true && pubkey && !isUnlocked && !showUnlockModal && !showCreateModal) { return ( setShowUnlockModal(true)} onClose={() => setShowUnlockModal(false)} /> ) } return ( <> {showRecoveryStep && ( )} {showUnlockModal && ( setShowUnlockModal(false)} /> )} ) }