stacker.news/components/use-has-new-notes.js
SatsAllDay 91a0d1ccd7
env vars for polling intervals (#1038)
* env vars for polling intervals

add env vars for 4 different common polling intervals,
fast (1000), normal (30000), long (60000), extra long (300000)

use env vars in all `pollInterval` params to `useQuery`

* replace `setInterval`'s interval with `FAST_POLL_INTERVAL`
2024-04-08 09:13:12 -05:00

33 lines
906 B
JavaScript

import { HAS_NOTIFICATIONS } from '@/fragments/notifications'
import { clearNotifications } from '@/lib/badge'
import { NORMAL_POLL_INTERVAL, SSR } from '@/lib/constants'
import { useQuery } from '@apollo/client'
import React, { useContext } from 'react'
export const HasNewNotesContext = React.createContext(false)
export function HasNewNotesProvider ({ me, children }) {
const { data } = useQuery(HAS_NOTIFICATIONS,
SSR
? {}
: {
pollInterval: NORMAL_POLL_INTERVAL,
nextFetchPolicy: 'cache-and-network',
onCompleted: ({ hasNewNotes }) => {
if (!hasNewNotes) {
clearNotifications()
}
}
})
return (
<HasNewNotesContext.Provider value={!!data?.hasNewNotes}>
{children}
</HasNewNotesContext.Provider>
)
}
export function useHasNewNotes () {
return useContext(HasNewNotesContext)
}