Files
eranos/src/lib/deduplicateEvents.ts
T
Chad Curtis 6dd29c571f refactor: extract useInfiniteScroll hook and deduplicateEvents utility
Consolidate duplicated infinite-scroll boilerplate (auto-fetch page 2,
IntersectionObserver, scroll trigger) into a shared useInfiniteScroll
hook, and extract the flatten+dedup pattern into deduplicateEvents.

Also migrate BadgesPage and ThemesPage to use useFeedTab for
consistent tab persistence across all feed pages.
2026-03-28 05:31:49 -05:00

18 lines
526 B
TypeScript

import type { NostrEvent } from '@nostrify/nostrify';
/**
* Flatten paginated Nostr event arrays and deduplicate by event ID.
*
* Accepts the `pages` property from a TanStack `useInfiniteQuery` result
* where each page is a `NostrEvent[]`.
*/
export function deduplicateEvents(pages: NostrEvent[][] | undefined): NostrEvent[] {
if (!pages) return [];
const seen = new Set<string>();
return pages.flat().filter((event) => {
if (seen.has(event.id)) return false;
seen.add(event.id);
return true;
});
}