mango-v4-ui/hooks/useProfileDetails.ts

33 lines
995 B
TypeScript
Raw Normal View History

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