about summary refs log tree commit diff
path: root/src/view/screens/CustomFeed.tsx
blob: 615a63261fcc43b7ddcc129c38d4569460a28fda (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
import {NativeStackScreenProps} from '@react-navigation/native-stack'
import {usePalette} from 'lib/hooks/usePalette'
import {HeartIcon, HeartIconSolid} from 'lib/icons'
import {CommonNavigatorParams} from 'lib/routes/types'
import {colors, s} from 'lib/styles'
import {observer} from 'mobx-react-lite'
import React, {useMemo, useRef} from 'react'
import {FlatList, StyleSheet, TouchableOpacity, View} from 'react-native'
import {useStores} from 'state/index'
import {PostsFeedModel} from 'state/models/feeds/posts'
import {useCustomFeed} from 'view/com/algos/useCustomFeed'
import {withAuthRequired} from 'view/com/auth/withAuthRequired'
import {Feed} from 'view/com/posts/Feed'
import {Link} from 'view/com/util/Link'
import {UserAvatar} from 'view/com/util/UserAvatar'
import {ViewHeader} from 'view/com/util/ViewHeader'
import {Button} from 'view/com/util/forms/Button'
import {Text} from 'view/com/util/text/Text'

type Props = NativeStackScreenProps<CommonNavigatorParams, 'CustomFeed'>
export const CustomFeed = withAuthRequired(
  observer(({route}: Props) => {
    const rootStore = useStores()
    const {rkey, name} = route.params
    const currentFeed = useCustomFeed(rkey)
    const pal = usePalette('default')

    const scrollElRef = useRef<FlatList>(null)

    const algoFeed: PostsFeedModel = useMemo(() => {
      const feed = new PostsFeedModel(rootStore, 'custom', {
        feed: rkey,
      })
      feed.setup()
      return feed
    }, [rkey, rootStore])

    return (
      <View style={[styles.container]}>
        <View>
          <ViewHeader
            title={name ?? `${currentFeed?.data.creator.displayName}'s feed`}
            showOnDesktop
          />
          <View style={[styles.center]}>
            <View style={[styles.header]}>
              <View style={styles.avatarContainer}>
                <UserAvatar
                  size={30}
                  avatar={currentFeed?.data.creator.avatar}
                />
                <Link href={`/profile/${currentFeed?.data.creator.handle}`}>
                  <Text style={[pal.textLight]}>
                    @{currentFeed?.data.creator.handle}
                  </Text>
                </Link>
              </View>
              <Text style={[pal.text]}>{currentFeed?.data.description}</Text>
            </View>

            <View style={[styles.buttonsContainer]}>
              <Button
                type={currentFeed?.isSaved ? 'default' : 'inverted'}
                style={[styles.saveButton]}
                onPress={() => {
                  if (currentFeed?.data.viewer?.saved) {
                    rootStore.me.savedFeeds.unsave(currentFeed!)
                  } else {
                    rootStore.me.savedFeeds.save(currentFeed!)
                  }
                }}
                label={currentFeed?.data.viewer?.saved ? 'Unsave' : 'Save'}
              />

              <TouchableOpacity
                accessibilityRole="button"
                onPress={() => {
                  if (currentFeed?.isLiked) {
                    currentFeed?.unlike()
                  } else {
                    currentFeed?.like()
                  }
                }}
                style={[styles.likeButton, pal.viewLight]}>
                <Text style={[pal.text, s.semiBold]}>
                  {currentFeed?.data.likeCount}
                </Text>
                {currentFeed?.isLiked ? (
                  <HeartIconSolid size={18} style={styles.liked} />
                ) : (
                  <HeartIcon strokeWidth={3} size={18} style={styles.liked} />
                )}
              </TouchableOpacity>
            </View>
          </View>
        </View>

        <Feed
          scrollElRef={scrollElRef}
          testID={'test-feed'}
          key="default"
          feed={algoFeed}
          headerOffset={12}
        />
      </View>
    )
  }),
)

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingBottom: 12,
  },
  center: {alignItems: 'center', justifyContent: 'center', gap: 8},
  header: {
    alignItems: 'center',
    gap: 8,
  },
  avatarContainer: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: 8,
  },
  buttonsContainer: {
    flexDirection: 'row',
    gap: 8,
  },
  saveButton: {
    minWidth: 100,
    alignItems: 'center',
  },
  liked: {
    color: colors.red3,
  },
  notLiked: {
    color: colors.gray3,
  },
  likeButton: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    paddingVertical: 4,
    paddingHorizontal: 8,
    borderRadius: 24,
    gap: 4,
  },
})