import { notifyKey, useAsync } from '@app/lib' import { useEffect, useState } from 'react' import { inboxUnreadCount, isInboxNotification } from './api' import { watchNotifications, watchUnreadBadge } from './live' /** * Live unread total for the inbox: an HTTP baseline plus refetches driven by * the realtime bridge and by the page's own read/delete writes. * * Two deliberate couplings to what `/app/@me/notify` actually shows: * the count is the inbox's own filtered list, and a live mention or reply does * not bump it at all — those rows are not there to be found. */ export function useUnreadNotificationCount(): { count: number; capped: boolean } { const [tick, setTick] = useState(0) useEffect(() => { const refresh = () => setTick((value) => value + 1) const offLive = watchNotifications((notification) => { if (isInboxNotification(notification)) refresh() }) const offBadge = watchUnreadBadge(refresh) return () => { offLive() offBadge() } }, []) const value = useAsync(inboxUnreadCount, [tick], { cacheKey: notifyKey('unread') }) return value.data ?? { count: 0, capped: false } }