Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271import { useCallback, useEffect, useImperativeHandle, useMemo, useState,} from 'react'import { findNodeHandle, type ListRenderItemInfo, type StyleProp, useWindowDimensions, View, type ViewStyle,} from 'react-native'import {msg} from '@lingui/core/macro'import {useLingui} from '@lingui/react'import {useNavigation} from '@react-navigation/native'import {useQueryClient} from '@tanstack/react-query'
import {cleanError} from '#/lib/strings/errors'import {logger} from '#/logger'import {usePreferencesQuery} from '#/state/queries/preferences'import {RQKEY, useProfileListsQuery} from '#/state/queries/profile-lists'import {useSession} from '#/state/session'import {EmptyState} from '#/view/com/util/EmptyState'import {ErrorMessage} from '#/view/com/util/error/ErrorMessage'import {List, type ListRef} from '#/view/com/util/List'import {FeedLoadingPlaceholder} from '#/view/com/util/LoadingPlaceholder'import {LoadMoreRetryBtn} from '#/view/com/util/LoadMoreRetryBtn'import {atoms as a, ios, useTheme} from '#/alf'import {BulletList_Stroke1_Corner0_Rounded as ListIcon} from '#/components/icons/BulletList'import * as ListCard from '#/components/ListCard'import {ListFooter} from '#/components/Lists'import {IS_IOS, IS_NATIVE, IS_WEB} from '#/env'
const LOADING = {_reactKey: '__loading__'}const EMPTY = {_reactKey: '__empty__'}const ERROR_ITEM = {_reactKey: '__error__'}const LOAD_MORE_ERROR_ITEM = {_reactKey: '__load_more_error__'}
interface SectionRef { scrollToTop: () => void}
interface ProfileListsProps { ref?: React.Ref<SectionRef> did: string scrollElRef: ListRef headerOffset: number enabled?: boolean style?: StyleProp<ViewStyle> testID?: string setScrollViewTag: (tag: number | null) => void}
export function ProfileLists({ ref, did, scrollElRef, headerOffset, enabled, style, testID, setScrollViewTag,}: ProfileListsProps) { const {_} = useLingui() const t = useTheme() const [isPTRing, setIsPTRing] = useState(false) const {height} = useWindowDimensions() const opts = useMemo(() => ({enabled}), [enabled]) const { data, isPending, isFetchingNextPage, hasNextPage, fetchNextPage, isError, error, refetch, } = useProfileListsQuery(did, opts) const isEmpty = !isPending && !data?.pages[0]?.lists.length const {data: preferences} = usePreferencesQuery() const navigation = useNavigation() const {currentAccount} = useSession() const isSelf = currentAccount?.did === did
const items = useMemo(() => { let items: any[] = [] if (isError && isEmpty) { items = items.concat([ERROR_ITEM]) } if (isPending) { items = items.concat([LOADING]) } else if (isEmpty) { items = items.concat([EMPTY]) } else if (data?.pages) { for (const page of data?.pages) { items = items.concat(page.lists) } } else if (isError && !isEmpty) { items = items.concat([LOAD_MORE_ERROR_ITEM]) } return items }, [isError, isEmpty, isPending, data])
// events // =
const queryClient = useQueryClient()
const onScrollToTop = useCallback(() => { scrollElRef.current?.scrollToOffset({ animated: IS_NATIVE, offset: -headerOffset, }) queryClient.invalidateQueries({queryKey: RQKEY(did)}) }, [scrollElRef, queryClient, headerOffset, did])
useImperativeHandle(ref, () => ({ scrollToTop: onScrollToTop, }))
const onRefresh = useCallback(async () => { setIsPTRing(true) try { await refetch() } catch (err) { logger.error('Failed to refresh lists', {message: err}) } setIsPTRing(false) }, [refetch, setIsPTRing])
const onEndReached = useCallback(async () => { if (isFetchingNextPage || !hasNextPage || isError) return
try { await fetchNextPage() } catch (err) { logger.error('Failed to load more lists', {message: err}) } }, [isFetchingNextPage, hasNextPage, isError, fetchNextPage])
const onPressRetryLoadMore = useCallback(() => { fetchNextPage() }, [fetchNextPage])
// rendering // =
const renderItem = useCallback( ({item, index}: ListRenderItemInfo<any>) => { if (item === EMPTY) { return ( <EmptyState icon={ListIcon} message={ isSelf ? _(msg`You haven't created any lists yet.`) : _(msg`No lists`) } textStyle={[t.atoms.text_contrast_medium, a.font_medium]} button={ isSelf ? { label: _(msg`Create a list`), text: _(msg`Create a list`), onPress: () => navigation.navigate('Lists' as never), size: 'small', color: 'primary', } : undefined } /> ) } else if (item === ERROR_ITEM) { return ( <ErrorMessage message={cleanError(error)} onPressTryAgain={refetch} /> ) } else if (item === LOAD_MORE_ERROR_ITEM) { return ( <LoadMoreRetryBtn label={_( msg`There was an issue fetching your lists. Tap here to try again.`, )} onPress={onPressRetryLoadMore} /> ) } else if (item === LOADING) { return <FeedLoadingPlaceholder /> } if (preferences) { return ( <View style={[ (index !== 0 || IS_WEB) && a.border_t, t.atoms.border_contrast_low, a.px_lg, a.py_lg, ]}> <ListCard.Default view={item} /> </View> ) } return null }, [ _, t, error, refetch, onPressRetryLoadMore, preferences, navigation, isSelf, ], )
useEffect(() => { if (IS_IOS && enabled && scrollElRef.current) { const nativeTag = findNodeHandle(scrollElRef.current) setScrollViewTag(nativeTag) } }, [enabled, scrollElRef, setScrollViewTag])
const ProfileListsFooter = useCallback(() => { if (isEmpty) return null return ( <ListFooter hasNextPage={hasNextPage} isFetchingNextPage={isFetchingNextPage} onRetry={fetchNextPage} error={cleanError(error)} height={180 + headerOffset} /> ) }, [ hasNextPage, error, isFetchingNextPage, headerOffset, fetchNextPage, isEmpty, ])
return ( <View testID={testID} style={style}> <List testID={testID ? `${testID}-flatlist` : undefined} ref={scrollElRef} data={items} keyExtractor={keyExtractor} renderItem={renderItem} ListFooterComponent={ProfileListsFooter} refreshing={isPTRing} onRefresh={onRefresh} headerOffset={headerOffset} progressViewOffset={ios(0)} removeClippedSubviews={true} desktopFixedHeight onEndReached={onEndReached} contentContainerStyle={{minHeight: height + headerOffset}} /> </View> )}
function keyExtractor(item: any) { return item._reactKey || item.uri}