import React, {memo, useCallback} from 'react' import { StyleProp, StyleSheet, TouchableOpacity, View, ViewStyle, } from 'react-native' import { AppBskyFeedDefs, AppBskyFeedPost, AtUri, RichText as RichTextAPI, } from '@atproto/api' import {msg} from '@lingui/macro' import {useLingui} from '@lingui/react' import {HITSLOP_10, HITSLOP_20} from '#/lib/constants' import {Haptics} from '#/lib/haptics' import {CommentBottomArrow, HeartIcon, HeartIconSolid} from '#/lib/icons' import {makeProfileLink} from '#/lib/routes/links' import {shareUrl} from '#/lib/sharing' import {pluralize} from '#/lib/strings/helpers' import {toShareUrl} from '#/lib/strings/url-helpers' import {s} from '#/lib/styles' import {useTheme} from '#/lib/ThemeContext' import {Shadow} from '#/state/cache/types' import {useModalControls} from '#/state/modals' import { usePostLikeMutationQueue, usePostRepostMutationQueue, } from '#/state/queries/post' import {useRequireAuth} from '#/state/session' import {useComposerControls} from '#/state/shell/composer' import {useDialogControl} from '#/components/Dialog' import {ArrowOutOfBox_Stroke2_Corner0_Rounded as ArrowOutOfBox} from '#/components/icons/ArrowOutOfBox' import * as Prompt from '#/components/Prompt' import {PostDropdownBtn} from '../forms/PostDropdownBtn' import {Text} from '../text/Text' import {RepostButton} from './RepostButton' let PostCtrls = ({ big, post, record, richText, style, onPressReply, logContext, }: { big?: boolean post: Shadow record: AppBskyFeedPost.Record richText: RichTextAPI style?: StyleProp onPressReply: () => void logContext: 'FeedItem' | 'PostThreadItem' | 'Post' }): React.ReactNode => { const theme = useTheme() const {_} = useLingui() const {openComposer} = useComposerControls() const {closeModal} = useModalControls() const [queueLike, queueUnlike] = usePostLikeMutationQueue(post, logContext) const [queueRepost, queueUnrepost] = usePostRepostMutationQueue( post, logContext, ) const requireAuth = useRequireAuth() const loggedOutWarningPromptControl = useDialogControl() const shouldShowLoggedOutWarning = React.useMemo(() => { return !!post.author.labels?.find( label => label.val === '!no-unauthenticated', ) }, [post]) const defaultCtrlColor = React.useMemo( () => ({ color: theme.palette.default.postCtrl, }), [theme], ) as StyleProp const onPressToggleLike = React.useCallback(async () => { try { if (!post.viewer?.like) { Haptics.default() await queueLike() } else { await queueUnlike() } } catch (e: any) { if (e?.name !== 'AbortError') { throw e } } }, [post.viewer?.like, queueLike, queueUnlike]) const onRepost = useCallback(async () => { closeModal() try { if (!post.viewer?.repost) { Haptics.default() await queueRepost() } else { await queueUnrepost() } } catch (e: any) { if (e?.name !== 'AbortError') { throw e } } }, [post.viewer?.repost, queueRepost, queueUnrepost, closeModal]) const onQuote = useCallback(() => { closeModal() openComposer({ quote: { uri: post.uri, cid: post.cid, text: record.text, author: post.author, indexedAt: post.indexedAt, }, }) Haptics.default() }, [ post.uri, post.cid, post.author, post.indexedAt, record.text, openComposer, closeModal, ]) const onShare = useCallback(() => { const urip = new AtUri(post.uri) const href = makeProfileLink(post.author, 'post', urip.rkey) const url = toShareUrl(href) shareUrl(url) }, [post.uri, post.author]) return ( { if (!post.viewer?.replyDisabled) { requireAuth(() => onPressReply()) } }} accessibilityRole="button" accessibilityLabel={`Reply (${post.replyCount} ${ post.replyCount === 1 ? 'reply' : 'replies' })`} accessibilityHint="" hitSlop={big ? HITSLOP_20 : HITSLOP_10}> {typeof post.replyCount !== 'undefined' && post.replyCount > 0 ? ( {post.replyCount} ) : undefined} { requireAuth(() => onPressToggleLike()) }} accessibilityRole="button" accessibilityLabel={`${ post.viewer?.like ? _(msg`Unlike`) : _(msg`Like`) } (${post.likeCount} ${pluralize(post.likeCount || 0, 'like')})`} accessibilityHint="" hitSlop={big ? HITSLOP_20 : HITSLOP_10}> {post.viewer?.like ? ( ) : ( )} {typeof post.likeCount !== 'undefined' && post.likeCount > 0 ? ( {post.likeCount} ) : undefined} {big && ( <> { if (shouldShowLoggedOutWarning) { loggedOutWarningPromptControl.open() } else { onShare() } }} accessibilityRole="button" accessibilityLabel={`${_(msg`Share`)}`} accessibilityHint="" hitSlop={big ? HITSLOP_20 : HITSLOP_10}> )} ) } PostCtrls = memo(PostCtrls) export {PostCtrls} const styles = StyleSheet.create({ ctrls: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', }, ctrl: { flex: 1, alignItems: 'flex-start', }, ctrlBig: { alignItems: 'center', }, btn: { flexDirection: 'row', alignItems: 'center', }, btnPad: { paddingTop: 5, paddingBottom: 5, paddingLeft: 5, paddingRight: 5, }, mt1: { marginTop: 1, }, })