Something went wrong. Try again.
The Social Wire — a comprehensive client for creating on the atmosphere thesocialwire.app
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293"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<string, number> | 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<string, number> { 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<string, number>(); 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<string, number>): number { return useMemo( () => sumUnreadForPublications(publications, publicationUnreadCounts), [publications, publicationUnreadCounts] );}