Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728import {memo, useCallback, useMemo} from 'react'import {Text as RNText, View} from 'react-native'import {RichText as RichTextAPI} from '@bsky/sdk/richtext'import {Plural, Trans, useLingui} from '@lingui/react/macro'
import {getCommunitySpaceUri} from '#/lib/api/community-post'import {postUriAuthor} from '#/lib/api/space-permalink'import {isSpaceRecordUri} from '#/lib/api/space-uri'import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'import {useOpenComposer} from '#/lib/hooks/useOpenComposer'import {makeProfileLink, postPermalink} from '#/lib/routes/links'import {sanitizeDisplayName} from '#/lib/strings/display-names'import {sanitizeHandle} from '#/lib/strings/handles'import {niceDate} from '#/lib/strings/time'import { POST_TOMBSTONE, type Shadow, usePostShadow,} from '#/state/cache/post-shadow'import {useProfileShadow} from '#/state/cache/profile-shadow'import {FeedFeedbackProvider, useFeedFeedback} from '#/state/feed-feedback'import {type ThreadItem} from '#/state/queries/usePostThread/types'import {useSession} from '#/state/session'import {type OnPostSuccessData} from '#/state/shell/composer'import {useMergedThreadgateHiddenReplies} from '#/state/threadgate-hidden-replies'import {type PostSource} from '#/state/unstable-post-source'import {PreviewableUserAvatar} from '#/view/com/util/UserAvatar'import {LikesStat} from '#/screens/PostThread/components/LikesStat'import {ThreadItemAnchorFollowButton} from '#/screens/PostThread/components/ThreadItemAnchorFollowButton'import { POST_NUMBER_INLINE_OFFSET, ThreadItemPostNumber, useHasThreadItemPostNumber,} from '#/screens/PostThread/components/ThreadItemPostNumber'import { LINEAR_AVI_WIDTH, OUTER_SPACE, REPLY_LINE_WIDTH,} from '#/screens/PostThread/const'import {atoms as a, useTheme} from '#/alf'import {Button} from '#/components/Button'import {DebugFieldDisplay} from '#/components/DebugFieldDisplay'import {CalendarClock_Stroke2_Corner0_Rounded as CalendarClockIcon} from '#/components/icons/CalendarClock'import {Trash_Stroke2_Corner0_Rounded as TrashIcon} from '#/components/icons/Trash'import {GalleryBleed} from '#/components/images/Gallery'import {Link} from '#/components/Link'import {ContentHider} from '#/components/moderation/ContentHider'import {PostAlerts} from '#/components/moderation/PostAlerts'import * as ReportDialogMetadataContext from '#/components/moderation/ReportDialog/ReportDialogMetadataContext'import {type AppModerationCause} from '#/components/Pills'import {Embed, PostEmbedViewContext} from '#/components/Post/Embed'import {KnownLikers} from '#/components/Post/KnownLikers'import {TranslatedPost} from '#/components/Post/Translated'import {PostControls, PostControlsSkeleton} from '#/components/PostControls'import {useFormatPostStatCount} from '#/components/PostControls/util'import {ProfileBadges} from '#/components/ProfileBadges'import {ProfileHoverCard} from '#/components/ProfileHoverCard'import * as Prompt from '#/components/Prompt'import {RichText} from '#/components/RichText'import * as Skele from '#/components/Skeleton'import {Text} from '#/components/Typography'import {WhoCanReply} from '#/components/WhoCanReply'import {Features, useAnalytics} from '#/analytics'import {useActorStatus} from '#/features/liveNow'import {app} from '#/lexicons'import * as bsky from '#/types/bsky'
export function ThreadItemAnchor({ item, onPostSuccess, threadgateRecord, postSource,}: { item: Extract<ThreadItem, {type: 'threadPost'}> onPostSuccess?: (data: OnPostSuccessData) => void threadgateRecord?: app.bsky.feed.threadgate.Main postSource?: PostSource}) { const postShadow = usePostShadow(item.value.post) const threadRootUri = item.value.post.record.reply?.root?.uri || item.uri const isRoot = threadRootUri === item.uri
if (postShadow === POST_TOMBSTONE) { return <ThreadItemAnchorDeleted isRoot={isRoot} /> }
return ( <ReportDialogMetadataContext.Provider key={postShadow.uri}> <ThreadItemAnchorInner item={item} isRoot={isRoot} postShadow={postShadow} onPostSuccess={onPostSuccess} threadgateRecord={threadgateRecord} postSource={postSource} /> </ReportDialogMetadataContext.Provider> )}
function ThreadItemAnchorDeleted({isRoot}: {isRoot: boolean}) { const t = useTheme()
return ( <> <ThreadItemAnchorParentReplyLine isRoot={isRoot} />
<View style={[ { paddingHorizontal: OUTER_SPACE, paddingBottom: OUTER_SPACE, }, isRoot && [a.pt_lg], ]}> <View style={[ a.flex_row, a.align_center, a.py_md, a.rounded_sm, t.atoms.bg_contrast_25, ]}> <View style={[ a.flex_row, a.align_center, a.justify_center, { width: LINEAR_AVI_WIDTH, }, ]}> <TrashIcon style={[t.atoms.text_contrast_medium]} /> </View> <Text style={[a.text_md, a.font_semi_bold, t.atoms.text_contrast_medium]}> <Trans>Post has been deleted</Trans> </Text> </View> </View> </> )}
function ThreadItemAnchorParentReplyLine({isRoot}: {isRoot: boolean}) { const t = useTheme()
return !isRoot ? ( <View style={[a.pl_lg, a.flex_row, a.pb_xs, {height: a.pt_lg.paddingTop}]}> <View style={{width: 42}}> <View style={[ { width: REPLY_LINE_WIDTH, marginLeft: 'auto', marginRight: 'auto', flexGrow: 1, backgroundColor: t.atoms.border_contrast_low.borderColor, }, ]} /> </View> </View> ) : null}
const ThreadItemAnchorInner = memo(function ThreadItemAnchorInner({ item, isRoot, postShadow, onPostSuccess, threadgateRecord, postSource,}: { item: Extract<ThreadItem, {type: 'threadPost'}> isRoot: boolean postShadow: Shadow<app.bsky.feed.defs.PostView> onPostSuccess?: (data: OnPostSuccessData) => void threadgateRecord?: app.bsky.feed.threadgate.Main postSource?: PostSource}) { const t = useTheme() const ax = useAnalytics() const {t: l} = useLingui() const {openComposer} = useOpenComposer() const {currentAccount, hasSession} = useSession() const feedFeedback = useFeedFeedback(postSource?.feedSourceInfo, hasSession) const formatPostStatCount = useFormatPostStatCount()
const post = postShadow const record = item.value.post.record const postNumbering = item.value const showPostNumber = useHasThreadItemPostNumber(postNumbering) const moderation = item.moderation const authorShadow = useProfileShadow(post.author) const {isActive: live} = useActorStatus(post.author) const richText = useMemo( () => new RichTextAPI({ text: record.text, facets: record.facets, }), [record], )
const authorHref = makeProfileLink(post.author) const isThreadAuthor = getThreadAuthor(post, record) === currentAccount?.did
const statHref = useCallback( (suffix: string) => { // Space likes and quotes have permission-aware endpoints. Reposts remain // unsupported, so their count deliberately has no destination. if (isSpaceRecordUri(post.uri) && suffix === 'reposted-by') { return undefined } return postPermalink(post.author, post.uri, suffix) }, [post.uri, post.author], ) const likesHref = useMemo(() => statHref('liked-by'), [statHref]) const repostsHref = useMemo(() => statHref('reposted-by'), [statHref]) const quotesHref = useMemo(() => statHref('quotes'), [statHref])
const threadgateHiddenReplies = useMergedThreadgateHiddenReplies({ threadgateRecord, }) const additionalPostAlerts: AppModerationCause[] = useMemo(() => { const isPostHiddenByThreadgate = threadgateHiddenReplies.has(post.uri) const isControlledByViewer = isThreadAuthor return isControlledByViewer && isPostHiddenByThreadgate ? [ { type: 'reply-hidden', source: {type: 'user', did: currentAccount?.did}, priority: 6, }, ] : [] }, [post, currentAccount?.did, isThreadAuthor, threadgateHiddenReplies]) const onlyFollowersCanReply = !!threadgateRecord?.allow?.find( rule => rule.$type === 'app.bsky.feed.threadgate#followerRule', ) const showFollowButton = currentAccount?.did !== post.author.did && !onlyFollowersCanReply
const viaRepost = useMemo(() => { const reason = postSource?.post.reason
if ( bsky.isType(app.bsky.feed.defs.reasonRepost, reason) && reason.uri && reason.cid ) { return { uri: reason.uri, cid: reason.cid, } } }, [postSource])
const onPressReply = useNonReactiveCallback(() => { openComposer({ replyTo: { uri: post.uri, cid: post.cid, text: record.text, author: post.author, embed: post.embed, moderation, langs: record.langs, communitySpace: getCommunitySpaceUri(post), }, onPostSuccess: onPostSuccess, logContext: 'PostReply', })
if (postSource) { feedFeedback.sendInteraction({ item: post.uri, event: 'app.bsky.feed.defs#interactionReply', feedContext: postSource.post.feedContext, reqId: postSource.post.reqId, }) } })
const onOpenAuthor = () => { ax.metric('post:clickthroughAuthor', { uri: post.uri, authorDid: post.author.did, logContext: 'PostThreadItem', feedDescriptor: feedFeedback.feedDescriptor, }) if (postSource) { feedFeedback.sendInteraction({ item: post.uri, event: 'app.bsky.feed.defs#clickthroughAuthor', feedContext: postSource.post.feedContext, reqId: postSource.post.reqId, }) } }
const onOpenEmbed = () => { ax.metric('post:clickthroughEmbed', { uri: post.uri, authorDid: post.author.did, logContext: 'PostThreadItem', feedDescriptor: feedFeedback.feedDescriptor, }) if (postSource) { feedFeedback.sendInteraction({ item: post.uri, event: 'app.bsky.feed.defs#clickthroughEmbed', feedContext: postSource.post.feedContext, reqId: postSource.post.reqId, }) } }
return ( <> <ThreadItemAnchorParentReplyLine isRoot={isRoot} /> <GalleryBleed> <View testID={`postThreadItem-by-${post.author.handle}`} style={[ { paddingHorizontal: OUTER_SPACE, }, isRoot && [a.pt_lg], ]}> <View style={[a.flex_row, a.gap_md, a.pb_md]}> <View collapsable={false}> <PreviewableUserAvatar size={42} profile={post.author} moderation={moderation.ui('avatar')} type={post.author.associated?.labeler ? 'labeler' : 'user'} live={live} onBeforePress={onOpenAuthor} /> </View> <Link to={authorHref} style={[a.flex_1]} label={sanitizeDisplayName( post.author.displayName || sanitizeHandle(post.author.handle), moderation.ui('displayName'), )} onPress={onOpenAuthor}> <View style={[a.flex_1, a.align_start]}> <ProfileHoverCard did={post.author.did} style={[a.w_full]}> <View style={[a.flex_row, a.align_center]}> <Text emoji style={[ a.flex_shrink, a.text_lg, a.font_semi_bold, a.leading_snug, ]} numberOfLines={1}> {sanitizeDisplayName( post.author.displayName || sanitizeHandle(post.author.handle), moderation.ui('displayName'), )} </Text>
<View style={[a.pl_xs]}> <ProfileBadges profile={authorShadow} size="md" interactive /> </View> </View> <Text style={[ a.text_md, a.leading_snug, t.atoms.text_contrast_medium, ]} numberOfLines={1}> {sanitizeHandle(post.author.handle, '@')} </Text> </ProfileHoverCard> </View> </Link> <View collapsable={false} style={[a.self_center]}> <ThreadItemAnchorFollowButton did={post.author.did} enabled={showFollowButton} /> </View> </View> <View style={[a.pb_sm]}> <ContentHider modui={moderation.ui('contentView')} ignoreMute childContainerStyle={[a.pt_sm]}> <PostAlerts post={post} modui={moderation.ui('contentView')} view="expanded" includeMute style={[a.pb_sm]} additionalCauses={additionalPostAlerts} /> {richText?.text ? ( <RichText enableTags selectable value={richText} style={[a.flex_1, a.text_lg]} authorHandle={post.author.handle} shouldProxyLinks={true} suffixOffset={POST_NUMBER_INLINE_OFFSET} suffix={ showPostNumber ? ( <ThreadItemPostNumber value={postNumbering} /> ) : undefined } /> ) : ( <ThreadItemPostNumber inline={false} value={postNumbering} /> )} <TranslatedPost post={post} postTextStyle={[a.text_lg]} /> {post.embed && ( <View style={[richText?.text ? a.py_xs : []]}> <Embed embed={post.embed} moderation={moderation} viewContext={PostEmbedViewContext.ThreadHighlighted} onOpen={onOpenEmbed} post={post} feedDescriptor={feedFeedback.feedDescriptor} /> </View> )} </ContentHider> <ExpandedPostDetails post={item.value.post} isThreadAuthor={isThreadAuthor} /> {post.repostCount !== 0 || post.likeCount !== 0 || post.quoteCount !== 0 || post.bookmarkCount !== 0 ? ( // Show this section unless we're *sure* it has no engagement. <View style={[ a.flex_row, a.flex_wrap, a.align_center, { rowGap: a.gap_sm.gap, columnGap: a.gap_lg.gap, }, a.border_t, a.mt_md, a.py_sm, t.atoms.border_contrast_low, ]}> {post.repostCount != null && post.repostCount !== 0 ? ( <StatLink to={repostsHref} label={l`Reposts of this post`}> <Text testID="repostCount-expanded" style={[a.text_md, t.atoms.text_contrast_medium]}> <Trans comment="Repost count display, the <0> tags enclose the number of reposts in bold (will never be 0)"> <Text style={[a.text_md, a.font_semi_bold, t.atoms.text]}> {formatPostStatCount(post.repostCount)} </Text>{' '} <Plural value={post.repostCount} one="repost" other="reposts" /> </Trans> </Text> </StatLink> ) : null} {post.quoteCount != null && post.quoteCount !== 0 && !post.viewer?.embeddingDisabled ? ( <StatLink to={quotesHref} label={l`Quotes of this post`}> <Text testID="quoteCount-expanded" style={[a.text_md, t.atoms.text_contrast_medium]}> <Trans comment="Quote count display, the <0> tags enclose the number of quotes in bold (will never be 0)"> <Text style={[a.text_md, a.font_semi_bold, t.atoms.text]}> {formatPostStatCount(post.quoteCount)} </Text>{' '} <Plural value={post.quoteCount} one="quote" other="quotes" /> </Trans> </Text> </StatLink> ) : null} <LikesStat post={post} href={likesHref} /> {post.bookmarkCount != null && post.bookmarkCount !== 0 ? ( <Text testID="bookmarkCount-expanded" style={[a.text_md, t.atoms.text_contrast_medium]}> <Trans comment="Save count display, the <0> tags enclose the number of saves in bold (will never be 0)"> <Text style={[a.text_md, a.font_semi_bold, t.atoms.text]}> {formatPostStatCount(post.bookmarkCount)} </Text>{' '} <Plural value={post.bookmarkCount} one="save" other="saves" /> </Trans> </Text> ) : null} </View> ) : null} <KnownLikers post={post} feature={Features.PostThreadKnownLikersEnable} outerStyle={[a.pt_xs, a.pb_sm]} /> <View style={[ a.pb_2xs, { marginLeft: -5, }, ]}> <FeedFeedbackProvider value={feedFeedback}> <PostControls big post={postShadow} record={record} richText={richText} onPressReply={onPressReply} logContext="PostThreadItem" threadgateRecord={threadgateRecord} feedContext={postSource?.post?.feedContext} reqId={postSource?.post?.reqId} viaRepost={viaRepost} /> </FeedFeedbackProvider> </View> <DebugFieldDisplay subject={post} /> </View> </View> </GalleryBleed> </> )})
function ExpandedPostDetails({ post, isThreadAuthor,}: { post: Extract<ThreadItem, {type: 'threadPost'}>['value']['post'] isThreadAuthor: boolean}) { const t = useTheme() const {i18n} = useLingui() const isRootPost = !('reply' in post.record)
return ( <View style={[a.gap_md, a.pt_md, a.align_start]}> <BackdatedPostIndicator post={post} /> <View style={[a.flex_row, a.align_center, a.flex_wrap, a.gap_sm]}> <Text style={[a.text_sm, t.atoms.text_contrast_medium]}> {niceDate(i18n, post.indexedAt, 'dot separated')} </Text> {isRootPost && ( <WhoCanReply post={post} isThreadAuthor={isThreadAuthor} /> )} </View> </View> )}
function BackdatedPostIndicator({post}: {post: app.bsky.feed.defs.PostView}) { const t = useTheme() const {t: l, i18n} = useLingui() const control = Prompt.usePromptControl()
const indexedAt = new Date(post.indexedAt) const createdAt = bsky.isType(app.bsky.feed.post, post.record) ? new Date(post.record.createdAt) : new Date(post.indexedAt)
// backdated if createdAt is 24 hours or more before indexedAt const isBackdated = indexedAt.getTime() - createdAt.getTime() > 24 * 60 * 60 * 1000
if (!isBackdated) return null
return ( <> <Button label={l`Archived post`} accessibilityHint={l`Shows information about when this post was created`} onPress={e => { e.preventDefault() e.stopPropagation() control.open() }}> {({hovered, pressed}) => ( <View style={[ a.flex_row, a.align_center, a.rounded_full, t.atoms.bg_contrast_25, (hovered || pressed) && t.atoms.bg_contrast_50, { gap: 3, paddingHorizontal: 6, paddingVertical: 3, }, ]}> <CalendarClockIcon fill={t.palette.yellow} size="sm" aria-hidden /> <Text style={[ a.text_xs, a.font_semi_bold, a.leading_tight, t.atoms.text_contrast_medium, ]}> <Trans>Archived from {niceDate(i18n, createdAt, 'medium')}</Trans> </Text> </View> )} </Button>
<Prompt.Outer control={control}> <Prompt.Content> <Prompt.TitleText> <Trans>Archived post</Trans> </Prompt.TitleText> <Prompt.DescriptionText> <Trans> This post claims to have been created on{' '} <RNText style={[a.font_semi_bold]}> {niceDate(i18n, createdAt)} </RNText> , but was first seen by Blacksky on{' '} <RNText style={[a.font_semi_bold]}> {niceDate(i18n, indexedAt)} </RNText> . </Trans> </Prompt.DescriptionText> <Prompt.DescriptionText> <Trans> Blacksky cannot confirm the authenticity of the claimed date. </Trans> </Prompt.DescriptionText> </Prompt.Content> <Prompt.Actions> <Prompt.Action cta={l`Okay`} onPress={() => {}} /> </Prompt.Actions> </Prompt.Outer> </> )}
function getThreadAuthor( post: app.bsky.feed.defs.PostView, record: app.bsky.feed.post.Main,): string { if (!record.reply) { return post.author.did } return postUriAuthor(record.reply.root.uri) ?? ''}
export function ThreadItemAnchorSkeleton() { return ( <View style={[a.p_lg, a.gap_md]}> <Skele.Row style={[a.align_center, a.gap_md]}> <Skele.Circle size={42} />
<Skele.Col> <Skele.Text style={[a.text_lg, {width: '20%'}]} /> <Skele.Text blend style={[a.text_md, {width: '40%'}]} /> </Skele.Col> </Skele.Row>
<View> <Skele.Text style={[a.text_xl, {width: '100%'}]} /> <Skele.Text style={[a.text_xl, {width: '60%'}]} /> </View>
<Skele.Text style={[a.text_sm, {width: '50%'}]} />
<PostControlsSkeleton big /> </View> )}
/** * A stat that links to its own screen when there is one to link to. * * Space posts have no liked-by / reposted-by / quotes screens, so the count * renders as plain text rather than a link that would 400. */function StatLink({ to, label, children,}: { to?: string label: string children: React.ReactElement}) { if (!to) return children return ( <Link to={to} label={label}> {children} </Link> )}