about summary refs log tree commit diff
path: root/src/view/com/util/post-ctrls/PostCtrls.tsx
blob: e548c45f7a1590ef5ca6b698b501a7de826f1671 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import React, {useCallback} from 'react'
import {
  StyleProp,
  StyleSheet,
  TouchableOpacity,
  View,
  ViewStyle,
} from 'react-native'
import {AppBskyFeedDefs, AppBskyFeedPost} from '@atproto/api'
import {Text} from '../text/Text'
import {PostDropdownBtn} from '../forms/PostDropdownBtn'
import {HeartIcon, HeartIconSolid, CommentBottomArrow} from 'lib/icons'
import {s, colors} from 'lib/styles'
import {pluralize} from 'lib/strings/helpers'
import {useTheme} from 'lib/ThemeContext'
import {RepostButton} from './RepostButton'
import {Haptics} from 'lib/haptics'
import {HITSLOP_10, HITSLOP_20} from 'lib/constants'
import {useModalControls} from '#/state/modals'
import {
  usePostLikeMutation,
  usePostUnlikeMutation,
  usePostRepostMutation,
  usePostUnrepostMutation,
} from '#/state/queries/post'
import {useComposerControls} from '#/state/shell/composer'
import {Shadow} from '#/state/cache/types'
import {useRequireAuth} from '#/state/session'

export function PostCtrls({
  big,
  post,
  record,
  style,
  onPressReply,
}: {
  big?: boolean
  post: Shadow<AppBskyFeedDefs.PostView>
  record: AppBskyFeedPost.Record
  style?: StyleProp<ViewStyle>
  onPressReply: () => void
}) {
  const theme = useTheme()
  const {openComposer} = useComposerControls()
  const {closeModal} = useModalControls()
  const postLikeMutation = usePostLikeMutation()
  const postUnlikeMutation = usePostUnlikeMutation()
  const postRepostMutation = usePostRepostMutation()
  const postUnrepostMutation = usePostUnrepostMutation()
  const requireAuth = useRequireAuth()

  const defaultCtrlColor = React.useMemo(
    () => ({
      color: theme.palette.default.postCtrl,
    }),
    [theme],
  ) as StyleProp<ViewStyle>

  const onPressToggleLike = React.useCallback(async () => {
    if (!post.viewer?.like) {
      Haptics.default()
      postLikeMutation.mutate({
        uri: post.uri,
        cid: post.cid,
        likeCount: post.likeCount || 0,
      })
    } else {
      postUnlikeMutation.mutate({
        postUri: post.uri,
        likeUri: post.viewer.like,
        likeCount: post.likeCount || 0,
      })
    }
  }, [post, postLikeMutation, postUnlikeMutation])

  const onRepost = useCallback(() => {
    closeModal()
    if (!post.viewer?.repost) {
      Haptics.default()
      postRepostMutation.mutate({
        uri: post.uri,
        cid: post.cid,
        repostCount: post.repostCount || 0,
      })
    } else {
      postUnrepostMutation.mutate({
        postUri: post.uri,
        repostUri: post.viewer.repost,
        repostCount: post.repostCount || 0,
      })
    }
  }, [post, closeModal, postRepostMutation, postUnrepostMutation])

  const onQuote = useCallback(() => {
    closeModal()
    openComposer({
      quote: {
        uri: post.uri,
        cid: post.cid,
        text: record.text,
        author: post.author,
        indexedAt: post.indexedAt,
      },
    })
    Haptics.default()
  }, [post, record, openComposer, closeModal])
  return (
    <View style={[styles.ctrls, style]}>
      <TouchableOpacity
        testID="replyBtn"
        style={[styles.ctrl, !big && styles.ctrlPad, {paddingLeft: 0}]}
        onPress={() => {
          requireAuth(() => onPressReply())
        }}
        accessibilityRole="button"
        accessibilityLabel={`Reply (${post.replyCount} ${
          post.replyCount === 1 ? 'reply' : 'replies'
        })`}
        accessibilityHint=""
        hitSlop={big ? HITSLOP_20 : HITSLOP_10}>
        <CommentBottomArrow
          style={[defaultCtrlColor, big ? s.mt2 : styles.mt1]}
          strokeWidth={3}
          size={big ? 20 : 15}
        />
        {typeof post.replyCount !== 'undefined' ? (
          <Text style={[defaultCtrlColor, s.ml5, s.f15]}>
            {post.replyCount}
          </Text>
        ) : undefined}
      </TouchableOpacity>
      <RepostButton
        big={big}
        isReposted={!!post.viewer?.repost}
        repostCount={post.repostCount}
        onRepost={onRepost}
        onQuote={onQuote}
      />
      <TouchableOpacity
        testID="likeBtn"
        style={[styles.ctrl, !big && styles.ctrlPad]}
        onPress={() => {
          requireAuth(() => onPressToggleLike())
        }}
        accessibilityRole="button"
        accessibilityLabel={`${post.viewer?.like ? 'Unlike' : 'Like'} (${
          post.likeCount
        } ${pluralize(post.likeCount || 0, 'like')})`}
        accessibilityHint=""
        hitSlop={big ? HITSLOP_20 : HITSLOP_10}>
        {post.viewer?.like ? (
          <HeartIconSolid style={styles.ctrlIconLiked} size={big ? 22 : 16} />
        ) : (
          <HeartIcon
            style={[defaultCtrlColor, big ? styles.mt1 : undefined]}
            strokeWidth={3}
            size={big ? 20 : 16}
          />
        )}
        {typeof post.likeCount !== 'undefined' ? (
          <Text
            testID="likeCount"
            style={
              post.viewer?.like
                ? [s.bold, s.red3, s.f15, s.ml5]
                : [defaultCtrlColor, s.f15, s.ml5]
            }>
            {post.likeCount}
          </Text>
        ) : undefined}
      </TouchableOpacity>
      {big ? undefined : (
        <PostDropdownBtn
          testID="postDropdownBtn"
          post={post}
          record={record}
          style={styles.ctrlPad}
        />
      )}
      {/* used for adding pad to the right side */}
      <View />
    </View>
  )
}

const styles = StyleSheet.create({
  ctrls: {
    flexDirection: 'row',
    justifyContent: 'space-between',
  },
  ctrl: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  ctrlPad: {
    paddingTop: 5,
    paddingBottom: 5,
    paddingLeft: 5,
    paddingRight: 5,
  },
  ctrlIconLiked: {
    color: colors.like,
  },
  mt1: {
    marginTop: 1,
  },
})