Profile cleanup (react-query refactor) (#1891)

* Only fetch profile tab content when focused

* Fix keys

* Add missing behaviors to post tabs

* Delete old profile mobx model
This commit is contained in:
Paul Frazee 2023-11-13 15:12:41 -08:00 committed by GitHub
parent 47204d9551
commit 0501c2be77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 327 deletions

View file

@ -35,6 +35,7 @@ export function ProfileFeedgens({
onScroll,
scrollEventThrottle,
headerOffset,
enabled,
style,
testID,
}: {
@ -43,12 +44,14 @@ export function ProfileFeedgens({
onScroll?: OnScrollHandler
scrollEventThrottle?: number
headerOffset: number
enabled?: boolean
style?: StyleProp<ViewStyle>
testID?: string
}) {
const pal = usePalette('default')
const theme = useTheme()
const [isPTRing, setIsPTRing] = React.useState(false)
const opts = React.useMemo(() => ({enabled}), [enabled])
const {
data,
isFetching,
@ -58,7 +61,7 @@ export function ProfileFeedgens({
isError,
error,
refetch,
} = useProfileFeedgensQuery(did)
} = useProfileFeedgensQuery(did, opts)
const isEmpty = !isFetching && !data?.pages[0]?.feeds.length
const {data: preferences} = usePreferencesQuery()
@ -163,7 +166,7 @@ export function ProfileFeedgens({
testID={testID ? `${testID}-flatlist` : undefined}
ref={scrollElRef}
data={items}
keyExtractor={(item: any) => item._reactKey}
keyExtractor={(item: any) => item._reactKey || item.uri}
renderItem={renderItemInner}
refreshControl={
<RefreshControl

View file

@ -34,6 +34,7 @@ export function ProfileLists({
onScroll,
scrollEventThrottle,
headerOffset,
enabled,
style,
testID,
}: {
@ -42,6 +43,7 @@ export function ProfileLists({
onScroll?: OnScrollHandler
scrollEventThrottle?: number
headerOffset: number
enabled?: boolean
style?: StyleProp<ViewStyle>
testID?: string
}) {
@ -49,6 +51,7 @@ export function ProfileLists({
const theme = useTheme()
const {track} = useAnalytics()
const [isPTRing, setIsPTRing] = React.useState(false)
const opts = React.useMemo(() => ({enabled}), [enabled])
const {
data,
isFetching,
@ -58,7 +61,7 @@ export function ProfileLists({
isError,
error,
refetch,
} = useProfileListsQuery(did)
} = useProfileListsQuery(did, opts)
const isEmpty = !isFetching && !data?.pages[0]?.lists.length
const items = React.useMemo(() => {

View file

@ -24,6 +24,7 @@ const SCROLLED_DOWN_LIMIT = 200
interface PagerWithHeaderChildParams {
headerHeight: number
isFocused: boolean
onScroll: OnScrollHandler
isScrolledDown: boolean
scrollElRef: React.MutableRefObject<FlatList<any> | ScrollView | null>
@ -202,6 +203,7 @@ export const PagerWithHeader = React.forwardRef<PagerRef, PagerWithHeaderProps>(
<PagerItem
headerHeight={headerHeight}
isReady={isReady}
isFocused={i === currentPage}
isScrolledDown={isScrolledDown}
onScrollWorklet={i === currentPage ? onScrollWorklet : noop}
registerRef={(r: AnimatedRef<any>) => registerRef(r, i)}
@ -218,12 +220,14 @@ export const PagerWithHeader = React.forwardRef<PagerRef, PagerWithHeaderProps>(
function PagerItem({
headerHeight,
isReady,
isFocused,
isScrolledDown,
onScrollWorklet,
renderTab,
registerRef,
}: {
headerHeight: number
isFocused: boolean
isReady: boolean
isScrolledDown: boolean
registerRef: (scrollRef: AnimatedRef<any>) => void
@ -244,6 +248,7 @@ function PagerItem({
return renderTab({
headerHeight,
isFocused,
isScrolledDown,
onScroll: scrollHandler,
scrollElRef: scrollElRef as React.MutableRefObject<

View file

@ -31,8 +31,11 @@ import {useProfileShadow} from '#/state/cache/profile-shadow'
import {useSession} from '#/state/session'
import {useModerationOpts} from '#/state/queries/preferences'
import {useProfileExtraInfoQuery} from '#/state/queries/profile-extra-info'
import {RQKEY as FEED_RQKEY} from '#/state/queries/post-feed'
import {useSetDrawerSwipeDisabled, useSetMinimalShellMode} from '#/state/shell'
import {cleanError} from '#/lib/strings/errors'
import {LoadLatestBtn} from '../com/util/load-latest/LoadLatestBtn'
import {useQueryClient} from '@tanstack/react-query'
type Props = NativeStackScreenProps<CommonNavigatorParams, 'Profile'>
export const ProfileScreen = withAuthRequired(function ProfileScreenImpl({
@ -224,67 +227,79 @@ function ProfileScreenLoaded({
items={sectionTitles}
onPageSelected={onPageSelected}
renderHeader={renderHeader}>
{({onScroll, headerHeight, isScrolledDown, scrollElRef}) => (
{({onScroll, headerHeight, isFocused, isScrolledDown, scrollElRef}) => (
<FeedSection
ref={null}
feed={`author|${profile.did}|posts_no_replies`}
onScroll={onScroll}
headerHeight={headerHeight}
isFocused={isFocused}
isScrolledDown={isScrolledDown}
scrollElRef={scrollElRef}
/>
)}
{({onScroll, headerHeight, isScrolledDown, scrollElRef}) => (
{({onScroll, headerHeight, isFocused, isScrolledDown, scrollElRef}) => (
<FeedSection
ref={null}
feed={`author|${profile.did}|posts_with_replies`}
onScroll={onScroll}
headerHeight={headerHeight}
isFocused={isFocused}
isScrolledDown={isScrolledDown}
scrollElRef={scrollElRef}
/>
)}
{({onScroll, headerHeight, isScrolledDown, scrollElRef}) => (
{({onScroll, headerHeight, isFocused, isScrolledDown, scrollElRef}) => (
<FeedSection
ref={null}
feed={`author|${profile.did}|posts_with_media`}
onScroll={onScroll}
headerHeight={headerHeight}
isFocused={isFocused}
isScrolledDown={isScrolledDown}
scrollElRef={scrollElRef}
/>
)}
{showLikesTab
? ({onScroll, headerHeight, isScrolledDown, scrollElRef}) => (
? ({
onScroll,
headerHeight,
isFocused,
isScrolledDown,
scrollElRef,
}) => (
<FeedSection
ref={null}
feed={`likes|${profile.did}`}
onScroll={onScroll}
headerHeight={headerHeight}
isFocused={isFocused}
isScrolledDown={isScrolledDown}
scrollElRef={scrollElRef}
/>
)
: null}
{showFeedsTab
? ({onScroll, headerHeight, scrollElRef}) => (
? ({onScroll, headerHeight, isFocused, scrollElRef}) => (
<ProfileFeedgens
did={profile.did}
scrollElRef={scrollElRef}
onScroll={onScroll}
scrollEventThrottle={1}
headerOffset={headerHeight}
enabled={isFocused}
/>
)
: null}
{showListsTab
? ({onScroll, headerHeight, scrollElRef}) => (
? ({onScroll, headerHeight, isFocused, scrollElRef}) => (
<ProfileLists
did={profile.did}
scrollElRef={scrollElRef}
onScroll={onScroll}
scrollEventThrottle={1}
headerOffset={headerHeight}
enabled={isFocused}
/>
)
: null}
@ -305,26 +320,23 @@ interface FeedSectionProps {
feed: FeedDescriptor
onScroll: OnScrollHandler
headerHeight: number
isFocused: boolean
isScrolledDown: boolean
scrollElRef: any /* TODO */
}
const FeedSection = React.forwardRef<SectionRef, FeedSectionProps>(
function FeedSectionImpl(
{
feed,
onScroll,
headerHeight,
// isScrolledDown,
scrollElRef,
},
{feed, onScroll, headerHeight, isFocused, isScrolledDown, scrollElRef},
ref,
) {
// const hasNew = false //TODO feed.hasNewLatest && !feed.isRefreshing
const queryClient = useQueryClient()
const [hasNew, setHasNew] = React.useState(false)
const onScrollToTop = React.useCallback(() => {
scrollElRef.current?.scrollToOffset({offset: -headerHeight})
// feed.refresh() TODO
}, [scrollElRef, headerHeight])
queryClient.invalidateQueries({queryKey: FEED_RQKEY(feed)})
setHasNew(false)
}, [scrollElRef, headerHeight, queryClient, feed, setHasNew])
React.useImperativeHandle(ref, () => ({
scrollToTop: onScrollToTop,
}))
@ -339,11 +351,20 @@ const FeedSection = React.forwardRef<SectionRef, FeedSectionProps>(
testID="postsFeed"
feed={feed}
scrollElRef={scrollElRef}
onHasNew={setHasNew}
onScroll={onScroll}
scrollEventThrottle={1}
renderEmptyState={renderPostsEmpty}
headerOffset={headerHeight}
enabled={isFocused}
/>
{(isScrolledDown || hasNew) && (
<LoadLatestBtn
onPress={onScrollToTop}
label="Load new posts"
showIndicator={hasNew}
/>
)}
</View>
)
},