mango-v4-ui/hooks/useFollowedAccounts.ts

36 lines
1022 B
TypeScript
Raw Normal View History

2023-11-09 13:09:24 -08:00
import { useWallet } from '@solana/wallet-adapter-react'
import { useQuery } from '@tanstack/react-query'
2023-11-13 20:41:16 -08:00
import { isEmpty } from 'lodash'
2023-11-09 13:09:24 -08:00
import { MANGO_DATA_API_URL } from 'utils/constants'
const fetchFollowedAccounts = async (walletPk: string | undefined) => {
try {
const response = await fetch(
`${MANGO_DATA_API_URL}/user-data/following?wallet-pk=${walletPk}`,
)
const data = await response.json()
2023-11-13 20:41:16 -08:00
if (isEmpty(data)) {
return []
} else return data
2023-11-09 13:09:24 -08:00
} catch (e) {
console.error('failed to fetch followed accounts', e)
2023-11-13 20:41:16 -08:00
return []
2023-11-09 13:09:24 -08:00
}
}
export default function useFollowedAccounts() {
const { publicKey } = useWallet()
const { data, isInitialLoading, refetch } = useQuery(
['followed-accounts', publicKey],
() => fetchFollowedAccounts(publicKey?.toString()),
{
cacheTime: 1000 * 60 * 10,
staleTime: 1000 * 60,
retry: 3,
refetchOnWindowFocus: false,
enabled: !!publicKey,
},
)
return { data, isInitialLoading, refetch }
}