Add deduplication to infinite feed scroll example

This change introduces a simple deduplication mechanicsm for the example infinite
scroll feed code we provide for the agent. I have also added a 'isFetchingNextPage'
check to avoid the potential scenario of aggressively overfetching via quick scrolling.

If a post exists in the feed already (it has been 'seen'), the it and all duplicate
post events will be filtered out from the returned content.
This commit is contained in:
Chad Curtis committed 2025-10-04 17:46:17 +00:00
1 parent 1f2fa9d03e
commit 23b9ca58c0
1 file changed
+12 -3
+12 -3
View File
@@ -34,18 +34,27 @@ Example usage with intersection observer for automatic loading:
```tsx ```tsx
import { useInView } from 'react-intersection-observer'; import { useInView } from 'react-intersection-observer';
import { useMemo } from 'react';
function GlobalFeed() { function GlobalFeed() {
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = useGlobalFeed(); const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = useGlobalFeed();
const { ref, inView } = useInView(); const { ref, inView } = useInView();
useEffect(() => { useEffect(() => {
if (inView && hasNextPage) { if (inView && hasNextPage && !isFetchingNextPage) {
fetchNextPage(); fetchNextPage();
} }
}, [inView, hasNextPage, fetchNextPage]); }, [inView, hasNextPage, isFetchingNextPage, fetchNextPage]);
const posts = data?.pages.flat() || []; // Remove duplicate events by ID
const posts = useMemo(() => {
const seen = new Set();
return data?.pages.flat().filter(event => {
if (!event.id || seen.has(event.id)) return false;
seen.add(event.id);
return true;
}) || [];
}, [data?.pages]);
return ( return (
<div className="space-y-4"> <div className="space-y-4">