/* eslint-disable valtio/state-snapshot-rule */ import { useMemo } from 'react'; import { useSnapshot } from 'valtio'; import { ConnectionsController, LogController } from '@reown/appkit-core-react-native'; import type { Provider, ChainNamespace } from '@reown/appkit-common-react-native'; import { useAppKitContext } from './useAppKitContext'; /** * Interface representing the result of the useProvider hook */ interface ProviderResult { /** The active connection provider instance */ provider?: Provider; /** The chain namespace supported by the provider */ providerType?: ChainNamespace; } /** * Hook that returns the active connection provider and its supported chain namespace. * * This hook accesses the current connection and returns the provider instance along with its supported namespace. * * @returns {ProviderResult} An object containing: * - `provider`: The active connection provider instance, or undefined if no connection exists * - `providerType`: The chain namespace supported by the provider, or undefined if no connection exists * * @example * ```typescript * const { provider, providerType } = useProvider(); * * if (provider) { * // Use the provider for blockchain operations * const balance = await provider.request({...}); * } * ``` */ export function useProvider(): ProviderResult { useAppKitContext(); const { connection } = useSnapshot(ConnectionsController.state); const returnValue = useMemo(() => { if (!connection || !connection.adapter) { return { provider: undefined, providerType: undefined }; } try { return { provider: connection.adapter.getProvider(), providerType: connection.adapter.getSupportedNamespace() }; } catch (error) { LogController.sendError(error, 'useProvider', 'useProvider'); // Provider not initialized yet during session restoration return { provider: undefined, providerType: undefined }; } }, [connection]); return returnValue; }