only show tab when user has starter packs

This commit is contained in:
Hailey
2024-06-11 22:46:06 -07:00
parent ad2fcd0070
commit e2f328eee4
2 changed files with 47 additions and 55 deletions
@@ -6,20 +6,17 @@ import {
View, View,
ViewStyle, ViewStyle,
} from 'react-native' } from 'react-native'
import {msg, Trans} from '@lingui/macro' import {AppBskyGraphGetActorStarterPacks} from '@atproto/api'
import {useLingui} from '@lingui/react' import {Trans} from '@lingui/macro'
import {InfiniteData, UseInfiniteQueryResult} from '@tanstack/react-query'
import {cleanError} from '#/lib/strings/errors'
import {useTheme} from '#/lib/ThemeContext' import {useTheme} from '#/lib/ThemeContext'
import {logger} from '#/logger' import {logger} from '#/logger'
import {isNative, isWeb} from '#/platform/detection' import {isNative, isWeb} from '#/platform/detection'
import {usePalette} from 'lib/hooks/usePalette' import {usePalette} from 'lib/hooks/usePalette'
import {useActorStarterPacksQuery} from 'state/queries/actor-starter-packs'
import {usePreferencesQuery} from 'state/queries/preferences' import {usePreferencesQuery} from 'state/queries/preferences'
import {FeedLoadingPlaceholder} from '#/view/com/util/LoadingPlaceholder' import {FeedLoadingPlaceholder} from '#/view/com/util/LoadingPlaceholder'
import {ErrorMessage} from 'view/com/util/error/ErrorMessage'
import {List, ListRef} from 'view/com/util/List' import {List, ListRef} from 'view/com/util/List'
import {LoadMoreRetryBtn} from 'view/com/util/LoadMoreRetryBtn'
import {Text} from 'view/com/util/text/Text' import {Text} from 'view/com/util/text/Text'
import {StarterPackCard} from '#/components/StarterPack/StarterPackCard' import {StarterPackCard} from '#/components/StarterPack/StarterPackCard'
@@ -33,7 +30,10 @@ interface SectionRef {
} }
interface ProfileFeedgensProps { interface ProfileFeedgensProps {
did: string starterPacksQuery: UseInfiniteQueryResult<
InfiniteData<AppBskyGraphGetActorStarterPacks.OutputSchema, unknown>,
Error
>
scrollElRef: ListRef scrollElRef: ListRef
headerOffset: number headerOffset: number
enabled?: boolean enabled?: boolean
@@ -46,47 +46,40 @@ export const ProfileStarterPacks = React.forwardRef<
SectionRef, SectionRef,
ProfileFeedgensProps ProfileFeedgensProps
>(function ProfileFeedgensImpl( >(function ProfileFeedgensImpl(
{did, scrollElRef, headerOffset, enabled, style, testID, setScrollViewTag}, {
starterPacksQuery: query,
scrollElRef,
headerOffset,
enabled,
style,
testID,
setScrollViewTag,
},
ref, ref,
) { ) {
const pal = usePalette('default') const pal = usePalette('default')
const {_} = useLingui()
const theme = useTheme() const theme = useTheme()
const [isPTRing, setIsPTRing] = React.useState(false) const [isPTRing, setIsPTRing] = React.useState(false)
const {data: pages, refetch, isFetching, hasNextPage, fetchNextPage} = query
const {
data,
isFetching,
isError,
isFetched,
fetchNextPage,
refetch,
hasNextPage,
error,
} = useActorStarterPacksQuery({
did,
})
const {data: preferences} = usePreferencesQuery() const {data: preferences} = usePreferencesQuery()
const isEmpty = !isFetching && data?.pages.length === 0 const isEmpty = pages?.pages.length === 0
const items = React.useMemo(() => { const items = React.useMemo(() => {
let items: any[] = [] let items: any[] = []
if (isError && isEmpty) { if (isEmpty) {
items = items.concat([ERROR_ITEM]) items = items.concat([ERROR_ITEM])
} }
if (!isFetched && isFetching) { if (isEmpty) {
items = items.concat([LOADING])
} else if (isEmpty) {
items = items.concat([EMPTY]) items = items.concat([EMPTY])
} else if (data?.pages) { } else if (pages?.pages) {
items = data?.pages.flatMap(page => page.starterPacks) items = pages?.pages.flatMap(page => page.starterPacks)
} }
if (isError && !isEmpty) { if (!isEmpty) {
items = items.concat([LOAD_MORE_ERROR_ITEM]) items = items.concat([LOAD_MORE_ERROR_ITEM])
} }
return items return items
}, [isError, isEmpty, isFetched, isFetching, data]) }, [isEmpty, pages])
React.useImperativeHandle(ref, () => ({ React.useImperativeHandle(ref, () => ({
scrollToTop: () => {}, scrollToTop: () => {},
@@ -103,18 +96,14 @@ export const ProfileStarterPacks = React.forwardRef<
}, [refetch, setIsPTRing]) }, [refetch, setIsPTRing])
const onEndReached = React.useCallback(async () => { const onEndReached = React.useCallback(async () => {
if (isFetching || !hasNextPage || isError) return if (isFetching || !hasNextPage) return
try { try {
await fetchNextPage() await fetchNextPage()
} catch (err) { } catch (err) {
logger.error('Failed to load more starter packs', {message: err}) logger.error('Failed to load more starter packs', {message: err})
} }
}, [isFetching, hasNextPage, isError, fetchNextPage]) }, [isFetching, hasNextPage, fetchNextPage])
const onPressRetryLoadMore = React.useCallback(() => {
fetchNextPage()
}, [fetchNextPage])
const renderItem = React.useCallback( const renderItem = React.useCallback(
({item, index}: ListRenderItemInfo<any>) => { ({item, index}: ListRenderItemInfo<any>) => {
@@ -128,19 +117,6 @@ export const ProfileStarterPacks = React.forwardRef<
</Text> </Text>
</View> </View>
) )
} 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) { } else if (item === LOADING) {
return <FeedLoadingPlaceholder /> return <FeedLoadingPlaceholder />
} }
@@ -155,7 +131,7 @@ export const ProfileStarterPacks = React.forwardRef<
} }
return null return null
}, },
[error, refetch, onPressRetryLoadMore, pal, preferences, _], [pal, preferences],
) )
React.useEffect(() => { React.useEffect(() => {
+20 -4
View File
@@ -2,6 +2,7 @@ import React, {useCallback, useMemo} from 'react'
import {StyleSheet} from 'react-native' import {StyleSheet} from 'react-native'
import { import {
AppBskyActorDefs, AppBskyActorDefs,
AppBskyGraphGetActorStarterPacks,
moderateProfile, moderateProfile,
ModerationOpts, ModerationOpts,
RichText as RichTextAPI, RichText as RichTextAPI,
@@ -9,7 +10,11 @@ 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 {useQueryClient} from '@tanstack/react-query' import {
InfiniteData,
UseInfiniteQueryResult,
useQueryClient,
} from '@tanstack/react-query'
import {cleanError} from '#/lib/strings/errors' import {cleanError} from '#/lib/strings/errors'
import {useProfileShadow} from '#/state/cache/profile-shadow' import {useProfileShadow} from '#/state/cache/profile-shadow'
@@ -29,6 +34,7 @@ import {combinedDisplayName} from 'lib/strings/display-names'
import {isInvalidHandle} from 'lib/strings/handles' import {isInvalidHandle} from 'lib/strings/handles'
import {colors, s} from 'lib/styles' import {colors, s} from 'lib/styles'
import {listenSoftReset} from 'state/events' import {listenSoftReset} from 'state/events'
import {useActorStarterPacksQuery} from 'state/queries/actor-starter-packs'
import {PagerWithHeader} from 'view/com/pager/PagerWithHeader' import {PagerWithHeader} from 'view/com/pager/PagerWithHeader'
import {ProfileHeader, ProfileHeaderLoading} from '#/screens/Profile/Header' import {ProfileHeader, ProfileHeaderLoading} from '#/screens/Profile/Header'
import {ProfileFeedSection} from '#/screens/Profile/Sections/Feed' import {ProfileFeedSection} from '#/screens/Profile/Sections/Feed'
@@ -70,6 +76,7 @@ export function ProfileScreen({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) {
@@ -87,7 +94,7 @@ export function ProfileScreen({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) { if (isLoadingDid || isLoadingProfile || starterPacksQuery.isLoading) {
return ( return (
<CenteredView> <CenteredView>
<ProfileHeaderLoading /> <ProfileHeaderLoading />
@@ -109,6 +116,7 @@ export function ProfileScreen({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}
@@ -132,11 +140,16 @@ 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()
@@ -185,10 +198,13 @@ function ProfileScreenLoaded({
const showMediaTab = !hasLabeler const showMediaTab = !hasLabeler
const showLikesTab = isMe const showLikesTab = isMe
const showFeedsTab = isMe || (profile.associated?.feedgens || 0) > 0 const showFeedsTab = isMe || (profile.associated?.feedgens || 0) > 0
const showStarterPacksTab = isMe || true const showStarterPacksTab =
isMe || !!starterPacksQuery.data?.pages?.[0].starterPacks.length
const showListsTab = const showListsTab =
hasSession && (isMe || (profile.associated?.lists || 0) > 0) hasSession && (isMe || (profile.associated?.lists || 0) > 0)
console.log(starterPacksQuery.data?.pages)
const sectionTitles = [ const sectionTitles = [
showFiltersTab ? _(msg`Labels`) : undefined, showFiltersTab ? _(msg`Labels`) : undefined,
showListsTab && hasLabeler ? _(msg`Lists`) : undefined, showListsTab && hasLabeler ? _(msg`Lists`) : undefined,
@@ -433,7 +449,7 @@ function ProfileScreenLoaded({
? ({headerHeight, isFocused, scrollElRef}) => ( ? ({headerHeight, isFocused, scrollElRef}) => (
<ProfileStarterPacks <ProfileStarterPacks
ref={starterPacksSectionRef} ref={starterPacksSectionRef}
did={profile.did} starterPacksQuery={starterPacksQuery}
scrollElRef={scrollElRef as ListRef} scrollElRef={scrollElRef as ListRef}
headerOffset={headerHeight} headerOffset={headerHeight}
enabled={isFocused} enabled={isFocused}