Skip to content

Commit ee6155f

Browse files
authored
connect hooks tsdoc (#367)
1 parent 8b090db commit ee6155f

13 files changed

+441
-3
lines changed

packages/connect/src/hooks/useChain.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
import { useChainId, useChains } from 'wagmi'
2+
import { Chain } from 'wagmi/chains'
23

3-
// Returns chain config for current chain or chain by id
4-
export const useChain = (chainId?: number) => {
4+
/**
5+
* Hook for retrieving chain configuration information from wagmi's chain configurations
6+
*
7+
* This hook provides functionality to:
8+
* - Get the configuration for the currently connected chain
9+
* - Get the configuration for a specific chain by ID
10+
* - Access chain-specific details like name, network configuration, RPC URLs, and block explorers
11+
*
12+
* The hook is commonly used in conjunction with other Sequence hooks to access chain-specific
13+
* information when working with transactions, indexer clients, or network-specific features.
14+
*
15+
* @see {@link https://docs.sequence.xyz/sdk/web/hooks/useChain} for more detailed documentation.
16+
*
17+
* @param chainId - Optional chain ID to get configuration for a specific chain. If not provided, returns the current chain's configuration.
18+
* @returns Chain configuration object for the specified or current chain, or undefined if not found
19+
*
20+
* @example
21+
* ```tsx
22+
* // Get current chain configuration
23+
* const currentChain = useChain();
24+
* console.log('Current chain name:', currentChain?.name);
25+
*
26+
* // Get configuration for a specific chain
27+
* const ethereumChain = useChain(1); // Ethereum Mainnet
28+
* console.log('Ethereum chain details:', ethereumChain);
29+
* ```
30+
*/
31+
export const useChain = (chainId?: number): Chain | undefined => {
532
const chains = useChains()
633
const currentChainId = useChainId()
734

packages/connect/src/hooks/useCheckWaasFeeOptions.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,47 @@
33
import { FeeOption, Transaction } from '@0xsequence/waas'
44
import { useConnections } from 'wagmi'
55

6+
/**
7+
* Hook for checking transaction fee options in Sequence WaaS (Wallet as a Service)
8+
*
9+
* This hook provides functionality to:
10+
* - Check if a transaction will be sponsored
11+
* - Get available fee options for unsponsored transactions
12+
* - Retrieve fee quotes for transactions
13+
*
14+
* The hook is commonly used in conjunction with `useWaasFeeOptions` to handle fee payments
15+
* for unsponsored transactions. It's particularly useful in
16+
* wallet interfaces and transaction confirmation flows.
17+
*
18+
* @see {@link https://docs.sequence.xyz/sdk/web/hooks/useCheckWaasFeeOptions} for more detailed documentation.
19+
*
20+
* @returns A function that accepts transaction details and returns fee information
21+
* @returns.feeQuote - The fee quote for the transaction if available
22+
* @returns.feeOptions - Available fee payment options if the transaction is not sponsored
23+
* @returns.isSponsored - Whether the transaction is sponsored (true) or requires fee payment (false)
24+
*
25+
* @example
26+
* ```tsx
27+
* import { useCheckWaasFeeOptions } from '@0xsequence/connect'
28+
*
29+
* const YourComponent = () => {
30+
* const checkFeeOptions = useCheckWaasFeeOptions()
31+
*
32+
* const handleTransaction = async () => {
33+
* const { isSponsored, feeOptions, feeQuote } = await checkFeeOptions({
34+
* transactions: [transaction],
35+
* chainId: 1 // Ethereum Mainnet
36+
* })
37+
*
38+
* if (!isSponsored && feeOptions) {
39+
* // Handle fee options for unsponsored transaction
40+
* console.log('Available fee options:', feeOptions)
41+
* console.log('Fee quote:', feeQuote)
42+
* }
43+
* }
44+
* }
45+
* ```
46+
*/
647
export function useCheckWaasFeeOptions(): (params: { transactions: Transaction[]; chainId: number }) => Promise<{
748
feeQuote: string | undefined
849
feeOptions: FeeOption[] | undefined

packages/connect/src/hooks/useDirectEcosystemConnect.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,38 @@ import { useConnect } from 'wagmi'
22

33
import { EcosystemConnector } from '../connectors/ecosystem/ecosystemWallet'
44

5+
/**
6+
* Hook to directly connect to an ecosystem wallet with email
7+
*
8+
* @returns A function that triggers the ecosystem wallet connection
9+
* @throws {Error} If the ecosystem wallet connector is not found among available connectors
10+
*
11+
* The returned function accepts:
12+
* - `auxData` (optional) - Additional data to pass to the ecosystem connector during connection
13+
*
14+
* @example
15+
* ```tsx
16+
* const triggerConnect = useDirectEcosystemConnect()
17+
*
18+
* // Connect without auxiliary data
19+
* await triggerConnect()
20+
*
21+
* // Connect with auxiliary data
22+
* await triggerConnect({
23+
* someKey: 'someValue',
24+
* anotherKey: 123
25+
* })
26+
*
27+
* // Handle connection errors
28+
* try {
29+
* await triggerConnect()
30+
* } catch (error) {
31+
* if (error.message === 'Ecosystem wallet connector not found') {
32+
* console.error('Ecosystem wallet is not configured')
33+
* }
34+
* }
35+
* ```
36+
*/
537
export const useDirectEcosystemConnect = () => {
638
const { connectors, connect } = useConnect()
739

packages/connect/src/hooks/useOpenConnectModal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type UseOpenConnectModalReturnType = {
1717
* This hook provides a method to open and close the connect modal, and access its current open state.
1818
* The Connect modal provides various wallet connection options including Sequence wallet and external wallets.
1919
*
20-
* Go to {@link https://docs.sequence.xyz/sdk/web/hooks/useOpenConnectModal} for more detailed documentation.
20+
* @see {@link https://docs.sequence.xyz/sdk/web/hooks/useOpenConnectModal} for more detailed documentation.
2121
*
2222
* @returns An object containing function to control the Connect modal and its state {@link UseOpenConnectModalReturnType}
2323
*

packages/connect/src/hooks/useProjectAccessKey.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
import { useConnectConfigContext } from '../contexts/ConnectConfig.js'
22

3+
/**
4+
* Hook to access the project access key from the Sequence Connect configuration.
5+
*
6+
* The project access key is a required configuration parameter used to authenticate and
7+
* identify your application with Sequence services. It is used across various SDK features
8+
* including marketplace integration and wallet connections.
9+
*
10+
* @see {@link https://docs.sequence.xyz/sdk/web/hooks/useProjectAccessKey} for more detailed documentation.
11+
*
12+
* @returns {string} The project access key configured for the application
13+
*
14+
* @example
15+
* ```tsx
16+
* const projectAccessKey = useProjectAccessKey()
17+
* const marketplaceClient = new MarketplaceIndexer(apiUrl, projectAccessKey)
18+
* ```
19+
*/
320
export const useProjectAccessKey = () => {
421
const { projectAccessKey } = useConnectConfigContext()
522

packages/connect/src/hooks/useSignInEmail.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ import { useConfig, useAccount } from 'wagmi'
55

66
import { LocalStorageKey } from '../constants/localStorage'
77

8+
/**
9+
* Hook to retrieve the email address associated with the currently connected wallet.
10+
*
11+
* This hook monitors the connection status and retrieves the stored email address when a wallet
12+
* is connected. It works with both WaaS (Wallet-as-a-Service) and universal wallet types.
13+
* The email is cleared when the wallet is disconnected.
14+
*
15+
* @see {@link https://docs.sequence.xyz/sdk/web/hooks/useSignInEmail} for more detailed documentation.
16+
*
17+
* @returns {string | null} The email address of the connected wallet user, or null if not connected
18+
* or no email is associated
19+
*
20+
* @example
21+
* ```tsx
22+
* const email = useSignInEmail()
23+
* if (email) {
24+
* console.log('Connected user email:', email)
25+
* }
26+
* ```
27+
*/
828
export const useSignInEmail = () => {
929
const { storage } = useConfig()
1030
const { isConnected } = useAccount()

packages/connect/src/hooks/useTheme.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
import { useThemeContext } from '../contexts/Theme'
22

3+
/**
4+
* Hook to access and modify the theme and modal position settings.
5+
*
6+
* This hook provides access to the current theme (light/dark) and modal position settings,
7+
* along with functions to update these values. The modal position can be set to various
8+
* predefined positions on the screen.
9+
*
10+
* @see {@link https://docs.sequence.xyz/sdk/web/hooks/useTheme} for more detailed documentation.
11+
*
12+
* @returns {Object} An object containing:
13+
* - `theme` - The current theme setting
14+
* - `setTheme` - Function to update the theme
15+
* - `position` - The current modal position ('center', 'top-right', 'bottom-left', etc.)
16+
* - `setPosition` - Function to update the modal position
17+
*
18+
* @example
19+
* ```tsx
20+
* const { theme, setTheme, position, setPosition } = useTheme()
21+
*
22+
* // Change theme
23+
* setTheme('dark')
24+
*
25+
* // Update modal position
26+
* setPosition('top-right')
27+
* ```
28+
*/
329
export const useTheme = () => {
430
const { setTheme, theme, position, setPosition } = useThemeContext()
531

packages/connect/src/hooks/useWaasConfirmationHandler.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,45 @@ export type WaasRequestConfirmation = {
1313
chainId?: number
1414
}
1515

16+
/**
17+
* Hook to handle transaction and message signing confirmations for WaaS (Wallet-as-a-Service) connections.
18+
*
19+
* This hook sets up confirmation handlers for signing transactions and messages when using a WaaS connector.
20+
* It manages the state of pending confirmations and provides functions to confirm or reject signing requests.
21+
*
22+
* @param waasConnector - The WaaS connector instance to handle confirmations for (optional)
23+
*
24+
* @returns A tuple containing:
25+
* - [0] {@link WaasRequestConfirmation} | undefined - The current pending request confirmation info or undefined if none
26+
* - [1] `(id: string) => void` - Function to confirm a pending request by ID
27+
* - [2] `(id: string) => void` - Function to reject a pending request by ID
28+
*
29+
* The {@link WaasRequestConfirmation} object contains:
30+
* - `id` - Unique identifier for the request
31+
* - `type` - Either 'signTransaction' or 'signMessage'
32+
* - `message?` - Optional message to sign (for signMessage requests)
33+
* - `txs?` - Optional array of transactions (for signTransaction requests)
34+
* - `chainId?` - Optional chain ID for the request
35+
*
36+
* @example
37+
* ```tsx
38+
* const [
39+
* pendingRequestConfirmation,
40+
* confirmPendingRequest,
41+
* rejectPendingRequest
42+
* ] = useWaasConfirmationHandler(waasConnector)
43+
*
44+
* // When user confirms the request
45+
* if (pendingRequestConfirmation) {
46+
* confirmPendingRequest(pendingRequestConfirmation.id)
47+
* }
48+
*
49+
* // When user rejects the request
50+
* if (pendingRequestConfirmation) {
51+
* rejectPendingRequest(pendingRequestConfirmation.id)
52+
* }
53+
* ```
54+
*/
1655
export function useWaasConfirmationHandler(
1756
waasConnector?: any
1857
): [WaasRequestConfirmation | undefined, (id: string) => void, (id: string) => void] {

packages/connect/src/hooks/useWaasEmailAuth.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,52 @@ interface SuccessResultV2 {
1515
signInResponse: SignInResponse
1616
}
1717

18+
/**
19+
* Hook to handle email-based authentication flow for WaaS (Wallet-as-a-Service).
20+
*
21+
* This hook manages the complete email authentication process, including:
22+
* - Initiating email authentication
23+
* - Handling verification code submission
24+
* - Managing loading and error states
25+
* - Supporting both v1 (idToken) and v2 (SignInResponse) authentication formats
26+
*
27+
* @param {ExtendedConnector} [params.connector] - The WaaS connector to use for authentication.
28+
* Optional because the user might not have selected a connector yet.
29+
* @param {Function} params.onSuccess - Callback function called when authentication succeeds.
30+
* Receives either a v1 result (with idToken) or v2 result (with SignInResponse).
31+
*
32+
* @returns {Object} An object containing:
33+
* - `inProgress` - Whether authentication is currently in progress
34+
* - `loading` - Whether a specific authentication operation is loading
35+
* - `error` - Any error that occurred during authentication
36+
* - `initiateAuth` - Function to start the email authentication process
37+
* - `sendChallengeAnswer` - Function to submit the verification code
38+
* - `cancel` - Function to cancel the authentication process
39+
* - `resetError` - Function to clear any error state
40+
*
41+
* @example
42+
* ```tsx
43+
* const {
44+
* inProgress,
45+
* loading,
46+
* error,
47+
* initiateAuth,
48+
* sendChallengeAnswer,
49+
* resetError
50+
* } = useEmailAuth({
51+
* connector: emailConnector,
52+
* onSuccess: async (result) => {
53+
* if ('signInResponse' in result) {
54+
* // Handle v2 authentication
55+
* await storage.setItem('email', result.signInResponse.email)
56+
* } else {
57+
* // Handle v1 authentication
58+
* await storage.setItem('idToken', result.idToken)
59+
* }
60+
* }
61+
* })
62+
* ```
63+
*/
1864
export function useEmailAuth({
1965
connector,
2066
onSuccess

packages/connect/src/hooks/useWaasEmailConflict.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@ import { IdentityType, EmailConflictInfo, SequenceWaaS } from '@0xsequence/waas'
22
import { useEffect, useRef, useState } from 'react'
33
import { useConnect } from 'wagmi'
44

5+
/**
6+
* Represents formatted email conflict information returned by the hook.
7+
* This is a user-friendly version of the EmailConflictInfo from the WaaS service.
8+
*/
59
export type FormattedEmailConflictInfo = {
10+
/** The email address that caused the conflict */
611
email: string
12+
/** User-friendly description of the account type (e.g., 'Google login', 'Email login') */
713
type: string
814
}
915

16+
/**
17+
* Helper function to convert raw account type information into user-friendly text.
18+
*
19+
* @param info - The raw email conflict information from WaaS
20+
* @returns A user-friendly string describing the account type
21+
*/
1022
const accountTypeText = (info: EmailConflictInfo | null) => {
1123
if (!info) {
1224
return 'Unknown account type'
@@ -34,6 +46,39 @@ const accountTypeText = (info: EmailConflictInfo | null) => {
3446
return info.type
3547
}
3648

49+
/**
50+
* Hook to handle email conflict detection and resolution for WaaS (Wallet-as-a-Service) authentication.
51+
*
52+
* This hook monitors all WaaS connectors for email conflicts that occur during sign-in attempts.
53+
* A conflict happens when a user tries to sign up with an email that's already associated with
54+
* a different authentication method (e.g., trying to use Google sign-in when the email was
55+
* previously used with Apple sign-in).
56+
*
57+
* @returns An object containing:
58+
* - `toggleEmailConflictModal` - Function to manually show/hide the conflict modal
59+
* - `isEmailConflictOpen` - Whether the conflict modal is currently open
60+
* - `emailConflictInfo` - Formatted information about the conflict (email and account type)
61+
* - `forceCreate` - Function to force create a new account despite the conflict
62+
*
63+
* @example
64+
* ```tsx
65+
* const {
66+
* isEmailConflictOpen,
67+
* emailConflictInfo,
68+
* toggleEmailConflictModal
69+
* } = useEmailConflict()
70+
*
71+
* // When a conflict is detected
72+
* if (isEmailConflictOpen && emailConflictInfo) {
73+
* console.log(
74+
* `Email ${emailConflictInfo.email} is already used with ${emailConflictInfo.type}`
75+
* )
76+
* }
77+
*
78+
* // Close the conflict modal
79+
* toggleEmailConflictModal(false)
80+
* ```
81+
*/
3782
export const useEmailConflict = () => {
3883
const { connectors } = useConnect()
3984
const forceCreateFuncRef = useRef<((forceCreate: boolean) => Promise<void>) | null>(null)

0 commit comments

Comments
 (0)