From 75d028002a9270a9f4a44fcbf5a5f2a3e6ec7055 Mon Sep 17 00:00:00 2001 From: Rishi Balakrishnan Date: Sun, 2 Aug 2026 09:57:14 -0400 Subject: [PATCH] fix(community-feed): stop interval refetch hijacking scroll, probe head for freshness Refetching the infinite timeline query on a 60s timer replays every loaded page and swaps the whole dataset; when scrolled deep this shifts content under the viewport, so iOS clamps the offset and the feed flashes, jumps to top, and re-triggers onEndReached in a loop (also dismissing fullscreen video when its item unmounts). Replace the interval and on-focus refetch with a one-post head probe that lights the LoadLatestBtn indicator; real refetches now only happen on pull-to-refresh, the load-latest pill, or when the feed is empty. --- src/state/queries/community-feed.ts | 58 ++++++++++++++++-------- src/view/com/feeds/CommunityFeedPage.tsx | 53 ++++++++++++++++++---- 2 files changed, 85 insertions(+), 26 deletions(-) diff --git a/src/state/queries/community-feed.ts b/src/state/queries/community-feed.ts index 1ad32f88e..558dbee0f 100644 --- a/src/state/queries/community-feed.ts +++ b/src/state/queries/community-feed.ts @@ -122,6 +122,45 @@ export function useCommunityTimelineQuery(enabled: boolean) { }) } +/** + * Fetch just the newest community timeline post, for cheap freshness probes. + */ +export async function fetchCommunityTimelineHead( + agent: ReturnType, +): Promise { + const res = await communityXrpc( + agent, + 'community.blacksky.feed.getCommunityTimeline', + {params: {limit: '1'}}, + ) + if (!res.ok) { + throw new Error(`getCommunityTimeline failed: ${res.status}`) + } + const page = jsonToLex(await res.json()) as CommunityFeedPage + return page.feed?.[0] +} + +// A reply only resurfaces its thread when the root author is +// continuing their own thread; other people's replies stay in the +// thread view. Stands in for followedRepliesOnly, which would pass +// everything here since the whole community is "followed". +export function surfacesInCommunityFeed( + item: AppBskyFeedDefs.FeedViewPost, +): boolean { + // Blocked/muted authors never surface, same as the Following feed. + const authorViewer = item.post.author.viewer + if ( + authorViewer?.blocking || + authorViewer?.blockedBy || + authorViewer?.muted + ) { + return false + } + const reply = (item.post.record as AppBskyFeedPost.Record)?.reply + if (!reply?.root?.uri) return true + return new AtUri(reply.root.uri).host === item.post.author.did +} + const COMMUNITY_POST_RQKEY_ROOT = 'community-post' export const COMMUNITY_POST_RQKEY = (uri: string) => [ COMMUNITY_POST_RQKEY_ROOT, @@ -201,24 +240,7 @@ export function useCommunityFeedSlices( return useMemo(() => { if (!moderationOpts) return [] - // A reply only resurfaces its thread when the root author is - // continuing their own thread; other people's replies stay in the - // thread view. Stands in for followedRepliesOnly, which would pass - // everything here since the whole community is "followed". - const surfaced = feedItems.filter(item => { - // Blocked/muted authors never surface, same as the Following feed. - const authorViewer = item.post.author.viewer - if ( - authorViewer?.blocking || - authorViewer?.blockedBy || - authorViewer?.muted - ) { - return false - } - const reply = (item.post.record as AppBskyFeedPost.Record)?.reply - if (!reply?.root?.uri) return true - return new AtUri(reply.root.uri).host === item.post.author.did - }) + const surfaced = feedItems.filter(surfacesInCommunityFeed) // Same tuner stack as the Following feed (see useFeedTuners). const tunerFns = [FeedTuner.removeOrphans] if (preferences?.feedViewPrefs.hideReposts) { diff --git a/src/view/com/feeds/CommunityFeedPage.tsx b/src/view/com/feeds/CommunityFeedPage.tsx index d1713e2cf..a1e6ab13a 100644 --- a/src/view/com/feeds/CommunityFeedPage.tsx +++ b/src/view/com/feeds/CommunityFeedPage.tsx @@ -1,17 +1,26 @@ import {useCallback, useEffect, useMemo, useRef, useState} from 'react' -import {ActivityIndicator, type ListRenderItemInfo, View} from 'react-native' +import { + ActivityIndicator, + AppState, + type ListRenderItemInfo, + View, +} from 'react-native' import {msg} from '@lingui/core/macro' import {useLingui} from '@lingui/react' import {Trans} from '@lingui/react/macro' +import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback' import {useOpenComposer} from '#/lib/hooks/useOpenComposer' import {s} from '#/lib/styles' +import {logger} from '#/logger' import { type CommunityFeedSlice, + fetchCommunityTimelineHead, + surfacesInCommunityFeed, useCommunityFeedSlices, useCommunityTimelineQuery, } from '#/state/queries/community-feed' -import {useSession} from '#/state/session' +import {useAgent, useSession} from '#/state/session' import {isThreadChildAt, isThreadParentAt} from '#/view/com/posts/PostFeed' import {PostFeedItem} from '#/view/com/posts/PostFeedItem' import {ViewFullThread} from '#/view/com/posts/ViewFullThread' @@ -42,6 +51,7 @@ type CommunityFeedRow = export function CommunityFeedPage({isPageFocused}: {isPageFocused: boolean}) { const {_} = useLingui() const t = useTheme() + const agent = useAgent() const {hasSession} = useSession() const headerOffset = useHeaderOffset() const scrollElRef = useRef(null) @@ -115,30 +125,57 @@ export function CommunityFeedPage({isPageFocused}: {isPageFocused: boolean}) { const [isRefreshing, setIsRefreshing] = useState(false) const [isScrolledDown, setIsScrolledDown] = useState(false) + const [hasNew, setHasNew] = useState(false) const onRefresh = useCallback(async () => { setIsRefreshing(true) try { await refetch() + setHasNew(false) } finally { setIsRefreshing(false) } }, [refetch]) + // Refetching an infinite query replays every loaded page, so doing it on + // a timer shifts content under the user's scroll position (see upstream + // PostFeed's "UI freakouts" note). Poll a one-post head probe instead and + // light the load-latest pill; only user actions trigger a real refetch. + const checkForNew = useNonReactiveCallback(async () => { + if (!isPageFocused || hasNew || isLoading || isRefreshing) return + if (AppState.currentState !== 'active') return + try { + const head = await fetchCommunityTimelineHead(agent) + if (!head || !surfacesInCommunityFeed(head)) return + if (feedItems.length === 0) { + void refetch() + return + } + if (!feedItems.some(item => item.post.uri === head.post.uri)) { + setHasNew(true) + } + } catch (e) { + logger.debug('CommunityFeedPage: head probe failed', { + message: String(e), + }) + } + }) + useEffect(() => { - if (isPageFocused) void refetch() - }, [isPageFocused, refetch]) + if (isPageFocused) void checkForNew() + }, [isPageFocused, checkForNew]) useEffect(() => { if (!isPageFocused) return - const id = setInterval(() => void refetch(), 60_000) + const id = setInterval(() => void checkForNew(), 60_000) return () => clearInterval(id) - }, [isPageFocused, refetch]) + }, [isPageFocused, checkForNew]) const onPressLoadLatest = useCallback(() => { scrollElRef.current?.scrollToOffset({ animated: IS_NATIVE, offset: -headerOffset, }) + setHasNew(false) void refetch() }, [scrollElRef, headerOffset, refetch]) @@ -256,11 +293,11 @@ export function CommunityFeedPage({isPageFocused}: {isPageFocused: boolean}) { onScrolledDownChange={setIsScrolledDown} /> - {isScrolledDown && ( + {(isScrolledDown || hasNew) && ( )} {hasSession && ( -- knotmirror 0.0.1