mars-v2-frontend/hooks/useCreditAccounts.tsx
Linkie Link 27cdd1c954
Linter and prettier adjustments (#50)
* tidy: added eslintrc and prettierrc rules

* tidy: formated the files via ‚yarn format‘

* import sort improvements

* format script regex fix

* replace eslint import severity to warning

* remove staged file

Co-authored-by: Gustavo Mauricio <gustavo.mauricio58@gmail.com>
2022-11-09 10:04:06 +01:00

52 lines
1.4 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { useMemo } from 'react'
import { contractAddresses } from 'config/contracts'
import useCreditManagerStore from 'stores/useCreditManagerStore'
import useWalletStore from 'stores/useWalletStore'
import { queryKeys } from 'types/query-keys-factory'
type Result = {
tokens: string[]
}
const useCreditAccounts = () => {
const address = useWalletStore((s) => s.address)
const client = useWalletStore((s) => s.client)
const selectedAccount = useCreditManagerStore((s) => s.selectedAccount)
const creditManagerActions = useCreditManagerStore((s) => s.actions)
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) {
creditManagerActions.setSelectedAccount(data.tokens[0])
}
},
},
)
return {
...result,
data: useMemo(() => {
if (!address) return []
return result?.data && result.data.tokens
}, [address, result?.data]),
}
}
export default useCreditAccounts