about summary refs log tree commit diff
path: root/src/screens/StarterPack/Wizard/StepFeeds.tsx
blob: 6752a95db3a43f551422a377d3d5dfabfbe4c721 (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
import React, {useState} from 'react'
import {ListRenderItemInfo, View} from 'react-native'
import {KeyboardAwareScrollView} from 'react-native-keyboard-controller'
import {AppBskyFeedDefs, ModerationOpts} from '@atproto/api'
import {Trans} from '@lingui/macro'

import {useA11y} from '#/state/a11y'
import {DISCOVER_FEED_URI} from 'lib/constants'
import {
  useGetPopularFeedsQuery,
  useSavedFeeds,
  useSearchPopularFeedsQuery,
} from 'state/queries/feed'
import {SearchInput} from 'view/com/util/forms/SearchInput'
import {List} from 'view/com/util/List'
import {useWizardState} from '#/screens/StarterPack/Wizard/State'
import {atoms as a, useTheme} from '#/alf'
import {useThrottledValue} from '#/components/hooks/useThrottledValue'
import {Loader} from '#/components/Loader'
import {ScreenTransition} from '#/components/StarterPack/Wizard/ScreenTransition'
import {WizardFeedCard} from '#/components/StarterPack/Wizard/WizardListCard'
import {Text} from '#/components/Typography'

function keyExtractor(item: AppBskyFeedDefs.GeneratorView) {
  return item.uri
}

export function StepFeeds({moderationOpts}: {moderationOpts: ModerationOpts}) {
  const t = useTheme()
  const [state, dispatch] = useWizardState()
  const [query, setQuery] = useState('')
  const throttledQuery = useThrottledValue(query, 500)
  const {screenReaderEnabled} = useA11y()

  const {data: savedFeedsAndLists} = useSavedFeeds()
  const savedFeeds = savedFeedsAndLists?.feeds
    .filter(f => f.type === 'feed' && f.view.uri !== DISCOVER_FEED_URI)
    .map(f => f.view) as AppBskyFeedDefs.GeneratorView[]

  const {data: popularFeedsPages, fetchNextPage} = useGetPopularFeedsQuery({
    limit: 30,
  })
  const popularFeeds =
    popularFeedsPages?.pages
      .flatMap(page => page.feeds)
      .filter(f => !savedFeeds?.some(sf => sf?.uri === f.uri)) ?? []

  const suggestedFeeds = savedFeeds?.concat(popularFeeds)

  const {data: searchedFeeds, isLoading: isLoadingSearch} =
    useSearchPopularFeedsQuery({q: throttledQuery})

  const renderItem = ({
    item,
  }: ListRenderItemInfo<AppBskyFeedDefs.GeneratorView>) => {
    return (
      <WizardFeedCard
        generator={item}
        state={state}
        dispatch={dispatch}
        moderationOpts={moderationOpts}
      />
    )
  }

  return (
    <ScreenTransition style={[a.flex_1]} direction={state.transitionDirection}>
      <View style={[a.border_b, t.atoms.border_contrast_medium]}>
        <View style={[a.my_sm, a.px_md, {height: 40}]}>
          <SearchInput
            query={query}
            onChangeQuery={t => setQuery(t)}
            onPressCancelSearch={() => setQuery('')}
            onSubmitQuery={() => {}}
          />
        </View>
      </View>
      <List
        data={query ? searchedFeeds : suggestedFeeds}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
        contentContainerStyle={{paddingTop: 6}}
        onEndReached={
          !query && !screenReaderEnabled ? () => fetchNextPage() : undefined
        }
        onEndReachedThreshold={2}
        renderScrollComponent={props => <KeyboardAwareScrollView {...props} />}
        keyboardShouldPersistTaps="handled"
        containWeb={true}
        sideBorders={false}
        style={{flex: 1}}
        ListEmptyComponent={
          <View style={[a.flex_1, a.align_center, a.mt_lg, a.px_lg]}>
            {isLoadingSearch ? (
              <Loader size="lg" />
            ) : (
              <Text
                style={[
                  a.font_bold,
                  a.text_lg,
                  a.text_center,
                  a.mt_lg,
                  a.leading_snug,
                ]}>
                <Trans>No feeds found. Try searching for something else.</Trans>
              </Text>
            )}
          </View>
        }
      />
    </ScreenTransition>
  )
}