Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149import {forwardRef, useCallback, useImperativeHandle, useState} from 'react'import {type ListRenderItemInfo, View} from 'react-native'import { type AppBskyActorDefs, type AppBskyGraphGetList, AtUri, type ModerationOpts,} from '@atproto/api'import { type InfiniteData, type UseInfiniteQueryResult,} from '@tanstack/react-query'
import {useBottomBarOffset} from '#/lib/hooks/useBottomBarOffset'import {useInitialNumToRender} from '#/lib/hooks/useInitialNumToRender'import {isBlockedOrBlocking} from '#/lib/moderation/blocked-and-muted'import {useAllListMembersQuery} from '#/state/queries/list-members'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 {ListFooter, ListMaybePlaceholder} from '#/components/Lists'import {Default as ProfileCard} from '#/components/ProfileCard'import {IS_NATIVE, IS_WEB} from '#/env'
function keyExtractor(item: AppBskyActorDefs.ProfileViewBasic, index: number) { return `${item.did}-${index}`}
interface ProfilesListProps { listUri: string listMembersQuery: UseInfiniteQueryResult< InfiniteData<AppBskyGraphGetList.OutputSchema> > 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 profiles = data ?.filter( p => !isBlockedOrBlocking(p.subject) && !p.subject.associated?.labeler, ) .map(p => p.subject) .reverse() const isOwn = new AtUri(listUri).host === currentAccount?.did
const getSortedProfiles = () => { if (!profiles) return if (!isOwn) return profiles
const myIndex = profiles.findIndex(p => p.did === currentAccount?.did) return myIndex !== -1 ? [ profiles[myIndex], ...profiles.slice(0, myIndex), ...profiles.slice(myIndex + 1), ] : profiles } const onScrollToTop = useCallback(() => { scrollElRef.current?.scrollToOffset({ animated: IS_NATIVE, offset: -headerHeight, }) }, [scrollElRef, headerHeight])
useImperativeHandle(ref, () => ({ scrollToTop: onScrollToTop, }))
const renderItem = ({ item, index, }: ListRenderItemInfo<AppBskyActorDefs.ProfileViewBasic>) => { return ( <View style={[ a.p_lg, t.atoms.border_contrast_low, (IS_WEB || index !== 0) && a.border_t, ]}> <ProfileCard profile={item} moderationOpts={moderationOpts} logContext="StarterPackProfilesList" /> </View> ) }
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={async () => { setIsPTRing(true) await refetch() setIsPTRing(false) }} /> ) },)