import type { ReactNode } from "react" ; import React from "react" ; import type { Container } from "unstated-next" ; import { createContainer , useContainer } from "unstated-next" ; import type { DefaultWalletType , WalletAdapter , WalletProviderInfo , WalletTypeEnum , } from "." ; import type { UseSolanaError } from "./error" ; import { ErrorLevel } from "./error" ; import type { UnknownWalletType , WalletProviderMap } from "./providers" ; import { DEFAULT_WALLET_PROVIDERS } from "./providers" ; import { LOCAL_STORAGE_ADAPTER } from "./storage" ; import type { ConnectionArgs , ConnectionContext , } from "./utils/useConnectionInternal" ; import { useConnectionInternal } from "./utils/useConnectionInternal" ; import type { UseProvider } from "./utils/useProviderInternal" ; import { useProviderInternal } from "./utils/useProviderInternal" ; import type { UseWallet , UseWalletArgs } from "./utils/useWalletInternal" ; import { useWalletInternal } from "./utils/useWalletInternal" ; export interface UseSolana < WalletType extends WalletTypeEnum < WalletType > = typeof DefaultWalletType , Connected extends boolean = boolean > extends ConnectionContext , UseWallet < WalletType , Connected > , UseProvider { } export interface UseSolanaArgs < WalletType extends WalletTypeEnum < WalletType > = typeof DefaultWalletType > extends Omit < ConnectionArgs , "storageAdapter" > , Partial < Pick < UseWalletArgs < WalletType > , "onConnect" | "onDisconnect" | "storageAdapter" | "walletProviders" > > { /**
     * Called when an error is thrown.
     */ onError ? : ( err : UseSolanaError ) => void ; } const defaultOnConnect = ( wallet , provider ) => { console . log ( `Connected to ${ provider . name } wallet: ${ wallet . publicKey . toString ( ) }` ) ; } ; const defaultOnDisconnect = ( _wallet , provider ) => { console . log ( `Disconnected from ${ provider . name } wallet` ) ; } ; const defaultOnError = ( err ) => { if ( err . level === ErrorLevel . WARN ) { console . warn ( err ) ; } else { console . error ( err ) ; } } ; returns const useSolanaInternal = < WalletType extends WalletTypeEnum < WalletType > > ( { onConnect = defaultOnConnect , onDisconnect = defaultOnDisconnect , onError = defaultOnError , storageAdapter = LOCAL_STORAGE_ADAPTER , walletProviders = DEFAULT_WALLET_PROVIDERS as unknown as WalletProviderMap < WalletType > , ... connectionArgs } = { } ) : UseSolana < WalletType > => { const connectionCtx = useConnectionInternal ( { ... connectionArgs , storageAdapter , } ) ; const { network , endpoint } = connectionCtx ; const walletCtx = useWalletInternal < WalletType > ( { onConnect , onDisconnect , network , endpoint , onError , storageAdapter , walletProviders , } ) ; const providerCtx = useProviderInternal ( { connection : connectionCtx . connection , wallet : walletCtx . wallet , } ) ; return { ... walletCtx , ... connectionCtx , ... providerCtx , } ; } ; const Solana = createContainer < UseSolana < UnknownWalletType > , UseSolanaArgs < UnknownWalletType > > ( useSolanaInternal ) ; type ProviderProps < WalletType extends WalletTypeEnum < WalletType > > = UseSolanaArgs < WalletType > & { children : ReactNode ; } ; returns export const SolanaProvider = < WalletType extends WalletTypeEnum < WalletType > = typeof DefaultWalletType > ( { children , ... args } ) => ( < Solana . Provider initialState = { args } > { children } < /Solana.Provider>); /**
 * Fetches the loaded Solana SDK.
 */ export const useSolana = < WalletType extends WalletTypeEnum < WalletType > > ( ) => useContainer ( Solana as unknown as Container < UseSolana < WalletType > , UseSolanaArgs < WalletType > > ) ;