Skip to content

Commit

Permalink
feat: useSubAccountsIds hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dalechyn committed Feb 22, 2024
1 parent 7bf2c80 commit 42f205d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-terms-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@abstract-money/react": patch
---

Add `useSubAccountsIds` hook.
1 change: 1 addition & 0 deletions packages/react/src/hooks/account/public/index.ts
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'
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 })
}

0 comments on commit 42f205d

Please sign in to comment.