"use client"; import { useMemo } from "react"; import { useQueryClient } from "@tanstack/react-query"; import { useEntriesCacheEpoch } from "@/hooks/useEntriesCacheEpoch"; import type { DiscoveredPublication } from "@/lib/atprotoClient"; import { effectivePublicationUnreadCount, findUnreadCountInMap, sumUnreadForPublications, } from "@/lib/unreadCounts"; export type SidebarUnreadControllerOptions = { publications: DiscoveredPublication[]; unreadCountsByPublicationId: Map | undefined; isEntryRead?: (entryId: string) => boolean; pendingEntryReadState?: (entryId: string) => boolean | undefined; entryReadAt?: (entryId: string) => string | undefined; /** When each publication's server count was taken. */ unreadCountedAt?: (publicationId: string) => string | undefined; /** From {@link useReadState}; bumps when local read map changes. */ readEpoch?: number; viewerDid?: string; }; /** * Per-publication unread counts merging AppView baseline with local read state * for cached feed rows. Subscribes to entries-cache updates via {@link useEntriesCacheEpoch}. */ export function useSidebarUnreadController( options: SidebarUnreadControllerOptions ): Map { const { publications, unreadCountsByPublicationId, isEntryRead, pendingEntryReadState, entryReadAt, unreadCountedAt, readEpoch, viewerDid } = options; const queryClient = useQueryClient(); const entriesEpoch = useEntriesCacheEpoch(); return useMemo(() => { void entriesEpoch; void readEpoch; const map = new Map(); for (const pub of publications) { const knownServerCount = unreadCountsByPublicationId ? findUnreadCountInMap(unreadCountsByPublicationId, pub.publicationId) : undefined; const serverCount = knownServerCount ?? 0; map.set( pub.publicationId, isEntryRead && viewerDid ? effectivePublicationUnreadCount( serverCount, queryClient, viewerDid, pub.publicationId, isEntryRead, { capRaiseToServerCount: knownServerCount != null, pendingEntryReadState, entryReadAt, countedAt: unreadCountedAt?.(pub.publicationId), } ) : serverCount ); } return map; // entriesEpoch drives recomputation when prefetch/bootstrap fills entry cache }, [ publications, unreadCountsByPublicationId, isEntryRead, pendingEntryReadState, entryReadAt, unreadCountedAt, queryClient, entriesEpoch, readEpoch, viewerDid, ]); } /** Sum unread badges for a publication list (folder/section headers). */ export function useSidebarSectionUnreadSum( publications: DiscoveredPublication[], publicationUnreadCounts: Map ): number { return useMemo( () => sumUnreadForPublications(publications, publicationUnreadCounts), [publications, publicationUnreadCounts] ); }