2023-04-21 07:23:27 -07:00
|
|
|
import { useWallet } from '@solana/wallet-adapter-react'
|
|
|
|
import NotificationCookieStore from '@store/notificationCookieStore'
|
|
|
|
import { useEffect } from 'react'
|
|
|
|
import { useNotifications } from './useNotifications'
|
|
|
|
import { notify } from 'utils/notifications'
|
|
|
|
|
|
|
|
type Error = {
|
|
|
|
status: number
|
|
|
|
error: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useCookies() {
|
2023-04-28 15:01:06 -07:00
|
|
|
const { publicKey } = useWallet()
|
2023-04-21 07:23:27 -07:00
|
|
|
const updateCookie = NotificationCookieStore((s) => s.updateCookie)
|
|
|
|
const removeCookie = NotificationCookieStore((s) => s.removeCookie)
|
2023-04-28 14:36:24 -07:00
|
|
|
const resetCurrentToken = NotificationCookieStore((s) => s.resetCurrentToken)
|
2023-04-21 07:23:27 -07:00
|
|
|
const token = NotificationCookieStore((s) => s.currentToken)
|
|
|
|
const { error } = useNotifications()
|
|
|
|
const errorResp = error as Error
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-04-28 15:01:06 -07:00
|
|
|
if (publicKey) {
|
|
|
|
updateCookie(publicKey?.toBase58())
|
|
|
|
} else {
|
2023-04-28 14:36:24 -07:00
|
|
|
resetCurrentToken()
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
resetCurrentToken()
|
|
|
|
}
|
2023-04-28 15:01:06 -07:00
|
|
|
}, [publicKey?.toBase58()])
|
2023-04-28 14:36:24 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (errorResp?.status === 401 && publicKey && token) {
|
|
|
|
removeCookie(publicKey?.toBase58())
|
2023-04-21 07:23:27 -07:00
|
|
|
notify({
|
|
|
|
title: errorResp.error,
|
|
|
|
type: 'error',
|
|
|
|
})
|
|
|
|
}
|
2023-04-28 14:36:24 -07:00
|
|
|
}, [errorResp, publicKey?.toBase58()])
|
2023-04-21 07:23:27 -07:00
|
|
|
}
|