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 {AtUri} from '../../../third-party/uri' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {PostThreadViewPostModel} from '../../../state/models/post-thread-view' import {Link} from '../util/Link' import {RichText} from '../util/text/RichText' import {Text} from '../util/text/Text' import {PostDropdownBtn} from '../util/forms/DropdownButton' import * as Toast from '../util/Toast' import {UserAvatar} from '../util/UserAvatar' import {s} from '../../lib/styles' import {ago, pluralize} from '../../../lib/strings' import {useStores} from '../../../state' import {PostMeta} from '../util/PostMeta' import {PostEmbeds} from '../util/PostEmbeds' import {PostCtrls} from '../util/PostCtrls' import {ErrorMessage} from '../util/error/ErrorMessage' import {ComposePrompt} from '../composer/Prompt' import {usePalette} from '../../lib/hooks/usePalette' const PARENT_REPLY_LINE_LENGTH = 8 export const PostThreadItem = observer(function PostThreadItem({ item, onPostReply, }: { item: PostThreadViewPostModel onPostReply: () => void }) { const pal = usePalette('default') const store = useStores() const [deleted, setDeleted] = useState(false) const record = item.postRecord const hasEngagement = item.post.upvoteCount || item.post.repostCount 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 authorTitle = item.post.author.handle const upvotesHref = useMemo(() => { const urip = new AtUri(item.post.uri) return `/profile/${item.post.author.handle}/post/${urip.rkey}/upvoted-by` }, [item.post.uri, item.post.author.handle]) const upvotesTitle = 'Upvotes on this post' const repostsHref = useMemo(() => { const urip = new AtUri(item.post.uri) return `/profile/${item.post.author.handle}/post/${urip.rkey}/reposted-by` }, [item.post.uri, item.post.author.handle]) const repostsTitle = 'Reposts of this post' 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, }, }, onPost: onPostReply, }) } const onPressToggleRepost = () => { item .toggleRepost() .catch(e => store.log.error('Failed to toggle repost', e)) } const onPressToggleUpvote = () => { 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 = () => { 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) { return } if (deleted) { return ( This post has been deleted. ) } if (item._isHighlightedPost) { return ( <> {item.post.author.displayName || item.post.author.handle} · {ago(item.post.indexedAt)} @{item.post.author.handle} {record.text ? ( ) : undefined} {item._isHighlightedPost && hasEngagement ? ( {item.post.repostCount ? ( {item.post.repostCount} {' '} {pluralize(item.post.repostCount, 'repost')} ) : ( <> )} {item.post.upvoteCount ? ( {item.post.upvoteCount} {' '} {pluralize(item.post.upvoteCount, 'upvote')} ) : ( <> )} ) : ( <> )} ) } else { return ( <> {record.reply && ( )} {item.replies?.length !== 0 && ( )} {item.post.author.viewer?.muted ? ( This post is by a muted account. ) : record.text ? ( ) : ( )} {item._hasMore ? ( Load more ) : undefined} ) } }) const styles = StyleSheet.create({ outer: { borderTopWidth: 1, }, parentReplyLine: { position: 'absolute', left: 34, top: -1 * PARENT_REPLY_LINE_LENGTH + 6, height: PARENT_REPLY_LINE_LENGTH, borderLeftWidth: 2, }, childReplyLine: { position: 'absolute', left: 34, top: 65, bottom: 0, borderLeftWidth: 2, }, layout: { flexDirection: 'row', }, layoutAvi: { width: 70, paddingLeft: 10, paddingTop: 10, paddingBottom: 10, }, layoutContent: { flex: 1, paddingRight: 10, paddingTop: 10, paddingBottom: 10, }, meta: { flexDirection: 'row', paddingTop: 2, paddingBottom: 2, }, metaItem: { paddingRight: 5, maxWidth: 240, }, mutedWarning: { flexDirection: 'row', alignItems: 'center', padding: 10, marginTop: 2, marginBottom: 6, borderRadius: 2, }, postTextContainer: { flexDirection: 'row', alignItems: 'center', flexWrap: 'wrap', paddingBottom: 8, minHeight: 36, }, postTextLargeContainer: { paddingLeft: 4, paddingBottom: 20, }, expandedInfo: { flexDirection: 'row', padding: 10, borderTopWidth: 1, borderBottomWidth: 1, marginTop: 5, marginBottom: 15, }, expandedInfoItem: { marginRight: 10, }, loadMore: { borderTopWidth: 1, paddingLeft: 28, paddingVertical: 10, }, })