Use lastActiveTab from AsyncStorage for non-desktop

zio/stable
Cooper Edmunds 2023-12-01 15:02:42 -05:00
parent f80bd30ef4
commit 9a13fe50cc
1 changed files with 37 additions and 3 deletions

View File

@ -14,13 +14,43 @@ 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 {save, load} 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 [lastActiveTab, setLastActiveTab] = React.useState<number | undefined>(
undefined,
)
if (preferences) { React.useEffect(() => {
return <HomeScreenReady {...props} preferences={preferences} /> const loadLastActieTab = async () => {
try {
const savedTab = await load('lastActiveTab')
const tabIndex = savedTab ? parseInt(savedTab, 10) : 0
setLastActiveTab(tabIndex)
} catch {
setLastActiveTab(0)
}
}
// Ignoring lastActiveTab for desktop
if (isDesktop) {
setLastActiveTab(0)
} else {
loadLastActieTab()
}
}, [isDesktop])
if (preferences && lastActiveTab !== undefined) {
return (
<HomeScreenReady
{...props}
preferences={preferences}
lastActiveTab={lastActiveTab}
/>
)
} else { } else {
return ( return (
<View style={styles.loading}> <View style={styles.loading}>
@ -32,13 +62,15 @@ export function HomeScreen(props: Props) {
function HomeScreenReady({ function HomeScreenReady({
preferences, preferences,
lastActiveTab,
}: Props & { }: Props & {
preferences: UsePreferencesQueryResponse preferences: UsePreferencesQueryResponse
lastActiveTab: number
}) { }) {
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 [selectedPage, setSelectedPage] = React.useState(lastActiveTab)
/** /**
* 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
@ -80,6 +112,7 @@ function HomeScreenReady({
(index: number) => { (index: number) => {
setMinimalShellMode(false) setMinimalShellMode(false)
setSelectedPage(index) setSelectedPage(index)
save('lastActiveTab', index)
setDrawerSwipeDisabled(index > 0) setDrawerSwipeDisabled(index > 0)
}, },
[setDrawerSwipeDisabled, setSelectedPage, setMinimalShellMode], [setDrawerSwipeDisabled, setSelectedPage, setMinimalShellMode],
@ -125,6 +158,7 @@ function HomeScreenReady({
<Pager <Pager
key={pinnedFeedOrderKey} key={pinnedFeedOrderKey}
testID="homeScreen" testID="homeScreen"
initialPage={selectedPage > customFeeds.length ? 0 : selectedPage}
onPageSelected={onPageSelected} onPageSelected={onPageSelected}
onPageScrollStateChanged={onPageScrollStateChanged} onPageScrollStateChanged={onPageScrollStateChanged}
renderTabBar={renderTabBar} renderTabBar={renderTabBar}