Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464import {memo, useMemo, useState} from 'react'import {type StyleProp, View, type ViewStyle} from 'react-native'import { type AppBskyFeedDefs, type AppBskyFeedPost, type AppBskyFeedThreadgate, type RichText as RichTextAPI,} from '@atproto/api'import {plural} from '@lingui/core/macro'import {useLingui} from '@lingui/react/macro'
import { getCommunitySpaceUri, isCommunityPostUri,} from '#/lib/api/community-post'import {isSpaceRecordUri} from '#/lib/api/space-uri'import {CountWheel} from '#/lib/custom-animations/CountWheel'import {AnimatedLikeIcon} from '#/lib/custom-animations/LikeIcon'import {useOpenComposer} from '#/lib/hooks/useOpenComposer'import {type Shadow} from '#/state/cache/types'import {useFeedFeedbackContext} from '#/state/feed-feedback'import { usePostLikeMutationQueue, usePostRepostMutationQueue,} from '#/state/queries/post'import {useRequireAuth} from '#/state/session'import { ProgressGuideAction, useProgressGuideControls,} from '#/state/shell/progress-guide'import {atoms as a, useBreakpoints, useTheme} from '#/alf'import {CommunityOnlyBadge} from '#/components/CommunityOnlyBadge'import {Reply as Bubble} from '#/components/icons/Reply'import {useFormatPostStatCount} from '#/components/PostControls/util'import * as Skele from '#/components/Skeleton'import * as Toast from '#/components/Toast'import {useAnalytics} from '#/analytics'import {BookmarkButton} from './BookmarkButton'import {HideButton} from './HideButton'import { PostControlButton, PostControlButtonIcon, PostControlButtonText,} from './PostControlButton'import {PostMenuButton} from './PostMenu'import {RepostButton} from './RepostButton'import {ShareMenuButton} from './ShareMenu'
let PostControls = ({ big, post, record, richText, feedContext, reqId, style, onPressReply, onPostReply, logContext, threadgateRecord, onShowLess, viaRepost, variant, forceGoogleTranslate: _forceGoogleTranslate = false,}: { big?: boolean post: Shadow<AppBskyFeedDefs.PostView> record: AppBskyFeedPost.Record richText: RichTextAPI feedContext?: string | undefined reqId?: string | undefined style?: StyleProp<ViewStyle> onPressReply: () => void onPostReply?: (postUri: string | undefined) => void logContext: 'FeedItem' | 'PostThreadItem' | 'Post' | 'ImmersiveVideo' threadgateRecord?: AppBskyFeedThreadgate.Record onShowLess?: (interaction: AppBskyFeedDefs.Interaction) => void viaRepost?: {uri: string; cid: string} variant?: 'compact' | 'normal' | 'large' forceGoogleTranslate?: boolean}): React.ReactNode => { const ax = useAnalytics() const t = useTheme() const {t: l} = useLingui() const {openComposer} = useOpenComposer() const {feedDescriptor} = useFeedFeedbackContext() const [queueLike, queueUnlike] = usePostLikeMutationQueue( post, viaRepost, feedDescriptor, logContext, ) const [queueRepost, queueUnrepost] = usePostRepostMutationQueue( post, viaRepost, feedDescriptor, logContext, ) const requireAuth = useRequireAuth() const {sendInteraction} = useFeedFeedbackContext() const {captureAction} = useProgressGuideControls() const isBlocked = Boolean( post.author.viewer?.blocking || post.author.viewer?.blockedBy || post.author.viewer?.blockingByList, ) const replyDisabled = post.viewer?.replyDisabled const {gtPhone} = useBreakpoints() const formatPostStatCount = useFormatPostStatCount()
const [hasLikeIconBeenToggled, setHasLikeIconBeenToggled] = useState(false)
const onPressToggleLike = async () => { if (isBlocked) { Toast.show(l`Cannot interact with a blocked user`, { type: 'warning', }) return }
try { setHasLikeIconBeenToggled(true) if (!post.viewer?.like) { sendInteraction({ item: post.uri, event: 'app.bsky.feed.defs#interactionLike', feedContext, reqId, }) captureAction(ProgressGuideAction.Like) await queueLike() } else { await queueUnlike() } } catch (err) { const e = err as Error if (e?.name !== 'AbortError') { throw e } } }
const onRepost = async () => { if (isBlocked) { Toast.show(l`Cannot interact with a blocked user`, { type: 'warning', }) return }
try { if (!post.viewer?.repost) { sendInteraction({ item: post.uri, event: 'app.bsky.feed.defs#interactionRepost', feedContext, reqId, }) await queueRepost() } else { await queueUnrepost() } } catch (err) { const e = err as Error if (e?.name !== 'AbortError') { throw e } } }
const onQuote = () => { if (isBlocked) { Toast.show(l`Cannot interact with a blocked user`, { type: 'warning', }) return }
sendInteraction({ item: post.uri, event: 'app.bsky.feed.defs#interactionQuote', feedContext, reqId, }) ax.metric('post:clickQuotePost', { uri: post.uri, authorDid: post.author.did, logContext, feedDescriptor, }) openComposer({ quote: post, onPost: onPostReply, logContext: 'QuotePost', }) }
const onShare = () => { sendInteraction({ item: post.uri, event: 'app.bsky.feed.defs#interactionShare', feedContext, reqId, }) }
const secondaryControlSpacingStyles = useSecondaryControlSpacingStyles({ variant, big, gtPhone, })
return ( <View style={[ a.flex_row, a.justify_between, a.align_center, !big && a.pt_2xs, a.gap_md, style, ]}> <View style={[a.flex_row, a.flex_1, {maxWidth: 320}]}> <View style={[ a.flex_1, a.align_start, {marginLeft: big ? -2 : -6}, replyDisabled ? {opacity: 0.6} : undefined, ]}> <PostControlButton testID="replyBtn" onPress={ !replyDisabled ? () => requireAuth(() => { ax.metric('post:clickReply', { uri: post.uri, authorDid: post.author.did, logContext, feedDescriptor, }) onPressReply() }) : undefined } label={l({ message: `Reply (${plural(post.replyCount || 0, { one: '# reply', other: '# replies', })})`, comment: 'Accessibility label for the reply button, verb form followed by number of replies and noun form', })} big={big}> <PostControlButtonIcon icon={Bubble} /> {typeof post.replyCount !== 'undefined' && post.replyCount > 0 && ( <PostControlButtonText> {formatPostStatCount(post.replyCount)} </PostControlButtonText> )} </PostControlButton> </View> <View style={[a.flex_1, a.align_start]}> <RepostButton isReposted={!!post.viewer?.repost} repostCount={(post.repostCount ?? 0) + (post.quoteCount ?? 0)} onRepost={() => void onRepost()} onQuote={onQuote} big={big} embeddingDisabled={Boolean(post.viewer?.embeddingDisabled)} repostDisabled={isSpaceRecordUri(post.uri)} /> </View> <View style={[a.flex_1, a.align_start]}> <PostControlButton testID="likeBtn" big={big} active={Boolean(post.viewer?.like)} activeColor={t.palette.pink} onPress={() => requireAuth(() => onPressToggleLike())} label={ post.viewer?.like ? l({ message: `Unlike (${plural(post.likeCount || 0, { one: '# like', other: '# likes', })})`, comment: 'Accessibility label for the like button when the post has been liked, verb followed by number of likes and noun', }) : l({ message: `Like (${plural(post.likeCount || 0, { one: '# like', other: '# likes', })})`, comment: 'Accessibility label for the like button when the post has not been liked, verb form followed by number of likes and noun form', }) }> <AnimatedLikeIcon isLiked={Boolean(post.viewer?.like)} big={big} hasBeenToggled={hasLikeIconBeenToggled} /> <CountWheel count={post.likeCount ?? 0} isToggled={Boolean(post.viewer?.like)} hasBeenToggled={hasLikeIconBeenToggled} renderCount={({count}) => ( <PostControlButtonText testID="likeCount"> {formatPostStatCount(count)} </PostControlButtonText> )} /> </PostControlButton> </View> {/* Spacer! */} <View /> </View> <View style={[ a.flex_row, a.justify_end, a.align_center, secondaryControlSpacingStyles, ]}> {isCommunityPostUri(post.uri) && ( <CommunityOnlyBadge communitySpace={getCommunitySpaceUri(post)} /> )} <HideButton post={post} big={big} logContext={logContext} hitSlop={{ right: secondaryControlSpacingStyles.gap / 2, }} /> {isCommunityPostUri(post.uri) ? null : ( <BookmarkButton post={post} big={big} logContext={logContext} hitSlop={{ left: secondaryControlSpacingStyles.gap / 2, right: secondaryControlSpacingStyles.gap / 2, }} /> )} <ShareMenuButton testID="postShareBtn" post={post} big={big} record={record} richText={richText} timestamp={post.indexedAt} threadgateRecord={threadgateRecord} onShare={onShare} hitSlop={{ left: secondaryControlSpacingStyles.gap / 2, right: secondaryControlSpacingStyles.gap / 2, }} logContext={logContext} /> <PostMenuButton testID="postDropdownBtn" post={post} postFeedContext={feedContext} postReqId={reqId} big={big} record={record} richText={richText} timestamp={post.indexedAt} threadgateRecord={threadgateRecord} onShowLess={onShowLess} hitSlop={{ left: secondaryControlSpacingStyles.gap / 2, }} logContext={logContext} /> </View> </View> )}PostControls = memo(PostControls)export {PostControls}
export function PostControlsSkeleton({ big, style, variant,}: { big?: boolean style?: StyleProp<ViewStyle> variant?: 'compact' | 'normal' | 'large'}) { const {gtPhone} = useBreakpoints()
const rowHeight = big ? 32 : 28 const padding = 4 const size = rowHeight - padding * 2
const secondaryControlSpacingStyles = useSecondaryControlSpacingStyles({ variant, big, gtPhone, })
const itemStyles = { padding, }
return ( <Skele.Row style={[a.flex_row, a.justify_between, a.align_center, a.gap_md, style]}> <View style={[a.flex_row, a.flex_1, {maxWidth: 320}]}> <View style={[itemStyles, a.flex_1, a.align_start, {marginLeft: -padding}]}> <Skele.Pill blend size={size} /> </View>
<View style={[itemStyles, a.flex_1, a.align_start]}> <Skele.Pill blend size={size} /> </View>
<View style={[itemStyles, a.flex_1, a.align_start]}> <Skele.Pill blend size={size} /> </View> </View> <View style={[a.flex_row, a.justify_end, secondaryControlSpacingStyles]}> <View style={itemStyles}> <Skele.Circle blend size={size} /> </View> <View style={itemStyles}> <Skele.Circle blend size={size} /> </View> <View style={itemStyles}> <Skele.Circle blend size={size} /> </View> <View style={itemStyles}> <Skele.Circle blend size={size} /> </View> </View> </Skele.Row> )}
function useSecondaryControlSpacingStyles({ variant, big, gtPhone,}: { variant?: 'compact' | 'normal' | 'large' big?: boolean gtPhone: boolean}) { return useMemo(() => { let gap = 0 // default, we want `gap` to be defined on the resulting object if (variant !== 'compact') gap = a.gap_xs.gap if (big || gtPhone) gap = a.gap_sm.gap return {gap} }, [variant, big, gtPhone])}