Merge branch 'CooperEdmunds-2047-last-active-tab' into main

zio/stable
Paul Frazee 2023-12-04 16:33:18 -08:00
commit 160055e4f9
1 changed files with 51 additions and 9 deletions

View File

@ -14,13 +14,38 @@ import {usePreferencesQuery} from '#/state/queries/preferences'
import {UsePreferencesQueryResponse} from '#/state/queries/preferences/types'
import {emitSoftReset} from '#/state/events'
import {useSession} from '#/state/session'
import {loadString, saveString} from '#/lib/storage'
import {useWebMediaQueries} from '#/lib/hooks/useWebMediaQueries'
type Props = NativeStackScreenProps<HomeTabNavigatorParams, 'Home'>
export function HomeScreen(props: Props) {
const {data: preferences} = usePreferencesQuery()
const {isDesktop} = useWebMediaQueries()
const [initialPage, setInitialPage] = React.useState<string | undefined>(
undefined,
)
if (preferences) {
return <HomeScreenReady {...props} preferences={preferences} />
React.useEffect(() => {
const loadLastActivePage = async () => {
try {
const lastActivePage =
(await loadString('lastActivePage')) ?? 'Following'
setInitialPage(lastActivePage)
} catch {
setInitialPage('Following')
}
}
loadLastActivePage()
}, [])
if (preferences && initialPage !== undefined) {
return (
<HomeScreenReady
{...props}
preferences={preferences}
initialPage={isDesktop ? 'Following' : initialPage}
/>
)
} else {
return (
<View style={styles.loading}>
@ -32,14 +57,16 @@ export function HomeScreen(props: Props) {
function HomeScreenReady({
preferences,
initialPage,
}: Props & {
preferences: UsePreferencesQueryResponse
initialPage: string
}) {
const {hasSession} = useSession()
const setMinimalShellMode = useSetMinimalShellMode()
const setDrawerSwipeDisabled = useSetDrawerSwipeDisabled()
const [selectedPage, setSelectedPage] = React.useState(0)
const isPageFocused = useIsFocused()
const [selectedPage, setSelectedPage] = React.useState<string>(initialPage)
/**
* Used to ensure that we re-compute `customFeeds` AND force a re-render of
@ -47,6 +74,13 @@ function HomeScreenReady({
*/
const pinnedFeedOrderKey = JSON.stringify(preferences.feeds.pinned)
const selectedPageIndex = React.useMemo(() => {
const index = ['Following', ...preferences.feeds.pinned].indexOf(
selectedPage,
)
return Math.max(index, 0)
}, [preferences.feeds.pinned, selectedPage])
const customFeeds = React.useMemo(() => {
const pinned = preferences.feeds.pinned
const feeds: FeedDescriptor[] = []
@ -70,20 +104,27 @@ function HomeScreenReady({
useFocusEffect(
React.useCallback(() => {
setMinimalShellMode(false)
setDrawerSwipeDisabled(selectedPage > 0)
setDrawerSwipeDisabled(selectedPageIndex > 0)
return () => {
setDrawerSwipeDisabled(false)
}
}, [setDrawerSwipeDisabled, selectedPage, setMinimalShellMode]),
}, [setDrawerSwipeDisabled, selectedPageIndex, setMinimalShellMode]),
)
const onPageSelected = React.useCallback(
(index: number) => {
setMinimalShellMode(false)
setSelectedPage(index)
setDrawerSwipeDisabled(index > 0)
const page = ['Following', ...preferences.feeds.pinned][index]
setSelectedPage(page)
saveString('lastActivePage', page)
},
[setDrawerSwipeDisabled, setSelectedPage, setMinimalShellMode],
[
setDrawerSwipeDisabled,
setSelectedPage,
setMinimalShellMode,
preferences.feeds.pinned,
],
)
const onPressSelected = React.useCallback(() => {
@ -126,6 +167,7 @@ function HomeScreenReady({
<Pager
key={pinnedFeedOrderKey}
testID="homeScreen"
initialPage={selectedPageIndex}
onPageSelected={onPageSelected}
onPageScrollStateChanged={onPageScrollStateChanged}
renderTabBar={renderTabBar}
@ -133,7 +175,7 @@ function HomeScreenReady({
<FeedPage
key="1"
testID="followingFeedPage"
isPageFocused={selectedPage === 0 && isPageFocused}
isPageFocused={selectedPageIndex === 0 && isPageFocused}
feed={homeFeedParams.mergeFeedEnabled ? 'home' : 'following'}
feedParams={homeFeedParams}
renderEmptyState={renderFollowingEmptyState}
@ -144,7 +186,7 @@ function HomeScreenReady({
<FeedPage
key={f}
testID="customFeedPage"
isPageFocused={selectedPage === 1 + index && isPageFocused}
isPageFocused={selectedPageIndex === 1 + index && isPageFocused}
feed={f}
renderEmptyState={renderCustomFeedEmptyState}
/>