SP state cleanup (#4569)
This commit is contained in:
+28
-42
@@ -22,12 +22,13 @@ import {useSelectedFeed, useSetSelectedFeed} from '#/state/shell/selected-feed'
|
||||
import {useOTAUpdates} from 'lib/hooks/useOTAUpdates'
|
||||
import {useRequestNotificationsPermission} from 'lib/notifications/notifications'
|
||||
import {HomeTabNavigatorParams, NativeStackScreenProps} from 'lib/routes/types'
|
||||
import {
|
||||
useCurrentStarterPack,
|
||||
useSetCurrentStarterPack,
|
||||
} from 'state/preferences/starter-pack'
|
||||
import {useUsedStarterPacks} from 'state/preferences/used-starter-packs'
|
||||
import {parseStarterPackUri} from 'lib/strings/starter-pack'
|
||||
import {isWeb} from 'platform/detection'
|
||||
import {useLoggedOutViewControls} from 'state/shell/logged-out'
|
||||
import {
|
||||
useActiveStarterPack,
|
||||
useSetActiveStarterPack,
|
||||
} from 'state/shell/starter-pack'
|
||||
import {FeedPage} from 'view/com/feeds/FeedPage'
|
||||
import {Pager, PagerRef, RenderTabBarFnProps} from 'view/com/pager/Pager'
|
||||
import {CustomFeedEmptyState} from 'view/com/posts/CustomFeedEmptyState'
|
||||
@@ -38,33 +39,37 @@ import {HomeHeader} from '../com/home/HomeHeader'
|
||||
|
||||
type Props = NativeStackScreenProps<HomeTabNavigatorParams, 'Home'>
|
||||
export function HomeScreen(props: Props) {
|
||||
const {navigation} = props
|
||||
const {hasSession} = useSession()
|
||||
const {data: preferences} = usePreferencesQuery()
|
||||
const {data: pinnedFeedInfos, isLoading: isPinnedFeedsLoading} =
|
||||
usePinnedFeedsInfos()
|
||||
const currentStarterPack = useCurrentStarterPack()
|
||||
const usedStarterPacks = useUsedStarterPacks()
|
||||
const activeStarterPack = useActiveStarterPack()
|
||||
const setActiveStarterPack = useSetActiveStarterPack()
|
||||
const {setShowLoggedOut, requestSwitchToAccount} = useLoggedOutViewControls()
|
||||
|
||||
React.useEffect(() => {
|
||||
if (currentStarterPack && !currentStarterPack?.initialFeed) {
|
||||
// In test environments, the URL won't start with `https://bsky.app`, so we want to find it by the route
|
||||
try {
|
||||
const route = new URL(currentStarterPack.uri).pathname
|
||||
const foundIndex = usedStarterPacks?.findIndex(p => p.includes(route))
|
||||
if (foundIndex === -1) {
|
||||
setShowLoggedOut(true)
|
||||
requestSwitchToAccount({requestedAccount: 'starterpack'})
|
||||
}
|
||||
} catch {
|
||||
// Don't need to handle anything here, just put the user on the home screen
|
||||
// This will be true if the app was launched with a starter pack referral.
|
||||
if (activeStarterPack?.uri) {
|
||||
if (hasSession) {
|
||||
const parsed = parseStarterPackUri(activeStarterPack.uri)
|
||||
if (!parsed) return
|
||||
setActiveStarterPack(undefined)
|
||||
navigation.navigate('StarterPack', parsed)
|
||||
} else {
|
||||
setShowLoggedOut(true)
|
||||
requestSwitchToAccount({
|
||||
requestedAccount: isWeb ? 'starterpack' : 'new',
|
||||
})
|
||||
}
|
||||
}
|
||||
}, [
|
||||
hasSession,
|
||||
setShowLoggedOut,
|
||||
requestSwitchToAccount,
|
||||
currentStarterPack?.initialFeed,
|
||||
currentStarterPack,
|
||||
usedStarterPacks,
|
||||
activeStarterPack,
|
||||
setActiveStarterPack,
|
||||
navigation,
|
||||
])
|
||||
|
||||
if (preferences && pinnedFeedInfos && !isPinnedFeedsLoading) {
|
||||
@@ -96,23 +101,9 @@ function HomeScreenReady({
|
||||
[pinnedFeedInfos],
|
||||
)
|
||||
|
||||
const currentStarterPack = useCurrentStarterPack()
|
||||
const setCurrentStarterPack = useSetCurrentStarterPack()
|
||||
|
||||
const starterPackInitialFeed = currentStarterPack?.initialFeed
|
||||
? allFeeds.find(f => {
|
||||
if (currentStarterPack.initialFeed === 'following') {
|
||||
return f === 'following'
|
||||
} else {
|
||||
return f === `feedgen|${currentStarterPack.initialFeed}`
|
||||
}
|
||||
})
|
||||
: undefined
|
||||
const rawSelectedFeed = useSelectedFeed() ?? allFeeds[0]
|
||||
const setSelectedFeed = useSetSelectedFeed()
|
||||
const maybeFoundIndex = allFeeds.indexOf(
|
||||
starterPackInitialFeed ?? rawSelectedFeed,
|
||||
)
|
||||
const maybeFoundIndex = allFeeds.indexOf(rawSelectedFeed)
|
||||
const selectedIndex = Math.max(0, maybeFoundIndex)
|
||||
const selectedFeed = allFeeds[selectedIndex]
|
||||
const requestNotificationsPermission = useRequestNotificationsPermission()
|
||||
@@ -138,12 +129,7 @@ function HomeScreenReady({
|
||||
lastPagerReportedIndexRef.current = selectedIndex
|
||||
pagerRef.current?.setPage(selectedIndex, 'desktop-sidebar-click')
|
||||
}
|
||||
}, [
|
||||
selectedIndex,
|
||||
allFeeds,
|
||||
setCurrentStarterPack,
|
||||
currentStarterPack?.initialFeed,
|
||||
])
|
||||
}, [selectedIndex, allFeeds])
|
||||
|
||||
const {hasSession} = useSession()
|
||||
const setMinimalShellMode = useSetMinimalShellMode()
|
||||
|
||||
Reference in New Issue
Block a user