Something went wrong. Try again.
The Social Wire — a comprehensive client for creating on the atmosphere thesocialwire.app
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246import type { QueryClient } from "@tanstack/react-query";
import { PUBLICATION_SIDEBAR_PROJECTION_QUERY_KEY } from "@/lib/sidebarQueryKeys";import { normalizeAtRepoParam, type DiscoveredPublication,} from "@/lib/atprotoClient";import { unreadCountsMapFromProjection, type PublicationSidebarProjection, type SidebarPublicationRow,} from "@/lib/publicationProjectionClient";import { countUnreadCachedEntries, getCachedEntriesForPublication,} from "@/lib/unreadCounts";
function clampUnreadCount(count: number): number { return Math.max(0, count);}
function applyDeltaToCount(current: number | undefined, delta: number): number { return clampUnreadCount((current ?? 0) + delta);}
function optimisticUnreadCountsGeneration( projection: PublicationSidebarProjection): number { return Math.max(Date.now(), (projection.unreadCountsGeneration ?? 0) + 1);}
export function countCachedUnreadForPublication( queryClient: QueryClient, viewerDid: string, publicationId: string, isEntryRead: (entryId: string) => boolean): number { return countUnreadCachedEntries( getCachedEntriesForPublication(queryClient, viewerDid, publicationId), isEntryRead );}
export function countCachedReadForPublication( queryClient: QueryClient, viewerDid: string, publicationId: string, isEntryRead: (entryId: string) => boolean): number { const entries = getCachedEntriesForPublication(queryClient, viewerDid, publicationId); const seen = new Set<string>(); let count = 0; for (const entry of entries) { if (seen.has(entry.entryId)) continue; seen.add(entry.entryId); if (isEntryRead(entry.entryId)) count += 1; } return count;}
function publicationIdsMatch(a: string, b: string): boolean { return normalizeAtRepoParam(a) === normalizeAtRepoParam(b);}
function patchSidebarRowUnreadCount( row: SidebarPublicationRow, normalizedPublicationId: string, delta: number): SidebarPublicationRow { if (!publicationIdsMatch(row.publicationId, normalizedPublicationId)) { return row; } if (row.unreadCount == null && delta === 0) { return row; } return { ...row, unreadCount: applyDeltaToCount(row.unreadCount, delta), };}
function patchProjectionPublicationRows( projection: PublicationSidebarProjection, normalizedPublicationId: string, delta: number): Pick< PublicationSidebarProjection, | "allPublicationRows" | "subscribedUnfoldered" | "followingTabPublications" | "myPublications" | "folderSections"> { const patchRows = (rows: SidebarPublicationRow[]) => rows.map((row) => patchSidebarRowUnreadCount(row, normalizedPublicationId, delta) );
return { allPublicationRows: patchRows(projection.allPublicationRows ?? []), subscribedUnfoldered: patchRows(projection.subscribedUnfoldered ?? []), followingTabPublications: patchRows( projection.followingTabPublications ?? [] ), myPublications: patchRows(projection.myPublications ?? []), folderSections: projection.folderSections?.map((section) => ({ ...section, publications: patchRows(section.publications), })), };}
export function applyPublicationUnreadCountDelta( queryClient: QueryClient, viewerDid: string, publicationId: string, delta: number): void { if (!viewerDid || !publicationId || delta === 0) return; const normalizedPublicationId = normalizeAtRepoParam(publicationId);
queryClient.setQueryData<PublicationSidebarProjection>( PUBLICATION_SIDEBAR_PROJECTION_QUERY_KEY(viewerDid), (old) => { if (!old) return old;
const unreadCountsByPublicationId = old.unreadCountsByPublicationId ? { ...old.unreadCountsByPublicationId } : undefined;
if (unreadCountsByPublicationId) { const existingKey = Object.keys(unreadCountsByPublicationId).find( (key) => publicationIdsMatch(key, normalizedPublicationId) ); const updated = applyDeltaToCount( existingKey ? unreadCountsByPublicationId[existingKey] : unreadCountsByPublicationId[normalizedPublicationId], delta ); if (existingKey) delete unreadCountsByPublicationId[existingKey]; if (updated === 0) { delete unreadCountsByPublicationId[normalizedPublicationId]; } else { unreadCountsByPublicationId[normalizedPublicationId] = updated; } }
return { ...old, ...patchProjectionPublicationRows(old, normalizedPublicationId, delta), unreadCountsByPublicationId, unreadCountsGeneration: optimisticUnreadCountsGeneration(old), unreadCountsAccuracy: "estimated", unreadCountsCountedAt: new Date().toISOString(), }; } );}
export function applyBulkPublicationUnreadCountDeltas( queryClient: QueryClient, viewerDid: string, deltas: ReadonlyMap<string, number>): void { for (const [publicationId, delta] of deltas) { applyPublicationUnreadCountDelta(queryClient, viewerDid, publicationId, delta); }}
/** Clears sidebar unread badges for publications (mark-all-read marks the full AppView scope). */export function clearPublicationUnreadCounts( queryClient: QueryClient, viewerDid: string, publications: DiscoveredPublication[]): void { if (!viewerDid || publications.length === 0) return;
const projection = queryClient.getQueryData<PublicationSidebarProjection>( PUBLICATION_SIDEBAR_PROJECTION_QUERY_KEY(viewerDid) ); if (!projection) return;
const countsMap = unreadCountsMapFromProjection(projection); for (const pub of publications) { const normalized = normalizeAtRepoParam(pub.publicationId); let current = 0; for (const [key, count] of countsMap) { if (publicationIdsMatch(key, normalized)) { current = count; break; } } if (current > 0) { applyPublicationUnreadCountDelta( queryClient, viewerDid, pub.publicationId, -current ); } }}
export function bulkReadDeltasForPublications( queryClient: QueryClient, viewerDid: string, publications: DiscoveredPublication[], isEntryRead: (entryId: string) => boolean): Map<string, number> { const deltas = new Map<string, number>(); for (const pub of publications) { const unread = countCachedUnreadForPublication( queryClient, viewerDid, pub.publicationId, isEntryRead ); if (unread > 0) { deltas.set(pub.publicationId, -unread); } } return deltas;}
export function bulkUnreadDeltasForPublications( queryClient: QueryClient, viewerDid: string, publications: DiscoveredPublication[], isEntryRead: (entryId: string) => boolean): Map<string, number> { const deltas = new Map<string, number>(); for (const pub of publications) { const read = countCachedReadForPublication( queryClient, viewerDid, pub.publicationId, isEntryRead ); if (read > 0) { deltas.set(pub.publicationId, read); } } return deltas;}