Skip to content

Commit

Permalink
Implement lists listing on profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrazee committed Nov 13, 2023
1 parent 7cba0dc commit f5b8e45
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {AppBskyGraphDefs as GraphDefs} from '@atproto/api'
import {ListCard} from './ListCard'
import {MyListsFilter, useMyListsQuery} from '#/state/queries/my-lists'
import {ErrorMessage} from '../util/error/ErrorMessage'
import {LoadMoreRetryBtn} from '../util/LoadMoreRetryBtn'
import {Text} from '../util/text/Text'
import {useAnalytics} from 'lib/analytics/analytics'
import {usePalette} from 'lib/hooks/usePalette'
Expand All @@ -25,9 +24,8 @@ import {cleanError} from '#/lib/strings/errors'
const LOADING = {_reactKey: '__loading__'}
const EMPTY = {_reactKey: '__empty__'}
const ERROR_ITEM = {_reactKey: '__error__'}
const LOAD_MORE_ERROR_ITEM = {_reactKey: '__load_more_error__'}

export function ListsList({
export function MyLists({
filter,
inline,
style,
Expand All @@ -42,7 +40,7 @@ export function ListsList({
}) {
const pal = usePalette('default')
const {track} = useAnalytics()
const [isRefreshing, setIsRefreshing] = React.useState(false)
const [isPTRing, setIsPTRing] = React.useState(false)
const {data, isFetching, isFetched, isError, error, refetch} =
useMyListsQuery(filter)
const isEmpty = !isFetching && !data?.length
Expand All @@ -67,14 +65,14 @@ export function ListsList({

const onRefresh = React.useCallback(async () => {
track('Lists:onRefresh')
setIsRefreshing(true)
setIsPTRing(true)
try {
await refetch()
} catch (err) {
logger.error('Failed to refresh lists', {error: err})
}
setIsRefreshing(false)
}, [refetch, track, setIsRefreshing])
setIsPTRing(false)
}, [refetch, track, setIsPTRing])

// rendering
// =
Expand All @@ -98,13 +96,6 @@ export function ListsList({
onPressTryAgain={onRefresh}
/>
)
} else if (item === LOAD_MORE_ERROR_ITEM) {
return (
<LoadMoreRetryBtn
label="There was an issue fetching your lists. Tap here to try again."
onPress={onRefresh}
/>
)
} else if (item === LOADING) {
return (
<View style={{padding: 20}}>
Expand Down Expand Up @@ -136,7 +127,7 @@ export function ListsList({
renderItem={renderItemInner}
refreshControl={
<RefreshControl
refreshing={isRefreshing}
refreshing={isPTRing}
onRefresh={onRefresh}
tintColor={pal.colors.text}
titleColor={pal.colors.text}
Expand Down
197 changes: 197 additions & 0 deletions src/view/com/lists/ProfileLists.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import React, {MutableRefObject} from 'react'
import {
ActivityIndicator,
Dimensions,
RefreshControl,
StyleProp,
StyleSheet,
View,
ViewStyle,
} from 'react-native'
import {FlatList} from '../util/Views'
import {ListCard} from './ListCard'
import {ErrorMessage} from '../util/error/ErrorMessage'
import {LoadMoreRetryBtn} from '../util/LoadMoreRetryBtn'
import {Text} from '../util/text/Text'
import {useAnalytics} from 'lib/analytics/analytics'
import {usePalette} from 'lib/hooks/usePalette'
import {useProfileListsQuery} from '#/state/queries/profile-lists'
import {OnScrollHandler} from '#/lib/hooks/useOnMainScroll'
import {logger} from '#/logger'
import {Trans} from '@lingui/macro'
import {cleanError} from '#/lib/strings/errors'
import {useAnimatedScrollHandler} from 'react-native-reanimated'
import {useTheme} from '#/lib/ThemeContext'

const LOADING = {_reactKey: '__loading__'}
const EMPTY = {_reactKey: '__empty__'}
const ERROR_ITEM = {_reactKey: '__error__'}
const LOAD_MORE_ERROR_ITEM = {_reactKey: '__load_more_error__'}

export function ProfileLists({
did,
scrollElRef,
onScroll,
scrollEventThrottle,
headerOffset,
style,
testID,
}: {
did: string
scrollElRef?: MutableRefObject<FlatList<any> | null>
onScroll?: OnScrollHandler
scrollEventThrottle?: number
headerOffset: number
style?: StyleProp<ViewStyle>
testID?: string
}) {
const pal = usePalette('default')
const theme = useTheme()
const {track} = useAnalytics()
const [isPTRing, setIsPTRing] = React.useState(false)
const {
data,
isFetching,
isFetched,
hasNextPage,
fetchNextPage,
isError,
error,
refetch,
} = useProfileListsQuery(did)
const isEmpty = !isFetching && !data?.pages[0]?.lists.length

const items = React.useMemo(() => {
let items: any[] = []
if (isError && isEmpty) {
items = items.concat([ERROR_ITEM])
}
if (!isFetched && isFetching) {
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)
}
}
if (isError && !isEmpty) {
items = items.concat([LOAD_MORE_ERROR_ITEM])
}
return items
}, [isError, isEmpty, isFetched, isFetching, data])

// events
// =

const onRefresh = React.useCallback(async () => {
track('Lists:onRefresh')
setIsPTRing(true)
try {
await refetch()
} catch (err) {
logger.error('Failed to refresh lists', {error: err})
}
setIsPTRing(false)
}, [refetch, track, setIsPTRing])

const onEndReached = React.useCallback(async () => {
if (isFetching || !hasNextPage || isError) return

track('Lists:onEndReached')
try {
await fetchNextPage()
} catch (err) {
logger.error('Failed to load more lists', {error: err})
}
}, [isFetching, hasNextPage, isError, fetchNextPage, track])

const onPressRetryLoadMore = React.useCallback(() => {
fetchNextPage()
}, [fetchNextPage])

// rendering
// =

const renderItemInner = React.useCallback(
({item}: {item: any}) => {
if (item === EMPTY) {
return (
<View
testID="listsEmpty"
style={[{padding: 18, borderTopWidth: 1}, pal.border]}>
<Text style={pal.textLight}>
<Trans>You have no lists.</Trans>
</Text>
</View>
)
} else if (item === ERROR_ITEM) {
return (
<ErrorMessage message={cleanError(error)} onPressTryAgain={refetch} />
)
} else if (item === LOAD_MORE_ERROR_ITEM) {
return (
<LoadMoreRetryBtn
label="There was an issue fetching your lists. Tap here to try again."
onPress={onPressRetryLoadMore}
/>
)
} else if (item === LOADING) {
return (
<View style={{padding: 20}}>
<ActivityIndicator />
</View>
)
}
return (
<ListCard
list={item}
testID={`list-${item.name}`}
style={styles.item}
/>
)
},
[error, refetch, onPressRetryLoadMore, pal],
)

const scrollHandler = useAnimatedScrollHandler(onScroll || {})
return (
<View testID={testID} style={style}>
<FlatList
testID={testID ? `${testID}-flatlist` : undefined}
ref={scrollElRef}
data={items}
keyExtractor={(item: any) => item._reactKey}
renderItem={renderItemInner}
refreshControl={
<RefreshControl
refreshing={isPTRing}
onRefresh={onRefresh}
tintColor={pal.colors.text}
titleColor={pal.colors.text}
progressViewOffset={headerOffset}
/>
}
contentContainerStyle={{
minHeight: Dimensions.get('window').height * 1.5,
}}
style={{paddingTop: headerOffset}}
onScroll={onScroll != null ? scrollHandler : undefined}
scrollEventThrottle={scrollEventThrottle}
indicatorStyle={theme.colorScheme === 'dark' ? 'white' : 'black'}
removeClippedSubviews={true}
contentOffset={{x: 0, y: headerOffset * -1}}
// @ts-ignore our .web version only -prf
desktopFixedHeight
onEndReached={onEndReached}
/>
</View>
)
}

const styles = StyleSheet.create({
item: {
paddingHorizontal: 18,
paddingVertical: 4,
},
})
4 changes: 2 additions & 2 deletions src/view/com/modals/UserAddRemoveLists.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {ActivityIndicator, StyleSheet, View} from 'react-native'
import {AppBskyGraphDefs as GraphDefs} from '@atproto/api'
import {Text} from '../util/text/Text'
import {UserAvatar} from '../util/UserAvatar'
import {ListsList} from '../lists/ListsList'
import {MyLists} from '../lists/MyLists'
import {Button} from '../util/forms/Button'
import * as Toast from '../util/Toast'
import {sanitizeDisplayName} from 'lib/strings/display-names'
Expand Down Expand Up @@ -51,7 +51,7 @@ export function Component({
<Text style={[styles.title, pal.text]}>
<Trans>Update {displayName} in Lists</Trans>
</Text>
<ListsList
<MyLists
filter="all"
inline
renderItem={(list, index) => (
Expand Down
4 changes: 2 additions & 2 deletions src/view/screens/Lists.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {AtUri} from '@atproto/api'
import {NativeStackScreenProps, CommonNavigatorParams} from 'lib/routes/types'
import {withAuthRequired} from 'view/com/auth/withAuthRequired'
import {ListsList} from 'view/com/lists/ListsList'
import {MyLists} from '#/view/com/lists/MyLists'
import {Text} from 'view/com/util/text/Text'
import {Button} from 'view/com/util/forms/Button'
import {NavigationProp} from 'lib/routes/types'
Expand Down Expand Up @@ -79,7 +79,7 @@ export const ListsScreen = withAuthRequired(
</Button>
</View>
</SimpleViewHeader>
<ListsList filter="curate" style={s.flexGrow1} />
<MyLists filter="curate" style={s.flexGrow1} />
</View>
)
},
Expand Down
4 changes: 2 additions & 2 deletions src/view/screens/ModerationModlists.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {AtUri} from '@atproto/api'
import {NativeStackScreenProps, CommonNavigatorParams} from 'lib/routes/types'
import {withAuthRequired} from 'view/com/auth/withAuthRequired'
import {ListsList} from 'view/com/lists/ListsList'
import {MyLists} from '#/view/com/lists/MyLists'
import {Text} from 'view/com/util/text/Text'
import {Button} from 'view/com/util/forms/Button'
import {NavigationProp} from 'lib/routes/types'
Expand Down Expand Up @@ -79,7 +79,7 @@ export const ModerationModlistsScreen = withAuthRequired(
</Button>
</View>
</SimpleViewHeader>
<ListsList filter="mod" style={s.flexGrow1} />
<MyLists filter="mod" style={s.flexGrow1} />
</View>
)
},
Expand Down
25 changes: 19 additions & 6 deletions src/view/screens/Profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {ViewSelectorHandle} from '../com/util/ViewSelector'
import {CenteredView} from '../com/util/Views'
import {ScreenHider} from 'view/com/util/moderation/ScreenHider'
import {Feed} from 'view/com/posts/Feed'
import {ProfileLists} from '../com/lists/ProfileLists'
import {useStores} from 'state/index'
import {ProfileHeader} from '../com/profile/ProfileHeader'
import {PagerWithHeader} from 'view/com/pager/PagerWithHeader'
Expand Down Expand Up @@ -264,14 +265,26 @@ function ProfileScreenLoaded({
/>
)
: null}
{showListsTab
? ({onScroll, headerHeight, isScrolledDown, scrollElRef}) => (
<View /> // TODO
{showFeedsTab
? ({onScroll, headerHeight, scrollElRef}) => (
<ProfileLists // TODO put feeds here, using this temporarily to avoid bugs
did={profile.did}
scrollElRef={scrollElRef}
onScroll={onScroll}
scrollEventThrottle={1}
headerOffset={headerHeight}
/>
)
: null}
{showFeedsTab
? ({onScroll, headerHeight, isScrolledDown, scrollElRef}) => (
<View /> // TODO
{showListsTab
? ({onScroll, headerHeight, scrollElRef}) => (
<ProfileLists
did={profile.did}
scrollElRef={scrollElRef}
onScroll={onScroll}
scrollEventThrottle={1}
headerOffset={headerHeight}
/>
)
: null}
</PagerWithHeader>
Expand Down

0 comments on commit f5b8e45

Please sign in to comment.