stacker.news/components/use-has-new-notes.js
ekzyis b1a0abe32c
Service Worker rewrite (#2274)
* Convert all top-level arrow functions to regular functions

* Refactor webPush.sendNotification call

* Refactor webPush logging

* Rename var to title

* Rewrite service worker

This rewrite simplifies the service worker by removing

* merging of push notifications via tag property
* badge count

These features weren't properly working on iOS. We concluded that we don't really need them.

For example, this means replies will no longer get merged to "you have X new replies" but show up as individual notifications.

Only zaps still use the tag property so devices that support it can still replace any previous "your post stacked X sats" notification for the same item.

* Don't use async/await in service worker

* Support app badge count

* Fix extremely slow notificationclick

* Fix serialization and save in pushsubscriptionchange event
2025-07-10 11:54:23 -05:00

33 lines
921 B
JavaScript

import { HAS_NOTIFICATIONS } from '@/fragments/notifications'
import { NORMAL_POLL_INTERVAL, SSR } from '@/lib/constants'
import { useQuery } from '@apollo/client'
import React, { useContext } from 'react'
import { clearNotifications } from '@/components/serviceworker'
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)
}