11import { AccountId , ApiClient } from '@abstract-money/core'
22import { UseQueryOptions , useQuery } from '@tanstack/react-query'
33import React from 'react'
4+ import { MaybeArray } from '../types/utils'
45
56/**
67 * Loads all accounts for a given owner and chain.
@@ -15,7 +16,7 @@ export function useAccounts(
1516 client,
1617 } : {
1718 owner : string | undefined
18- chain : string | undefined
19+ chain : MaybeArray < string > | undefined
1920 client : ApiClient | undefined
2021 } ,
2122 {
@@ -28,29 +29,33 @@ export function useAccounts(
2829 readonly [
2930 'accountsOf' ,
3031 string | undefined ,
31- string | undefined ,
32+ string [ ] | undefined ,
3233 ApiClient | undefined ,
3334 ]
3435 > = { } ,
3536) {
37+ const chains = React . useMemo (
38+ ( ) => ( chain ? ( Array . isArray ( chain ) ? chain : [ chain ] ) : undefined ) ,
39+ [ chain ] ,
40+ )
3641 const queryKey = React . useMemo (
37- ( ) => [ 'accountsOf' , owner , chain , client ] as const ,
38- [ owner , chain , client ] ,
42+ ( ) => [ 'accountsOf' , owner , chains , client ] as const ,
43+ [ owner , chains , client ] ,
3944 )
4045
4146 const enabled = React . useMemo (
42- ( ) => Boolean ( client && chain && owner && enabled_ ) ,
43- [ enabled_ , client , owner , chain ] ,
47+ ( ) => Boolean ( client && chains && owner && enabled_ ) ,
48+ [ enabled_ , client , owner , chains ] ,
4449 )
4550
4651 const queryFn = React . useCallback ( ( ) => {
47- if ( ! client || ! owner || ! chain )
52+ if ( ! client || ! owner || ! chains )
4853 throw new Error ( 'No client or owner or chain' )
4954
5055 return client . getAccountsByOwnerFromApi ( {
51- args : { owner, chains : [ chain ] } ,
56+ args : { owner, chains } ,
5257 } )
53- } , [ client , owner , chain ] )
58+ } , [ client , owner , chains ] )
5459
5560 return useQuery ( queryKey , queryFn , { enabled, ...rest } )
5661}
0 commit comments