about summary refs log tree commit diff
path: root/src/view/com/modals/ProfilePreview.tsx
blob: 8a505397a9902ac3b5afb8308da50b0a2752a713 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, {useState, useEffect} from 'react'
import {ActivityIndicator, StyleSheet, View} from 'react-native'
import {AppBskyActorDefs, ModerationOpts, moderateProfile} from '@atproto/api'
import {ThemedText} from '../util/text/ThemedText'
import {usePalette} from 'lib/hooks/usePalette'
import {useAnalytics} from 'lib/analytics/analytics'
import {ProfileHeader} from '../profile/ProfileHeader'
import {InfoCircleIcon} from 'lib/icons'
import {useNavigationState} from '@react-navigation/native'
import {s} from 'lib/styles'
import {useModerationOpts} from '#/state/queries/preferences'
import {useProfileQuery} from '#/state/queries/profile'
import {ErrorScreen} from '../util/error/ErrorScreen'
import {CenteredView} from '../util/Views'
import {cleanError} from '#/lib/strings/errors'
import {useProfileShadow} from '#/state/cache/profile-shadow'

export const snapPoints = [520, '100%']

export function Component({did}: {did: string}) {
  const pal = usePalette('default')
  const moderationOpts = useModerationOpts()
  const {
    data: profile,
    dataUpdatedAt,
    error: profileError,
    refetch: refetchProfile,
    isFetching: isFetchingProfile,
  } = useProfileQuery({
    did: did,
  })

  if (isFetchingProfile || !moderationOpts) {
    return (
      <CenteredView style={[pal.view, s.flex1]}>
        <ProfileHeader
          profile={null}
          moderation={null}
          isProfilePreview={true}
        />
      </CenteredView>
    )
  }
  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,
}: {
  profile: AppBskyActorDefs.ProfileViewDetailed
  dataUpdatedAt: number
  moderationOpts: ModerationOpts
}) {
  const pal = usePalette('default')
  const profile = useProfileShadow(profileUnshadowed, dataUpdatedAt)
  const {screen} = useAnalytics()
  const moderation = React.useMemo(
    () => moderateProfile(profile, moderationOpts),
    [profile, moderationOpts],
  )

  // track the navigator state to detect if a page-load occurred
  const navState = useNavigationState(state => state)
  const [initNavState] = useState(navState)
  const isLoading = initNavState !== navState

  useEffect(() => {
    screen('Profile:Preview')
  }, [screen])

  return (
    <View testID="profilePreview" style={[pal.view, s.flex1]}>
      <View style={[styles.headerWrapper]}>
        <ProfileHeader
          profile={profile}
          moderation={moderation}
          hideBackButton
          isProfilePreview
        />
      </View>
      <View style={[styles.hintWrapper, pal.view]}>
        <View style={styles.hint}>
          {isLoading ? (
            <ActivityIndicator />
          ) : (
            <>
              <InfoCircleIcon size={21} style={pal.textLight} />
              <ThemedText type="xl" fg="light">
                Swipe up to see more
              </ThemedText>
            </>
          )}
        </View>
      </View>
    </View>
  )
}

const styles = StyleSheet.create({
  headerWrapper: {
    height: 440,
  },
  hintWrapper: {
    height: 80,
  },
  hint: {
    flexDirection: 'row',
    justifyContent: 'center',
    gap: 8,
    paddingHorizontal: 14,
    borderRadius: 6,
  },
})