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