Fix optimistic rendering of profile page (#7830)

* don't hold up rendering on starter packs

* add tab based on profile.associated

* move query into component, fix pending states
This commit is contained in:
Samuel Newman
2025-02-24 11:50:52 -08:00
committed by GitHub
parent bdcddee7a5
commit f9392d4a96
5 changed files with 42 additions and 50 deletions
@@ -1,4 +1,9 @@
import React from 'react' import React, {
useCallback,
useEffect,
useImperativeHandle,
useState,
} from 'react'
import { import {
findNodeHandle, findNodeHandle,
ListRenderItemInfo, ListRenderItemInfo,
@@ -6,11 +11,10 @@ import {
View, View,
ViewStyle, ViewStyle,
} from 'react-native' } from 'react-native'
import {AppBskyGraphDefs, AppBskyGraphGetActorStarterPacks} from '@atproto/api' import {AppBskyGraphDefs} from '@atproto/api'
import {msg, Trans} from '@lingui/macro' 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 {InfiniteData, UseInfiniteQueryResult} from '@tanstack/react-query'
import {useGenerateStarterPackMutation} from '#/lib/generate-starterpack' import {useGenerateStarterPackMutation} from '#/lib/generate-starterpack'
import {useBottomBarOffset} from '#/lib/hooks/useBottomBarOffset' import {useBottomBarOffset} from '#/lib/hooks/useBottomBarOffset'
@@ -19,28 +23,27 @@ import {useWebMediaQueries} from '#/lib/hooks/useWebMediaQueries'
import {NavigationProp} from '#/lib/routes/types' import {NavigationProp} from '#/lib/routes/types'
import {parseStarterPackUri} from '#/lib/strings/starter-pack' import {parseStarterPackUri} from '#/lib/strings/starter-pack'
import {logger} from '#/logger' import {logger} from '#/logger'
import {useActorStarterPacksQuery} from '#/state/queries/actor-starter-packs'
import {List, ListRef} from '#/view/com/util/List' import {List, ListRef} from '#/view/com/util/List'
import {Text} from '#/view/com/util/text/Text' import {FeedLoadingPlaceholder} from '#/view/com/util/LoadingPlaceholder'
import {atoms as a, ios, useTheme} from '#/alf' import {atoms as a, ios, useTheme} from '#/alf'
import {Button, ButtonIcon, ButtonText} from '#/components/Button' import {Button, ButtonIcon, ButtonText} from '#/components/Button'
import {useDialogControl} from '#/components/Dialog' import {useDialogControl} from '#/components/Dialog'
import {VerifyEmailDialog} from '#/components/dialogs/VerifyEmailDialog'
import {PlusSmall_Stroke2_Corner0_Rounded as Plus} from '#/components/icons/Plus'
import {LinearGradientBackground} from '#/components/LinearGradientBackground' import {LinearGradientBackground} from '#/components/LinearGradientBackground'
import {Loader} from '#/components/Loader' import {Loader} from '#/components/Loader'
import * as Prompt from '#/components/Prompt' import * as Prompt from '#/components/Prompt'
import {Default as StarterPackCard} from '#/components/StarterPack/StarterPackCard' import {Default as StarterPackCard} from '#/components/StarterPack/StarterPackCard'
import {VerifyEmailDialog} from '../dialogs/VerifyEmailDialog' import {Text} from '#/components/Typography'
import {PlusSmall_Stroke2_Corner0_Rounded as Plus} from '../icons/Plus'
interface SectionRef { interface SectionRef {
scrollToTop: () => void scrollToTop: () => void
} }
interface ProfileFeedgensProps { interface ProfileFeedgensProps {
starterPacksQuery: UseInfiniteQueryResult<
InfiniteData<AppBskyGraphGetActorStarterPacks.OutputSchema, unknown>,
Error
>
scrollElRef: ListRef scrollElRef: ListRef
did: string
headerOffset: number headerOffset: number
enabled?: boolean enabled?: boolean
style?: StyleProp<ViewStyle> style?: StyleProp<ViewStyle>
@@ -58,8 +61,8 @@ export const ProfileStarterPacks = React.forwardRef<
ProfileFeedgensProps ProfileFeedgensProps
>(function ProfileFeedgensImpl( >(function ProfileFeedgensImpl(
{ {
starterPacksQuery: query,
scrollElRef, scrollElRef,
did,
headerOffset, headerOffset,
enabled, enabled,
style, style,
@@ -71,17 +74,18 @@ export const ProfileStarterPacks = React.forwardRef<
) { ) {
const t = useTheme() const t = useTheme()
const bottomBarOffset = useBottomBarOffset(100) const bottomBarOffset = useBottomBarOffset(100)
const [isPTRing, setIsPTRing] = React.useState(false) const [isPTRing, setIsPTRing] = useState(false)
const {data, refetch, isFetching, hasNextPage, fetchNextPage} = query const {data, refetch, isFetching, hasNextPage, fetchNextPage} =
useActorStarterPacksQuery({did, enabled})
const {isTabletOrDesktop} = useWebMediaQueries() const {isTabletOrDesktop} = useWebMediaQueries()
const items = data?.pages.flatMap(page => page.starterPacks) const items = data?.pages.flatMap(page => page.starterPacks)
React.useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
scrollToTop: () => {}, scrollToTop: () => {},
})) }))
const onRefresh = React.useCallback(async () => { const onRefresh = useCallback(async () => {
setIsPTRing(true) setIsPTRing(true)
try { try {
await refetch() await refetch()
@@ -91,7 +95,7 @@ export const ProfileStarterPacks = React.forwardRef<
setIsPTRing(false) setIsPTRing(false)
}, [refetch, setIsPTRing]) }, [refetch, setIsPTRing])
const onEndReached = React.useCallback(async () => { const onEndReached = useCallback(async () => {
if (isFetching || !hasNextPage) return if (isFetching || !hasNextPage) return
try { try {
@@ -101,7 +105,7 @@ export const ProfileStarterPacks = React.forwardRef<
} }
}, [isFetching, hasNextPage, fetchNextPage]) }, [isFetching, hasNextPage, fetchNextPage])
React.useEffect(() => { useEffect(() => {
if (enabled && scrollElRef.current) { if (enabled && scrollElRef.current) {
const nativeTag = findNodeHandle(scrollElRef.current) const nativeTag = findNodeHandle(scrollElRef.current)
setScrollViewTag(nativeTag) setScrollViewTag(nativeTag)
@@ -140,9 +144,11 @@ export const ProfileStarterPacks = React.forwardRef<
desktopFixedHeight desktopFixedHeight
onEndReached={onEndReached} onEndReached={onEndReached}
onRefresh={onRefresh} onRefresh={onRefresh}
ListEmptyComponent={Empty} ListEmptyComponent={
data ? (isMe ? Empty : undefined) : FeedLoadingPlaceholder
}
ListFooterComponent={ ListFooterComponent={
items?.length !== 0 && isMe ? CreateAnother : undefined !!data && items?.length !== 0 && isMe ? CreateAnother : undefined
} }
/> />
</View> </View>
@@ -181,7 +187,6 @@ function CreateAnother() {
function Empty() { function Empty() {
const {_} = useLingui() const {_} = useLingui()
const t = useTheme()
const navigation = useNavigation<NavigationProp>() const navigation = useNavigation<NavigationProp>()
const confirmDialogControl = useDialogControl() const confirmDialogControl = useDialogControl()
const followersDialogControl = useDialogControl() const followersDialogControl = useDialogControl()
@@ -190,7 +195,7 @@ function Empty() {
const {needsEmailVerification} = useEmail() const {needsEmailVerification} = useEmail()
const verifyEmailControl = useDialogControl() const verifyEmailControl = useDialogControl()
const [isGenerating, setIsGenerating] = React.useState(false) const [isGenerating, setIsGenerating] = useState(false)
const {mutate: generateStarterPack} = useGenerateStarterPackMutation({ const {mutate: generateStarterPack} = useGenerateStarterPackMutation({
onSuccess: ({uri}) => { onSuccess: ({uri}) => {
@@ -227,16 +232,10 @@ function Empty() {
a.justify_between, a.justify_between,
a.gap_lg, a.gap_lg,
a.shadow_lg, a.shadow_lg,
{marginTop: 1}, {marginTop: a.border.borderWidth},
]}> ]}>
<View style={[a.gap_xs]}> <View style={[a.gap_xs]}>
<Text <Text style={[a.font_bold, a.text_lg, {color: 'white'}]}>
style={[
a.font_bold,
a.text_lg,
t.atoms.text_contrast_medium,
{color: 'white'},
]}>
<Trans>You haven't created a starter pack yet!</Trans> <Trans>You haven't created a starter pack yet!</Trans>
</Text> </Text>
<Text style={[a.text_md, {color: 'white'}]}> <Text style={[a.text_md, {color: 'white'}]}>
+8 -2
View File
@@ -11,7 +11,13 @@ import {useAgent} from '#/state/session'
export const RQKEY_ROOT = 'actor-starter-packs' export const RQKEY_ROOT = 'actor-starter-packs'
export const RQKEY = (did?: string) => [RQKEY_ROOT, did] export const RQKEY = (did?: string) => [RQKEY_ROOT, did]
export function useActorStarterPacksQuery({did}: {did?: string}) { export function useActorStarterPacksQuery({
did,
enabled = true,
}: {
did?: string
enabled?: boolean
}) {
const agent = useAgent() const agent = useAgent()
return useInfiniteQuery< return useInfiniteQuery<
@@ -30,7 +36,7 @@ export function useActorStarterPacksQuery({did}: {did?: string}) {
}) })
return res.data return res.data
}, },
enabled: Boolean(did), enabled: Boolean(did) && enabled,
initialPageParam: undefined, initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor, getNextPageParam: lastPage => lastPage.cursor,
}) })
+1 -1
View File
@@ -76,7 +76,7 @@ export const ProfileFeedgens = React.forwardRef<
if (isError && isEmpty) { if (isError && isEmpty) {
items = items.concat([ERROR_ITEM]) items = items.concat([ERROR_ITEM])
} }
if (!isFetched && isFetching) { if (!isFetched || isFetching) {
items = items.concat([LOADING]) items = items.concat([LOADING])
} else if (isEmpty) { } else if (isEmpty) {
items = items.concat([EMPTY]) items = items.concat([EMPTY])
+1 -1
View File
@@ -72,7 +72,7 @@ export const ProfileLists = React.forwardRef<SectionRef, ProfileListsProps>(
if (isError && isEmpty) { if (isError && isEmpty) {
items = items.concat([ERROR_ITEM]) items = items.concat([ERROR_ITEM])
} }
if (!isFetched && isFetching) { if (!isFetched || isFetching) {
items = items.concat([LOADING]) items = items.concat([LOADING])
} else if (isEmpty) { } else if (isEmpty) {
items = items.concat([EMPTY]) items = items.concat([EMPTY])
+4 -17
View File
@@ -3,7 +3,6 @@ import {StyleSheet} from 'react-native'
import {SafeAreaView} from 'react-native-safe-area-context' import {SafeAreaView} from 'react-native-safe-area-context'
import { import {
AppBskyActorDefs, AppBskyActorDefs,
AppBskyGraphGetActorStarterPacks,
moderateProfile, moderateProfile,
ModerationOpts, ModerationOpts,
RichText as RichTextAPI, RichText as RichTextAPI,
@@ -11,11 +10,7 @@ import {
import {msg} from '@lingui/macro' import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
import {useFocusEffect} from '@react-navigation/native' import {useFocusEffect} from '@react-navigation/native'
import { import {useQueryClient} from '@tanstack/react-query'
InfiniteData,
UseInfiniteQueryResult,
useQueryClient,
} from '@tanstack/react-query'
import {useSetTitle} from '#/lib/hooks/useSetTitle' import {useSetTitle} from '#/lib/hooks/useSetTitle'
import {ComposeIcon2} from '#/lib/icons' import {ComposeIcon2} from '#/lib/icons'
@@ -27,7 +22,6 @@ import {colors, s} from '#/lib/styles'
import {useProfileShadow} from '#/state/cache/profile-shadow' import {useProfileShadow} from '#/state/cache/profile-shadow'
import {listenSoftReset} from '#/state/events' import {listenSoftReset} from '#/state/events'
import {useModerationOpts} from '#/state/preferences/moderation-opts' import {useModerationOpts} from '#/state/preferences/moderation-opts'
import {useActorStarterPacksQuery} from '#/state/queries/actor-starter-packs'
import {useLabelerInfoQuery} from '#/state/queries/labeler' import {useLabelerInfoQuery} from '#/state/queries/labeler'
import {resetProfilePostsQueries} from '#/state/queries/post-feed' import {resetProfilePostsQueries} from '#/state/queries/post-feed'
import {useProfileQuery} from '#/state/queries/profile' import {useProfileQuery} from '#/state/queries/profile'
@@ -86,7 +80,6 @@ function ProfileScreenInner({route}: Props) {
} = useProfileQuery({ } = useProfileQuery({
did: resolvedDid, did: resolvedDid,
}) })
const starterPacksQuery = useActorStarterPacksQuery({did: resolvedDid})
const onPressTryAgain = React.useCallback(() => { const onPressTryAgain = React.useCallback(() => {
if (resolveError) { if (resolveError) {
@@ -114,7 +107,7 @@ function ProfileScreenInner({route}: Props) {
}, [queryClient, profile?.viewer?.blockedBy, resolvedDid]) }, [queryClient, profile?.viewer?.blockedBy, resolvedDid])
// Most pushes will happen here, since we will have only placeholder data // Most pushes will happen here, since we will have only placeholder data
if (isLoadingDid || isLoadingProfile || starterPacksQuery.isLoading) { if (isLoadingDid || isLoadingProfile) {
return ( return (
<Layout.Content> <Layout.Content>
<ProfileHeaderLoading /> <ProfileHeaderLoading />
@@ -138,7 +131,6 @@ function ProfileScreenInner({route}: Props) {
return ( return (
<ProfileScreenLoaded <ProfileScreenLoaded
profile={profile} profile={profile}
starterPacksQuery={starterPacksQuery}
moderationOpts={moderationOpts} moderationOpts={moderationOpts}
isPlaceholderProfile={isPlaceholderProfile} isPlaceholderProfile={isPlaceholderProfile}
hideBackButton={!!route.params.hideBackButton} hideBackButton={!!route.params.hideBackButton}
@@ -164,16 +156,11 @@ function ProfileScreenLoaded({
isPlaceholderProfile, isPlaceholderProfile,
moderationOpts, moderationOpts,
hideBackButton, hideBackButton,
starterPacksQuery,
}: { }: {
profile: AppBskyActorDefs.ProfileViewDetailed profile: AppBskyActorDefs.ProfileViewDetailed
moderationOpts: ModerationOpts moderationOpts: ModerationOpts
hideBackButton: boolean hideBackButton: boolean
isPlaceholderProfile: boolean isPlaceholderProfile: boolean
starterPacksQuery: UseInfiniteQueryResult<
InfiniteData<AppBskyGraphGetActorStarterPacks.OutputSchema, unknown>,
Error
>
}) { }) {
const profile = useProfileShadow(profileUnshadowed) const profile = useProfileShadow(profileUnshadowed)
const {hasSession, currentAccount} = useSession() const {hasSession, currentAccount} = useSession()
@@ -223,7 +210,7 @@ function ProfileScreenLoaded({
const showLikesTab = isMe const showLikesTab = isMe
const showFeedsTab = isMe || (profile.associated?.feedgens || 0) > 0 const showFeedsTab = isMe || (profile.associated?.feedgens || 0) > 0
const showStarterPacksTab = const showStarterPacksTab =
isMe || !!starterPacksQuery.data?.pages?.[0].starterPacks.length isMe || (profile.associated?.starterPacks || 0) > 0
const showListsTab = const showListsTab =
hasSession && (isMe || (profile.associated?.lists || 0) > 0) hasSession && (isMe || (profile.associated?.lists || 0) > 0)
@@ -487,8 +474,8 @@ function ProfileScreenLoaded({
? ({headerHeight, isFocused, scrollElRef}) => ( ? ({headerHeight, isFocused, scrollElRef}) => (
<ProfileStarterPacks <ProfileStarterPacks
ref={starterPacksSectionRef} ref={starterPacksSectionRef}
did={profile.did}
isMe={isMe} isMe={isMe}
starterPacksQuery={starterPacksQuery}
scrollElRef={scrollElRef as ListRef} scrollElRef={scrollElRef as ListRef}
headerOffset={headerHeight} headerOffset={headerHeight}
enabled={isFocused} enabled={isFocused}