Skip to content

Commit

Permalink
Fix scroll on profile lists/feeds (#2168)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Dec 11, 2023
1 parent b82c517 commit 8929ff5
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/lib/hooks/useAnimatedScrollHandler_FIXED.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
// - https://github.com/software-mansion/react-native-reanimated/issues/5364
//
// It's great when it works though.
export {useAnimatedScrollHandler} from 'react-native-reanimated'
export {useAnimatedScrollHandler as useAnimatedScrollHandler_FIXED} from 'react-native-reanimated'
81 changes: 41 additions & 40 deletions src/lib/hooks/useAnimatedScrollHandler_FIXED.web.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
import {useRef, useEffect} from 'react'
import {useAnimatedScrollHandler as useAnimatedScrollHandler_BUGGY} from 'react-native-reanimated'

export const useAnimatedScrollHandler: typeof useAnimatedScrollHandler_BUGGY = (
config,
deps,
) => {
const ref = useRef(config)
useEffect(() => {
ref.current = config
})
return useAnimatedScrollHandler_BUGGY(
{
onBeginDrag(e, ctx) {
if (typeof ref.current !== 'function' && ref.current.onBeginDrag) {
ref.current.onBeginDrag(e, ctx)
}
export const useAnimatedScrollHandler_FIXED: typeof useAnimatedScrollHandler_BUGGY =
(config, deps) => {
const ref = useRef(config)
useEffect(() => {
ref.current = config
})
return useAnimatedScrollHandler_BUGGY(
{
onBeginDrag(e, ctx) {
if (typeof ref.current !== 'function' && ref.current.onBeginDrag) {
ref.current.onBeginDrag(e, ctx)
}
},
onEndDrag(e, ctx) {
if (typeof ref.current !== 'function' && ref.current.onEndDrag) {
ref.current.onEndDrag(e, ctx)
}
},
onMomentumBegin(e, ctx) {
if (
typeof ref.current !== 'function' &&
ref.current.onMomentumBegin
) {
ref.current.onMomentumBegin(e, ctx)
}
},
onMomentumEnd(e, ctx) {
if (typeof ref.current !== 'function' && ref.current.onMomentumEnd) {
ref.current.onMomentumEnd(e, ctx)
}
},
onScroll(e, ctx) {
if (typeof ref.current === 'function') {
ref.current(e, ctx)
} else if (ref.current.onScroll) {
ref.current.onScroll(e, ctx)
}
},
},
onEndDrag(e, ctx) {
if (typeof ref.current !== 'function' && ref.current.onEndDrag) {
ref.current.onEndDrag(e, ctx)
}
},
onMomentumBegin(e, ctx) {
if (typeof ref.current !== 'function' && ref.current.onMomentumBegin) {
ref.current.onMomentumBegin(e, ctx)
}
},
onMomentumEnd(e, ctx) {
if (typeof ref.current !== 'function' && ref.current.onMomentumEnd) {
ref.current.onMomentumEnd(e, ctx)
}
},
onScroll(e, ctx) {
if (typeof ref.current === 'function') {
ref.current(e, ctx)
} else if (ref.current.onScroll) {
ref.current.onScroll(e, ctx)
}
},
},
deps,
)
}
deps,
)
}
4 changes: 2 additions & 2 deletions src/view/com/feeds/ProfileFeedgens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {OnScrollHandler} from '#/lib/hooks/useOnMainScroll'
import {logger} from '#/logger'
import {Trans} from '@lingui/macro'
import {cleanError} from '#/lib/strings/errors'
import {useAnimatedScrollHandler} from 'react-native-reanimated'
import {useAnimatedScrollHandler_FIXED} from '#/lib/hooks/useAnimatedScrollHandler_FIXED'
import {useTheme} from '#/lib/ThemeContext'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {hydrateFeedGenerator} from '#/state/queries/feed'
Expand Down Expand Up @@ -184,7 +184,7 @@ export const ProfileFeedgens = React.forwardRef<
[error, refetch, onPressRetryLoadMore, pal, preferences],
)

const scrollHandler = useAnimatedScrollHandler(onScroll || {})
const scrollHandler = useAnimatedScrollHandler_FIXED(onScroll || {})
return (
<View testID={testID} style={style}>
<FlatList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import Animated, {
interpolate,
runOnJS,
useAnimatedRef,
useAnimatedScrollHandler,
useAnimatedStyle,
useSharedValue,
} from 'react-native-reanimated'
import {useAnimatedScrollHandler_FIXED} from '#/lib/hooks/useAnimatedScrollHandler_FIXED'
import {Gesture, GestureDetector} from 'react-native-gesture-handler'

import useImageDimensions from '../../hooks/useImageDimensions'
Expand Down Expand Up @@ -61,7 +61,7 @@ const ImageItem = ({imageSrc, onTap, onZoom, onRequestClose}: Props) => {
}
})

const scrollHandler = useAnimatedScrollHandler({
const scrollHandler = useAnimatedScrollHandler_FIXED({
onScroll(e) {
const nextIsScaled = e.zoomScale > 1
translationY.value = nextIsScaled ? 0 : e.contentOffset.y
Expand Down
4 changes: 2 additions & 2 deletions src/view/com/lists/ProfileLists.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {OnScrollHandler} from '#/lib/hooks/useOnMainScroll'
import {logger} from '#/logger'
import {Trans} from '@lingui/macro'
import {cleanError} from '#/lib/strings/errors'
import {useAnimatedScrollHandler} from 'react-native-reanimated'
import {useAnimatedScrollHandler_FIXED} from '#/lib/hooks/useAnimatedScrollHandler_FIXED'
import {useTheme} from '#/lib/ThemeContext'
import {FeedLoadingPlaceholder} from '#/view/com/util/LoadingPlaceholder'
import {isNative} from '#/platform/detection'
Expand Down Expand Up @@ -187,7 +187,7 @@ export const ProfileLists = React.forwardRef<SectionRef, ProfileListsProps>(
[error, refetch, onPressRetryLoadMore, pal],
)

const scrollHandler = useAnimatedScrollHandler(onScroll || {})
const scrollHandler = useAnimatedScrollHandler_FIXED(onScroll || {})
return (
<View testID={testID} style={style}>
<FlatList
Expand Down
4 changes: 2 additions & 2 deletions src/view/com/notifications/Feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {ErrorMessage} from '../util/error/ErrorMessage'
import {LoadMoreRetryBtn} from '../util/LoadMoreRetryBtn'
import {EmptyState} from '../util/EmptyState'
import {OnScrollHandler} from 'lib/hooks/useOnMainScroll'
import {useAnimatedScrollHandler} from '#/lib/hooks/useAnimatedScrollHandler_FIXED'
import {useAnimatedScrollHandler_FIXED} from '#/lib/hooks/useAnimatedScrollHandler_FIXED'
import {s} from 'lib/styles'
import {usePalette} from 'lib/hooks/usePalette'
import {useNotificationFeedQuery} from '#/state/queries/notifications/feed'
Expand Down Expand Up @@ -135,7 +135,7 @@ export function Feed({
[isFetchingNextPage],
)

const scrollHandler = useAnimatedScrollHandler(onScroll || {})
const scrollHandler = useAnimatedScrollHandler_FIXED(onScroll || {})
return (
<View style={s.hContentRegion}>
{error && (
Expand Down
4 changes: 2 additions & 2 deletions src/view/com/posts/Feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {LoadMoreRetryBtn} from '../util/LoadMoreRetryBtn'
import {OnScrollHandler} from 'lib/hooks/useOnMainScroll'
import {useAnalytics} from 'lib/analytics/analytics'
import {usePalette} from 'lib/hooks/usePalette'
import {useAnimatedScrollHandler} from '#/lib/hooks/useAnimatedScrollHandler_FIXED'
import {useAnimatedScrollHandler_FIXED} from '#/lib/hooks/useAnimatedScrollHandler_FIXED'
import {useTheme} from 'lib/ThemeContext'
import {logger} from '#/logger'
import {
Expand Down Expand Up @@ -271,7 +271,7 @@ let Feed = ({
)
}, [isFetchingNextPage, shouldRenderEndOfFeed, renderEndOfFeed, headerOffset])

const scrollHandler = useAnimatedScrollHandler(onScroll || {})
const scrollHandler = useAnimatedScrollHandler_FIXED(onScroll || {})
return (
<View testID={testID} style={style}>
<FlatList
Expand Down
4 changes: 2 additions & 2 deletions src/view/screens/ProfileFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {logger} from '#/logger'
import {Trans, msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useModalControls} from '#/state/modals'
import {useAnimatedScrollHandler} from '#/lib/hooks/useAnimatedScrollHandler_FIXED'
import {useAnimatedScrollHandler_FIXED} from '#/lib/hooks/useAnimatedScrollHandler_FIXED'
import {
useFeedSourceInfoQuery,
FeedSourceFeedInfo,
Expand Down Expand Up @@ -572,7 +572,7 @@ function AboutSection({
}) {
const pal = usePalette('default')
const {_} = useLingui()
const scrollHandler = useAnimatedScrollHandler(onScroll)
const scrollHandler = useAnimatedScrollHandler_FIXED(onScroll)
const [likeUri, setLikeUri] = React.useState(feedInfo.likeUri)
const {hasSession} = useSession()
const {track} = useAnalytics()
Expand Down

0 comments on commit 8929ff5

Please sign in to comment.