Update profile preview to use react-query (#1890)

zio/stable
Paul Frazee 2023-11-13 14:57:53 -08:00 committed by GitHub
parent 9fca7b3af6
commit 47204d9551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 14 deletions

View File

@ -1,27 +1,84 @@
import React, {useState, useEffect} from 'react' import React, {useState, useEffect} from 'react'
import {ActivityIndicator, StyleSheet, View} from 'react-native' import {ActivityIndicator, StyleSheet, View} from 'react-native'
import {observer} from 'mobx-react-lite' import {AppBskyActorDefs, ModerationOpts, moderateProfile} from '@atproto/api'
import {ThemedText} from '../util/text/ThemedText' import {ThemedText} from '../util/text/ThemedText'
import {useStores} from 'state/index'
import {ProfileModel} from 'state/models/content/profile'
import {usePalette} from 'lib/hooks/usePalette' import {usePalette} from 'lib/hooks/usePalette'
import {useAnalytics} from 'lib/analytics/analytics' import {useAnalytics} from 'lib/analytics/analytics'
import {ProfileHeader} from '../profile/ProfileHeader' import {ProfileHeader} from '../profile/ProfileHeader'
import {InfoCircleIcon} from 'lib/icons' import {InfoCircleIcon} from 'lib/icons'
import {useNavigationState} from '@react-navigation/native' import {useNavigationState} from '@react-navigation/native'
import {s} from 'lib/styles' import {s} from 'lib/styles'
import {useModerationOpts} from '#/state/queries/preferences'
import {useProfileQuery} from '#/state/queries/profile'
import {ErrorScreen} from '../util/error/ErrorScreen'
import {cleanError} from '#/lib/strings/errors'
import {useProfileShadow} from '#/state/cache/profile-shadow'
export const snapPoints = [520, '100%'] export const snapPoints = [520, '100%']
export const Component = observer(function ProfilePreviewImpl({ export function Component({did}: {did: string}) {
did, const moderationOpts = useModerationOpts()
const {
data: profile,
dataUpdatedAt,
error: profileError,
refetch: refetchProfile,
isFetching: isFetchingProfile,
} = useProfileQuery({
did: did,
})
if (isFetchingProfile || !moderationOpts) {
return (
<View style={s.p20}>
<ActivityIndicator size="large" />
</View>
)
}
if (profileError) {
return (
<ErrorScreen
title="Oops!"
message={cleanError(profileError)}
onPressTryAgain={refetchProfile}
/>
)
}
if (profile && moderationOpts) {
return (
<ComponentLoaded
profile={profile}
dataUpdatedAt={dataUpdatedAt}
moderationOpts={moderationOpts}
/>
)
}
// should never happen
return (
<ErrorScreen
title="Oops!"
message="Something went wrong and we're not sure what."
onPressTryAgain={refetchProfile}
/>
)
}
function ComponentLoaded({
profile: profileUnshadowed,
dataUpdatedAt,
moderationOpts,
}: { }: {
did: string profile: AppBskyActorDefs.ProfileViewDetailed
dataUpdatedAt: number
moderationOpts: ModerationOpts
}) { }) {
const store = useStores()
const pal = usePalette('default') const pal = usePalette('default')
const [model] = useState(new ProfileModel(store, {actor: did})) const profile = useProfileShadow(profileUnshadowed, dataUpdatedAt)
const {screen} = useAnalytics() const {screen} = useAnalytics()
const moderation = React.useMemo(
() => moderateProfile(profile, moderationOpts),
[profile, moderationOpts],
)
// track the navigator state to detect if a page-load occurred // track the navigator state to detect if a page-load occurred
const navState = useNavigationState(state => state) const navState = useNavigationState(state => state)
@ -30,16 +87,15 @@ export const Component = observer(function ProfilePreviewImpl({
useEffect(() => { useEffect(() => {
screen('Profile:Preview') screen('Profile:Preview')
model.setup() }, [screen])
}, [model, screen])
return ( return (
<View testID="profilePreview" style={[pal.view, s.flex1]}> <View testID="profilePreview" style={[pal.view, s.flex1]}>
<View style={[styles.headerWrapper]}> <View style={[styles.headerWrapper]}>
<ProfileHeader <ProfileHeader
view={model} profile={profile}
moderation={moderation}
hideBackButton hideBackButton
onRefreshAll={() => {}}
isProfilePreview isProfilePreview
/> />
</View> </View>
@ -59,7 +115,7 @@ export const Component = observer(function ProfilePreviewImpl({
</View> </View>
</View> </View>
) )
}) }
const styles = StyleSheet.create({ const styles = StyleSheet.create({
headerWrapper: { headerWrapper: {

View File

@ -66,7 +66,7 @@ export const ProfileScreen = withAuthRequired(function ProfileScreenImpl({
} }
}, [resolveError, refetchDid, refetchProfile]) }, [resolveError, refetchDid, refetchProfile])
if (isFetchingDid || isFetchingProfile) { if (isFetchingDid || isFetchingProfile || !moderationOpts) {
return ( return (
<CenteredView> <CenteredView>
<View style={s.p20}> <View style={s.p20}>