Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371import {Fragment, useMemo, useRef} from 'react'import {Keyboard, type StyleProp, View, type ViewStyle} from 'react-native'import {AtUri} from '@atproto/syntax'import {msg} from '@lingui/core/macro'import {useLingui} from '@lingui/react'import {Trans} from '@lingui/react/macro'
import {HITSLOP_10} from '#/lib/constants'import {makeListLink, makeProfileLink} from '#/lib/routes/links'import { type ThreadgateAllowUISetting, threadgateViewToAllowUISetting,} from '#/state/queries/threadgate'import {atoms as a, native, useTheme, web} from '#/alf'import {Button, ButtonText} from '#/components/Button'import * as Dialog from '#/components/Dialog'import {useDialogControl} from '#/components/Dialog'import { PostInteractionSettingsDialog, usePrefetchPostInteractionSettings,} from '#/components/dialogs/PostInteractionSettingsDialog'import {TinyChevronBottom_Stroke2_Corner0_Rounded as TinyChevronDownIcon} from '#/components/icons/Chevron'import {CircleBanSign_Stroke2_Corner0_Rounded as CircleBanSignIcon} from '#/components/icons/CircleBanSign'import {Earth_Stroke2_Corner0_Rounded as EarthIcon} from '#/components/icons/Globe'import {Group3_Stroke2_Corner0_Rounded as GroupIcon} from '#/components/icons/Group'import {InlineLinkText} from '#/components/Link'import {Text} from '#/components/Typography'import {useAnalytics} from '#/analytics'import {IS_NATIVE, IS_WEB} from '#/env'import {app} from '#/lexicons'import * as bsky from '#/types/bsky'
interface WhoCanReplyProps { post: app.bsky.feed.defs.PostView isThreadAuthor: boolean style?: StyleProp<ViewStyle>}
export function WhoCanReply({post, isThreadAuthor, style}: WhoCanReplyProps) { const t = useTheme() const ax = useAnalytics() const {_} = useLingui() const infoDialogControl = useDialogControl() const editDialogControl = useDialogControl()
/* * `WhoCanReply` is only used for root posts atm, in case this changes * unexpectedly, we should check to make sure it's for sure the root URI. */ const rootUri = bsky.isType(app.bsky.feed.post, post.record) && post.record.reply?.root ? post.record.reply.root.uri : post.uri const settings = useMemo(() => { return threadgateViewToAllowUISetting(post.threadgate) }, [post.threadgate])
const prefetchPostInteractionSettings = usePrefetchPostInteractionSettings({ postUri: post.uri, rootPostUri: rootUri, }) const prefetchPromise = useRef<Promise<void>>(Promise.resolve())
const prefetch = () => { prefetchPromise.current = prefetchPostInteractionSettings() }
const anyoneCanReply = settings.length === 1 && settings[0].type === 'everybody' const noOneCanReply = settings.length === 1 && settings[0].type === 'nobody' const isCommunityPost = post.uri.includes('community.blacksky.feed.post') const description = anyoneCanReply ? isCommunityPost ? _(msg`Every community member can reply`) : _(msg`Everybody can reply`) : noOneCanReply ? _(msg`Replies disabled`) : isCommunityPost ? _(msg`Some community members can reply`) : _(msg`Some people can reply`)
const onPressOpen = () => { if (IS_NATIVE && Keyboard.isVisible()) { Keyboard.dismiss() } if (isThreadAuthor) { ax.metric('thread:click:editOwnThreadgate', {})
// wait on prefetch if it manages to resolve in under 200ms // otherwise, proceed immediately and show the spinner -sfn Promise.race([ prefetchPromise.current, new Promise(res => setTimeout(res, 200)), ]).finally(() => { editDialogControl.open() }) } else { ax.metric('thread:click:viewSomeoneElsesThreadgate', {})
infoDialogControl.open() } }
return ( <> <Button label={ isThreadAuthor ? _(msg`Edit who can reply`) : _(msg`Who can reply`) } onPress={onPressOpen} {...(isThreadAuthor ? IS_WEB ? {onHoverIn: prefetch} : {onPressIn: prefetch} : {})} hitSlop={HITSLOP_10}> {({hovered, focused, pressed}) => ( <View style={[ a.flex_row, a.align_center, a.gap_xs, (hovered || focused || pressed) && native({opacity: 0.5}), style, ]}> <Icon color={ isThreadAuthor ? t.palette.primary_500 : t.palette.contrast_400 } width={16} settings={settings} /> <Text style={[ a.text_sm, a.leading_tight, isThreadAuthor ? {color: t.palette.primary_500} : t.atoms.text_contrast_medium, (hovered || focused || pressed) && web(a.underline), ]}> {description} </Text>
{isThreadAuthor && ( <TinyChevronDownIcon width={8} fill={t.palette.primary_500} /> )} </View> )} </Button>
{isThreadAuthor ? ( <PostInteractionSettingsDialog postUri={post.uri} rootPostUri={rootUri} control={editDialogControl} initialThreadgateView={post.threadgate} /> ) : ( <WhoCanReplyDialog control={infoDialogControl} post={post} settings={settings} embeddingDisabled={Boolean(post.viewer?.embeddingDisabled)} /> )} </> )}
function Icon({ color, width, settings,}: { color: string width?: number settings: ThreadgateAllowUISetting[]}) { const isEverybody = settings.length === 0 || settings.every(setting => setting.type === 'everybody') const isNobody = !!settings.find(gate => gate.type === 'nobody') const IconComponent = isEverybody ? EarthIcon : isNobody ? CircleBanSignIcon : GroupIcon return <IconComponent fill={color} width={width} />}
function WhoCanReplyDialog({ control, post, settings, embeddingDisabled,}: { control: Dialog.DialogControlProps post: app.bsky.feed.defs.PostView settings: ThreadgateAllowUISetting[] embeddingDisabled: boolean}) { const {_} = useLingui()
return ( <Dialog.Outer control={control} nativeOptions={{preventExpansion: true}}> <Dialog.Handle /> <Dialog.ScrollableInner label={_(msg`Dialog: adjust who can interact with this post`)} style={web({maxWidth: 400})}> <View style={[a.gap_sm]}> <Text style={[a.font_semi_bold, a.text_xl, a.pb_sm]}> <Trans>Who can interact with this post?</Trans> </Text> <Rules post={post} settings={settings} embeddingDisabled={embeddingDisabled} /> </View> {IS_NATIVE && ( <Button label={_(msg`Close`)} onPress={() => control.close()} size="small" variant="solid" color="secondary" style={[a.mt_5xl]}> <ButtonText> <Trans>Close</Trans> </ButtonText> </Button> )} <Dialog.Close /> </Dialog.ScrollableInner> </Dialog.Outer> )}
function Rules({ post, settings, embeddingDisabled,}: { post: app.bsky.feed.defs.PostView settings: ThreadgateAllowUISetting[] embeddingDisabled: boolean}) { const t = useTheme()
return ( <> <Text style={[ a.text_sm, a.leading_snug, a.flex_wrap, t.atoms.text_contrast_medium, ]}> {settings.length === 0 ? ( <Trans> This post has an unknown type of threadgate on it. Your app may be out of date. </Trans> ) : settings[0].type === 'everybody' ? ( post.uri.includes('community.blacksky.feed.post') ? ( <Trans>Every community member can reply to this post.</Trans> ) : ( <Trans>Everybody can reply to this post.</Trans> ) ) : settings[0].type === 'nobody' ? ( <Trans>Replies to this post are disabled.</Trans> ) : ( <Trans> Only{' '} {settings.map((rule, i) => ( <Fragment key={`rule-${i}`}> <Rule rule={rule} post={post} lists={post.threadgate!.lists} /> <Separator i={i} length={settings.length} /> </Fragment> ))}{' '} can reply. </Trans> )}{' '} </Text> {embeddingDisabled && ( <Text style={[ a.text_sm, a.leading_snug, a.flex_wrap, t.atoms.text_contrast_medium, ]}> <Trans>No one but the author can quote this post.</Trans> </Text> )} </> )}
function Rule({ rule, post, lists,}: { rule: ThreadgateAllowUISetting post: app.bsky.feed.defs.PostView lists: app.bsky.graph.defs.ListViewBasic[] | undefined}) { if (rule.type === 'mention') { return <Trans>mentioned users</Trans> } if (rule.type === 'followers') { return ( <Trans> users following{' '} <InlineLinkText label={`@${post.author.handle}`} to={makeProfileLink(post.author)} style={[a.text_sm, a.leading_snug]}> @{post.author.handle} </InlineLinkText> </Trans> ) } if (rule.type === 'following') { return ( <Trans> users followed by{' '} <InlineLinkText label={`@${post.author.handle}`} to={makeProfileLink(post.author)} style={[a.text_sm, a.leading_snug]}> @{post.author.handle} </InlineLinkText> </Trans> ) } if (rule.type === 'list') { const list = lists?.find(l => l.uri === rule.list) if (list) { const listUrip = new AtUri(list.uri) return ( <Trans> <InlineLinkText label={list.name} to={makeListLink(listUrip.hostname, listUrip.rkey)} style={[a.text_sm, a.leading_snug]}> {list.name} </InlineLinkText>{' '} members </Trans> ) } }}
function Separator({i, length}: {i: number; length: number}) { if (length < 2 || i === length - 1) { return null } if (i === length - 2) { return ( <> {length > 2 ? ',' : ''} <Trans>and</Trans>{' '} </> ) } return <>, </>}