Skip to content

Commit

Permalink
Fix crash when nothing is pinned (#7510)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Jan 20, 2025
1 parent fe9bb79 commit 2e8f2d5
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/view/screens/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {logEvent} from '#/lib/statsig/statsig'
import {isWeb} from '#/platform/detection'
import {emitSoftReset} from '#/state/events'
import {SavedFeedSourceInfo, usePinnedFeedsInfos} from '#/state/queries/feed'
import {FeedParams} from '#/state/queries/post-feed'
import {FeedDescriptor, FeedParams} from '#/state/queries/post-feed'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {UsePreferencesQueryResponse} from '#/state/queries/preferences/types'
import {useSession} from '#/state/session'
Expand Down Expand Up @@ -99,11 +99,12 @@ function HomeScreenReady({
() => pinnedFeedInfos.map(f => f.feedDescriptor),
[pinnedFeedInfos],
)
const rawSelectedFeed = useSelectedFeed() ?? allFeeds[0]
const maybeRawSelectedFeed: FeedDescriptor | undefined =
useSelectedFeed() ?? allFeeds[0]
const setSelectedFeed = useSetSelectedFeed()
const maybeFoundIndex = allFeeds.indexOf(rawSelectedFeed)
const maybeFoundIndex = allFeeds.indexOf(maybeRawSelectedFeed)
const selectedIndex = Math.max(0, maybeFoundIndex)
const selectedFeed = allFeeds[selectedIndex]
const maybeSelectedFeed: FeedDescriptor | undefined = allFeeds[selectedIndex]
const requestNotificationsPermission = useRequestNotificationsPermission()

useSetTitle(pinnedFeedInfos[selectedIndex]?.displayName)
Expand Down Expand Up @@ -135,11 +136,11 @@ function HomeScreenReady({

useFocusEffect(
useNonReactiveCallback(() => {
if (selectedFeed) {
if (maybeSelectedFeed) {
logEvent('home:feedDisplayed', {
index: selectedIndex,
feedType: selectedFeed.split('|')[0],
feedUrl: selectedFeed,
feedType: maybeSelectedFeed.split('|')[0],
feedUrl: maybeSelectedFeed,
reason: 'focus',
})
}
Expand All @@ -149,16 +150,20 @@ function HomeScreenReady({
const onPageSelected = React.useCallback(
(index: number) => {
setMinimalShellMode(false)
const feed = allFeeds[index]
const maybeFeed = allFeeds[index]

// Mutate the ref before setting state to avoid the imperative syncing effect
// above from starting a loop on Android when swiping back and forth.
lastPagerReportedIndexRef.current = index
setSelectedFeed(feed)
logEvent('home:feedDisplayed', {
index,
feedType: feed.split('|')[0],
feedUrl: feed,
})
setSelectedFeed(maybeFeed)

if (maybeFeed) {
logEvent('home:feedDisplayed', {
index,
feedType: maybeFeed.split('|')[0],
feedUrl: maybeFeed,
})
}
},
[setSelectedFeed, setMinimalShellMode, allFeeds],
)
Expand Down Expand Up @@ -228,7 +233,7 @@ function HomeScreenReady({
<FeedPage
key={feed}
testID="followingFeedPage"
isPageFocused={selectedFeed === feed}
isPageFocused={maybeSelectedFeed === feed}
isPageAdjacent={Math.abs(selectedIndex - index) === 1}
feed={feed}
feedParams={homeFeedParams}
Expand All @@ -243,7 +248,7 @@ function HomeScreenReady({
<FeedPage
key={feed}
testID="customFeedPage"
isPageFocused={selectedFeed === feed}
isPageFocused={maybeSelectedFeed === feed}
isPageAdjacent={Math.abs(selectedIndex - index) === 1}
feed={feed}
renderEmptyState={renderCustomFeedEmptyState}
Expand Down

0 comments on commit 2e8f2d5

Please sign in to comment.