import { useEffect, useState } from 'react' export function useAutoConnect(params: { accountExists: boolean | null pubkey: string | null showRecoveryStep: boolean showUnlockModal: boolean connect: () => Promise }): void { const { accountExists, pubkey, showRecoveryStep, showUnlockModal, connect } = params useEffect(() => { if (accountExists === true && !pubkey && !showRecoveryStep && !showUnlockModal) { void connect() } }, [accountExists, pubkey, showRecoveryStep, showUnlockModal, connect]) } export function useConnectButtonUiState(): { showRecoveryStep: boolean showUnlockModal: boolean setShowUnlockModal: (show: boolean) => void recoveryPhrase: string[] npub: string creatingAccount: boolean createError: string | null onCreateAccount: () => void onRecoveryContinue: () => void onUnlockSuccess: () => void openUnlockModal: () => void closeUnlockModal: () => void } { const unlockModal = useUnlockModalVisibility() const recovery = useRecoveryStepState() const [creatingAccount, setCreatingAccount] = useState(false) const [createError, setCreateError] = useState(null) const onCreateAccount = (): void => { void handleCreateAccount({ setCreatingAccount, setCreateError, setRecoveryPhrase: recovery.setRecoveryPhrase, setNpub: recovery.setNpub, setShowRecoveryStep: recovery.setShowRecoveryStep, }) } const onRecoveryContinue = (): void => { recovery.hideRecoveryStep() unlockModal.openUnlockModal() } const onUnlockSuccess = (): void => { unlockModal.closeUnlockModal() recovery.resetRecoveryData() } return { showRecoveryStep: recovery.showRecoveryStep, showUnlockModal: unlockModal.showUnlockModal, setShowUnlockModal: unlockModal.setShowUnlockModal, recoveryPhrase: recovery.recoveryPhrase, npub: recovery.npub, creatingAccount, createError, onCreateAccount, onRecoveryContinue, onUnlockSuccess, openUnlockModal: unlockModal.openUnlockModal, closeUnlockModal: unlockModal.closeUnlockModal, } } function useUnlockModalVisibility(): { showUnlockModal: boolean setShowUnlockModal: (show: boolean) => void openUnlockModal: () => void closeUnlockModal: () => void } { const [showUnlockModal, setShowUnlockModal] = useState(false) const openUnlockModal = (): void => setShowUnlockModal(true) const closeUnlockModal = (): void => setShowUnlockModal(false) return { showUnlockModal, setShowUnlockModal, openUnlockModal, closeUnlockModal } } function useRecoveryStepState(): { showRecoveryStep: boolean recoveryPhrase: string[] npub: string setShowRecoveryStep: (show: boolean) => void setRecoveryPhrase: (words: string[]) => void setNpub: (npub: string) => void hideRecoveryStep: () => void resetRecoveryData: () => void } { const [showRecoveryStep, setShowRecoveryStep] = useState(false) const [recoveryPhrase, setRecoveryPhrase] = useState([]) const [npub, setNpub] = useState('') const hideRecoveryStep = (): void => setShowRecoveryStep(false) const resetRecoveryData = (): void => { setRecoveryPhrase([]) setNpub('') } return { showRecoveryStep, recoveryPhrase, npub, setShowRecoveryStep, setRecoveryPhrase, setNpub, hideRecoveryStep, resetRecoveryData, } } async function handleCreateAccount(params: { setCreatingAccount: (creating: boolean) => void setCreateError: (error: string | null) => void setRecoveryPhrase: (words: string[]) => void setNpub: (npub: string) => void setShowRecoveryStep: (show: boolean) => void }): Promise { params.setCreatingAccount(true) params.setCreateError(null) try { const { nostrAuthService } = await import('@/lib/nostrAuth') const result = await nostrAuthService.createAccount() params.setRecoveryPhrase(result.recoveryPhrase) params.setNpub(result.npub) params.setShowRecoveryStep(true) } catch (e) { params.setCreateError(e instanceof Error ? e.message : 'Failed to create account') } finally { params.setCreatingAccount(false) } }