c4f8f4eab0
* MP-1757: implemented the WalletProvider and connect buttons * tidy: tidy up the search * MP-1691: moved modals outside of the DOM * MP-1691: changed CreditManager into AccountDetails * fix: fixed the naming * MP-1691: UX approvements * MP-1691: global confirm and delete modal added * fix: merged the credit-account and wallet branch * MP-1757: added the status store * fix: updated the store interaction * MP-1757: major cleanup of stores * tidy: format
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { useMemo } from 'react'
|
|
|
|
import { contractAddresses } from 'config/contracts'
|
|
import { useAccountDetailsStore, useWalletStore } from 'stores'
|
|
import { queryKeys } from 'types/query-keys-factory'
|
|
|
|
type Result = {
|
|
tokens: string[]
|
|
}
|
|
|
|
const useCreditAccounts = () => {
|
|
const address = useWalletStore((s) => s.address)
|
|
const client = useWalletStore((s) => s.signingClient)
|
|
const selectedAccount = useAccountDetailsStore((s) => s.selectedAccount)
|
|
const setSelectedAccount = (account: string) => {
|
|
useAccountDetailsStore.setState({ selectedAccount: account })
|
|
}
|
|
|
|
const queryMsg = useMemo(() => {
|
|
return {
|
|
tokens: {
|
|
owner: address,
|
|
},
|
|
}
|
|
}, [address])
|
|
|
|
const result = useQuery<Result>(
|
|
queryKeys.creditAccounts(address ?? ''),
|
|
async () => client?.queryContractSmart(contractAddresses.accountNft, queryMsg),
|
|
{
|
|
staleTime: Infinity,
|
|
enabled: !!address && !!client,
|
|
onSuccess: (data) => {
|
|
if (!data.tokens.includes(selectedAccount || '') && data.tokens.length > 0) {
|
|
setSelectedAccount(data.tokens[0])
|
|
}
|
|
},
|
|
},
|
|
)
|
|
|
|
return {
|
|
...result,
|
|
data: useMemo(() => {
|
|
if (!address) return []
|
|
|
|
return result?.data && result.data.tokens
|
|
}, [address, result?.data]),
|
|
}
|
|
}
|
|
|
|
export default useCreditAccounts
|