about summary refs log tree commit diff
path: root/src/screens/Search/util/useSuggestedUsers.ts
blob: aa29dad8c6d61e510307e00880ed2d0084064669 (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
import {useMemo} from 'react'

import {useActorSearchPaginated} from '#/state/queries/actor-search'
import {useGetSuggestedUsersQuery} from '#/state/queries/trending/useGetSuggestedUsersQuery'
import {useInterestsDisplayNames} from '#/screens/Onboarding/state'

/**
 * Conditional hook, used in case a user is a non-english speaker, in which
 * case we fall back to searching for users instead of our more curated set.
 */
export function useSuggestedUsers({
  category = null,
  search = false,
}: {
  category?: string | null
  /**
   * If true, we'll search for users using the translated value of `category`,
   * based on the user's "app language setting
   */
  search?: boolean
}) {
  const interestsDisplayNames = useInterestsDisplayNames()
  const curated = useGetSuggestedUsersQuery({
    enabled: !search,
    category,
  })
  const searched = useActorSearchPaginated({
    enabled: !!search,
    // use user's app language translation for this value
    query: category ? interestsDisplayNames[category] : '',
    limit: 10,
  })

  return useMemo(() => {
    if (search) {
      return {
        // we're not paginating right now
        data: searched?.data
          ? {
              actors: searched.data.pages.flatMap(p => p.actors) ?? [],
            }
          : undefined,
        isLoading: searched.isLoading,
        error: searched.error,
        isRefetching: searched.isRefetching,
      }
    } else {
      return {
        data: curated.data,
        isLoading: curated.isLoading,
        error: curated.error,
        isRefetching: curated.isRefetching,
      }
    }
  }, [curated, searched, search])
}