2023-11-20 15:04:38 +00:00
|
|
|
import React, { useContext } from 'react'
|
2021-11-28 17:29:17 +00:00
|
|
|
import { useQuery } from '@apollo/client'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { ME } from '@/fragments/users'
|
2024-04-08 14:13:12 +00:00
|
|
|
import { FAST_POLL_INTERVAL, SSR } from '@/lib/constants'
|
2021-05-25 00:08:56 +00:00
|
|
|
|
|
|
|
export const MeContext = React.createContext({
|
|
|
|
me: null
|
|
|
|
})
|
|
|
|
|
2021-11-28 17:29:17 +00:00
|
|
|
export function MeProvider ({ me, children }) {
|
2024-04-08 14:13:12 +00:00
|
|
|
const { data } = useQuery(ME, SSR ? {} : { pollInterval: FAST_POLL_INTERVAL, nextFetchPolicy: 'cache-and-network' })
|
2021-05-25 00:08:56 +00:00
|
|
|
|
|
|
|
return (
|
2023-11-20 15:04:38 +00:00
|
|
|
<MeContext.Provider value={data?.me || me}>
|
2021-05-25 00:08:56 +00:00
|
|
|
{children}
|
|
|
|
</MeContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useMe () {
|
2023-11-20 15:04:38 +00:00
|
|
|
return useContext(MeContext)
|
2021-05-25 00:08:56 +00:00
|
|
|
}
|