-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@abstract-money/react": patch | ||
--- | ||
|
||
Add `useSubAccountsIds` hook. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './use-account-base-addresses-from-api' | ||
export * from './use-module-instantiate2-address-from-api' | ||
export * from './use-sub-accounts-ids-from-api' |
89 changes: 89 additions & 0 deletions
89
packages/react/src/hooks/account/public/use-sub-accounts-ids-from-api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { AccountId, AccountPublicClient } from '@abstract-money/core' | ||
import { | ||
UseQueryOptions, | ||
UseQueryResult, | ||
useQuery, | ||
} from '@tanstack/react-query' | ||
import React from 'react' | ||
import { useAccountId, useConfig } from '../../../contexts' | ||
import { parseParametersWithArgs } from '../utils' | ||
|
||
type QueryFnData = Awaited<ReturnType<AccountPublicClient['getSubAccountIds']>> | ||
|
||
type QueryError = unknown | ||
type QueryData = QueryFnData | ||
type UseSubAccountIdsFromApiArgs = Parameters< | ||
AccountPublicClient['getSubAccountIds'] | ||
>[0] | ||
type QueryKey = readonly [ | ||
'getSubAccountIdsFromApi', | ||
AccountPublicClient | undefined, | ||
AccountId | undefined, | ||
UseSubAccountIdsFromApiArgs['args'], | ||
] | ||
|
||
type QueryOptions = Omit< | ||
UseQueryOptions<QueryFnData, QueryError, QueryData, QueryKey>, | ||
'queryFn' | ||
> | ||
type QueryResult = UseQueryResult<QueryData, QueryError> | ||
|
||
export function useSubAccountIdsFromApi( | ||
parameters: UseSubAccountIdsFromApiArgs & { | ||
accountId: AccountId | undefined | ||
}, | ||
options?: QueryOptions, | ||
): QueryResult | ||
export function useSubAccountIdsFromApi( | ||
parameters: UseSubAccountIdsFromApiArgs, | ||
options?: QueryOptions, | ||
): QueryResult | ||
export function useSubAccountIdsFromApi( | ||
arg1: UseSubAccountIdsFromApiArgs & | ||
( | ||
| { | ||
accountId: AccountId | undefined | ||
} | ||
| {} | ||
), | ||
arg2: QueryOptions = { enabled: true }, | ||
) { | ||
const { | ||
args, | ||
accountId: accountIdParameter, | ||
options, | ||
} = parseParametersWithArgs(arg1, arg2) | ||
|
||
const { accountId } = useAccountId({ accountId: accountIdParameter }) | ||
const config = useConfig() | ||
const accountPublicClient = config.useAccountPublicClient({ | ||
accountId, | ||
chainName: accountId?.chainName, | ||
}) | ||
const queryKey = React.useMemo( | ||
() => | ||
[ | ||
'getSubAccountIdsFromApi', | ||
accountPublicClient, | ||
accountId, | ||
args, | ||
] as const, | ||
[accountPublicClient, accountId, args], | ||
) | ||
|
||
const enabled = React.useMemo( | ||
() => Boolean(accountPublicClient && args && options.enabled), | ||
[options.enabled, accountPublicClient], | ||
) | ||
|
||
const queryFn = React.useCallback(() => { | ||
if (!accountPublicClient) throw new Error('No client') | ||
if (!args) throw new Error('No args') | ||
|
||
return accountPublicClient.getSubAccountIds({ | ||
args, | ||
}) | ||
}, [accountPublicClient]) | ||
|
||
return useQuery(queryKey, queryFn, { ...options, enabled }) | ||
} |