From 040ce0321545c7b7588b96e26d988fc19ffbbba3 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Thu, 7 Dec 2023 16:30:04 -0800 Subject: [PATCH] Grab-bag of post-feed improvements (#2140) * Sanity check against cases where empty pages may occur * Use the mergefeed as an emergency fallback to an empty feed * Check for new posts on focus --- src/lib/api/feed/merge.ts | 7 +++++-- src/state/queries/notifications/feed.ts | 6 +++++- src/state/queries/post-feed.ts | 6 +++++- src/view/com/posts/Feed.tsx | 9 ++++++++- src/view/screens/Home.tsx | 2 +- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/lib/api/feed/merge.ts b/src/lib/api/feed/merge.ts index 11e963f0..381159d1 100644 --- a/src/lib/api/feed/merge.ts +++ b/src/lib/api/feed/merge.ts @@ -29,7 +29,7 @@ export class MergeFeedAPI implements FeedAPI { this.feedCursor = 0 this.itemCursor = 0 this.sampleCursor = 0 - if (this.params.mergeFeedEnabled && this.params.mergeFeedSources) { + if (this.params.mergeFeedSources) { this.customFeeds = shuffle( this.params.mergeFeedSources.map( feedUri => new MergeFeedSource_Custom(feedUri, this.feedTuners), @@ -108,7 +108,10 @@ export class MergeFeedAPI implements FeedAPI { // this condition establishes the frequency that custom feeds are woven into follows const shouldSample = - i >= 15 && candidateFeeds.length >= 2 && (i % 4 === 0 || i % 5 === 0) + this.params.mergeFeedEnabled && + i >= 15 && + candidateFeeds.length >= 2 && + (i % 4 === 0 || i % 5 === 0) if (!canSample && !hasFollows) { // no data available diff --git a/src/state/queries/notifications/feed.ts b/src/state/queries/notifications/feed.ts index 3f9700c7..b8d8d616 100644 --- a/src/state/queries/notifications/feed.ts +++ b/src/state/queries/notifications/feed.ts @@ -91,11 +91,15 @@ export function useNotificationFeedQuery(opts?: {enabled?: boolean}) { const {isFetching, hasNextPage, data} = query let count = 0 + let numEmpties = 0 for (const page of data?.pages || []) { + if (!page.items.length) { + numEmpties++ + } count += page.items.length } - if (!isFetching && hasNextPage && count < PAGE_SIZE) { + if (!isFetching && hasNextPage && count < PAGE_SIZE && numEmpties < 3) { query.fetchNextPage() } }, [query]) diff --git a/src/state/queries/post-feed.ts b/src/state/queries/post-feed.ts index dede8a95..9bd1dacb 100644 --- a/src/state/queries/post-feed.ts +++ b/src/state/queries/post-feed.ts @@ -217,13 +217,17 @@ export function usePostFeedQuery( const {isFetching, hasNextPage, data} = query let count = 0 + let numEmpties = 0 for (const page of data?.pages || []) { + if (page.slices.length === 0) { + numEmpties++ + } for (const slice of page.slices) { count += slice.items.length } } - if (!isFetching && hasNextPage && count < PAGE_SIZE) { + if (!isFetching && hasNextPage && count < PAGE_SIZE && numEmpties < 3) { query.fetchNextPage() } }, [query]) diff --git a/src/view/com/posts/Feed.tsx b/src/view/com/posts/Feed.tsx index 03fa95ee..43f327c0 100644 --- a/src/view/com/posts/Feed.tsx +++ b/src/view/com/posts/Feed.tsx @@ -132,13 +132,20 @@ let Feed = ({ React.useEffect(() => { // we store the interval handler in a ref to avoid needless - // reassignments of the interval + // reassignments in other effects checkForNewRef.current = checkForNew }, [checkForNew]) + React.useEffect(() => { + if (enabled && checkForNewRef.current) { + // check for new on enable (aka on focus) + checkForNewRef.current() + } + }, [enabled]) React.useEffect(() => { if (!pollInterval) { return } + // check for new on interval const i = setInterval(() => checkForNewRef.current?.(), pollInterval) return () => clearInterval(i) }, [pollInterval]) diff --git a/src/view/screens/Home.tsx b/src/view/screens/Home.tsx index 8a0bbf10..42a958b9 100644 --- a/src/view/screens/Home.tsx +++ b/src/view/screens/Home.tsx @@ -175,7 +175,7 @@ function HomeScreenReady({ key="1" testID="followingFeedPage" isPageFocused={selectedPageIndex === 0} - feed={homeFeedParams.mergeFeedEnabled ? 'home' : 'following'} + feed="home" feedParams={homeFeedParams} renderEmptyState={renderFollowingEmptyState} renderEndOfFeed={FollowingEndOfFeed}