2023-04-21 07:23:27 -07:00
|
|
|
import { useQuery } from '@tanstack/react-query'
|
2023-04-28 14:36:24 -07:00
|
|
|
import { fetchNotifications } from 'apis/notifications/notifications'
|
2023-04-21 07:23:27 -07:00
|
|
|
import NotificationCookieStore from '@store/notificationCookieStore'
|
|
|
|
import { useWallet } from '@solana/wallet-adapter-react'
|
2023-08-01 13:27:12 -07:00
|
|
|
import useMangoAccount from 'hooks/useMangoAccount'
|
2023-04-21 07:23:27 -07:00
|
|
|
|
|
|
|
//10min
|
|
|
|
const refetchMs = 600000
|
|
|
|
|
|
|
|
export function useNotifications() {
|
2023-04-28 14:36:24 -07:00
|
|
|
const { publicKey } = useWallet()
|
2023-08-01 13:27:12 -07:00
|
|
|
const { mangoAccountAddress } = useMangoAccount()
|
|
|
|
|
2023-04-28 14:36:24 -07:00
|
|
|
const walletPubKey = publicKey?.toBase58()
|
2023-04-21 07:23:27 -07:00
|
|
|
const token = NotificationCookieStore((s) => s.currentToken)
|
2023-08-01 13:27:12 -07:00
|
|
|
const criteria = [token, mangoAccountAddress]
|
2023-04-21 07:23:27 -07:00
|
|
|
|
|
|
|
return useQuery(
|
2023-08-01 13:27:12 -07:00
|
|
|
['notifications', ...criteria],
|
|
|
|
() => fetchNotifications(walletPubKey!, token!, mangoAccountAddress),
|
2023-04-21 07:23:27 -07:00
|
|
|
{
|
2023-08-01 13:27:12 -07:00
|
|
|
enabled: !!(walletPubKey && token && mangoAccountAddress),
|
2023-04-21 07:23:27 -07:00
|
|
|
staleTime: refetchMs,
|
|
|
|
retry: 1,
|
|
|
|
refetchInterval: refetchMs,
|
2023-07-21 11:47:53 -07:00
|
|
|
},
|
2023-04-21 07:23:27 -07:00
|
|
|
)
|
|
|
|
}
|