[Perf] Drawer gesture perf fix + related cleanup (#8953)
* split drawer layout into own component * don't put props in dep array * memoize pager view
This commit is contained in:
@@ -1,15 +1,15 @@
|
|||||||
import React from 'react'
|
import {createContext, useContext, useState} from 'react'
|
||||||
|
|
||||||
type StateContext = boolean
|
type StateContext = boolean
|
||||||
type SetContext = (v: boolean) => void
|
type SetContext = (v: boolean) => void
|
||||||
|
|
||||||
const stateContext = React.createContext<StateContext>(false)
|
const stateContext = createContext<StateContext>(false)
|
||||||
stateContext.displayName = 'DrawerOpenStateContext'
|
stateContext.displayName = 'DrawerOpenStateContext'
|
||||||
const setContext = React.createContext<SetContext>((_: boolean) => {})
|
const setContext = createContext<SetContext>((_: boolean) => {})
|
||||||
setContext.displayName = 'DrawerOpenSetContext'
|
setContext.displayName = 'DrawerOpenSetContext'
|
||||||
|
|
||||||
export function Provider({children}: React.PropsWithChildren<{}>) {
|
export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
const [state, setState] = React.useState(false)
|
const [state, setState] = useState(false)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<stateContext.Provider value={state}>
|
<stateContext.Provider value={state}>
|
||||||
@@ -19,9 +19,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function useIsDrawerOpen() {
|
export function useIsDrawerOpen() {
|
||||||
return React.useContext(stateContext)
|
return useContext(stateContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useSetDrawerOpen() {
|
export function useSetDrawerOpen() {
|
||||||
return React.useContext(setContext)
|
return useContext(setContext)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {useNavigation} from '@react-navigation/native'
|
import {useNavigation} from '@react-navigation/native'
|
||||||
|
|
||||||
import {NavigationProp} from '#/lib/routes/types'
|
import {type NavigationProp} from '#/lib/routes/types'
|
||||||
import {FeedSourceInfo} from '#/state/queries/feed'
|
import {type FeedSourceInfo} from '#/state/queries/feed'
|
||||||
import {useSession} from '#/state/session'
|
import {useSession} from '#/state/session'
|
||||||
import {RenderTabBarFnProps} from '#/view/com/pager/Pager'
|
import {type RenderTabBarFnProps} from '#/view/com/pager/Pager'
|
||||||
import {TabBar} from '../pager/TabBar'
|
import {TabBar} from '../pager/TabBar'
|
||||||
import {HomeHeaderLayout} from './HomeHeaderLayout'
|
import {HomeHeaderLayout} from './HomeHeaderLayout'
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ export function HomeHeader(
|
|||||||
feeds: FeedSourceInfo[]
|
feeds: FeedSourceInfo[]
|
||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const {feeds} = props
|
const {feeds, onSelect: onSelectProp} = props
|
||||||
const {hasSession} = useSession()
|
const {hasSession} = useSession()
|
||||||
const navigation = useNavigation<NavigationProp>()
|
const navigation = useNavigation<NavigationProp>()
|
||||||
|
|
||||||
@@ -43,11 +43,11 @@ export function HomeHeader(
|
|||||||
(index: number) => {
|
(index: number) => {
|
||||||
if (!hasPinnedCustom && index === items.length - 1) {
|
if (!hasPinnedCustom && index === items.length - 1) {
|
||||||
onPressFeedsLink()
|
onPressFeedsLink()
|
||||||
} else if (props.onSelect) {
|
} else if (onSelectProp) {
|
||||||
props.onSelect(index)
|
onSelectProp(index)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[items.length, onPressFeedsLink, props, hasPinnedCustom],
|
[items.length, onPressFeedsLink, onSelectProp, hasPinnedCustom],
|
||||||
)
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import {
|
import {
|
||||||
|
memo,
|
||||||
useCallback,
|
useCallback,
|
||||||
useContext,
|
useContext,
|
||||||
useImperativeHandle,
|
useImperativeHandle,
|
||||||
|
useMemo,
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
@@ -56,6 +58,7 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const AnimatedPagerView = Animated.createAnimatedComponent(PagerView)
|
const AnimatedPagerView = Animated.createAnimatedComponent(PagerView)
|
||||||
|
const MemoizedAnimatedPagerView = memo(AnimatedPagerView)
|
||||||
|
|
||||||
export function Pager({
|
export function Pager({
|
||||||
ref,
|
ref,
|
||||||
@@ -139,10 +142,6 @@ export function Pager({
|
|||||||
[parentOnPageScrollStateChanged],
|
[parentOnPageScrollStateChanged],
|
||||||
)
|
)
|
||||||
|
|
||||||
const drawerGesture = useContext(DrawerGestureContext) ?? Gesture.Native() // noop for web
|
|
||||||
const nativeGesture =
|
|
||||||
Gesture.Native().requireExternalGestureToFail(drawerGesture)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View testID={testID} style={[a.flex_1, native(a.overflow_hidden)]}>
|
<View testID={testID} style={[a.flex_1, native(a.overflow_hidden)]}>
|
||||||
{renderTabBar({
|
{renderTabBar({
|
||||||
@@ -151,19 +150,33 @@ export function Pager({
|
|||||||
dragProgress,
|
dragProgress,
|
||||||
dragState,
|
dragState,
|
||||||
})}
|
})}
|
||||||
<GestureDetector gesture={nativeGesture}>
|
<DrawerGestureRequireFail>
|
||||||
<AnimatedPagerView
|
<MemoizedAnimatedPagerView
|
||||||
ref={pagerView}
|
ref={pagerView}
|
||||||
style={[a.flex_1]}
|
style={a.flex_1}
|
||||||
initialPage={initialPage}
|
initialPage={initialPage}
|
||||||
onPageScroll={handlePageScroll}>
|
onPageScroll={handlePageScroll}>
|
||||||
{children}
|
{children}
|
||||||
</AnimatedPagerView>
|
</MemoizedAnimatedPagerView>
|
||||||
</GestureDetector>
|
</DrawerGestureRequireFail>
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function DrawerGestureRequireFail({children}: {children: React.ReactNode}) {
|
||||||
|
const drawerGesture = useContext(DrawerGestureContext)
|
||||||
|
|
||||||
|
const nativeGesture = useMemo(() => {
|
||||||
|
const gesture = Gesture.Native()
|
||||||
|
if (drawerGesture) {
|
||||||
|
gesture.requireExternalGestureToFail(drawerGesture)
|
||||||
|
}
|
||||||
|
return gesture
|
||||||
|
}, [drawerGesture])
|
||||||
|
|
||||||
|
return <GestureDetector gesture={nativeGesture}>{children}</GestureDetector>
|
||||||
|
}
|
||||||
|
|
||||||
function usePagerHandlers(
|
function usePagerHandlers(
|
||||||
handlers: {
|
handlers: {
|
||||||
onPageScroll: (e: PagerViewOnPageScrollEventData) => void
|
onPageScroll: (e: PagerViewOnPageScrollEventData) => void
|
||||||
|
|||||||
+56
-47
@@ -45,25 +45,10 @@ import {Composer} from './Composer'
|
|||||||
import {DrawerContent} from './Drawer'
|
import {DrawerContent} from './Drawer'
|
||||||
|
|
||||||
function ShellInner() {
|
function ShellInner() {
|
||||||
const t = useTheme()
|
|
||||||
const isDrawerOpen = useIsDrawerOpen()
|
|
||||||
const isDrawerSwipeDisabled = useIsDrawerSwipeDisabled()
|
|
||||||
const setIsDrawerOpen = useSetDrawerOpen()
|
|
||||||
const winDim = useWindowDimensions()
|
const winDim = useWindowDimensions()
|
||||||
const insets = useSafeAreaInsets()
|
const insets = useSafeAreaInsets()
|
||||||
const {state: policyUpdateState} = usePolicyUpdateContext()
|
const {state: policyUpdateState} = usePolicyUpdateContext()
|
||||||
|
|
||||||
const renderDrawerContent = useCallback(() => <DrawerContent />, [])
|
|
||||||
const onOpenDrawer = useCallback(
|
|
||||||
() => setIsDrawerOpen(true),
|
|
||||||
[setIsDrawerOpen],
|
|
||||||
)
|
|
||||||
const onCloseDrawer = useCallback(
|
|
||||||
() => setIsDrawerOpen(false),
|
|
||||||
[setIsDrawerOpen],
|
|
||||||
)
|
|
||||||
const canGoBack = useNavigationState(state => !isStateAtTabRoot(state))
|
|
||||||
const {hasSession} = useSession()
|
|
||||||
const closeAnyActiveElement = useCloseAnyActiveElement()
|
const closeAnyActiveElement = useCloseAnyActiveElement()
|
||||||
|
|
||||||
useNotificationsRegistration()
|
useNotificationsRegistration()
|
||||||
@@ -102,20 +87,69 @@ function ShellInner() {
|
|||||||
}
|
}
|
||||||
}, [dedupe, navigation])
|
}, [dedupe, navigation])
|
||||||
|
|
||||||
const swipeEnabled = !canGoBack && hasSession && !isDrawerSwipeDisabled
|
|
||||||
const [trendingScrollGesture] = useState(() => Gesture.Native())
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<View style={[a.h_full]}>
|
<View style={[a.h_full]}>
|
||||||
<ErrorBoundary
|
<ErrorBoundary
|
||||||
style={{paddingTop: insets.top, paddingBottom: insets.bottom}}>
|
style={{paddingTop: insets.top, paddingBottom: insets.bottom}}>
|
||||||
|
<DrawerLayout>
|
||||||
|
<TabsNavigator />
|
||||||
|
</DrawerLayout>
|
||||||
|
</ErrorBoundary>
|
||||||
|
</View>
|
||||||
|
|
||||||
|
<Composer winHeight={winDim.height} />
|
||||||
|
<ModalsContainer />
|
||||||
|
<MutedWordsDialog />
|
||||||
|
<SigninDialog />
|
||||||
|
<EmailDialog />
|
||||||
|
<AgeAssuranceRedirectDialog />
|
||||||
|
<InAppBrowserConsentDialog />
|
||||||
|
<LinkWarningDialog />
|
||||||
|
<Lightbox />
|
||||||
|
|
||||||
|
{/* Until policy update has been completed by the user, don't render anything that is portaled */}
|
||||||
|
{policyUpdateState.completed && (
|
||||||
|
<>
|
||||||
|
<PortalOutlet />
|
||||||
|
<BottomSheetOutlet />
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<PolicyUpdateOverlayPortalOutlet />
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function DrawerLayout({children}: {children: React.ReactNode}) {
|
||||||
|
const t = useTheme()
|
||||||
|
const isDrawerOpen = useIsDrawerOpen()
|
||||||
|
const setIsDrawerOpen = useSetDrawerOpen()
|
||||||
|
const isDrawerSwipeDisabled = useIsDrawerSwipeDisabled()
|
||||||
|
const winDim = useWindowDimensions()
|
||||||
|
|
||||||
|
const canGoBack = useNavigationState(state => !isStateAtTabRoot(state))
|
||||||
|
const {hasSession} = useSession()
|
||||||
|
|
||||||
|
const swipeEnabled = !canGoBack && hasSession && !isDrawerSwipeDisabled
|
||||||
|
const [trendingScrollGesture] = useState(() => Gesture.Native())
|
||||||
|
|
||||||
|
const renderDrawerContent = useCallback(() => <DrawerContent />, [])
|
||||||
|
const onOpenDrawer = useCallback(
|
||||||
|
() => setIsDrawerOpen(true),
|
||||||
|
[setIsDrawerOpen],
|
||||||
|
)
|
||||||
|
const onCloseDrawer = useCallback(
|
||||||
|
() => setIsDrawerOpen(false),
|
||||||
|
[setIsDrawerOpen],
|
||||||
|
)
|
||||||
|
|
||||||
|
return (
|
||||||
<Drawer
|
<Drawer
|
||||||
renderDrawerContent={renderDrawerContent}
|
renderDrawerContent={renderDrawerContent}
|
||||||
drawerStyle={{width: Math.min(400, winDim.width * 0.8)}}
|
drawerStyle={{width: Math.min(400, winDim.width * 0.8)}}
|
||||||
configureGestureHandler={handler => {
|
configureGestureHandler={handler => {
|
||||||
handler = handler.requireExternalGestureToFail(
|
handler = handler.requireExternalGestureToFail(trendingScrollGesture)
|
||||||
trendingScrollGesture,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (swipeEnabled) {
|
if (swipeEnabled) {
|
||||||
if (isDrawerOpen) {
|
if (isDrawerOpen) {
|
||||||
@@ -148,37 +182,12 @@ function ShellInner() {
|
|||||||
overlayStyle={{
|
overlayStyle={{
|
||||||
backgroundColor: select(t.name, {
|
backgroundColor: select(t.name, {
|
||||||
light: 'rgba(0, 57, 117, 0.1)',
|
light: 'rgba(0, 57, 117, 0.1)',
|
||||||
dark: isAndroid
|
dark: isAndroid ? 'rgba(16, 133, 254, 0.1)' : 'rgba(1, 82, 168, 0.1)',
|
||||||
? 'rgba(16, 133, 254, 0.1)'
|
|
||||||
: 'rgba(1, 82, 168, 0.1)',
|
|
||||||
dim: 'rgba(10, 13, 16, 0.8)',
|
dim: 'rgba(10, 13, 16, 0.8)',
|
||||||
}),
|
}),
|
||||||
}}>
|
}}>
|
||||||
<TabsNavigator />
|
{children}
|
||||||
</Drawer>
|
</Drawer>
|
||||||
</ErrorBoundary>
|
|
||||||
</View>
|
|
||||||
|
|
||||||
<Composer winHeight={winDim.height} />
|
|
||||||
<ModalsContainer />
|
|
||||||
<MutedWordsDialog />
|
|
||||||
<SigninDialog />
|
|
||||||
<EmailDialog />
|
|
||||||
<AgeAssuranceRedirectDialog />
|
|
||||||
<InAppBrowserConsentDialog />
|
|
||||||
<LinkWarningDialog />
|
|
||||||
<Lightbox />
|
|
||||||
|
|
||||||
{/* Until policy update has been completed by the user, don't render anything that is portaled */}
|
|
||||||
{policyUpdateState.completed && (
|
|
||||||
<>
|
|
||||||
<PortalOutlet />
|
|
||||||
<BottomSheetOutlet />
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<PolicyUpdateOverlayPortalOutlet />
|
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user