import { useContext, useCallback, useState } from 'react' import { ConnectionContext } from '../providers/ConnectionProvider' interface UseDisconnectReturn { disconnect: () => Promise isLoading: boolean } /** * Hook for disconnecting the current wallet * @returns Disconnect function and loading state * @throws Error if used outside of ConnectionProvider * @example * ```tsx * const { disconnect, isLoading } = useDisconnect() * * const handleDisconnect = async () => { * await disconnect() * console.log('Wallet disconnected') * } * * return ( * * ) * ``` */ export function useDisconnect(): UseDisconnectReturn { const context = useContext(ConnectionContext) if (!context) { throw new Error('useDisconnect must be used within a ConnectionProvider') } const { connector } = context const [isLoading, setIsLoading] = useState(false) const disconnect = useCallback(async () => { setIsLoading(true) try { await connector.disconnect() } finally { setIsLoading(false) } }, [connector]) return { disconnect, isLoading } }