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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
import React, {useMemo, useState} from 'react'
import {observer} from 'mobx-react-lite'
import {StyleSheet, View} from 'react-native'
import Clipboard from '@react-native-clipboard/clipboard'
import Svg, {Circle, Line} from 'react-native-svg'
import {AtUri} from '../../../third-party/uri'
import {
FontAwesomeIcon,
FontAwesomeIconStyle,
} from '@fortawesome/react-native-fontawesome'
import {FeedItemModel} from 'state/models/feed-view'
import {Link} from '../util/Link'
import {Text} from '../util/text/Text'
import {UserInfoText} from '../util/UserInfoText'
import {PostMeta} from '../util/PostMeta'
import {PostCtrls} from '../util/PostCtrls'
import {PostEmbeds} from '../util/PostEmbeds'
import {RichText} from '../util/text/RichText'
import * as Toast from '../util/Toast'
import {UserAvatar} from '../util/UserAvatar'
import {s} from 'lib/styles'
import {useStores} from 'state/index'
import {usePalette} from 'lib/hooks/usePalette'
import {useAnalytics} from 'lib/analytics'
export const FeedItem = observer(function ({
item,
showReplyLine,
ignoreMuteFor,
}: {
item: FeedItemModel
showReplyLine?: boolean
ignoreMuteFor?: string
}) {
const store = useStores()
const pal = usePalette('default')
const {track} = useAnalytics()
const [deleted, setDeleted] = useState(false)
const record = item.postRecord
const itemUri = item.post.uri
const itemCid = item.post.cid
const itemHref = useMemo(() => {
const urip = new AtUri(item.post.uri)
return `/profile/${item.post.author.handle}/post/${urip.rkey}`
}, [item.post.uri, item.post.author.handle])
const itemTitle = `Post by ${item.post.author.handle}`
const authorHref = `/profile/${item.post.author.handle}`
const replyAuthorDid = useMemo(() => {
if (!record?.reply) {
return ''
}
const urip = new AtUri(record.reply.parent?.uri || record.reply.root.uri)
return urip.hostname
}, [record?.reply])
const onPressReply = () => {
track('FeedItem:PostReply')
store.shell.openComposer({
replyTo: {
uri: item.post.uri,
cid: item.post.cid,
text: record?.text || '',
author: {
handle: item.post.author.handle,
displayName: item.post.author.displayName,
avatar: item.post.author.avatar,
},
},
})
}
const onPressToggleRepost = () => {
track('FeedItem:PostRepost')
return item
.toggleRepost()
.catch(e => store.log.error('Failed to toggle repost', e))
}
const onPressToggleUpvote = () => {
track('FeedItem:PostLike')
return item
.toggleUpvote()
.catch(e => store.log.error('Failed to toggle upvote', e))
}
const onCopyPostText = () => {
Clipboard.setString(record?.text || '')
Toast.show('Copied to clipboard')
}
const onDeletePost = () => {
track('FeedItem:PostDelete')
item.delete().then(
() => {
setDeleted(true)
Toast.show('Post deleted')
},
e => {
store.log.error('Failed to delete post', e)
Toast.show('Failed to delete post, please try again')
},
)
}
if (!record || deleted) {
return <View />
}
const isChild =
item._isThreadChild || (!item.reason && !item._hideParent && item.reply)
const isSmallTop = isChild && item._isThreadChild
const isNoTop = isChild && !item._isThreadChild
const outerStyles = [
styles.outer,
pal.view,
{borderColor: pal.colors.border},
isSmallTop ? styles.outerSmallTop : undefined,
isNoTop ? styles.outerNoTop : undefined,
item._isThreadParent ? styles.outerNoBottom : undefined,
]
return (
<>
{isChild && !item._isThreadChild && item.replyParent ? (
<FeedItem
item={item.replyParent}
showReplyLine
ignoreMuteFor={ignoreMuteFor}
/>
) : undefined}
<Link style={outerStyles} href={itemHref} title={itemTitle} noFeedback>
{item._isThreadChild && (
<View
style={[styles.topReplyLine, {borderColor: pal.colors.replyLine}]}
/>
)}
{(showReplyLine || item._isThreadParent) && (
<View
style={[
styles.bottomReplyLine,
{borderColor: pal.colors.replyLine},
isNoTop ? styles.bottomReplyLineNoTop : undefined,
]}
/>
)}
{item.reasonRepost && (
<Link
style={styles.includeReason}
href={`/profile/${item.reasonRepost.by.handle}`}
title={
item.reasonRepost.by.displayName || item.reasonRepost.by.handle
}>
<FontAwesomeIcon
icon="retweet"
style={[
styles.includeReasonIcon,
{color: pal.colors.textLight} as FontAwesomeIconStyle,
]}
/>
<Text type="sm-bold" style={pal.textLight} lineHeight={1.2}>
Reposted by{' '}
{item.reasonRepost.by.displayName || item.reasonRepost.by.handle}
</Text>
</Link>
)}
<View style={styles.layout}>
<View style={styles.layoutAvi}>
<Link href={authorHref} title={item.post.author.handle}>
<UserAvatar
size={52}
displayName={item.post.author.displayName}
handle={item.post.author.handle}
avatar={item.post.author.avatar}
/>
</Link>
</View>
<View style={styles.layoutContent}>
<PostMeta
authorHandle={item.post.author.handle}
authorDisplayName={item.post.author.displayName}
timestamp={item.post.indexedAt}
/>
{!isChild && replyAuthorDid !== '' && (
<View style={[s.flexRow, s.mb2, s.alignCenter]}>
<FontAwesomeIcon
icon="reply"
size={9}
style={[
{color: pal.colors.textLight} as FontAwesomeIconStyle,
s.mr5,
]}
/>
<Text type="md" style={[pal.textLight, s.mr2]} lineHeight={1.2}>
Reply to
</Text>
<UserInfoText
type="md"
did={replyAuthorDid}
attr="displayName"
style={[pal.textLight, s.ml2]}
/>
</View>
)}
{item.post.author.viewer?.muted &&
ignoreMuteFor !== item.post.author.did ? (
<View style={[styles.mutedWarning, pal.btn]}>
<FontAwesomeIcon icon={['far', 'eye-slash']} style={s.mr2} />
<Text type="sm">This post is by a muted account.</Text>
</View>
) : item.richText?.text ? (
<View style={styles.postTextContainer}>
<RichText
type="post-text"
richText={item.richText}
lineHeight={1.3}
/>
</View>
) : undefined}
{item.post.embed ? (
<PostEmbeds embed={item.post.embed} style={styles.embed} />
) : null}
<PostCtrls
style={styles.ctrls}
itemUri={itemUri}
itemCid={itemCid}
itemHref={itemHref}
itemTitle={itemTitle}
isAuthor={item.post.author.did === store.me.did}
replyCount={item.post.replyCount}
repostCount={item.post.repostCount}
upvoteCount={item.post.upvoteCount}
isReposted={!!item.post.viewer.repost}
isUpvoted={!!item.post.viewer.upvote}
onPressReply={onPressReply}
onPressToggleRepost={onPressToggleRepost}
onPressToggleUpvote={onPressToggleUpvote}
onCopyPostText={onCopyPostText}
onDeletePost={onDeletePost}
/>
</View>
</View>
</Link>
{item._isThreadChildElided ? (
<Link
style={[pal.view, styles.viewFullThread]}
href={itemHref}
title={itemTitle}
noFeedback>
<View style={styles.viewFullThreadDots}>
<Svg width="4" height="30">
<Line
x1="2"
y1="0"
x2="2"
y2="8"
stroke={pal.colors.replyLine}
strokeWidth="2"
/>
<Circle x="2" y="14" r="1.5" fill={pal.colors.replyLineDot} />
<Circle x="2" y="20" r="1.5" fill={pal.colors.replyLineDot} />
<Circle x="2" y="26" r="1.5" fill={pal.colors.replyLineDot} />
</Svg>
</View>
<Text type="md" style={pal.link}>
View full thread
</Text>
</Link>
) : undefined}
</>
)
})
const styles = StyleSheet.create({
outer: {
borderTopWidth: 1,
padding: 10,
paddingRight: 15,
paddingBottom: 8,
},
outerNoTop: {
borderTopWidth: 0,
paddingTop: 0,
},
outerSmallTop: {
borderTopWidth: 0,
},
outerNoBottom: {
paddingBottom: 2,
},
topReplyLine: {
position: 'absolute',
left: 42,
top: 0,
height: 6,
borderLeftWidth: 2,
},
bottomReplyLine: {
position: 'absolute',
left: 42,
top: 72,
bottom: 0,
borderLeftWidth: 2,
},
bottomReplyLineNoTop: {top: 64},
includeReason: {
flexDirection: 'row',
paddingLeft: 50,
marginTop: 2,
marginBottom: 2,
},
includeReasonIcon: {
marginRight: 4,
},
layout: {
flexDirection: 'row',
marginTop: 1,
},
layoutAvi: {
width: 70,
paddingLeft: 8,
},
layoutContent: {
flex: 1,
},
mutedWarning: {
flexDirection: 'row',
alignItems: 'center',
padding: 10,
marginTop: 2,
marginBottom: 6,
borderRadius: 2,
},
postTextContainer: {
flexDirection: 'row',
alignItems: 'center',
flexWrap: 'wrap',
paddingBottom: 4,
},
embed: {
marginBottom: 6,
},
ctrls: {
marginTop: 4,
},
viewFullThread: {
paddingTop: 12,
paddingBottom: 2,
paddingLeft: 80,
},
viewFullThreadDots: {
position: 'absolute',
left: 41,
top: 0,
},
})
|