Track active page using string instead of index
parent
9a13fe50cc
commit
17c58d40ff
|
@ -14,41 +14,36 @@ 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 {loadString, saveString} from '#/lib/storage'
|
||||||
import {useWebMediaQueries} from '#/lib/hooks/useWebMediaQueries'
|
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 {isDesktop} = useWebMediaQueries()
|
||||||
const [lastActiveTab, setLastActiveTab] = React.useState<number | undefined>(
|
const [initialPage, setInitialPage] = React.useState<string | undefined>(
|
||||||
undefined,
|
undefined,
|
||||||
)
|
)
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const loadLastActieTab = async () => {
|
const loadLastActivePage = async () => {
|
||||||
try {
|
try {
|
||||||
const savedTab = await load('lastActiveTab')
|
const lastActivePage =
|
||||||
const tabIndex = savedTab ? parseInt(savedTab, 10) : 0
|
(await loadString('lastActivePage')) ?? 'Following'
|
||||||
setLastActiveTab(tabIndex)
|
setInitialPage(lastActivePage)
|
||||||
} catch {
|
} catch {
|
||||||
setLastActiveTab(0)
|
setInitialPage('Following')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Ignoring lastActiveTab for desktop
|
loadLastActivePage()
|
||||||
if (isDesktop) {
|
}, [])
|
||||||
setLastActiveTab(0)
|
|
||||||
} else {
|
|
||||||
loadLastActieTab()
|
|
||||||
}
|
|
||||||
}, [isDesktop])
|
|
||||||
|
|
||||||
if (preferences && lastActiveTab !== undefined) {
|
if (preferences && initialPage !== undefined) {
|
||||||
return (
|
return (
|
||||||
<HomeScreenReady
|
<HomeScreenReady
|
||||||
{...props}
|
{...props}
|
||||||
preferences={preferences}
|
preferences={preferences}
|
||||||
lastActiveTab={lastActiveTab}
|
initialPage={isDesktop ? 'Following' : initialPage}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
|
@ -62,15 +57,15 @@ export function HomeScreen(props: Props) {
|
||||||
|
|
||||||
function HomeScreenReady({
|
function HomeScreenReady({
|
||||||
preferences,
|
preferences,
|
||||||
lastActiveTab,
|
initialPage,
|
||||||
}: Props & {
|
}: Props & {
|
||||||
preferences: UsePreferencesQueryResponse
|
preferences: UsePreferencesQueryResponse
|
||||||
lastActiveTab: number
|
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(lastActiveTab)
|
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
|
||||||
|
@ -78,6 +73,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[] = []
|
||||||
|
@ -101,21 +103,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)
|
|
||||||
save('lastActiveTab', 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(() => {
|
||||||
|
@ -158,7 +166,7 @@ function HomeScreenReady({
|
||||||
<Pager
|
<Pager
|
||||||
key={pinnedFeedOrderKey}
|
key={pinnedFeedOrderKey}
|
||||||
testID="homeScreen"
|
testID="homeScreen"
|
||||||
initialPage={selectedPage > customFeeds.length ? 0 : selectedPage}
|
initialPage={selectedPageIndex}
|
||||||
onPageSelected={onPageSelected}
|
onPageSelected={onPageSelected}
|
||||||
onPageScrollStateChanged={onPageScrollStateChanged}
|
onPageScrollStateChanged={onPageScrollStateChanged}
|
||||||
renderTabBar={renderTabBar}
|
renderTabBar={renderTabBar}
|
||||||
|
@ -166,7 +174,7 @@ function HomeScreenReady({
|
||||||
<FeedPage
|
<FeedPage
|
||||||
key="1"
|
key="1"
|
||||||
testID="followingFeedPage"
|
testID="followingFeedPage"
|
||||||
isPageFocused={selectedPage === 0}
|
isPageFocused={selectedPageIndex === 0}
|
||||||
feed={homeFeedParams.mergeFeedEnabled ? 'home' : 'following'}
|
feed={homeFeedParams.mergeFeedEnabled ? 'home' : 'following'}
|
||||||
feedParams={homeFeedParams}
|
feedParams={homeFeedParams}
|
||||||
renderEmptyState={renderFollowingEmptyState}
|
renderEmptyState={renderFollowingEmptyState}
|
||||||
|
@ -177,7 +185,7 @@ function HomeScreenReady({
|
||||||
<FeedPage
|
<FeedPage
|
||||||
key={f}
|
key={f}
|
||||||
testID="customFeedPage"
|
testID="customFeedPage"
|
||||||
isPageFocused={selectedPage === 1 + index}
|
isPageFocused={selectedPageIndex === 1 + index}
|
||||||
feed={f}
|
feed={f}
|
||||||
renderEmptyState={renderCustomFeedEmptyState}
|
renderEmptyState={renderCustomFeedEmptyState}
|
||||||
/>
|
/>
|
||||||
|
|
Loading…
Reference in New Issue