feat(web): show pending items on the installed app icon

Keep the Badging API count equal to the primary list's pending items, update it from optimistic changes and the list's SSE stream (shared across pages), and re-sync when the app returns to the foreground.

On iOS the badge needs notification permission, so Settings offers to enable it; Android does not support numeric web app badges.
This commit is contained in:
Jose Selesan
2026-10-29 14:20:21 -03:00
parent 7ed4d27844
commit aa170de8d1
14 changed files with 432 additions and 45 deletions

View File

@@ -1,47 +1,35 @@
import { useQueryClient } from '@tanstack/react-query'
import { useEffect } from 'react'
import { apiBaseUrl } from '@/lib/api-client'
import { LIST_EVENT_TYPES, queryKeysForListEvent } from '@/lib/list-events'
import { queryKeysForListEvent } from '@/lib/list-events'
import { subscribeToListEvents } from '@/lib/list-events-stream'
/**
* Subscribes to a list's SSE stream (`GET /lists/:listId/events`) and refreshes
* the affected queries whenever another member adds or buys an item, invites
* Follows a list's SSE stream (`GET /lists/:listId/events`) and refreshes the
* affected queries whenever another member adds or buys an item, invites
* somebody or renames the list — so the page updates without a reload.
*
* The session cookie is sent via `withCredentials`. `EventSource` reconnects on
* its own; a `ready` event is emitted on every (re)connection, used here to
* reconcile anything missed while disconnected.
* reconcile anything missed while disconnected. The connection is shared with
* any other subscriber of the same list (see `list-events-stream.ts`).
*
* Pass `undefined` to follow nothing (e.g. while the primary list is unknown).
*/
export function useListEvents(listId: string) {
export function useListEvents(listId: string | undefined) {
const queryClient = useQueryClient()
useEffect(() => {
const source = new EventSource(`${apiBaseUrl}/lists/${listId}/events`, {
withCredentials: true,
})
if (!listId) return
const onReady = () => {
queryClient.invalidateQueries({ queryKey: ['lists', listId, 'items'] })
}
source.addEventListener('ready', onReady)
const listeners = LIST_EVENT_TYPES.map((type) => {
const listener = () => {
return subscribeToListEvents(listId, {
onEvent: (type) => {
for (const queryKey of queryKeysForListEvent(type, listId)) {
queryClient.invalidateQueries({ queryKey })
void queryClient.invalidateQueries({ queryKey })
}
}
source.addEventListener(type, listener)
return { type, listener }
},
onReady: () => {
void queryClient.invalidateQueries({ queryKey: ['lists', listId, 'items'] })
},
})
return () => {
source.removeEventListener('ready', onReady)
for (const { type, listener } of listeners) source.removeEventListener(type, listener)
source.close()
}
}, [listId, queryClient])
}