Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216import {forwardRef, useCallback, useImperativeHandle, useState} from 'react'import {type ListRenderItemInfo, View} from 'react-native'import {AtUri} from '@atproto/syntax'import {type ModerationOpts} from '@bsky/sdk/moderation'import {useLingui} from '@lingui/react/macro'import {Trans} from '@lingui/react/macro'
import {useBottomBarOffset} from '#/lib/hooks/useBottomBarOffset'import {useInitialNumToRender} from '#/lib/hooks/useInitialNumToRender'import {isBlockedOrBlocking} from '#/lib/moderation/blocked-and-muted'import {cleanError} from '#/lib/strings/errors'import {useAllListMembersQuery} from '#/state/queries/list-members'import {useListMembershipRemoveMutation} from '#/state/queries/list-memberships'import {useSession} from '#/state/session'import {List, type ListRef} from '#/view/com/util/List'import {type SectionRef} from '#/screens/Profile/Sections/types'import {atoms as a, useTheme} from '#/alf'import * as Admonition from '#/components/Admonition'import {ButtonIcon, ButtonText} from '#/components/Button'import {ListFooter, ListMaybePlaceholder} from '#/components/Lists'import {Loader} from '#/components/Loader'import {Default as ProfileCard} from '#/components/ProfileCard'import * as Toast from '#/components/Toast'import {useAnalytics} from '#/analytics'import {IS_NATIVE, IS_WEB} from '#/env'import {type app} from '#/lexicons'
function keyExtractor(item: app.bsky.graph.defs.ListItemView) { return item.uri}
interface ProfilesListProps { listUri: string moderationOpts: ModerationOpts headerHeight: number scrollElRef: ListRef}
export const ProfilesList = forwardRef<SectionRef, ProfilesListProps>( function ProfilesListImpl( {listUri, moderationOpts, headerHeight, scrollElRef}, ref, ) { const t = useTheme() const bottomBarOffset = useBottomBarOffset(headerHeight) const initialNumToRender = useInitialNumToRender() const {currentAccount} = useSession() const {data, refetch, isError} = useAllListMembersQuery(listUri)
const [isPTRing, setIsPTRing] = useState(false)
// The server returns these sorted by descending creation date, so we want to invert
const listItems = data ?.filter( p => !isBlockedOrBlocking(p.subject) && !p.subject.associated?.labeler, ) .reverse() const isOwn = new AtUri(listUri).host === currentAccount?.did
const getSortedProfiles = () => { if (!listItems) return
return [...listItems].sort((a, b) => { if (a.subjectOptedOut !== b.subjectOptedOut) { return a.subjectOptedOut ? -1 : 1 } if (isOwn) { if (a.subject.did === currentAccount?.did) return -1 if (b.subject.did === currentAccount?.did) return 1 } return 0 }) } const onScrollToTop = useCallback(() => { scrollElRef.current?.scrollToOffset({ animated: IS_NATIVE, offset: -headerHeight, }) }, [scrollElRef, headerHeight])
useImperativeHandle(ref, () => ({ scrollToTop: onScrollToTop, }))
const renderItem = ({ item, index, }: ListRenderItemInfo<app.bsky.graph.defs.ListItemView>) => { return ( <View style={[ a.p_lg, t.atoms.border_contrast_low, (IS_WEB || index !== 0) && a.border_t, ]}> <ProfileCard profile={item.subject} moderationOpts={moderationOpts} logContext="StarterPackProfilesList" /> {item.subjectOptedOut ? ( <OptedOutControls item={item} listUri={listUri} canRemove={isOwn} /> ) : null} </View> ) }
const onRefresh = async () => { setIsPTRing(true) await refetch() setIsPTRing(false) }
if (!data) { return ( <View style={[ a.h_full_vh, {marginTop: headerHeight, marginBottom: bottomBarOffset}, ]}> <ListMaybePlaceholder isLoading={true} isError={isError} onRetry={refetch} /> </View> ) }
if (data) return ( <List data={getSortedProfiles()} renderItem={renderItem} keyExtractor={keyExtractor} ref={scrollElRef} headerOffset={headerHeight} ListFooterComponent={ <ListFooter style={{paddingBottom: bottomBarOffset, borderTopWidth: 0}} /> } showsVerticalScrollIndicator={false} desktopFixedHeight initialNumToRender={initialNumToRender} refreshing={isPTRing} onRefresh={() => void onRefresh()} /> ) },)
function OptedOutControls({ item, listUri, canRemove,}: { item: app.bsky.graph.defs.ListItemView listUri: string canRemove: boolean}) { const {t: l} = useLingui() const ax = useAnalytics() const [isRemoved, setIsRemoved] = useState(false) const {mutate: removeMembership, isPending} = useListMembershipRemoveMutation( { onSuccess: () => { setIsRemoved(true) Toast.show(l`Removed from Starter Pack`) }, onError: error => Toast.show(cleanError(error), { type: 'error', }), }, )
if (isRemoved) return null
return ( <Admonition.Outer type="info" style={[a.mt_sm]}> <Admonition.Row style={[a.align_center]}> <Admonition.Icon /> <Admonition.Content> <Admonition.Text> <Trans>Opted out of this Starter Pack</Trans> </Admonition.Text> </Admonition.Content> {canRemove ? ( <Admonition.Button label={l`Remove user from Starter Pack`} color="secondary" disabled={isPending} onPress={() => { ax.metric('starterPack:removeUser', {context: 'opt-out'}) removeMembership({ listUri, actorDid: item.subject.did, membershipUri: item.uri, }) }}> {isPending ? ( <ButtonIcon icon={Loader} /> ) : ( <ButtonText> <Trans>Remove</Trans> </ButtonText> )} </Admonition.Button> ) : null} </Admonition.Row> </Admonition.Outer> )}