about summary refs log tree commit diff
path: root/src/screens/Search/modules/ExploreSuggestedAccounts.tsx
blob: 71bfd65472adc0fe673f805e0865d2aa4f0ac21d (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {memo, useEffect} from 'react'
import {View} from 'react-native'
import {type AppBskyActorSearchActors, type ModerationOpts} from '@atproto/api'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {type InfiniteData} from '@tanstack/react-query'

import {logger} from '#/logger'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {BlockDrawerGesture} from '#/view/shell/BlockDrawerGesture'
import {
  popularInterests,
  useInterestsDisplayNames,
} from '#/screens/Onboarding/state'
import {useTheme} from '#/alf'
import {atoms as a} from '#/alf'
import {boostInterests, InterestTabs} from '#/components/InterestTabs'
import * as ProfileCard from '#/components/ProfileCard'
import {SubtleHover} from '#/components/SubtleHover'
import type * as bsky from '#/types/bsky'

export function useLoadEnoughProfiles({
  interest,
  data,
  isLoading,
  isFetchingNextPage,
  hasNextPage,
  fetchNextPage,
}: {
  interest: string | null
  data?: InfiniteData<AppBskyActorSearchActors.OutputSchema>
  isLoading: boolean
  isFetchingNextPage: boolean
  hasNextPage: boolean
  fetchNextPage: () => Promise<unknown>
}) {
  const profileCount =
    data?.pages.flatMap(page =>
      page.actors.filter(actor => !actor.viewer?.following),
    ).length || 0
  const isAnyLoading = isLoading || isFetchingNextPage
  const isEnoughProfiles = profileCount > 3
  const shouldFetchMore = !isEnoughProfiles && hasNextPage && !!interest
  useEffect(() => {
    if (shouldFetchMore && !isAnyLoading) {
      logger.info('Not enough suggested accounts - fetching more')
      fetchNextPage()
    }
  }, [shouldFetchMore, fetchNextPage, isAnyLoading, interest])

  return {
    isReady: !shouldFetchMore,
  }
}

export function SuggestedAccountsTabBar({
  selectedInterest,
  onSelectInterest,
  hideDefaultTab,
  defaultTabLabel,
}: {
  selectedInterest: string | null
  onSelectInterest: (interest: string | null) => void
  hideDefaultTab?: boolean
  defaultTabLabel?: string
}) {
  const {_} = useLingui()
  const interestsDisplayNames = useInterestsDisplayNames()
  const {data: preferences} = usePreferencesQuery()
  const personalizedInterests = preferences?.interests?.tags
  const interests = Object.keys(interestsDisplayNames)
    .sort(boostInterests(popularInterests))
    .sort(boostInterests(personalizedInterests))

  return (
    <BlockDrawerGesture>
      <InterestTabs
        interests={hideDefaultTab ? interests : ['all', ...interests]}
        selectedInterest={
          selectedInterest || (hideDefaultTab ? interests[0] : 'all')
        }
        onSelectTab={tab => {
          logger.metric(
            'explore:suggestedAccounts:tabPressed',
            {tab: tab},
            {statsig: true},
          )
          onSelectInterest(tab === 'all' ? null : tab)
        }}
        interestsDisplayNames={
          hideDefaultTab
            ? interestsDisplayNames
            : {
                all: defaultTabLabel || _(msg`For You`),
                ...interestsDisplayNames,
              }
        }
      />
    </BlockDrawerGesture>
  )
}

/**
 * Profile card for suggested accounts. Note: border is on the bottom edge
 */
let SuggestedProfileCard = ({
  profile,
  moderationOpts,
  recId,
  position,
}: {
  profile: bsky.profile.AnyProfileView
  moderationOpts: ModerationOpts
  recId?: number
  position: number
}): React.ReactNode => {
  const t = useTheme()
  return (
    <ProfileCard.Link
      profile={profile}
      style={[a.flex_1]}
      onPress={() => {
        logger.metric(
          'suggestedUser:press',
          {
            logContext: 'Explore',
            recId,
            position,
          },
          {statsig: true},
        )
      }}>
      {s => (
        <>
          <SubtleHover hover={s.hovered || s.pressed} />
          <View
            style={[
              a.flex_1,
              a.w_full,
              a.py_lg,
              a.px_lg,
              a.border_t,
              t.atoms.border_contrast_low,
            ]}>
            <ProfileCard.Outer>
              <ProfileCard.Header>
                <ProfileCard.Avatar
                  profile={profile}
                  moderationOpts={moderationOpts}
                />
                <ProfileCard.NameAndHandle
                  profile={profile}
                  moderationOpts={moderationOpts}
                />
                <ProfileCard.FollowButton
                  profile={profile}
                  moderationOpts={moderationOpts}
                  withIcon={false}
                  logContext="ExploreSuggestedAccounts"
                  onFollow={() => {
                    logger.metric(
                      'suggestedUser:follow',
                      {
                        logContext: 'Explore',
                        location: 'Card',
                        recId,
                        position,
                      },
                      {statsig: true},
                    )
                  }}
                />
              </ProfileCard.Header>
              <ProfileCard.Description profile={profile} numberOfLines={2} />
            </ProfileCard.Outer>
          </View>
        </>
      )}
    </ProfileCard.Link>
  )
}
SuggestedProfileCard = memo(SuggestedProfileCard)
export {SuggestedProfileCard}