about summary refs log tree commit diff
path: root/src/screens/Onboarding/StepTopicalFeeds.tsx
blob: 26b1c243b0bebf31746d7cbea0af5d0e1037b9ec (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
import React from 'react'
import {View} from 'react-native'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {useAnalytics} from '#/lib/analytics/analytics'
import {logEvent} from '#/lib/statsig/statsig'
import {capitalize} from '#/lib/strings/capitalize'
import {IS_TEST_USER} from 'lib/constants'
import {useSession} from 'state/session'
import {
  Description,
  OnboardingControls,
  Title,
} from '#/screens/Onboarding/Layout'
import {Context} from '#/screens/Onboarding/state'
import {FeedCard} from '#/screens/Onboarding/StepAlgoFeeds/FeedCard'
import {aggregateInterestItems} from '#/screens/Onboarding/util'
import {atoms as a} from '#/alf'
import {Button, ButtonIcon, ButtonText} from '#/components/Button'
import * as Toggle from '#/components/forms/Toggle'
import {IconCircle} from '#/components/IconCircle'
import {ChevronRight_Stroke2_Corner0_Rounded as ChevronRight} from '#/components/icons/Chevron'
import {ListMagnifyingGlass_Stroke2_Corner0_Rounded as ListMagnifyingGlass} from '#/components/icons/ListMagnifyingGlass'
import {Loader} from '#/components/Loader'

export function StepTopicalFeeds() {
  const {_} = useLingui()
  const {track} = useAnalytics()
  const {currentAccount} = useSession()
  const {state, dispatch, interestsDisplayNames} = React.useContext(Context)
  const [selectedFeedUris, setSelectedFeedUris] = React.useState<string[]>([])
  const [saving, setSaving] = React.useState(false)
  const suggestedFeedUris = React.useMemo(() => {
    if (IS_TEST_USER(currentAccount?.handle)) return []
    return aggregateInterestItems(
      state.interestsStepResults.selectedInterests,
      state.interestsStepResults.apiResponse.suggestedFeedUris,
      state.interestsStepResults.apiResponse.suggestedFeedUris.default || [],
    ).slice(0, 10)
  }, [
    currentAccount?.handle,
    state.interestsStepResults.apiResponse.suggestedFeedUris,
    state.interestsStepResults.selectedInterests,
  ])

  const interestsText = React.useMemo(() => {
    const i = state.interestsStepResults.selectedInterests.map(
      i => interestsDisplayNames[i] || capitalize(i),
    )
    return i.join(', ')
  }, [state.interestsStepResults.selectedInterests, interestsDisplayNames])

  const saveFeeds = React.useCallback(async () => {
    setSaving(true)

    dispatch({type: 'setTopicalFeedsStepResults', feedUris: selectedFeedUris})

    setSaving(false)
    dispatch({type: 'next'})
    track('OnboardingV2:StepTopicalFeeds:End', {
      selectedFeeds: selectedFeedUris,
      selectedFeedsLength: selectedFeedUris.length,
    })
    logEvent('onboarding:topicalFeeds:nextPressed', {
      selectedFeeds: selectedFeedUris,
      selectedFeedsLength: selectedFeedUris.length,
    })
  }, [selectedFeedUris, dispatch, track])

  React.useEffect(() => {
    track('OnboardingV2:StepTopicalFeeds:Start')
  }, [track])

  return (
    <View style={[a.align_start]}>
      <IconCircle icon={ListMagnifyingGlass} style={[a.mb_2xl]} />

      <Title>
        <Trans>Feeds can be topical as well!</Trans>
      </Title>
      <Description>
        {state.interestsStepResults.selectedInterests.length ? (
          <Trans>
            Here are some topical feeds based on your interests: {interestsText}
            . You can choose to follow as many as you like.
          </Trans>
        ) : (
          <Trans>
            Here are some popular topical feeds. You can choose to follow as
            many as you like.
          </Trans>
        )}
      </Description>

      <View style={[a.w_full, a.pb_2xl, a.pt_2xl]}>
        <Toggle.Group
          values={selectedFeedUris}
          onChange={setSelectedFeedUris}
          label={_(msg`Select topical feeds to follow from the list below`)}>
          <View style={[a.gap_md]}>
            {suggestedFeedUris.map(uri => (
              <FeedCard key={uri} config={{default: false, uri}} />
            ))}
          </View>
        </Toggle.Group>
      </View>

      <OnboardingControls.Portal>
        <Button
          key={state.activeStep} // remove focus state on nav
          variant="gradient"
          color="gradient_sky"
          size="large"
          label={_(msg`Continue to next step`)}
          onPress={saveFeeds}>
          <ButtonText>
            <Trans>Continue</Trans>
          </ButtonText>
          <ButtonIcon icon={saving ? Loader : ChevronRight} position="right" />
        </Button>
      </OnboardingControls.Portal>
    </View>
  )
}