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 {AppBskyFeedPost} from '@atproto/api'
import {FontAwesomeIcon} 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, colors} from '../../lib/styles'
import {useStores} from '../../../state'
export const FeedItem = observer(function ({
item,
showReplyLine,
}: {
item: FeedItemModel
showReplyLine?: boolean
}) {
const store = useStores()
const [deleted, setDeleted] = useState(false)
const record = item.post.record as unknown as AppBskyFeedPost.Record
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 replyHref = useMemo(() => {
if (!record.reply) return ''
const urip = new AtUri(record.reply.parent?.uri || record.reply.root.uri)
return `/profile/${urip.hostname}/post/${urip.rkey}`
}, [record.reply])
const onPressReply = () => {
store.shell.openComposer({
replyTo: {
uri: item.post.uri,
cid: item.post.cid,
text: record.text as string,
author: {
handle: item.post.author.handle,
displayName: item.post.author.displayName,
avatar: item.post.author.avatar,
},
},
})
}
const onPressToggleRepost = () => {
item
.toggleRepost()
.catch(e => console.error('Failed to toggle repost', record, e))
}
const onPressToggleUpvote = () => {
item
.toggleUpvote()
.catch(e => console.error('Failed to toggle upvote', record, e))
}
const onCopyPostText = () => {
Clipboard.setString(record.text)
Toast.show('Copied to clipboard')
}
const onDeletePost = () => {
item.delete().then(
() => {
setDeleted(true)
Toast.show('Post deleted')
},
e => {
console.error(e)
Toast.show('Failed to delete post, please try again')
},
)
}
if (deleted) {
return
}
const isChild = item._isThreadChild || (!item.reason && item.reply)
const isSmallTop = isChild && item._isThreadChild
const isNoTop = isChild && !item._isThreadChild
const outerStyles = [
styles.outer,
isSmallTop ? styles.outerSmallTop : undefined,
isNoTop ? styles.outerNoTop : undefined,
item._isThreadParent ? styles.outerNoBottom : undefined,
]
return (
<>
{isChild && !item._isThreadChild && item.replyParent ? (
) : undefined}
{item._isThreadChild && }
{(showReplyLine || item._isThreadParent) && (
)}
{item.reasonRepost && (
Reposted by{' '}
{item.reasonRepost.by.displayName || item.reasonRepost.by.handle}
)}
{item.reasonTrend && (
Trending with{' '}
{item.reasonTrend.by.displayName || item.reasonTrend.by.handle}
)}
{!isChild && replyHref !== '' && (
Reply to
)}
{record.text ? (
) : (
)}
{item._isThreadChildElided ? (
View full thread
) : undefined}
>
)
})
const styles = StyleSheet.create({
outer: {
borderRadius: 6,
margin: 2,
marginBottom: 0,
backgroundColor: colors.white,
padding: 10,
},
outerNoTop: {
marginTop: 0,
paddingTop: 0,
borderTopLeftRadius: 0,
borderTopRightRadius: 0,
},
outerSmallTop: {
marginTop: 0,
paddingTop: 8,
borderTopLeftRadius: 0,
borderTopRightRadius: 0,
},
outerNoBottom: {
marginBottom: 0,
paddingBottom: 0,
borderBottomLeftRadius: 0,
borderBottomRightRadius: 0,
},
topReplyLine: {
position: 'absolute',
left: 34,
top: 0,
height: 6,
borderLeftWidth: 2,
borderLeftColor: colors.gray2,
},
bottomReplyLine: {
position: 'absolute',
left: 34,
top: 72,
bottom: 0,
borderLeftWidth: 2,
borderLeftColor: colors.gray2,
},
includeReason: {
flexDirection: 'row',
paddingLeft: 60,
},
includeReasonIcon: {
marginRight: 4,
color: colors.gray4,
},
layout: {
flexDirection: 'row',
},
layoutAvi: {
width: 60,
paddingTop: 5,
},
layoutContent: {
flex: 1,
},
postTextContainer: {
flexDirection: 'row',
alignItems: 'center',
flexWrap: 'wrap',
paddingBottom: 8,
},
postText: {
fontFamily: 'System',
fontSize: 16,
lineHeight: 20.8, // 1.3 of 16px
color: colors.black,
},
postEmbeds: {
marginBottom: 10,
},
viewFullThread: {
backgroundColor: colors.white,
paddingTop: 12,
paddingBottom: 4,
paddingLeft: 72,
},
viewFullThreadDots: {
position: 'absolute',
left: 35,
top: 0,
},
viewFullThreadText: {
color: colors.blue3,
fontSize: 16,
},
})