mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-03 08:07:21 +00:00
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:
1 parent
1f2fa9d03e
commit
23b9ca58c0
1 file changed
+12
-3
@@ -34,18 +34,27 @@ Example usage with intersection observer for automatic loading:
|
||||
|
||||
```tsx
|
||||
import { useInView } from 'react-intersection-observer';
|
||||
import { useMemo } from 'react';
|
||||
|
||||
function GlobalFeed() {
|
||||
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = useGlobalFeed();
|
||||
const { ref, inView } = useInView();
|
||||
|
||||
useEffect(() => {
|
||||
if (inView && hasNextPage) {
|
||||
if (inView && hasNextPage && !isFetchingNextPage) {
|
||||
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 (
|
||||
<div className="space-y-4">
|
||||
|
||||
Reference in new issue
Block a user