From 5cd75c27f97b7fd944f403541ceb725b98757f28 Mon Sep 17 00:00:00 2001 From: gosangam Date: Sun, 21 Jun 2026 20:00:14 +0530 Subject: [PATCH] fix: user post feed view --- .../social/post/use-post-scroll-feed.ts | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/components/social/post/use-post-scroll-feed.ts b/components/social/post/use-post-scroll-feed.ts index 57cce4bd..ff1f416b 100644 --- a/components/social/post/use-post-scroll-feed.ts +++ b/components/social/post/use-post-scroll-feed.ts @@ -41,6 +41,10 @@ export function usePostScrollFeed(fetchPage: FetchPage) { const startedRef = useRef(false); const cursorRef = useRef(null); const anchorIdRef = useRef(null); + // Every post id we've already appended — used to dedupe AND to detect a + // stalled cursor (a page that adds nothing new), which we treat as the end + // so a backend ordering hiccup can never become an infinite fetch loop. + const idsRef = useRef>(new Set()); // Append the next older page, keyed off the last loaded post. const loadOlder = useCallback(async () => { @@ -48,13 +52,15 @@ export function usePostScrollFeed(fetchPage: FetchPage) { fetchingRef.current = true; try { const rows = (await fetchPage(cursorRef.current, PAGE)) ?? []; - setFeed((prev) => { - const seen = new Set(prev.map((p) => p.id)); - const merged = [...prev, ...rows.filter((p) => !seen.has(p.id))]; - if (merged.length > 0) cursorRef.current = cursorOf(merged[merged.length - 1]); - return merged; - }); - if (rows.length < PAGE) setHasMore(false); + const fresh = rows.filter((p) => !idsRef.current.has(p.id)); + if (fresh.length > 0) { + fresh.forEach((p) => idsRef.current.add(p.id)); + // rows come back newest→oldest; the oldest fresh row is the next cursor. + cursorRef.current = cursorOf(fresh[fresh.length - 1]); + setFeed((prev) => [...prev, ...fresh]); + } + // Stop on a short page OR a page that produced nothing new (stalled). + if (rows.length < PAGE || fresh.length === 0) setHasMore(false); } catch { setHasMore(false); setError((e) => e ?? "Could not load posts."); @@ -80,6 +86,7 @@ export function usePostScrollFeed(fetchPage: FetchPage) { if (anchor) { anchorIdRef.current = anchor.id; cursorRef.current = cursorOf(anchor); + idsRef.current.add(anchor.id); setFeed([anchor]); setLoading(false); await loadOlder(); // posts older than the anchor