Skip to content

Commit 95664f6

Browse files
committed
feat: ambitious array-value chain parameter
1 parent 7283bba commit 95664f6

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

packages/react/src/hooks/use-accounts.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AccountId, ApiClient } from '@abstract-money/core'
22
import { UseQueryOptions, useQuery } from '@tanstack/react-query'
33
import 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
}

packages/react/src/types/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type MaybeArray<T> = T | T[]

0 commit comments

Comments
 (0)