rm forward ref from profiles

This commit is contained in:
Samuel Newman
2025-08-29 11:16:47 +01:00
parent 0114cfcfe1
commit 9cf341fe46
6 changed files with 228 additions and 222 deletions
@@ -1,9 +1,4 @@
import React, { import {useCallback, useEffect, useImperativeHandle, useState} from 'react'
useCallback,
useEffect,
useImperativeHandle,
useState,
} from 'react'
import { import {
findNodeHandle, findNodeHandle,
type ListRenderItemInfo, type ListRenderItemInfo,
@@ -42,6 +37,7 @@ interface SectionRef {
} }
interface ProfileFeedgensProps { interface ProfileFeedgensProps {
ref?: React.Ref<SectionRef>
scrollElRef: ListRef scrollElRef: ListRef
did: string did: string
headerOffset: number headerOffset: number
@@ -56,22 +52,17 @@ function keyExtractor(item: AppBskyGraphDefs.StarterPackView) {
return item.uri return item.uri
} }
export const ProfileStarterPacks = React.forwardRef< export function ProfileStarterPacks({
SectionRef,
ProfileFeedgensProps
>(function ProfileFeedgensImpl(
{
scrollElRef,
did,
headerOffset,
enabled,
style,
testID,
setScrollViewTag,
isMe,
},
ref, ref,
) { scrollElRef,
did,
headerOffset,
enabled,
style,
testID,
setScrollViewTag,
isMe,
}: ProfileFeedgensProps) {
const t = useTheme() const t = useTheme()
const bottomBarOffset = useBottomBarOffset(100) const bottomBarOffset = useBottomBarOffset(100)
const [isPTRing, setIsPTRing] = useState(false) const [isPTRing, setIsPTRing] = useState(false)
@@ -101,7 +92,7 @@ export const ProfileStarterPacks = React.forwardRef<
setIsPTRing(false) setIsPTRing(false)
}, [refetch, setIsPTRing]) }, [refetch, setIsPTRing])
const onEndReached = React.useCallback(async () => { const onEndReached = useCallback(async () => {
if (isFetchingNextPage || !hasNextPage || isError) return if (isFetchingNextPage || !hasNextPage || isError) return
try { try {
await fetchNextPage() await fetchNextPage()
@@ -158,7 +149,7 @@ export const ProfileStarterPacks = React.forwardRef<
/> />
</View> </View>
) )
}) }
function CreateAnother() { function CreateAnother() {
const {_} = useLingui() const {_} = useLingui()
+1 -1
View File
@@ -4,7 +4,7 @@ import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/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 {atoms as a, useTheme} from '#/alf' import {atoms as a, useTheme} from '#/alf'
import {Button, ButtonText} from '#/components/Button' import {Button, ButtonText} from '#/components/Button'
import {CircleInfo_Stroke2_Corner0_Rounded as CircleInfo} from '#/components/icons/CircleInfo' import {CircleInfo_Stroke2_Corner0_Rounded as CircleInfo} from '#/components/icons/CircleInfo'
+17 -21
View File
@@ -1,4 +1,4 @@
import React from 'react' import {useCallback, useEffect, useImperativeHandle, useState} from 'react'
import {findNodeHandle, View} from 'react-native' import {findNodeHandle, View} from 'react-native'
import {msg, Trans} from '@lingui/macro' import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
@@ -18,6 +18,7 @@ import {Text} from '#/components/Typography'
import {type SectionRef} from './types' import {type SectionRef} from './types'
interface FeedSectionProps { interface FeedSectionProps {
ref?: React.Ref<SectionRef>
feed: FeedDescriptor feed: FeedDescriptor
headerHeight: number headerHeight: number
isFocused: boolean isFocused: boolean
@@ -25,31 +26,26 @@ interface FeedSectionProps {
ignoreFilterFor?: string ignoreFilterFor?: string
setScrollViewTag: (tag: number | null) => void setScrollViewTag: (tag: number | null) => void
} }
export const ProfileFeedSection = React.forwardRef< export function ProfileFeedSection({
SectionRef,
FeedSectionProps
>(function FeedSectionImpl(
{
feed,
headerHeight,
isFocused,
scrollElRef,
ignoreFilterFor,
setScrollViewTag,
},
ref, ref,
) { feed,
headerHeight,
isFocused,
scrollElRef,
ignoreFilterFor,
setScrollViewTag,
}: FeedSectionProps) {
const {_} = useLingui() const {_} = useLingui()
const queryClient = useQueryClient() const queryClient = useQueryClient()
const [hasNew, setHasNew] = React.useState(false) const [hasNew, setHasNew] = useState(false)
const [isScrolledDown, setIsScrolledDown] = React.useState(false) const [isScrolledDown, setIsScrolledDown] = useState(false)
const shouldUseAdjustedNumToRender = feed.endsWith('posts_and_author_threads') const shouldUseAdjustedNumToRender = feed.endsWith('posts_and_author_threads')
const isVideoFeed = isNative && feed.endsWith('posts_with_video') const isVideoFeed = isNative && feed.endsWith('posts_with_video')
const adjustedInitialNumToRender = useInitialNumToRender({ const adjustedInitialNumToRender = useInitialNumToRender({
screenHeightOffset: headerHeight, screenHeightOffset: headerHeight,
}) })
const onScrollToTop = React.useCallback(() => { const onScrollToTop = useCallback(() => {
scrollElRef.current?.scrollToOffset({ scrollElRef.current?.scrollToOffset({
animated: isNative, animated: isNative,
offset: -headerHeight, offset: -headerHeight,
@@ -58,15 +54,15 @@ export const ProfileFeedSection = React.forwardRef<
setHasNew(false) setHasNew(false)
}, [scrollElRef, headerHeight, queryClient, feed, setHasNew]) }, [scrollElRef, headerHeight, queryClient, feed, setHasNew])
React.useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
scrollToTop: onScrollToTop, scrollToTop: onScrollToTop,
})) }))
const renderPostsEmpty = React.useCallback(() => { const renderPostsEmpty = useCallback(() => {
return <EmptyState icon="growth" message={_(msg`No posts yet.`)} /> return <EmptyState icon="growth" message={_(msg`No posts yet.`)} />
}, [_]) }, [_])
React.useEffect(() => { useEffect(() => {
if (isIOS && isFocused && scrollElRef.current) { if (isIOS && isFocused && scrollElRef.current) {
const nativeTag = findNodeHandle(scrollElRef.current) const nativeTag = findNodeHandle(scrollElRef.current)
setScrollViewTag(nativeTag) setScrollViewTag(nativeTag)
@@ -101,7 +97,7 @@ export const ProfileFeedSection = React.forwardRef<
)} )}
</View> </View>
) )
}) }
function ProfileEndOfFeed() { function ProfileEndOfFeed() {
const t = useTheme() const t = useTheme()
+1
View File
@@ -33,6 +33,7 @@ interface LabelsSectionProps {
isFocused: boolean isFocused: boolean
setScrollViewTag: (tag: number | null) => void setScrollViewTag: (tag: number | null) => void
} }
export function ProfileLabelsSection({ export function ProfileLabelsSection({
ref, ref,
isLabelerLoading, isLabelerLoading,
+29 -19
View File
@@ -1,4 +1,10 @@
import React from 'react' import {
useCallback,
useEffect,
useImperativeHandle,
useMemo,
useState,
} from 'react'
import { import {
findNodeHandle, findNodeHandle,
type ListRenderItemInfo, type ListRenderItemInfo,
@@ -34,6 +40,7 @@ interface SectionRef {
} }
interface ProfileFeedgensProps { interface ProfileFeedgensProps {
ref?: React.Ref<SectionRef>
did: string did: string
scrollElRef: ListRef scrollElRef: ListRef
headerOffset: number headerOffset: number
@@ -43,17 +50,20 @@ interface ProfileFeedgensProps {
setScrollViewTag: (tag: number | null) => void setScrollViewTag: (tag: number | null) => void
} }
export const ProfileFeedgens = React.forwardRef< export function ProfileFeedgens({
SectionRef,
ProfileFeedgensProps
>(function ProfileFeedgensImpl(
{did, scrollElRef, headerOffset, enabled, style, testID, setScrollViewTag},
ref, ref,
) { did,
scrollElRef,
headerOffset,
enabled,
style,
testID,
setScrollViewTag,
}: ProfileFeedgensProps) {
const {_} = useLingui() const {_} = useLingui()
const t = useTheme() const t = useTheme()
const [isPTRing, setIsPTRing] = React.useState(false) const [isPTRing, setIsPTRing] = useState(false)
const opts = React.useMemo(() => ({enabled}), [enabled]) const opts = useMemo(() => ({enabled}), [enabled])
const { const {
data, data,
isPending, isPending,
@@ -67,7 +77,7 @@ export const ProfileFeedgens = React.forwardRef<
const isEmpty = !isPending && !data?.pages[0]?.feeds.length const isEmpty = !isPending && !data?.pages[0]?.feeds.length
const {data: preferences} = usePreferencesQuery() const {data: preferences} = usePreferencesQuery()
const items = React.useMemo(() => { const items = useMemo(() => {
let items: any[] = [] let items: any[] = []
if (isError && isEmpty) { if (isError && isEmpty) {
items = items.concat([ERROR_ITEM]) items = items.concat([ERROR_ITEM])
@@ -91,7 +101,7 @@ export const ProfileFeedgens = React.forwardRef<
const queryClient = useQueryClient() const queryClient = useQueryClient()
const onScrollToTop = React.useCallback(() => { const onScrollToTop = useCallback(() => {
scrollElRef.current?.scrollToOffset({ scrollElRef.current?.scrollToOffset({
animated: isNative, animated: isNative,
offset: -headerOffset, offset: -headerOffset,
@@ -99,11 +109,11 @@ export const ProfileFeedgens = React.forwardRef<
queryClient.invalidateQueries({queryKey: RQKEY(did)}) queryClient.invalidateQueries({queryKey: RQKEY(did)})
}, [scrollElRef, queryClient, headerOffset, did]) }, [scrollElRef, queryClient, headerOffset, did])
React.useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
scrollToTop: onScrollToTop, scrollToTop: onScrollToTop,
})) }))
const onRefresh = React.useCallback(async () => { const onRefresh = useCallback(async () => {
setIsPTRing(true) setIsPTRing(true)
try { try {
await refetch() await refetch()
@@ -113,7 +123,7 @@ export const ProfileFeedgens = React.forwardRef<
setIsPTRing(false) setIsPTRing(false)
}, [refetch, setIsPTRing]) }, [refetch, setIsPTRing])
const onEndReached = React.useCallback(async () => { const onEndReached = useCallback(async () => {
if (isFetchingNextPage || !hasNextPage || isError) return if (isFetchingNextPage || !hasNextPage || isError) return
try { try {
@@ -123,14 +133,14 @@ export const ProfileFeedgens = React.forwardRef<
} }
}, [isFetchingNextPage, hasNextPage, isError, fetchNextPage]) }, [isFetchingNextPage, hasNextPage, isError, fetchNextPage])
const onPressRetryLoadMore = React.useCallback(() => { const onPressRetryLoadMore = useCallback(() => {
fetchNextPage() fetchNextPage()
}, [fetchNextPage]) }, [fetchNextPage])
// rendering // rendering
// = // =
const renderItem = React.useCallback( const renderItem = useCallback(
({item, index}: ListRenderItemInfo<any>) => { ({item, index}: ListRenderItemInfo<any>) => {
if (item === EMPTY) { if (item === EMPTY) {
return ( return (
@@ -174,14 +184,14 @@ export const ProfileFeedgens = React.forwardRef<
[_, t, error, refetch, onPressRetryLoadMore, preferences], [_, t, error, refetch, onPressRetryLoadMore, preferences],
) )
React.useEffect(() => { useEffect(() => {
if (isIOS && enabled && scrollElRef.current) { if (isIOS && enabled && scrollElRef.current) {
const nativeTag = findNodeHandle(scrollElRef.current) const nativeTag = findNodeHandle(scrollElRef.current)
setScrollViewTag(nativeTag) setScrollViewTag(nativeTag)
} }
}, [enabled, scrollElRef, setScrollViewTag]) }, [enabled, scrollElRef, setScrollViewTag])
const ProfileFeedgensFooter = React.useCallback(() => { const ProfileFeedgensFooter = useCallback(() => {
if (isEmpty) return null if (isEmpty) return null
return ( return (
<ListFooter <ListFooter
@@ -220,7 +230,7 @@ export const ProfileFeedgens = React.forwardRef<
/> />
</View> </View>
) )
}) }
function keyExtractor(item: any) { function keyExtractor(item: any) {
return item._reactKey || item.uri return item._reactKey || item.uri
+166 -158
View File
@@ -1,4 +1,10 @@
import React from 'react' import {
useCallback,
useEffect,
useImperativeHandle,
useMemo,
useState,
} from 'react'
import { import {
findNodeHandle, findNodeHandle,
type ListRenderItemInfo, type ListRenderItemInfo,
@@ -33,6 +39,7 @@ interface SectionRef {
} }
interface ProfileListsProps { interface ProfileListsProps {
ref?: React.Ref<SectionRef>
did: string did: string
scrollElRef: ListRef scrollElRef: ListRef
headerOffset: number headerOffset: number
@@ -42,182 +49,183 @@ interface ProfileListsProps {
setScrollViewTag: (tag: number | null) => void setScrollViewTag: (tag: number | null) => void
} }
export const ProfileLists = React.forwardRef<SectionRef, ProfileListsProps>( export function ProfileLists({
function ProfileListsImpl( ref,
{did, scrollElRef, headerOffset, enabled, style, testID, setScrollViewTag}, did,
ref, scrollElRef,
) { headerOffset,
const t = useTheme() enabled,
const {_} = useLingui() style,
const [isPTRing, setIsPTRing] = React.useState(false) testID,
const opts = React.useMemo(() => ({enabled}), [enabled]) setScrollViewTag,
const { }: ProfileListsProps) {
data, const t = useTheme()
isPending, const {_} = useLingui()
hasNextPage, const [isPTRing, setIsPTRing] = useState(false)
fetchNextPage, const opts = useMemo(() => ({enabled}), [enabled])
isFetchingNextPage, const {
isError, data,
error, isPending,
refetch, hasNextPage,
} = useProfileListsQuery(did, opts) fetchNextPage,
const isEmpty = !isPending && !data?.pages[0]?.lists.length isFetchingNextPage,
isError,
error,
refetch,
} = useProfileListsQuery(did, opts)
const isEmpty = !isPending && !data?.pages[0]?.lists.length
const items = React.useMemo(() => { const items = useMemo(() => {
let items: any[] = [] let items: any[] = []
if (isError && isEmpty) { if (isError && isEmpty) {
items = items.concat([ERROR_ITEM]) items = items.concat([ERROR_ITEM])
}
if (isPending) {
items = items.concat([LOADING])
} else if (isEmpty) {
items = items.concat([EMPTY])
} else if (data?.pages) {
for (const page of data?.pages) {
items = items.concat(page.lists)
} }
if (isPending) { }
items = items.concat([LOADING]) if (isError && !isEmpty) {
} else if (isEmpty) { items = items.concat([LOAD_MORE_ERROR_ITEM])
items = items.concat([EMPTY]) }
} else if (data?.pages) { return items
for (const page of data?.pages) { }, [isError, isEmpty, isPending, data])
items = items.concat(page.lists)
}
}
if (isError && !isEmpty) {
items = items.concat([LOAD_MORE_ERROR_ITEM])
}
return items
}, [isError, isEmpty, isPending, data])
// events // events
// = // =
const queryClient = useQueryClient() const queryClient = useQueryClient()
const onScrollToTop = React.useCallback(() => { const onScrollToTop = useCallback(() => {
scrollElRef.current?.scrollToOffset({ scrollElRef.current?.scrollToOffset({
animated: isNative, animated: isNative,
offset: -headerOffset, offset: -headerOffset,
}) })
queryClient.invalidateQueries({queryKey: RQKEY(did)}) queryClient.invalidateQueries({queryKey: RQKEY(did)})
}, [scrollElRef, queryClient, headerOffset, did]) }, [scrollElRef, queryClient, headerOffset, did])
React.useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
scrollToTop: onScrollToTop, scrollToTop: onScrollToTop,
})) }))
const onRefresh = React.useCallback(async () => { const onRefresh = useCallback(async () => {
setIsPTRing(true) setIsPTRing(true)
try { try {
await refetch() await refetch()
} catch (err) { } catch (err) {
logger.error('Failed to refresh lists', {message: err}) logger.error('Failed to refresh lists', {message: err})
} }
setIsPTRing(false) setIsPTRing(false)
}, [refetch, setIsPTRing]) }, [refetch, setIsPTRing])
const onEndReached = React.useCallback(async () => { const onEndReached = useCallback(async () => {
if (isFetchingNextPage || !hasNextPage || isError) return if (isFetchingNextPage || !hasNextPage || isError) return
try { try {
await fetchNextPage() await fetchNextPage()
} catch (err) { } catch (err) {
logger.error('Failed to load more lists', {message: err}) logger.error('Failed to load more lists', {message: err})
} }
}, [isFetchingNextPage, hasNextPage, isError, fetchNextPage]) }, [isFetchingNextPage, hasNextPage, isError, fetchNextPage])
const onPressRetryLoadMore = React.useCallback(() => { const onPressRetryLoadMore = useCallback(() => {
fetchNextPage() fetchNextPage()
}, [fetchNextPage]) }, [fetchNextPage])
// rendering // rendering
// = // =
const renderItemInner = React.useCallback( const renderItemInner = useCallback(
({item, index}: ListRenderItemInfo<any>) => { ({item, index}: ListRenderItemInfo<any>) => {
if (item === EMPTY) { if (item === EMPTY) {
return (
<EmptyState
icon="list-ul"
message={_(msg`You have no lists.`)}
testID="listsEmpty"
/>
)
} else if (item === ERROR_ITEM) {
return (
<ErrorMessage
message={cleanError(error)}
onPressTryAgain={refetch}
/>
)
} else if (item === LOAD_MORE_ERROR_ITEM) {
return (
<LoadMoreRetryBtn
label={_(
msg`There was an issue fetching your lists. Tap here to try again.`,
)}
onPress={onPressRetryLoadMore}
/>
)
} else if (item === LOADING) {
return <FeedLoadingPlaceholder />
}
return ( return (
<View <EmptyState
style={[ icon="list-ul"
(index !== 0 || isWeb) && a.border_t, message={_(msg`You have no lists.`)}
t.atoms.border_contrast_low, testID="listsEmpty"
a.px_lg, />
a.py_lg,
]}>
<ListCard.Default view={item} />
</View>
) )
}, } else if (item === ERROR_ITEM) {
[error, refetch, onPressRetryLoadMore, _, t.atoms.border_contrast_low], return (
) <ErrorMessage message={cleanError(error)} onPressTryAgain={refetch} />
)
React.useEffect(() => { } else if (item === LOAD_MORE_ERROR_ITEM) {
if (isIOS && enabled && scrollElRef.current) { return (
const nativeTag = findNodeHandle(scrollElRef.current) <LoadMoreRetryBtn
setScrollViewTag(nativeTag) label={_(
msg`There was an issue fetching your lists. Tap here to try again.`,
)}
onPress={onPressRetryLoadMore}
/>
)
} else if (item === LOADING) {
return <FeedLoadingPlaceholder />
} }
}, [enabled, scrollElRef, setScrollViewTag])
const ProfileListsFooter = React.useCallback(() => {
if (isEmpty) return null
return ( return (
<ListFooter <View
hasNextPage={hasNextPage} style={[
isFetchingNextPage={isFetchingNextPage} (index !== 0 || isWeb) && a.border_t,
onRetry={fetchNextPage} t.atoms.border_contrast_low,
error={cleanError(error)} a.px_lg,
height={180 + headerOffset} a.py_lg,
/> ]}>
<ListCard.Default view={item} />
</View>
) )
}, [ },
hasNextPage, [error, refetch, onPressRetryLoadMore, _, t.atoms.border_contrast_low],
error, )
isFetchingNextPage,
headerOffset,
fetchNextPage,
isEmpty,
])
useEffect(() => {
if (isIOS && enabled && scrollElRef.current) {
const nativeTag = findNodeHandle(scrollElRef.current)
setScrollViewTag(nativeTag)
}
}, [enabled, scrollElRef, setScrollViewTag])
const ProfileListsFooter = useCallback(() => {
if (isEmpty) return null
return ( return (
<View testID={testID} style={style}> <ListFooter
<List hasNextPage={hasNextPage}
testID={testID ? `${testID}-flatlist` : undefined} isFetchingNextPage={isFetchingNextPage}
ref={scrollElRef} onRetry={fetchNextPage}
data={items} error={cleanError(error)}
keyExtractor={keyExtractor} height={180 + headerOffset}
renderItem={renderItemInner} />
ListFooterComponent={ProfileListsFooter}
refreshing={isPTRing}
onRefresh={onRefresh}
headerOffset={headerOffset}
progressViewOffset={ios(0)}
removeClippedSubviews={true}
desktopFixedHeight
onEndReached={onEndReached}
/>
</View>
) )
}, }, [
) hasNextPage,
error,
isFetchingNextPage,
headerOffset,
fetchNextPage,
isEmpty,
])
return (
<View testID={testID} style={style}>
<List
testID={testID ? `${testID}-flatlist` : undefined}
ref={scrollElRef}
data={items}
keyExtractor={keyExtractor}
renderItem={renderItemInner}
ListFooterComponent={ProfileListsFooter}
refreshing={isPTRing}
onRefresh={onRefresh}
headerOffset={headerOffset}
progressViewOffset={ios(0)}
removeClippedSubviews={true}
desktopFixedHeight
onEndReached={onEndReached}
/>
</View>
)
}
function keyExtractor(item: any) { function keyExtractor(item: any) {
return item._reactKey || item.uri return item._reactKey || item.uri