UNPKG

@towns-protocol/react-sdk

Version:

React Hooks for Towns Protocol SDK

83 lines 3.08 kB
import type { SyncAgentConfig } from '@towns-protocol/sdk'; import type { ethers } from 'ethers'; type AgentConnectConfig = Omit<SyncAgentConfig, 'context' | 'onTokenExpired'>; /** * Hook for managing the connection to the sync agent * * @example You can connect the Sync Agent to Towns Protocol using a Bearer Token or using a Signer. * * ### Bearer Token * ```tsx * import { useAgentConnection } from '@towns-protocol/react-sdk' * import { townsEnv } from '@towns-protocol/sdk' * import { useState } from 'react' * * const townsConfig = townsEnv().makeTownsConfig('beta') * * const Login = () => { * const { connectUsingBearerToken, isAgentConnecting, isAgentConnected } = useAgentConnection() * const [bearerToken, setBearerToken] = useState('') * * return ( * <> * <input value={bearerToken} onChange={(e) => setBearerToken(e.target.value)} /> * <button onClick={() => connectUsingBearerToken(bearerToken, { townsConfig })}> * Login * </button> * {isAgentConnecting && <span>Connecting... ⏳</span>} * {isAgentConnected && <span>Connected ✅</span>} * </> * ) * } * ``` * * ### Signer * * If you're using Wagmi and Viem, you can use the [`useEthersSigner`](https://wagmi.sh/react/guides/ethers#usage-1) hook to get an ethers.js v5 Signer from a Viem Wallet Client. * * ```tsx * import { useAgentConnection } from '@towns-protocol/react-sdk' * import { townsEnv } from '@towns-protocol/sdk' * import { useEthersSigner } from './utils/viem-to-ethers' * * const townsConfig = townsEnv().makeTownsConfig('beta') * * const Login = () => { * const { connect, isAgentConnecting, isAgentConnected } = useAgentConnection() * const signer = useEthersSigner() * * return ( * <> * <button onClick={async () => { * if (!signer) { * return * } * connect(signer, { townsConfig }) * }}> * Login * </button> * {isAgentConnecting && <span>Connecting... ⏳</span>} * {isAgentConnected && <span>Connected ✅</span>} * </> * ) * } * ``` * * @returns The connection state and methods (connect, connectUsingBearerToken, disconnect) */ export declare const useAgentConnection: () => { /** Connect to Towns Protocol using a Signer */ connect: (signer: ethers.Signer, config: AgentConnectConfig) => Promise<import("@towns-protocol/sdk").SyncAgent | undefined>; /** Connect to Towns Protocol using a Bearer Token */ connectUsingBearerToken: (bearerToken: string, config: AgentConnectConfig) => Promise<import("@towns-protocol/sdk").SyncAgent | undefined>; /** Disconnect from Towns Protocol */ disconnect: () => void | undefined; /** Whether the agent is currently connecting */ isAgentConnecting: boolean; /** Whether the agent is connected */ isAgentConnected: boolean; /** The environment of the current connection (beta, omega, alpha, local_dev, etc.) */ env: string | undefined; }; export {}; //# sourceMappingURL=useAgentConnection.d.ts.map