about summary refs log tree commit diff
path: root/src/view/shell/desktop/Feeds.tsx
blob: 441b35e3b56717a10628467c4abc39c4faf7703e (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
import {View} from 'react-native'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useNavigation, useNavigationState} from '@react-navigation/native'

import {getCurrentRoute} from '#/lib/routes/helpers'
import {type NavigationProp} from '#/lib/routes/types'
import {emitSoftReset} from '#/state/events'
import {usePinnedFeedsInfos} from '#/state/queries/feed'
import {useSelectedFeed, useSetSelectedFeed} from '#/state/shell/selected-feed'
import {atoms as a, useTheme, web} from '#/alf'
import {createStaticClick, InlineLinkText} from '#/components/Link'

export function DesktopFeeds() {
  const t = useTheme()
  const {_} = useLingui()
  const {data: pinnedFeedInfos, error, isLoading} = usePinnedFeedsInfos()
  const selectedFeed = useSelectedFeed()
  const setSelectedFeed = useSetSelectedFeed()
  const navigation = useNavigation<NavigationProp>()
  const route = useNavigationState(state => {
    if (!state) {
      return {name: 'Home'}
    }
    return getCurrentRoute(state)
  })

  if (isLoading) {
    return (
      <View
        style={[
          {
            gap: 10,
            paddingVertical: 2,
          },
        ]}>
        {Array(5)
          .fill(0)
          .map((_, i) => (
            <View
              key={i}
              style={[
                a.rounded_sm,
                t.atoms.bg_contrast_25,
                {
                  height: 16,
                  width: i % 2 === 0 ? '60%' : '80%',
                },
              ]}
            />
          ))}
      </View>
    )
  }

  if (error || !pinnedFeedInfos) {
    return null
  }

  return (
    <View
      style={[
        web({
          gap: 10,
          /*
           * Small padding prevents overflow prior to actually overflowing the
           * height of the screen with lots of feeds.
           */
          paddingVertical: 2,
          marginHorizontal: -2,
          overflowY: 'auto',
        }),
      ]}>
      {pinnedFeedInfos.map(feedInfo => {
        const feed = feedInfo.feedDescriptor
        const current = route.name === 'Home' && feed === selectedFeed

        return (
          <InlineLinkText
            key={feedInfo.uri}
            label={feedInfo.displayName}
            {...createStaticClick(() => {
              setSelectedFeed(feed)
              navigation.navigate('Home')
              if (route.name === 'Home' && feed === selectedFeed) {
                emitSoftReset()
              }
            })}
            style={[
              a.text_md,
              a.leading_snug,
              current
                ? [a.font_bold, t.atoms.text]
                : [t.atoms.text_contrast_medium],
              web({
                marginHorizontal: 2,
                width: 'calc(100% - 4px)',
              }),
            ]}
            numberOfLines={1}>
            {feedInfo.displayName}
          </InlineLinkText>
        )
      })}

      <InlineLinkText
        to="/feeds"
        label={_(msg`More feeds`)}
        style={[
          a.text_md,
          a.leading_snug,
          web({
            marginHorizontal: 2,
            width: 'calc(100% - 4px)',
          }),
        ]}
        numberOfLines={1}>
        {_(msg`More feeds`)}
      </InlineLinkText>
    </View>
  )
}